An ebook/comic library service and web client
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

86 lines
2.1 KiB

  1. from datetime import datetime, timedelta
  2. import pytest
  3. from mock import MagicMock, patch
  4. from atheneum.model import UserToken, User
  5. from atheneum.service import patch_service, role_service
  6. service_module = 'atheneum.service.patch_service'
  7. @patch(service_module + '.db.session.commit')
  8. def test_patch_models(
  9. mock_db_session_commit: MagicMock):
  10. request_user = User()
  11. request_user.role = role_service.Role.ADMIN
  12. user = User()
  13. user.name = 'TestUser'
  14. user.version = 1
  15. user.last_login_time = datetime.now() - timedelta(days=1)
  16. user_patch = User()
  17. user_patch.name = 'TestUser'
  18. user_patch.version = 1
  19. user_patch.last_login_time = datetime.now()
  20. patched_user = patch_service.patch(request_user, user, user_patch)
  21. assert patched_user.version > 1
  22. assert patched_user.last_login_time == user_patch.last_login_time
  23. mock_db_session_commit.assert_called_once()
  24. def test_patch_of_different_types():
  25. request_user = User()
  26. request_user.role = role_service.Role.ADMIN
  27. user = User()
  28. user_token = UserToken()
  29. with pytest.raises(ValueError) as error_info:
  30. patch_service.patch(request_user, user, user_token)
  31. def test_patch_different_ids():
  32. request_user = User()
  33. request_user.role = role_service.Role.ADMIN
  34. user1 = User()
  35. user1.id = 1
  36. user2 = User()
  37. user2.id = 2
  38. with pytest.raises(ValueError) as error_info:
  39. patch_service.patch(request_user, user1, user2)
  40. def test_patch_different_versions():
  41. request_user = User()
  42. request_user.role = role_service.Role.ADMIN
  43. user1 = User()
  44. user1.version = 1
  45. user2 = User()
  46. user2.version = 2
  47. with pytest.raises(ValueError) as error_info:
  48. patch_service.patch(request_user, user1, user2)
  49. def test_patch_restricted_attributes():
  50. request_user = User()
  51. request_user.role = role_service.Role.USER
  52. user1 = User()
  53. user1.version = 1
  54. user1.name = 'Bob'
  55. user2 = User()
  56. user2.version = 1
  57. user2.name = 'Chris'
  58. with pytest.raises(ValueError) as error_info:
  59. patch_service.patch(request_user, user1, user2)