TestAccount.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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_sync_success():
  62. account = Account(MagicMock())
  63. service1 = "test_service1"
  64. service2 = "test_service2"
  65. account.setSyncState(service1, SyncState.SYNCING)
  66. assert account.syncState == SyncState.SYNCING
  67. account.setSyncState(service2, SyncState.SYNCING)
  68. assert account.syncState == SyncState.SYNCING
  69. account.setSyncState(service1, SyncState.SUCCESS)
  70. # service2 still syncing
  71. assert account.syncState == SyncState.SYNCING
  72. account.setSyncState(service2, SyncState.SUCCESS)
  73. assert account.syncState == SyncState.SUCCESS
  74. def test_sync_update_action():
  75. account = Account(MagicMock())
  76. service1 = "test_service1"
  77. mockUpdateCallback = MagicMock()
  78. account.setSyncState(service1, SyncState.SYNCING)
  79. assert account.syncState == SyncState.SYNCING
  80. account.setUpdatePackagesAction(mockUpdateCallback)
  81. account.onUpdatePackagesClicked()
  82. mockUpdateCallback.assert_called_once_with()
  83. account.setSyncState(service1, SyncState.SUCCESS)
  84. account.sync() # starting a new sync resets the update action to None
  85. account.setSyncState(service1, SyncState.SYNCING)
  86. assert account.syncState == SyncState.SYNCING
  87. account.onUpdatePackagesClicked() # Should not be connected to an action anymore
  88. mockUpdateCallback.assert_called_once_with() # No additional calls
  89. assert account.updatePackagesEnabled is False
  90. account.setSyncState(service1, SyncState.SUCCESS)