diff options
author | Ghostkeeper <rubend@tutanota.com> | 2021-11-22 17:11:35 +0300 |
---|---|---|
committer | Ghostkeeper <rubend@tutanota.com> | 2021-11-22 17:11:35 +0300 |
commit | 41393504963c9e34f2273954d9614d3d29e09c6e (patch) | |
tree | 8628904b5ad6807253fb1042d776a9c2c4d5726b | |
parent | 3236be1c2002d4cea2d1452ec3c3d4bb6f712f58 (diff) |
Fix test checking for failure of refresh token to reset auth
It was previously mocking some return values that should now get returned via callbacks. And it was previously relying on a web service which might not connect at all.
Contributes to issue CURA-8539.
-rw-r--r-- | tests/TestOAuth2.py | 25 |
1 files changed, 19 insertions, 6 deletions
diff --git a/tests/TestOAuth2.py b/tests/TestOAuth2.py index 9993440ed4..d39dc35258 100644 --- a/tests/TestOAuth2.py +++ b/tests/TestOAuth2.py @@ -170,14 +170,27 @@ def test_initialize(): original_preference.addPreference.assert_not_called() def test_refreshAccessTokenFailed(): + """ + Test if the authentication is reset once the refresh token fails to refresh access. + """ authorization_service = AuthorizationService(OAUTH_SETTINGS, Preferences()) authorization_service.initialize() - with patch.object(AuthorizationService, "getUserProfile", return_value=UserProfile()): - authorization_service._storeAuthData(SUCCESSFUL_AUTH_RESPONSE) - authorization_service.onAuthStateChanged.emit = MagicMock() - with patch.object(AuthorizationHelpers, "getAccessTokenUsingRefreshToken", return_value=FAILED_AUTH_RESPONSE): - authorization_service.refreshAccessToken() - assert authorization_service.onAuthStateChanged.emit.called_with(False) + + def mock_refresh(self, refresh_token, callback): # Refreshing gives a valid token. + callback(FAILED_AUTH_RESPONSE) + mock_reply = Mock() # The response that the request should give, containing an error about it failing to authenticate. + mock_reply.error = Mock(return_value = QNetworkReply.NetworkError.AuthenticationRequiredError) # The reply is 403: Authentication required, meaning the server responded with a "Can't do that, Dave". + http_mock = Mock() + http_mock.get = lambda url, headers_dict, callback, error_callback: callback(mock_reply) + http_mock.post = lambda url, data, headers_dict, callback, error_callback: callback(mock_reply) + + with patch("UM.TaskManagement.HttpRequestManager.HttpRequestManager.readJSON", Mock(return_value = {"error_description": "Mock a failed request!"})): + with patch("UM.TaskManagement.HttpRequestManager.HttpRequestManager.getInstance", MagicMock(return_value = http_mock)): + authorization_service._storeAuthData(SUCCESSFUL_AUTH_RESPONSE) + authorization_service.onAuthStateChanged.emit = MagicMock() + with patch("cura.OAuth2.AuthorizationHelpers.AuthorizationHelpers.getAccessTokenUsingRefreshToken", mock_refresh): + authorization_service.refreshAccessToken() + assert authorization_service.onAuthStateChanged.emit.called_with(False) def test_refreshAccesTokenWithoutData(): authorization_service = AuthorizationService(OAUTH_SETTINGS, Preferences()) |