TestAccount.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. from unittest.mock import MagicMock, patch, PropertyMock
  2. import pytest
  3. from cura.API import CuraAPI, Account
  4. from cura.OAuth2.AuthorizationService import AuthorizationService
  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.login()
  18. mocked_auth_service.startAuthorizationFlow.assert_called_once_with()
  19. # Fake a sucesfull 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()
  24. def test_initialize():
  25. account = Account(MagicMock())
  26. mocked_auth_service = MagicMock()
  27. account._authorization_service = mocked_auth_service
  28. account.initialize()
  29. mocked_auth_service.loadAuthDataFromPreferences.assert_called_once_with()
  30. def test_logout():
  31. account = Account(MagicMock())
  32. mocked_auth_service = MagicMock()
  33. account._authorization_service = mocked_auth_service
  34. account.logout()
  35. mocked_auth_service.deleteAuthData.assert_not_called() # We weren't logged in, so nothing should happen
  36. assert not account.isLoggedIn
  37. # Pretend the stage changed
  38. account._onLoginStateChanged(True)
  39. assert account.isLoggedIn
  40. account.logout()
  41. mocked_auth_service.deleteAuthData.assert_called_once_with()
  42. def test_errorLoginState():
  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