@@ -38,47 +38,63 @@ def setup_method(self):
3838 def test_verify_header_valid (self ):
3939 sig = _make_sig_header (SAMPLE_ACTION_PAYLOAD , SECRET )
4040 self .actions .verify_header (
41- payload = SAMPLE_ACTION_PAYLOAD , sig_header = sig , secret = SECRET ,
41+ payload = SAMPLE_ACTION_PAYLOAD ,
42+ sig_header = sig ,
43+ secret = SECRET ,
4244 )
4345
4446 def test_verify_header_invalid_signature (self ):
4547 sig = _make_sig_header (SAMPLE_ACTION_PAYLOAD , SECRET )
4648 with pytest .raises (ValueError , match = "does not match" ):
4749 self .actions .verify_header (
48- payload = '{"tampered": true}' , sig_header = sig , secret = SECRET ,
50+ payload = '{"tampered": true}' ,
51+ sig_header = sig ,
52+ secret = SECRET ,
4953 )
5054
5155 def test_verify_header_stale_timestamp (self ):
5256 old_ts = int ((time .time () - 60 ) * 1000 )
5357 sig = _make_sig_header (SAMPLE_ACTION_PAYLOAD , SECRET , old_ts )
5458 with pytest .raises (ValueError , match = "tolerance zone" ):
5559 self .actions .verify_header (
56- payload = SAMPLE_ACTION_PAYLOAD , sig_header = sig , secret = SECRET , tolerance = 30 ,
60+ payload = SAMPLE_ACTION_PAYLOAD ,
61+ sig_header = sig ,
62+ secret = SECRET ,
63+ tolerance = 30 ,
5764 )
5865
5966 def test_verify_header_custom_tolerance (self ):
6067 old_ts = int ((time .time () - 10 ) * 1000 )
6168 sig = _make_sig_header (SAMPLE_ACTION_PAYLOAD , SECRET , old_ts )
6269 self .actions .verify_header (
63- payload = SAMPLE_ACTION_PAYLOAD , sig_header = sig , secret = SECRET , tolerance = 60 ,
70+ payload = SAMPLE_ACTION_PAYLOAD ,
71+ sig_header = sig ,
72+ secret = SECRET ,
73+ tolerance = 60 ,
6474 )
6575
6676 def test_verify_header_malformed_header (self ):
6777 with pytest .raises (ValueError , match = "Unable to extract" ):
6878 self .actions .verify_header (
69- payload = SAMPLE_ACTION_PAYLOAD , sig_header = "invalid-header" , secret = SECRET ,
79+ payload = SAMPLE_ACTION_PAYLOAD ,
80+ sig_header = "invalid-header" ,
81+ secret = SECRET ,
7082 )
7183
7284 def test_verify_header_bytes_payload (self ):
7385 sig = _make_sig_header (SAMPLE_ACTION_PAYLOAD , SECRET )
7486 self .actions .verify_header (
75- payload = SAMPLE_ACTION_PAYLOAD .encode ("utf-8" ), sig_header = sig , secret = SECRET ,
87+ payload = SAMPLE_ACTION_PAYLOAD .encode ("utf-8" ),
88+ sig_header = sig ,
89+ secret = SECRET ,
7690 )
7791
7892 def test_construct_action_valid (self ):
7993 sig = _make_sig_header (SAMPLE_ACTION_PAYLOAD , SECRET )
8094 result = self .actions .construct_action (
81- payload = SAMPLE_ACTION_PAYLOAD , sig_header = sig , secret = SECRET ,
95+ payload = SAMPLE_ACTION_PAYLOAD ,
96+ sig_header = sig ,
97+ secret = SECRET ,
8298 )
8399 assert result ["type" ] == "authentication"
84100 assert result ["user" ]["id" ] == "user_01"
@@ -87,12 +103,16 @@ def test_construct_action_invalid_signature(self):
87103 sig = _make_sig_header (SAMPLE_ACTION_PAYLOAD , "wrong_secret" )
88104 with pytest .raises (ValueError ):
89105 self .actions .construct_action (
90- payload = SAMPLE_ACTION_PAYLOAD , sig_header = sig , secret = SECRET ,
106+ payload = SAMPLE_ACTION_PAYLOAD ,
107+ sig_header = sig ,
108+ secret = SECRET ,
91109 )
92110
93111 def test_sign_response_authentication_allow (self ):
94112 result = self .actions .sign_response (
95- action_type = "authentication" , verdict = "Allow" , secret = SECRET ,
113+ action_type = "authentication" ,
114+ verdict = "Allow" ,
115+ secret = SECRET ,
96116 )
97117 assert result ["object" ] == "authentication_action_response"
98118 assert result ["payload" ]["verdict" ] == "Allow"
@@ -101,22 +121,27 @@ def test_sign_response_authentication_allow(self):
101121
102122 def test_sign_response_user_registration_deny (self ):
103123 result = self .actions .sign_response (
104- action_type = "user_registration" , verdict = "Deny" ,
105- error_message = "Account suspended" , secret = SECRET ,
124+ action_type = "user_registration" ,
125+ verdict = "Deny" ,
126+ error_message = "Account suspended" ,
127+ secret = SECRET ,
106128 )
107129 assert result ["object" ] == "user_registration_action_response"
108130 assert result ["payload" ]["verdict" ] == "Deny"
109131 assert result ["payload" ]["error_message" ] == "Account suspended"
110132
111133 def test_sign_response_signature_is_verifiable (self ):
112134 result = self .actions .sign_response (
113- action_type = "authentication" , verdict = "Allow" , secret = SECRET ,
135+ action_type = "authentication" ,
136+ verdict = "Allow" ,
137+ secret = SECRET ,
114138 )
115139 ts = result ["payload" ]["timestamp" ]
116140 payload_json = json .dumps (result ["payload" ], separators = ("," , ":" ))
117141 signed_payload = f"{ ts } .{ payload_json } "
118142 expected = hmac .new (
119- SECRET .encode ("utf-8" ), signed_payload .encode ("utf-8" ),
143+ SECRET .encode ("utf-8" ),
144+ signed_payload .encode ("utf-8" ),
120145 digestmod = hashlib .sha256 ,
121146 ).hexdigest ()
122147 assert result ["signature" ] == expected
@@ -129,18 +154,24 @@ def setup_method(self):
129154 def test_verify_header_valid (self ):
130155 sig = _make_sig_header (SAMPLE_ACTION_PAYLOAD , SECRET )
131156 self .actions .verify_header (
132- payload = SAMPLE_ACTION_PAYLOAD , sig_header = sig , secret = SECRET ,
157+ payload = SAMPLE_ACTION_PAYLOAD ,
158+ sig_header = sig ,
159+ secret = SECRET ,
133160 )
134161
135162 def test_construct_action_valid (self ):
136163 sig = _make_sig_header (SAMPLE_ACTION_PAYLOAD , SECRET )
137164 result = self .actions .construct_action (
138- payload = SAMPLE_ACTION_PAYLOAD , sig_header = sig , secret = SECRET ,
165+ payload = SAMPLE_ACTION_PAYLOAD ,
166+ sig_header = sig ,
167+ secret = SECRET ,
139168 )
140169 assert result ["type" ] == "authentication"
141170
142171 def test_sign_response (self ):
143172 result = self .actions .sign_response (
144- action_type = "authentication" , verdict = "Allow" , secret = SECRET ,
173+ action_type = "authentication" ,
174+ verdict = "Allow" ,
175+ secret = SECRET ,
145176 )
146177 assert result ["object" ] == "authentication_action_response"
0 commit comments