conftest.py 4.8 KB

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