TestAccount.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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.login()
  17. mocked_auth_service.startAuthorizationFlow.assert_called_once_with()
  18. # Fake a sucesfull login
  19. account._onLoginStateChanged(True)
  20. # Attempting to log in again shouldn't change anything.
  21. account.login()
  22. mocked_auth_service.startAuthorizationFlow.assert_called_once_with()
  23. def test_initialize():
  24. account = Account(MagicMock())
  25. mocked_auth_service = MagicMock()
  26. account._authorization_service = mocked_auth_service
  27. account.initialize()
  28. mocked_auth_service.loadAuthDataFromPreferences.assert_called_once_with()
  29. def test_logout():
  30. account = Account(MagicMock())
  31. mocked_auth_service = MagicMock()
  32. account._authorization_service = mocked_auth_service
  33. account.logout()
  34. mocked_auth_service.deleteAuthData.assert_not_called() # We weren't logged in, so nothing should happen
  35. assert not account.isLoggedIn
  36. # Pretend the stage changed
  37. account._onLoginStateChanged(True)
  38. assert account.isLoggedIn
  39. account.logout()
  40. mocked_auth_service.deleteAuthData.assert_called_once_with()
  41. @patch("UM.Application.Application.getInstance")
  42. def test_errorLoginState(application):
  43. account = Account(MagicMock())
  44. mocked_auth_service = MagicMock()
  45. account._authorization_service = mocked_auth_service
  46. account.loginStateChanged = MagicMock()
  47. account._onLoginStateChanged(True, "BLARG!")
  48. # Even though we said that the login worked, it had an error message, so the login failed.
  49. account.loginStateChanged.emit.called_with(False)
  50. account._onLoginStateChanged(True)
  51. account._onLoginStateChanged(False, "OMGZOMG!")
  52. account.loginStateChanged.emit.called_with(False)
  53. def test_userName(user_profile):
  54. account = Account(MagicMock())
  55. mocked_auth_service = MagicMock()
  56. account._authorization_service = mocked_auth_service
  57. mocked_auth_service.getUserProfile = MagicMock(return_value = user_profile)
  58. assert account.userName == "username!"
  59. mocked_auth_service.getUserProfile = MagicMock(return_value=None)
  60. assert account.userName is None
  61. def test_profileImageUrl(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.profileImageUrl == "profile_image_url!"
  67. mocked_auth_service.getUserProfile = MagicMock(return_value=None)
  68. assert account.profileImageUrl is None
  69. def test_userProfile(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. returned_user_profile = account.userProfile
  75. assert returned_user_profile["username"] == "username!"
  76. assert returned_user_profile["profile_image_url"] == "profile_image_url!"
  77. assert returned_user_profile["user_id"] == "user_id!"
  78. mocked_auth_service.getUserProfile = MagicMock(return_value=None)
  79. assert account.userProfile is None