11import pytest
22import responses # https://github.com/getsentry/responses
3+ from responses import matchers
34import time
45
56from datacrunch .exceptions import APIException
1819TOKEN_TYPE = 'Bearer'
1920EXPIRES_IN = 3600
2021
22+ ACCESS_TOKEN2 = 'access2'
23+ REFRESH_TOKEN2 = 'refresh2'
24+
2125class TestAuthenticationService :
2226
2327 @pytest .fixture
@@ -74,9 +78,10 @@ def test_authenticate_failed(self, authentication_service, endpoint):
7478 assert excinfo .value .code == INVALID_REQUEST
7579 assert excinfo .value .message == INVALID_REQUEST_MESSAGE
7680 assert responses .assert_call_count (endpoint , 1 ) is True
77- assert responses .calls [0 ].request .body == f'grant_type= client_credentials& client_id= { CLIENT_ID } & client_secret= { CLIENT_SECRET } '
81+ assert responses .calls [0 ].request .body == f'{{" grant_type": " client_credentials", " client_id": " { CLIENT_ID } ", " client_secret": " { CLIENT_SECRET } "}}' . encode ()
7882
7983 def test_refresh_successful (self , authentication_service , endpoint ):
84+ # add a response for the client credentials grant
8085 responses .add (
8186 responses .POST ,
8287 endpoint ,
@@ -87,12 +92,27 @@ def test_refresh_successful(self, authentication_service, endpoint):
8792 'token_type' : TOKEN_TYPE ,
8893 'expires_in' : EXPIRES_IN
8994 },
95+ match = [matchers .json_params_matcher ({"grant_type" :"client_credentials" , "client_id" : CLIENT_ID , "client_secret" : CLIENT_SECRET })],
96+ status = 200
97+ )
98+
99+ # add another response for the refresh token grant
100+ responses .add (
101+ responses .POST ,
102+ endpoint ,
103+ json = {
104+ 'access_token' : ACCESS_TOKEN2 ,
105+ 'refresh_token' : REFRESH_TOKEN2 ,
106+ 'scope' : SCOPE ,
107+ 'token_type' : TOKEN_TYPE ,
108+ 'expires_in' : EXPIRES_IN
109+ },
110+ match = [matchers .json_params_matcher ({"grant_type" :"refresh_token" , "refresh_token" : REFRESH_TOKEN })],
90111 status = 200
91112 )
92113
93114 # act
94- authentication_service .authenticate () # authenticate first
95- auth_data = authentication_service .refresh () # refresh
115+ auth_data = authentication_service .authenticate () # authenticate first
96116
97117 # assert
98118 assert type (auth_data ) == dict
@@ -101,8 +121,17 @@ def test_refresh_successful(self, authentication_service, endpoint):
101121 assert authentication_service ._scope == SCOPE
102122 assert authentication_service ._token_type == TOKEN_TYPE
103123 assert authentication_service ._expires_at != None
104- assert responses .calls [0 ].request .body == f'grant_type=client_credentials&client_id={ CLIENT_ID } &client_secret={ CLIENT_SECRET } '
105- assert responses .calls [1 ].request .body == f'grant_type=refresh_token&refresh_token={ REFRESH_TOKEN } '
124+ assert responses .calls [0 ].request .body == f'{{"grant_type": "client_credentials", "client_id": "{ CLIENT_ID } ", "client_secret": "{ CLIENT_SECRET } "}}' .encode ()
125+
126+ auth_data2 = authentication_service .refresh () # refresh
127+
128+ assert type (auth_data2 ) == dict
129+ assert authentication_service ._access_token == ACCESS_TOKEN2
130+ assert authentication_service ._refresh_token == REFRESH_TOKEN2
131+ assert authentication_service ._scope == SCOPE
132+ assert authentication_service ._token_type == TOKEN_TYPE
133+ assert authentication_service ._expires_at != None
134+ assert responses .calls [1 ].request .body == f'{{"grant_type": "refresh_token", "refresh_token": "{ REFRESH_TOKEN } "}}' .encode ()
106135 assert responses .assert_call_count (endpoint , 2 ) is True
107136
108137 def test_refresh_failed (self , authentication_service , endpoint ):
@@ -118,6 +147,7 @@ def test_refresh_failed(self, authentication_service, endpoint):
118147 'token_type' : TOKEN_TYPE ,
119148 'expires_in' : EXPIRES_IN
120149 },
150+ match = [matchers .json_params_matcher ({"grant_type" :"client_credentials" , "client_id" : CLIENT_ID , "client_secret" : CLIENT_SECRET })],
121151 status = 200
122152 )
123153
@@ -126,6 +156,7 @@ def test_refresh_failed(self, authentication_service, endpoint):
126156 responses .POST ,
127157 endpoint ,
128158 json = {"code" : INVALID_REQUEST , "message" : INVALID_REQUEST_MESSAGE },
159+ match = [matchers .json_params_matcher ({"grant_type" :"refresh_token" , "refresh_token" : REFRESH_TOKEN })],
129160 status = 400
130161 )
131162
@@ -139,8 +170,8 @@ def test_refresh_failed(self, authentication_service, endpoint):
139170 assert excinfo .value .code == INVALID_REQUEST
140171 assert excinfo .value .message == INVALID_REQUEST_MESSAGE
141172 assert responses .assert_call_count (endpoint , 2 ) is True
142- assert responses .calls [0 ].request .body == f'grant_type= client_credentials& client_id= { CLIENT_ID } & client_secret= { CLIENT_SECRET } '
143- assert responses .calls [1 ].request .body == f'grant_type= refresh_token& refresh_token= { REFRESH_TOKEN } '
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 ()
144175
145176 def test_is_expired (self , authentication_service , endpoint ):
146177 # arrange
0 commit comments