diff options
author | Jaime van Kessel <nallath@gmail.com> | 2019-05-17 16:29:28 +0300 |
---|---|---|
committer | Jaime van Kessel <nallath@gmail.com> | 2019-05-17 16:29:28 +0300 |
commit | 23bd6114700fbc3214f5ce2d8bc3e4ab11cded15 (patch) | |
tree | 1e3ca1951641e35f15297841ca75e1369bfd832b /tests/TestOAuth2.py | |
parent | b6643a9fbe697354374b4d0d7059d796ff6e4889 (diff) |
Added some tests for unhappy path of OAuth
Diffstat (limited to 'tests/TestOAuth2.py')
-rw-r--r-- | tests/TestOAuth2.py | 35 |
1 files changed, 34 insertions, 1 deletions
diff --git a/tests/TestOAuth2.py b/tests/TestOAuth2.py index 26d6b8e332..5fa03cc424 100644 --- a/tests/TestOAuth2.py +++ b/tests/TestOAuth2.py @@ -2,6 +2,8 @@ import webbrowser from datetime import datetime from unittest.mock import MagicMock, patch +import requests + from UM.Preferences import Preferences from cura.OAuth2.AuthorizationHelpers import AuthorizationHelpers, TOKEN_TIMESTAMP_FORMAT from cura.OAuth2.AuthorizationService import AuthorizationService @@ -32,7 +34,8 @@ SUCCESSFUL_AUTH_RESPONSE = AuthenticationResponse( access_token = "beep", refresh_token = "beep?", received_at = datetime.now().strftime(TOKEN_TIMESTAMP_FORMAT), - expires_in = 300 # 5 minutes should be more than enough for testing + expires_in = 300, # 5 minutes should be more than enough for testing + success = True ) MALFORMED_AUTH_RESPONSE = AuthenticationResponse() @@ -46,6 +49,36 @@ def test_cleanAuthService() -> None: assert authorization_service.getAccessToken() is None +def test_refreshAccessTokenSuccess(): + 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=SUCCESSFUL_AUTH_RESPONSE): + authorization_service.refreshAccessToken() + assert authorization_service.onAuthStateChanged.emit.called_with(True) + + +def test_refreshAccessTokenFailed(): + 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 test_userProfileException(): + authorization_service = AuthorizationService(OAUTH_SETTINGS, Preferences()) + authorization_service.initialize() + authorization_service._parseJWT = MagicMock(side_effect=requests.exceptions.ConnectionError) + assert authorization_service.getUserProfile() is None + + def test_failedLogin() -> None: authorization_service = AuthorizationService(OAUTH_SETTINGS, Preferences()) authorization_service.onAuthenticationError.emit = MagicMock() |