22from uuid import uuid4
33
44import pytest
5- from aws_encryption_sdk .exceptions import DecryptKeyError
65
76from aws_lambda_powertools .utilities .data_masking import DataMasking
8- from aws_lambda_powertools .utilities .data_masking .exceptions import DataMaskingContextMismatchError
7+ from aws_lambda_powertools .utilities .data_masking .exceptions import (
8+ DataMaskingContextMismatchError ,
9+ DataMaskingDecryptKeyError ,
10+ )
911from aws_lambda_powertools .utilities .data_masking .provider .kms .aws_encryption_sdk import (
1012 AWSEncryptionSDKProvider ,
1113)
1214from tests .e2e .utils import data_fetcher
1315
1416
17+ @pytest .fixture
18+ def security_context ():
19+ return {"this" : "is_secure" }
20+
21+
1522@pytest .fixture
1623def basic_handler_fn (infrastructure : dict ) -> str :
1724 return infrastructure .get ("BasicHandler" , "" )
@@ -53,36 +60,35 @@ def test_encryption(data_masker):
5360
5461
5562@pytest .mark .xdist_group (name = "data_masking" )
56- def test_encryption_context (data_masker ):
63+ def test_encryption_context (data_masker , security_context ):
5764 # GIVEN an instantiation of DataMasking with the AWS encryption provider
5865
5966 value = [1 , 2 , "string" , 4.5 ]
60- context = {"this" : "is_secure" }
6167
6268 # WHEN encrypting and then decrypting the encrypted data with an encryption_context
63- encrypted_data = data_masker .encrypt (value , encryption_context = context )
64- decrypted_data = data_masker .decrypt (encrypted_data , encryption_context = context )
69+ encrypted_data = data_masker .encrypt (value , ** security_context )
70+ decrypted_data = data_masker .decrypt (encrypted_data , ** security_context )
6571
6672 # THEN the result is the original input data
6773 assert decrypted_data == value
6874
6975
7076@pytest .mark .xdist_group (name = "data_masking" )
71- def test_encryption_context_mismatch (data_masker ):
77+ def test_encryption_context_mismatch (data_masker , security_context ):
7278 # GIVEN an instantiation of DataMasking with the AWS encryption provider
7379
7480 value = [1 , 2 , "string" , 4.5 ]
7581
7682 # WHEN encrypting with a encryption_context
77- encrypted_data = data_masker .encrypt (value , encryption_context = { "this" : "is_secure" } )
83+ encrypted_data = data_masker .encrypt (value , ** security_context )
7884
7985 # THEN decrypting with a different encryption_context should raise a ContextMismatchError
8086 with pytest .raises (DataMaskingContextMismatchError ):
81- data_masker .decrypt (encrypted_data , encryption_context = { "not" : "same_context" } )
87+ data_masker .decrypt (encrypted_data , this = "different_context" )
8288
8389
8490@pytest .mark .xdist_group (name = "data_masking" )
85- def test_encryption_no_context_fail (data_masker ):
91+ def test_encryption_no_context_fail (data_masker , security_context ):
8692 # GIVEN an instantiation of DataMasking with the AWS encryption provider
8793
8894 value = [1 , 2 , "string" , 4.5 ]
@@ -92,7 +98,7 @@ def test_encryption_no_context_fail(data_masker):
9298
9399 # THEN decrypting with an encryption_context should raise a ContextMismatchError
94100 with pytest .raises (DataMaskingContextMismatchError ):
95- data_masker .decrypt (encrypted_data , encryption_context = { "this" : "is_secure" } )
101+ data_masker .decrypt (encrypted_data , ** security_context )
96102
97103
98104@pytest .mark .xdist_group (name = "data_masking" )
@@ -106,7 +112,7 @@ def test_encryption_decryption_key_mismatch(data_masker, kms_key2_arn):
106112 # THEN when decrypting with a different key it should fail
107113 data_masker_key2 = DataMasking (provider = AWSEncryptionSDKProvider (keys = [kms_key2_arn ]))
108114
109- with pytest .raises (DecryptKeyError ):
115+ with pytest .raises (DataMaskingDecryptKeyError ):
110116 data_masker_key2 .decrypt (encrypted_data )
111117
112118
0 commit comments