mirror of
https://git.mirrors.martin98.com/https://github.com/Ultimaker/Cura
synced 2025-08-20 01:09:06 +08:00
Simulate an actual expired authentication token
The test should trigger the refresh token to be used to get a new authentication token. Contributes to issue CURA-8539.
This commit is contained in:
parent
70924f17aa
commit
e8f9e92f02
@ -40,6 +40,14 @@ SUCCESSFUL_AUTH_RESPONSE = AuthenticationResponse(
|
|||||||
success = True
|
success = True
|
||||||
)
|
)
|
||||||
|
|
||||||
|
EXPIRED_AUTH_RESPONSE = AuthenticationResponse(
|
||||||
|
access_token = "expired",
|
||||||
|
refresh_token = "beep?",
|
||||||
|
received_at = datetime.now().strftime(TOKEN_TIMESTAMP_FORMAT),
|
||||||
|
expires_in = 300, # 5 minutes should be more than enough for testing
|
||||||
|
success = True
|
||||||
|
)
|
||||||
|
|
||||||
NO_REFRESH_AUTH_RESPONSE = AuthenticationResponse(
|
NO_REFRESH_AUTH_RESPONSE = AuthenticationResponse(
|
||||||
access_token = "beep",
|
access_token = "beep",
|
||||||
received_at = datetime.now().strftime(TOKEN_TIMESTAMP_FORMAT),
|
received_at = datetime.now().strftime(TOKEN_TIMESTAMP_FORMAT),
|
||||||
@ -124,15 +132,25 @@ def test__parseJWTSucceedOnRefresh():
|
|||||||
authorization_service = AuthorizationService(OAUTH_SETTINGS, Preferences())
|
authorization_service = AuthorizationService(OAUTH_SETTINGS, Preferences())
|
||||||
authorization_service.initialize()
|
authorization_service.initialize()
|
||||||
with patch.object(AuthorizationService, "getUserProfile", return_value = UserProfile()):
|
with patch.object(AuthorizationService, "getUserProfile", return_value = UserProfile()):
|
||||||
authorization_service._storeAuthData(SUCCESSFUL_AUTH_RESPONSE)
|
authorization_service._storeAuthData(EXPIRED_AUTH_RESPONSE)
|
||||||
|
|
||||||
mock_callback = Mock() # To log the final profile 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_success = Mock() # The reply should be a failure when using the expired access token, but succeed when using the refresh token.
|
||||||
mock_reply.error = Mock(return_value = QNetworkReply.NetworkError.NoError)
|
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()
|
http_mock = Mock()
|
||||||
http_mock.get = lambda url, headers_dict, callback, error_callback: callback(mock_reply)
|
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": "Ghostkeeper"}})
|
http_mock.readJSON = Mock(return_value = {"data": {"user_id": "user_idea", "username": "Ghostkeeper"}})
|
||||||
|
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)):
|
with patch("UM.TaskManagement.HttpRequestManager.HttpRequestManager.getInstance", MagicMock(return_value = http_mock)):
|
||||||
authorization_service._parseJWT(mock_callback)
|
authorization_service._parseJWT(mock_callback)
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user