test_user_notifications.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. """Tests for user email notification preferences and permissions."""
  2. from backend.app.core.permissions import (
  3. ALL_PERMISSIONS,
  4. DEFAULT_GROUPS,
  5. PERMISSION_CATEGORIES,
  6. Permission,
  7. )
  8. from backend.app.schemas.user_notifications import (
  9. UserEmailPreferenceResponse,
  10. UserEmailPreferenceUpdate,
  11. )
  12. class TestNotificationsUserEmailPermission:
  13. """Test the NOTIFICATIONS_USER_EMAIL permission integration."""
  14. def test_permission_exists(self):
  15. """notifications:user_email permission should exist in the enum."""
  16. assert hasattr(Permission, "NOTIFICATIONS_USER_EMAIL")
  17. assert Permission.NOTIFICATIONS_USER_EMAIL == "notifications:user_email"
  18. def test_permission_in_all_permissions(self):
  19. """notifications:user_email should be in ALL_PERMISSIONS list."""
  20. assert "notifications:user_email" in ALL_PERMISSIONS
  21. def test_permission_in_notifications_category(self):
  22. """notifications:user_email should be in the Notifications permission category."""
  23. notifications_perms = PERMISSION_CATEGORIES["Notifications"]
  24. assert Permission.NOTIFICATIONS_USER_EMAIL in notifications_perms
  25. def test_administrators_have_permission(self):
  26. """Administrators should have notifications:user_email via ALL_PERMISSIONS."""
  27. admins = DEFAULT_GROUPS["Administrators"]
  28. assert "notifications:user_email" in admins["permissions"]
  29. def test_operators_have_permission(self):
  30. """Operators should have notifications:user_email for managing their own preferences."""
  31. operators = DEFAULT_GROUPS["Operators"]
  32. assert "notifications:user_email" in operators["permissions"]
  33. def test_viewers_do_not_have_permission(self):
  34. """Viewers (read-only) should not have notifications:user_email."""
  35. viewers = DEFAULT_GROUPS["Viewers"]
  36. assert "notifications:user_email" not in viewers["permissions"]
  37. def test_permission_separate_from_notifications_read(self):
  38. """user_email and read should be distinct permissions."""
  39. assert Permission.NOTIFICATIONS_USER_EMAIL != Permission.NOTIFICATIONS_READ
  40. assert Permission.NOTIFICATIONS_USER_EMAIL.value != Permission.NOTIFICATIONS_READ.value
  41. class TestUserEmailPreferenceSchemas:
  42. """Test the user email preference Pydantic schemas."""
  43. def test_response_schema_defaults(self):
  44. """Response schema should accept all four boolean fields."""
  45. resp = UserEmailPreferenceResponse(
  46. notify_print_start=True,
  47. notify_print_complete=True,
  48. notify_print_failed=True,
  49. notify_print_stopped=True,
  50. )
  51. assert resp.notify_print_start is True
  52. assert resp.notify_print_complete is True
  53. assert resp.notify_print_failed is True
  54. assert resp.notify_print_stopped is True
  55. def test_response_schema_all_disabled(self):
  56. """Response schema should handle all-disabled preferences."""
  57. resp = UserEmailPreferenceResponse(
  58. notify_print_start=False,
  59. notify_print_complete=False,
  60. notify_print_failed=False,
  61. notify_print_stopped=False,
  62. )
  63. assert resp.notify_print_start is False
  64. assert resp.notify_print_complete is False
  65. assert resp.notify_print_failed is False
  66. assert resp.notify_print_stopped is False
  67. def test_update_schema_accepts_mixed(self):
  68. """Update schema should accept a mix of enabled/disabled."""
  69. update = UserEmailPreferenceUpdate(
  70. notify_print_start=True,
  71. notify_print_complete=False,
  72. notify_print_failed=True,
  73. notify_print_stopped=False,
  74. )
  75. assert update.notify_print_start is True
  76. assert update.notify_print_complete is False
  77. assert update.notify_print_failed is True
  78. assert update.notify_print_stopped is False
  79. def test_response_schema_from_attributes(self):
  80. """Response schema should support from_attributes (ORM mode)."""
  81. assert UserEmailPreferenceResponse.model_config.get("from_attributes") is True
  82. class TestNotificationTemplateTypes:
  83. """Test that user print notification template types are registered."""
  84. def test_user_print_template_types_exist(self):
  85. """All four user print email template types should be in EVENT_NAMES."""
  86. from backend.app.api.routes.notification_templates import EVENT_NAMES
  87. expected_types = [
  88. "user_print_start",
  89. "user_print_complete",
  90. "user_print_failed",
  91. "user_print_stopped",
  92. ]
  93. for event_type in expected_types:
  94. assert event_type in EVENT_NAMES, f"{event_type} not in EVENT_NAMES"
  95. def test_user_print_template_display_names(self):
  96. """User print template display names should be descriptive."""
  97. from backend.app.api.routes.notification_templates import EVENT_NAMES
  98. assert EVENT_NAMES["user_print_start"] == "User Print Started Email"
  99. assert EVENT_NAMES["user_print_complete"] == "User Print Completed Email"
  100. assert EVENT_NAMES["user_print_failed"] == "User Print Failed Email"
  101. assert EVENT_NAMES["user_print_stopped"] == "User Print Stopped Email"