11import pytest
2- import responses # https://github.com/getsentry/responses
2+ import responses # https://github.com/getsentry/responses
33from responses import matchers
44import time
55
2222ACCESS_TOKEN2 = 'access2'
2323REFRESH_TOKEN2 = 'refresh2'
2424
25+
2526class TestAuthenticationService :
2627
2728 @pytest .fixture
@@ -78,7 +79,8 @@ def test_authenticate_failed(self, authentication_service, endpoint):
7879 assert excinfo .value .code == INVALID_REQUEST
7980 assert excinfo .value .message == INVALID_REQUEST_MESSAGE
8081 assert responses .assert_call_count (endpoint , 1 ) is True
81- assert responses .calls [0 ].request .body == f'{{"grant_type": "client_credentials", "client_id": "{ CLIENT_ID } ", "client_secret": "{ CLIENT_SECRET } "}}' .encode ()
82+ assert responses .calls [0 ].request .body == f'{{"grant_type": "client_credentials", "client_id": "{ CLIENT_ID } ", "client_secret": "{ CLIENT_SECRET } "}}' .encode (
83+ )
8284
8385 def test_refresh_successful (self , authentication_service , endpoint ):
8486 # add a response for the client credentials grant
@@ -92,7 +94,8 @@ def test_refresh_successful(self, authentication_service, endpoint):
9294 'token_type' : TOKEN_TYPE ,
9395 'expires_in' : EXPIRES_IN
9496 },
95- match = [matchers .json_params_matcher ({"grant_type" :"client_credentials" , "client_id" : CLIENT_ID , "client_secret" : CLIENT_SECRET })],
97+ match = [matchers .json_params_matcher (
98+ {"grant_type" : "client_credentials" , "client_id" : CLIENT_ID , "client_secret" : CLIENT_SECRET })],
9699 status = 200
97100 )
98101
@@ -107,12 +110,13 @@ def test_refresh_successful(self, authentication_service, endpoint):
107110 'token_type' : TOKEN_TYPE ,
108111 'expires_in' : EXPIRES_IN
109112 },
110- match = [matchers .json_params_matcher ({"grant_type" :"refresh_token" , "refresh_token" : REFRESH_TOKEN })],
113+ match = [matchers .json_params_matcher (
114+ {"grant_type" : "refresh_token" , "refresh_token" : REFRESH_TOKEN })],
111115 status = 200
112116 )
113117
114118 # act
115- auth_data = authentication_service .authenticate () # authenticate first
119+ auth_data = authentication_service .authenticate () # authenticate first
116120
117121 # assert
118122 assert type (auth_data ) == dict
@@ -121,17 +125,19 @@ def test_refresh_successful(self, authentication_service, endpoint):
121125 assert authentication_service ._scope == SCOPE
122126 assert authentication_service ._token_type == TOKEN_TYPE
123127 assert authentication_service ._expires_at != None
124- assert responses .calls [0 ].request .body == f'{{"grant_type": "client_credentials", "client_id": "{ CLIENT_ID } ", "client_secret": "{ CLIENT_SECRET } "}}' .encode ()
128+ assert responses .calls [0 ].request .body == f'{{"grant_type": "client_credentials", "client_id": "{ CLIENT_ID } ", "client_secret": "{ CLIENT_SECRET } "}}' .encode (
129+ )
125130
126- auth_data2 = authentication_service .refresh () # refresh
131+ auth_data2 = authentication_service .refresh () # refresh
127132
128133 assert type (auth_data2 ) == dict
129134 assert authentication_service ._access_token == ACCESS_TOKEN2
130135 assert authentication_service ._refresh_token == REFRESH_TOKEN2
131136 assert authentication_service ._scope == SCOPE
132137 assert authentication_service ._token_type == TOKEN_TYPE
133138 assert authentication_service ._expires_at != None
134- assert responses .calls [1 ].request .body == f'{{"grant_type": "refresh_token", "refresh_token": "{ REFRESH_TOKEN } "}}' .encode ()
139+ assert responses .calls [1 ].request .body == f'{{"grant_type": "refresh_token", "refresh_token": "{ REFRESH_TOKEN } "}}' .encode (
140+ )
135141 assert responses .assert_call_count (endpoint , 2 ) is True
136142
137143 def test_refresh_failed (self , authentication_service , endpoint ):
@@ -147,7 +153,8 @@ def test_refresh_failed(self, authentication_service, endpoint):
147153 'token_type' : TOKEN_TYPE ,
148154 'expires_in' : EXPIRES_IN
149155 },
150- match = [matchers .json_params_matcher ({"grant_type" :"client_credentials" , "client_id" : CLIENT_ID , "client_secret" : CLIENT_SECRET })],
156+ match = [matchers .json_params_matcher (
157+ {"grant_type" : "client_credentials" , "client_id" : CLIENT_ID , "client_secret" : CLIENT_SECRET })],
151158 status = 200
152159 )
153160
@@ -156,12 +163,13 @@ def test_refresh_failed(self, authentication_service, endpoint):
156163 responses .POST ,
157164 endpoint ,
158165 json = {"code" : INVALID_REQUEST , "message" : INVALID_REQUEST_MESSAGE },
159- match = [matchers .json_params_matcher ({"grant_type" :"refresh_token" , "refresh_token" : REFRESH_TOKEN })],
160- status = 400
166+ match = [matchers .json_params_matcher (
167+ {"grant_type" : "refresh_token" , "refresh_token" : REFRESH_TOKEN })],
168+ status = 500
161169 )
162170
163171 # act
164- authentication_service .authenticate () # authenticate first
172+ authentication_service .authenticate () # authenticate first
165173
166174 with pytest .raises (APIException ) as excinfo :
167175 authentication_service .refresh ()
@@ -170,21 +178,25 @@ def test_refresh_failed(self, authentication_service, endpoint):
170178 assert excinfo .value .code == INVALID_REQUEST
171179 assert excinfo .value .message == INVALID_REQUEST_MESSAGE
172180 assert responses .assert_call_count (endpoint , 2 ) is True
173- assert responses .calls [0 ].request .body == f'{{"grant_type": "client_credentials", "client_id": "{ CLIENT_ID } ", "client_secret": "{ CLIENT_SECRET } "}}' .encode ()
174- assert responses .calls [1 ].request .body == f'{{"grant_type": "refresh_token", "refresh_token": "{ REFRESH_TOKEN } "}}' .encode ()
181+ assert responses .calls [0 ].request .body == f'{{"grant_type": "client_credentials", "client_id": "{ CLIENT_ID } ", "client_secret": "{ CLIENT_SECRET } "}}' .encode (
182+ )
183+ assert responses .calls [1 ].request .body == f'{{"grant_type": "refresh_token", "refresh_token": "{ REFRESH_TOKEN } "}}' .encode (
184+ )
175185
176186 def test_is_expired (self , authentication_service , endpoint ):
177187 # arrange
178188 current_time = time .time ()
179189 future_time = current_time + 3600
180190
181191 # act
182- authentication_service ._expires_at = current_time # set the expired_at as current time
192+ # set the expired_at as current time
193+ authentication_service ._expires_at = current_time
183194 is_expired_current = authentication_service .is_expired ()
184195
185- authentication_service ._expires_at = future_time # set the expired_at as future time
196+ # set the expired_at as future time
197+ authentication_service ._expires_at = future_time
186198 is_expired_future = authentication_service .is_expired ()
187199
188200 # assert
189201 assert is_expired_current == True
190- assert is_expired_future == False
202+ assert is_expired_future == False
0 commit comments