2
2
from uuid import uuid4
3
3
4
4
import pytest
5
- from aws_encryption_sdk .exceptions import DecryptKeyError
6
5
7
6
from 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
+ )
9
11
from aws_lambda_powertools .utilities .data_masking .provider .kms .aws_encryption_sdk import (
10
12
AWSEncryptionSDKProvider ,
11
13
)
12
14
from tests .e2e .utils import data_fetcher
13
15
14
16
17
+ @pytest .fixture
18
+ def security_context ():
19
+ return {"this" : "is_secure" }
20
+
21
+
15
22
@pytest .fixture
16
23
def basic_handler_fn (infrastructure : dict ) -> str :
17
24
return infrastructure .get ("BasicHandler" , "" )
@@ -53,36 +60,35 @@ def test_encryption(data_masker):
53
60
54
61
55
62
@pytest .mark .xdist_group (name = "data_masking" )
56
- def test_encryption_context (data_masker ):
63
+ def test_encryption_context (data_masker , security_context ):
57
64
# GIVEN an instantiation of DataMasking with the AWS encryption provider
58
65
59
66
value = [1 , 2 , "string" , 4.5 ]
60
- context = {"this" : "is_secure" }
61
67
62
68
# 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 )
65
71
66
72
# THEN the result is the original input data
67
73
assert decrypted_data == value
68
74
69
75
70
76
@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 ):
72
78
# GIVEN an instantiation of DataMasking with the AWS encryption provider
73
79
74
80
value = [1 , 2 , "string" , 4.5 ]
75
81
76
82
# 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 )
78
84
79
85
# THEN decrypting with a different encryption_context should raise a ContextMismatchError
80
86
with pytest .raises (DataMaskingContextMismatchError ):
81
- data_masker .decrypt (encrypted_data , encryption_context = { "not" : "same_context" } )
87
+ data_masker .decrypt (encrypted_data , this = "different_context" )
82
88
83
89
84
90
@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 ):
86
92
# GIVEN an instantiation of DataMasking with the AWS encryption provider
87
93
88
94
value = [1 , 2 , "string" , 4.5 ]
@@ -92,7 +98,7 @@ def test_encryption_no_context_fail(data_masker):
92
98
93
99
# THEN decrypting with an encryption_context should raise a ContextMismatchError
94
100
with pytest .raises (DataMaskingContextMismatchError ):
95
- data_masker .decrypt (encrypted_data , encryption_context = { "this" : "is_secure" } )
101
+ data_masker .decrypt (encrypted_data , ** security_context )
96
102
97
103
98
104
@pytest .mark .xdist_group (name = "data_masking" )
@@ -106,7 +112,7 @@ def test_encryption_decryption_key_mismatch(data_masker, kms_key2_arn):
106
112
# THEN when decrypting with a different key it should fail
107
113
data_masker_key2 = DataMasking (provider = AWSEncryptionSDKProvider (keys = [kms_key2_arn ]))
108
114
109
- with pytest .raises (DecryptKeyError ):
115
+ with pytest .raises (DataMaskingDecryptKeyError ):
110
116
data_masker_key2 .decrypt (encrypted_data )
111
117
112
118
0 commit comments