TestAccount.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. from unittest.mock import MagicMock, patch
  2. import pytest
  3. from cura.API import Account
  4. from cura.OAuth2.Models import UserProfile
  5. @pytest.fixture()
  6. def user_profile():
  7. result = UserProfile()
  8. result.username = "username!"
  9. result.profile_image_url = "profile_image_url!"
  10. result.user_id = "user_id!"
  11. return result
  12. def test_login():
  13. account = Account(MagicMock())
  14. mocked_auth_service = MagicMock()
  15. account._authorization_service = mocked_auth_service
  16. account.logout = MagicMock()
  17. account.login()
  18. mocked_auth_service.startAuthorizationFlow.assert_called_once_with(False)
  19. # Fake a successful login
  20. account._onLoginStateChanged(True)
  21. # Attempting to log in again shouldn't change anything.
  22. account.login()
  23. mocked_auth_service.startAuthorizationFlow.assert_called_once_with(False)
  24. # Attempting to log in with force_logout_before_login as True should call the logout before calling the
  25. # startAuthorizationFlow(True).
  26. account.login(force_logout_before_login=True)
  27. account.logout.assert_called_once_with()
  28. mocked_auth_service.startAuthorizationFlow.assert_called_with(True)
  29. assert mocked_auth_service.startAuthorizationFlow.call_count == 2
  30. def test_initialize():
  31. account = Account(MagicMock())
  32. mocked_auth_service = MagicMock()
  33. account._authorization_service = mocked_auth_service
  34. account.initialize()
  35. mocked_auth_service.loadAuthDataFromPreferences.assert_called_once_with()
  36. def test_logout():
  37. account = Account(MagicMock())
  38. mocked_auth_service = MagicMock()
  39. account._authorization_service = mocked_auth_service
  40. account.logout()
  41. mocked_auth_service.deleteAuthData.assert_not_called() # We weren't logged in, so nothing should happen
  42. assert not account.isLoggedIn
  43. # Pretend the stage changed
  44. account._onLoginStateChanged(True)
  45. assert account.isLoggedIn
  46. account.logout()
  47. mocked_auth_service.deleteAuthData.assert_called_once_with()
  48. @patch("UM.Application.Application.getInstance")
  49. def test_errorLoginState(application):
  50. account = Account(MagicMock())
  51. mocked_auth_service = MagicMock()
  52. account._authorization_service = mocked_auth_service
  53. account.loginStateChanged = MagicMock()
  54. account._onLoginStateChanged(True, "BLARG!")
  55. # Even though we said that the login worked, it had an error message, so the login failed.
  56. account.loginStateChanged.emit.called_with(False)
  57. account._onLoginStateChanged(True)
  58. account._onLoginStateChanged(False, "OMGZOMG!")
  59. account.loginStateChanged.emit.called_with(False)
  60. def test_userName(user_profile):
  61. account = Account(MagicMock())
  62. mocked_auth_service = MagicMock()
  63. account._authorization_service = mocked_auth_service
  64. mocked_auth_service.getUserProfile = MagicMock(return_value = user_profile)
  65. assert account.userName == "username!"
  66. mocked_auth_service.getUserProfile = MagicMock(return_value=None)
  67. assert account.userName is None
  68. def test_profileImageUrl(user_profile):
  69. account = Account(MagicMock())
  70. mocked_auth_service = MagicMock()
  71. account._authorization_service = mocked_auth_service
  72. mocked_auth_service.getUserProfile = MagicMock(return_value = user_profile)
  73. assert account.profileImageUrl == "profile_image_url!"
  74. mocked_auth_service.getUserProfile = MagicMock(return_value=None)
  75. assert account.profileImageUrl is None
  76. def test_userProfile(user_profile):
  77. account = Account(MagicMock())
  78. mocked_auth_service = MagicMock()
  79. account._authorization_service = mocked_auth_service
  80. mocked_auth_service.getUserProfile = MagicMock(return_value=user_profile)
  81. returned_user_profile = account.userProfile
  82. assert returned_user_profile["username"] == "username!"
  83. assert returned_user_profile["profile_image_url"] == "profile_image_url!"
  84. assert returned_user_profile["user_id"] == "user_id!"
  85. mocked_auth_service.getUserProfile = MagicMock(return_value=None)
  86. assert account.userProfile is None