test_users_api.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. from datetime import timedelta
  2. from django.contrib.auth import get_user_model
  3. from django.utils import timezone
  4. from misago.acl.testutils import override_acl
  5. from misago.conf import settings
  6. from misago.core import threadstore
  7. from misago.core.cache import cache
  8. from misago.forums.models import Forum
  9. from misago.threads.testutils import post_thread
  10. from misago.users.models import Online, Rank
  11. from misago.users.testutils import AuthenticatedUserTestCase
  12. class ActivePostersListTests(AuthenticatedUserTestCase):
  13. """
  14. tests for active posters list (GET /users/?list=active)
  15. """
  16. def setUp(self):
  17. super(ActivePostersListTests, self).setUp()
  18. self.link = '/api/users/?list=active'
  19. cache.clear()
  20. threadstore.clear()
  21. self.forum = Forum.objects.all_forums().filter(role="forum")[:1][0]
  22. self.forum.labels = []
  23. def test_empty_list(self):
  24. """empty list is served"""
  25. response = self.client.get(self.link)
  26. self.assertEqual(response.status_code, 200)
  27. self.assertNotIn(self.user.username, response.content)
  28. response = self.client.get(self.link)
  29. self.assertEqual(response.status_code, 200)
  30. self.assertNotIn(self.user.username, response.content)
  31. def test_filled_list(self):
  32. """filled list is served"""
  33. post_thread(self.forum, poster=self.user)
  34. self.user.posts = 1
  35. self.user.save()
  36. response = self.client.get(self.link)
  37. self.assertEqual(response.status_code, 200)
  38. self.assertIn(self.user.username, response.content)
  39. self.assertIn('"is_online":true', response.content)
  40. self.assertIn('"is_offline":false', response.content)
  41. self.logout_user()
  42. response = self.client.get(self.link)
  43. self.assertEqual(response.status_code, 200)
  44. self.assertIn(self.user.username, response.content)
  45. self.assertIn('"is_online":false', response.content)
  46. self.assertIn('"is_offline":true', response.content)
  47. class RankListTests(AuthenticatedUserTestCase):
  48. """
  49. tests for rank list (GET /users/?list=rank&rank=slug)
  50. """
  51. def setUp(self):
  52. super(RankListTests, self).setUp()
  53. self.link = '/api/users/?list=rank&rank='
  54. def test_nonexistent_rank(self):
  55. """list for non-existing rank returns 404"""
  56. response = self.client.get(self.link + 'this-rank-is-non-existing')
  57. self.assertEqual(response.status_code, 404)
  58. def test_empty_list(self):
  59. """tab rank without members returns 200"""
  60. rank_slug = self.user.rank.slug
  61. self.user.rank = Rank.objects.filter(is_tab=False)[:1][0]
  62. self.user.rank.save()
  63. response = self.client.get(self.link + rank_slug)
  64. self.assertEqual(response.status_code, 404)
  65. def test_disabled_list(self):
  66. """non-tab rank with members returns 404"""
  67. self.user.rank.is_tab = False
  68. self.user.rank.save()
  69. response = self.client.get(self.link + self.user.rank.slug)
  70. self.assertEqual(response.status_code, 404)
  71. def test_filled_list(self):
  72. """tab rank with members return 200"""
  73. self.user.rank.is_tab = True
  74. self.user.rank.save()
  75. response = self.client.get(self.link + self.user.rank.slug)
  76. self.assertEqual(response.status_code, 200)
  77. self.assertIn(self.user.username, response.content)
  78. class UserForumOptionsTests(AuthenticatedUserTestCase):
  79. """
  80. tests for user forum options RPC (POST to /api/users/1/forum-options/)
  81. """
  82. def setUp(self):
  83. super(UserForumOptionsTests, self).setUp()
  84. self.link = '/api/users/%s/forum-options/' % self.user.pk
  85. def test_empty_request(self):
  86. """empty request is handled"""
  87. response = self.client.post(self.link)
  88. self.assertEqual(response.status_code, 400)
  89. fields = (
  90. 'limits_private_thread_invites_to',
  91. 'subscribe_to_started_threads',
  92. 'subscribe_to_replied_threads'
  93. )
  94. for field in fields:
  95. self.assertIn('"%s"' % field, response.content)
  96. def test_change_forum_options(self):
  97. """forum options are changed"""
  98. response = self.client.post(self.link, data={
  99. 'limits_private_thread_invites_to': 1,
  100. 'subscribe_to_started_threads': 2,
  101. 'subscribe_to_replied_threads': 1
  102. })
  103. self.assertEqual(response.status_code, 200)
  104. self.reload_user();
  105. self.assertFalse(self.user.is_hiding_presence)
  106. self.assertEqual(self.user.limits_private_thread_invites_to, 1)
  107. self.assertEqual(self.user.subscribe_to_started_threads, 2)
  108. self.assertEqual(self.user.subscribe_to_replied_threads, 1)