conftest.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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 .threads.test import post_thread
  10. from .users import BANS_CACHE
  11. from .users.models import AnonymousUser
  12. from .users.test import create_test_superuser, create_test_user
  13. def get_cache_versions():
  14. return {
  15. ACL_CACHE: "abcdefgh",
  16. BANS_CACHE: "abcdefgh",
  17. SETTINGS_CACHE: "abcdefgh",
  18. THEME_CACHE: "abcdefgh",
  19. }
  20. @pytest.fixture
  21. def cache_versions():
  22. return get_cache_versions()
  23. @pytest.fixture
  24. def dynamic_settings(db, cache_versions):
  25. return DynamicSettings(cache_versions)
  26. @pytest.fixture
  27. def settings():
  28. return StaticSettings()
  29. @pytest.fixture
  30. def user_password():
  31. return "p4ssw0rd!"
  32. @pytest.fixture
  33. def anonymous_user():
  34. return AnonymousUser()
  35. @pytest.fixture
  36. def anonymous_user_acl(anonymous_user, cache_versions):
  37. return useracl.get_user_acl(anonymous_user, cache_versions)
  38. @pytest.fixture
  39. def user(db, user_password):
  40. return create_test_user("User", "user@example.com", user_password)
  41. @pytest.fixture
  42. def user_acl(user, cache_versions):
  43. return useracl.get_user_acl(user, cache_versions)
  44. @pytest.fixture
  45. def other_user(db, user_password):
  46. return create_test_user("OtherUser", "otheruser@example.com", user_password)
  47. @pytest.fixture
  48. def other_user_acl(other_user, cache_versions):
  49. return useracl.get_user_acl(other_user, cache_versions)
  50. @pytest.fixture
  51. def staffuser(db, user_password):
  52. user = create_test_superuser("Staffuser", "staffuser@example.com", user_password)
  53. user.is_superuser = False
  54. user.save()
  55. return user
  56. @pytest.fixture
  57. def staffuser_acl(staffuser, cache_versions):
  58. return useracl.get_user_acl(staffuser, cache_versions)
  59. @pytest.fixture
  60. def other_staffuser(db, user_password):
  61. user = create_test_superuser(
  62. "OtherStaffuser", "otherstaffuser@example.com", user_password
  63. )
  64. user.is_superuser = False
  65. user.save()
  66. return user
  67. @pytest.fixture
  68. def superuser(db, user_password):
  69. return create_test_superuser("Superuser", "superuser@example.com", user_password)
  70. @pytest.fixture
  71. def superuser_acl(superuser, cache_versions):
  72. return useracl.get_user_acl(superuser, cache_versions)
  73. @pytest.fixture
  74. def other_superuser(db, user_password):
  75. return create_test_superuser(
  76. "OtherSuperuser", "othersuperuser@example.com", user_password
  77. )
  78. @pytest.fixture
  79. def user_client(mocker, client, user):
  80. client.force_login(user)
  81. session = client.session
  82. session.save()
  83. return client
  84. @pytest.fixture
  85. def admin_client(mocker, client, superuser):
  86. client.force_login(superuser)
  87. session = client.session
  88. authorize_admin(mocker.Mock(session=session, user=superuser))
  89. session.save()
  90. return client
  91. @pytest.fixture
  92. def staff_client(mocker, client, staffuser):
  93. client.force_login(staffuser)
  94. session = client.session
  95. authorize_admin(mocker.Mock(session=session, user=staffuser))
  96. session.save()
  97. return client
  98. @pytest.fixture
  99. def root_category(db):
  100. return Category.objects.root_category()
  101. @pytest.fixture
  102. def default_category(db):
  103. return Category.objects.get(slug="first-category")
  104. @pytest.fixture
  105. def thread(default_category):
  106. return post_thread(default_category)
  107. @pytest.fixture
  108. def hidden_thread(default_category):
  109. return post_thread(default_category, is_hidden=True)
  110. @pytest.fixture
  111. def unapproved_thread(default_category):
  112. return post_thread(default_category, is_unapproved=True)
  113. @pytest.fixture
  114. def post(thread):
  115. return thread.first_post
  116. @pytest.fixture
  117. def user_thread(default_category, user):
  118. return post_thread(default_category, poster=user)
  119. @pytest.fixture
  120. def user_hidden_thread(default_category, user):
  121. return post_thread(default_category, poster=user, is_hidden=True)
  122. @pytest.fixture
  123. def user_unapproved_thread(default_category, user):
  124. return post_thread(default_category, poster=user, is_unapproved=True)
  125. @pytest.fixture
  126. def other_user_thread(default_category, other_user):
  127. return post_thread(default_category, poster=other_user)
  128. @pytest.fixture
  129. def other_user_hidden_thread(default_category, other_user):
  130. return post_thread(default_category, poster=other_user, is_hidden=True)
  131. @pytest.fixture
  132. def other_user_unapproved_thread(default_category, other_user):
  133. return post_thread(default_category, poster=other_user, is_unapproved=True)