conftest.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. import pytest
  2. from .acl import ACL_CACHE, useracl
  3. from .admin.auth import authorize_admin
  4. from .categories.models import Category
  5. from .conf import SETTINGS_CACHE
  6. from .conf.dynamicsettings import DynamicSettings
  7. from .conf.staticsettings import StaticSettings
  8. from .themes import THEME_CACHE
  9. from .users import BANS_CACHE
  10. from .users.models import AnonymousUser
  11. from .users.test import create_test_superuser, create_test_user
  12. def get_cache_versions():
  13. return {
  14. ACL_CACHE: "abcdefgh",
  15. BANS_CACHE: "abcdefgh",
  16. SETTINGS_CACHE: "abcdefgh",
  17. THEME_CACHE: "abcdefgh",
  18. }
  19. @pytest.fixture
  20. def cache_versions():
  21. return get_cache_versions()
  22. @pytest.fixture
  23. def dynamic_settings(db, cache_versions):
  24. return DynamicSettings(cache_versions)
  25. @pytest.fixture
  26. def settings():
  27. return StaticSettings()
  28. @pytest.fixture
  29. def user_password():
  30. return "p4ssw0rd!"
  31. @pytest.fixture
  32. def anonymous_user():
  33. return AnonymousUser()
  34. @pytest.fixture
  35. def user(db, user_password):
  36. return create_test_user("User", "user@example.com", user_password)
  37. @pytest.fixture
  38. def user_acl(user, cache_versions):
  39. return useracl.get_user_acl(user, cache_versions)
  40. @pytest.fixture
  41. def other_user(db, user_password):
  42. return create_test_user("OtherUser", "otheruser@example.com", user_password)
  43. @pytest.fixture
  44. def other_user_acl(other_user, cache_versions):
  45. return useracl.get_user_acl(other_user, cache_versions)
  46. @pytest.fixture
  47. def staffuser(db, user_password):
  48. user = create_test_superuser("Staffuser", "staffuser@example.com", user_password)
  49. user.is_superuser = False
  50. user.save()
  51. return user
  52. @pytest.fixture
  53. def staffuser_acl(staffuser, cache_versions):
  54. return useracl.get_user_acl(staffuser, cache_versions)
  55. @pytest.fixture
  56. def other_staffuser(db, user_password):
  57. user = create_test_superuser(
  58. "OtherStaffuser", "otherstaffuser@example.com", user_password
  59. )
  60. user.is_superuser = False
  61. user.save()
  62. return user
  63. @pytest.fixture
  64. def superuser(db, user_password):
  65. return create_test_superuser("Superuser", "superuser@example.com", user_password)
  66. @pytest.fixture
  67. def superuser_acl(superuser, cache_versions):
  68. return useracl.get_user_acl(superuser, cache_versions)
  69. @pytest.fixture
  70. def other_superuser(db, user_password):
  71. return create_test_superuser(
  72. "OtherSuperuser", "othersuperuser@example.com", user_password
  73. )
  74. @pytest.fixture
  75. def admin_client(mocker, client, superuser):
  76. client.force_login(superuser)
  77. session = client.session
  78. authorize_admin(mocker.Mock(session=session, user=superuser))
  79. session.save()
  80. return client
  81. @pytest.fixture
  82. def staff_client(mocker, client, staffuser):
  83. client.force_login(staffuser)
  84. session = client.session
  85. authorize_admin(mocker.Mock(session=session, user=staffuser))
  86. session.save()
  87. return client
  88. @pytest.fixture
  89. def root_category(db):
  90. return Category.objects.root_category()
  91. @pytest.fixture
  92. def default_category(db):
  93. return Category.objects.get(slug="first-category")