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()