def test__parseJWTNoRefreshToken(): """ Tests parsing the user profile if there is no refresh token stored, but there is a normal authentication token. The request for the user profile using the authentication token should still work normally. """ authorization_service = AuthorizationService(OAUTH_SETTINGS, Preferences()) with patch.object(AuthorizationService, "getUserProfile", return_value=UserProfile()): authorization_service._storeAuthData(NO_REFRESH_AUTH_RESPONSE) mock_callback = Mock() # To log the final profile response. mock_reply = Mock( ) # The user profile that the service should respond with. mock_reply.error = Mock(return_value=QNetworkReply.NetworkError.NoError) http_mock = Mock() http_mock.get = lambda url, headers_dict, callback, error_callback: callback( mock_reply) http_mock.readJSON = Mock(return_value={ "data": { "user_id": "id_ego_or_superego", "username": "******" } }) with patch( "UM.TaskManagement.HttpRequestManager.HttpRequestManager.getInstance", MagicMock(return_value=http_mock)): authorization_service._parseJWT(mock_callback) mock_callback.assert_called_once() profile_reply = mock_callback.call_args_list[0][0][0] assert profile_reply.user_id == "id_ego_or_superego" assert profile_reply.username == "Ghostkeeper"
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(): """ Tries to refresh the authentication token using an invalid refresh token. The request should fail. """ authorization_service = AuthorizationService(OAUTH_SETTINGS, Preferences()) with patch.object(AuthorizationService, "getUserProfile", return_value=UserProfile()): authorization_service._storeAuthData(SUCCESSFUL_AUTH_RESPONSE) mock_callback = Mock() # To log the final profile 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._parseJWT(mock_callback) mock_callback.assert_called_once_with(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_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__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__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_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__parseJWTSucceedOnRefresh(): """ Tries to refresh the authentication token using a valid refresh token. The request should succeed. """ authorization_service = AuthorizationService(OAUTH_SETTINGS, Preferences()) authorization_service.initialize() with patch.object(AuthorizationService, "getUserProfile", return_value=UserProfile()): authorization_service._storeAuthData(EXPIRED_AUTH_RESPONSE) mock_callback = Mock() # To log the final profile response. mock_reply_success = Mock( ) # The reply should be a failure when using the expired access token, but succeed when using the refresh token. mock_reply_success.error = Mock( return_value=QNetworkReply.NetworkError.NoError) mock_reply_failure = Mock() mock_reply_failure.error = Mock( return_value=QNetworkReply.NetworkError.AuthenticationRequiredError) http_mock = Mock() def mock_get(url, headers_dict, callback, error_callback): if (headers_dict == {"Authorization": "Bearer beep"}): callback(mock_reply_success) else: callback(mock_reply_failure) http_mock.get = mock_get http_mock.readJSON = Mock(return_value={ "data": { "user_id": "user_idea", "username": "******" } }) def mock_refresh(self, refresh_token, callback): # Refreshing gives a valid token. callback(SUCCESSFUL_AUTH_RESPONSE) with patch( "cura.OAuth2.AuthorizationHelpers.AuthorizationHelpers.getAccessTokenUsingRefreshToken", mock_refresh): with patch( "UM.TaskManagement.HttpRequestManager.HttpRequestManager.getInstance", MagicMock(return_value=http_mock)): authorization_service._parseJWT(mock_callback) mock_callback.assert_called_once() profile_reply = mock_callback.call_args_list[0][0][0] assert profile_reply.user_id == "user_idea" assert profile_reply.username == "Ghostkeeper"
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_storeAuthData(get_user_profile) -> None: preferences = Preferences() authorization_service = AuthorizationService(OAUTH_SETTINGS, preferences) authorization_service.initialize() # Write stuff to the preferences. authorization_service._storeAuthData(SUCCESSFUL_AUTH_RESPONSE) preference_value = preferences.getValue(OAUTH_SETTINGS.AUTH_DATA_PREFERENCE_KEY) # Check that something was actually put in the preferences assert preference_value is not None and preference_value != {} # Create a second auth service, so we can load the data. second_auth_service = AuthorizationService(OAUTH_SETTINGS, preferences) second_auth_service.initialize() second_auth_service.loadAuthDataFromPreferences() assert second_auth_service.getAccessToken() == SUCCESSFUL_AUTH_RESPONSE.access_token
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() 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__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