def test_recover_from_expired_token(aggregator): # First api answers with 403 to force the check to re-authenticate unauthentified_response = MagicMock(status_code=403) # Api answer when a request is being made to the login endpoint login_response = MagicMock() # Third api answer, when the check retries the initial endpoint but is now authenticated valid_response = MagicMock() valid_response.json = MagicMock(return_value={"foo": "bar"}) session = MagicMock() session.send = MagicMock( side_effect=[unauthentified_response, login_response, valid_response]) session_wrapper = SessionWrapper(aci_url=common.ACI_URL, log=MagicMock(), session=session, apic_cookie="cookie") api = Api(common.ACI_URLS, common.USERNAME, password=common.PASSWORD, sessions=[session_wrapper]) api._refresh_sessions = False data = api.make_request("") # Assert that we retrieved the value from `valid_response.json()` assert data == {"foo": "bar"} session_calls = session.send._mock_call_args_list # Assert that the first call was to the ACI_URL assert session_calls[0].args[0].url == common.ACI_URL + "/" # Assert that the second call was to the login endpoint assert 'aaaLogin.xml' in session_calls[1].args[0].url # Assert that the last call was to the ACI_URL again assert session_calls[2].args[0].url == common.ACI_URL + "/"
def test_recover_from_expired_token(aggregator, api_kwargs): # First api answers with 403 to force the check to re-authenticate unauthentified_response = MagicMock(status_code=403) # Api answer when a request is being made to the login endpoint login_response = MagicMock() # Third api answer, when the check retries the initial endpoint but is now authenticated valid_response = MagicMock() valid_response.json = MagicMock(return_value={"foo": "bar"}) http = MagicMock() http.post = MagicMock(side_effect=[login_response]) http.get = MagicMock(side_effect=[unauthentified_response, valid_response]) session_wrapper = SessionWrapper(aci_url=common.ACI_URL, http=http, log=MagicMock()) session_wrapper.apic_cookie = "cookie" api = Api(common.ACI_URLS, http, common.USERNAME, **api_kwargs) api.sessions = {common.ACI_URL: session_wrapper} data = api.make_request("") # Assert that we retrieved the value from `valid_response.json()` assert data == {"foo": "bar"} get_calls = http.get._mock_call_args_list post_calls = http.post._mock_call_args_list # Assert that the first call was to the ACI_URL assert get_calls[0].args[0] == common.ACI_URL if 'password' in api_kwargs: # Assert that the second call was to the login endpoint assert 'aaaLogin.xml' in post_calls[0].args[0] # Assert that the last call was to the ACI_URL again assert get_calls[1].args[0] == common.ACI_URL # Assert session correctly renewed assert len(api.sessions) == 1 # check the number of sessions doesn't grow assert api.sessions[ common.ACI_URL] != session_wrapper # check session renewed # Assert cookie to check the session changed assert get_calls[0].kwargs['headers']['Cookie'] == 'cookie' assert get_calls[1].kwargs['headers']['Cookie'] != 'cookie'
valid_response = MagicMock() valid_response.json = MagicMock(return_value={"foo": "bar"}) http = MagicMock() http.post = MagicMock(side_effect=[login_response]) http.get = MagicMock(side_effect=[unauthentified_response, valid_response]) session_wrapper = SessionWrapper(aci_url=common.ACI_URL, http=http, log=MagicMock()) session_wrapper.apic_cookie = "cookie" api = Api(common.ACI_URLS, http, common.USERNAME, **api_kwargs) api.sessions = {common.ACI_URL: session_wrapper} data = api.make_request("") # Assert that we retrieved the value from `valid_response.json()` assert data == {"foo": "bar"} get_calls = http.get._mock_call_args_list post_calls = http.post._mock_call_args_list # Assert that the first call was to the ACI_URL assert get_calls[0].args[0] == common.ACI_URL if 'password' in api_kwargs: # Assert that the second call was to the login endpoint assert 'aaaLogin.xml' in post_calls[0].args[0] # Assert that the last call was to the ACI_URL again assert get_calls[1].args[0] == common.ACI_URL