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.

87 lines
2.2 KiB

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