conftest.py 4.7 KB

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