diff options
author | Jaime van Kessel <nallath@gmail.com> | 2019-05-17 16:55:43 +0300 |
---|---|---|
committer | Jaime van Kessel <nallath@gmail.com> | 2019-05-17 17:09:23 +0300 |
commit | 0c11304a7b79629e2775cbb4ee1b6b29642ce3f8 (patch) | |
tree | 519a4e8d940c24c36e343c008f5a8b37f2c367bb /tests/TestOAuth2.py | |
parent | 2937575be50631e690f1b8dc08a4ef9fb002b723 (diff) |
Expand the oath tests with some extra failure cases
Diffstat (limited to 'tests/TestOAuth2.py')
-rw-r--r-- | tests/TestOAuth2.py | 52 |
1 files changed, 52 insertions, 0 deletions
diff --git a/tests/TestOAuth2.py b/tests/TestOAuth2.py index 5fa03cc424..d4af485130 100644 --- a/tests/TestOAuth2.py +++ b/tests/TestOAuth2.py @@ -38,6 +38,13 @@ SUCCESSFUL_AUTH_RESPONSE = AuthenticationResponse( success = True ) +NO_REFRESH_AUTH_RESPONSE = AuthenticationResponse( + access_token = "beep", + received_at = datetime.now().strftime(TOKEN_TIMESTAMP_FORMAT), + expires_in = 300, # 5 minutes should be more than enough for testing + success = True +) + MALFORMED_AUTH_RESPONSE = AuthenticationResponse() @@ -61,6 +68,43 @@ def test_refreshAccessTokenSuccess(): assert authorization_service.onAuthStateChanged.emit.called_with(True) +def test__parseJWTNoRefreshToken(): + authorization_service = AuthorizationService(OAUTH_SETTINGS, Preferences()) + with patch.object(AuthorizationService, "getUserProfile", return_value=UserProfile()): + authorization_service._storeAuthData(NO_REFRESH_AUTH_RESPONSE) + assert authorization_service._parseJWT() is None + + +def test__parseJWTFailOnRefresh(): + authorization_service = AuthorizationService(OAUTH_SETTINGS, Preferences()) + with patch.object(AuthorizationService, "getUserProfile", return_value=UserProfile()): + authorization_service._storeAuthData(SUCCESSFUL_AUTH_RESPONSE) + + with patch.object(AuthorizationHelpers, "getAccessTokenUsingRefreshToken", return_value=FAILED_AUTH_RESPONSE): + assert authorization_service._parseJWT() is None + + +def test__parseJWTSucceedOnRefresh(): + authorization_service = AuthorizationService(OAUTH_SETTINGS, Preferences()) + authorization_service.initialize() + with patch.object(AuthorizationService, "getUserProfile", return_value=UserProfile()): + authorization_service._storeAuthData(SUCCESSFUL_AUTH_RESPONSE) + + with patch.object(AuthorizationHelpers, "getAccessTokenUsingRefreshToken", return_value=SUCCESSFUL_AUTH_RESPONSE): + with patch.object(AuthorizationHelpers, "parseJWT", MagicMock(return_value = None)) as mocked_parseJWT: + authorization_service._parseJWT() + mocked_parseJWT.assert_called_with("beep") + + +def test_initialize(): + original_preference = MagicMock() + initialize_preferences = MagicMock() + authorization_service = AuthorizationService(OAUTH_SETTINGS, original_preference) + authorization_service.initialize(initialize_preferences) + initialize_preferences.addPreference.assert_called_once() + original_preference.addPreference.assert_not_called() + + def test_refreshAccessTokenFailed(): authorization_service = AuthorizationService(OAUTH_SETTINGS, Preferences()) authorization_service.initialize() @@ -72,6 +116,14 @@ def test_refreshAccessTokenFailed(): assert authorization_service.onAuthStateChanged.emit.called_with(False) +def test_refreshAccesTokenWithoutData(): + authorization_service = AuthorizationService(OAUTH_SETTINGS, Preferences()) + authorization_service.initialize() + authorization_service.onAuthStateChanged.emit = MagicMock() + authorization_service.refreshAccessToken() + authorization_service.onAuthStateChanged.emit.assert_not_called() + + def test_userProfileException(): authorization_service = AuthorizationService(OAUTH_SETTINGS, Preferences()) authorization_service.initialize() |