TestAccount.py 5.4 KB

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