TestAccount.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. # Copyright (c) 2022 Ultimaker B.V.
  2. # Cura is released under the terms of the LGPLv3 or higher.
  3. from unittest.mock import MagicMock, patch
  4. import pytest
  5. from cura.API import Account
  6. from cura.API.Account import SyncState
  7. from cura.OAuth2.Models import UserProfile
  8. @pytest.fixture()
  9. def user_profile():
  10. result = UserProfile()
  11. result.username = "username!"
  12. result.profile_image_url = "profile_image_url!"
  13. result.user_id = "user_id!"
  14. return result
  15. def test_login():
  16. account = Account(MagicMock())
  17. mocked_auth_service = MagicMock()
  18. account._authorization_service = mocked_auth_service
  19. account.logout = MagicMock()
  20. account.login()
  21. mocked_auth_service.startAuthorizationFlow.assert_called_once_with(False)
  22. # Fake a successful login
  23. with patch("UM.TaskManagement.HttpRequestManager.HttpRequestManager.getInstance"): # Don't want triggers for account information to actually make HTTP requests.
  24. account._onLoginStateChanged(True)
  25. # Attempting to log in again shouldn't change anything.
  26. account.login()
  27. mocked_auth_service.startAuthorizationFlow.assert_called_once_with(False)
  28. # Attempting to log in with force_logout_before_login as True should call the logout before calling the
  29. # startAuthorizationFlow(True).
  30. account.login(force_logout_before_login=True)
  31. account.logout.assert_called_once_with()
  32. mocked_auth_service.startAuthorizationFlow.assert_called_with(True)
  33. assert mocked_auth_service.startAuthorizationFlow.call_count == 2
  34. def test_initialize():
  35. account = Account(MagicMock())
  36. mocked_auth_service = MagicMock()
  37. account._authorization_service = mocked_auth_service
  38. account.initialize()
  39. mocked_auth_service.loadAuthDataFromPreferences.assert_called_once_with()
  40. def test_logout():
  41. account = Account(MagicMock())
  42. mocked_auth_service = MagicMock()
  43. account._authorization_service = mocked_auth_service
  44. account.logout()
  45. mocked_auth_service.deleteAuthData.assert_not_called() # We weren't logged in, so nothing should happen
  46. assert not account.isLoggedIn
  47. # Pretend the stage changed
  48. with patch("UM.TaskManagement.HttpRequestManager.HttpRequestManager.getInstance"): # Don't want triggers for account information to actually make HTTP requests.
  49. account._onLoginStateChanged(True)
  50. assert account.isLoggedIn
  51. account.logout()
  52. mocked_auth_service.deleteAuthData.assert_called_once_with()
  53. @patch("UM.Application.Application.getInstance")
  54. def test_errorLoginState(application):
  55. account = Account(MagicMock())
  56. mocked_auth_service = MagicMock()
  57. account._authorization_service = mocked_auth_service
  58. account.loginStateChanged = MagicMock()
  59. with patch("UM.TaskManagement.HttpRequestManager.HttpRequestManager.getInstance"): # Don't want triggers for account information to actually make HTTP requests.
  60. account._onLoginStateChanged(True, "BLARG!")
  61. # Even though we said that the login worked, it had an error message, so the login failed.
  62. account.loginStateChanged.emit.called_with(False)
  63. with patch("UM.TaskManagement.HttpRequestManager.HttpRequestManager.getInstance"):
  64. account._onLoginStateChanged(True)
  65. account._onLoginStateChanged(False, "OMGZOMG!")
  66. account.loginStateChanged.emit.called_with(False)
  67. def test_sync_success():
  68. account = Account(MagicMock())
  69. service1 = "test_service1"
  70. service2 = "test_service2"
  71. with patch("UM.TaskManagement.HttpRequestManager.HttpRequestManager.getInstance"): # Don't want triggers for account information to actually make HTTP requests.
  72. account.setSyncState(service1, SyncState.SYNCING)
  73. assert account.syncState == SyncState.SYNCING
  74. account.setSyncState(service2, SyncState.SYNCING)
  75. assert account.syncState == SyncState.SYNCING
  76. account.setSyncState(service1, SyncState.SUCCESS)
  77. # service2 still syncing
  78. assert account.syncState == SyncState.SYNCING
  79. account.setSyncState(service2, SyncState.SUCCESS)
  80. assert account.syncState == SyncState.SUCCESS
  81. def test_sync_update_action():
  82. account = Account(MagicMock())
  83. service1 = "test_service1"
  84. mockUpdateCallback = MagicMock()
  85. with patch("UM.TaskManagement.HttpRequestManager.HttpRequestManager.getInstance"): # Don't want triggers for account information to actually make HTTP requests.
  86. account.setSyncState(service1, SyncState.SYNCING)
  87. assert account.syncState == SyncState.SYNCING
  88. account.setUpdatePackagesAction(mockUpdateCallback)
  89. account.onUpdatePackagesClicked()
  90. mockUpdateCallback.assert_called_once_with()
  91. account.setSyncState(service1, SyncState.SUCCESS)
  92. account.sync() # starting a new sync resets the update action to None
  93. account.setSyncState(service1, SyncState.SYNCING)
  94. assert account.syncState == SyncState.SYNCING
  95. account.onUpdatePackagesClicked() # Should not be connected to an action anymore
  96. mockUpdateCallback.assert_called_once_with() # No additional calls
  97. assert account.updatePackagesEnabled is False
  98. account.setSyncState(service1, SyncState.SUCCESS)