conftest.py 4.6 KB

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