123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209 |
- from django.contrib.auth import get_user_model
- from django.urls import reverse
- from misago.acl.testutils import override_acl
- from misago.categories.models import Category
- from misago.threads import testutils
- from misago.users.testutils import AuthenticatedUserTestCase
- UserModel = get_user_model()
- class SubscriptionMiddlewareTestCase(AuthenticatedUserTestCase):
- def setUp(self):
- super().setUp()
- self.category = Category.objects.get(slug='first-category')
- self.override_acl()
- def override_acl(self):
- new_acl = self.user.acl_cache
- new_acl['can_omit_flood_protection'] = True
- new_acl['categories'][self.category.pk].update({
- 'can_see': 1,
- 'can_browse': 1,
- 'can_start_threads': 1,
- 'can_reply_threads': 1,
- })
- override_acl(self.user, new_acl)
- class SubscribeStartedThreadTests(SubscriptionMiddlewareTestCase):
- def setUp(self):
- super().setUp()
- self.api_link = reverse('misago:api:thread-list')
- def test_dont_subscribe(self):
- """middleware makes no subscription to thread"""
- self.user.subscribe_to_started_threads = UserModel.SUBSCRIBE_NONE
- self.user.subscribe_to_replied_threads = UserModel.SUBSCRIBE_NOTIFY
- self.user.save()
- response = self.client.post(
- self.api_link,
- data={
- 'category': self.category.id,
- 'title': "This is an test thread!",
- 'post': "This is test response!",
- }
- )
- self.assertEqual(response.status_code, 200)
- # user has no subscriptions
- self.assertEqual(self.user.subscription_set.count(), 0)
- def test_subscribe(self):
- """middleware subscribes thread"""
- self.user.subscribe_to_started_threads = UserModel.SUBSCRIBE_NOTIFY
- self.user.save()
- response = self.client.post(
- self.api_link,
- data={
- 'category': self.category.id,
- 'title': "This is an test thread!",
- 'post': "This is test response!",
- }
- )
- self.assertEqual(response.status_code, 200)
- # user has subscribed to thread
- thread = self.user.thread_set.order_by('id').last()
- subscription = self.user.subscription_set.get(thread=thread)
- self.assertEqual(subscription.category_id, self.category.id)
- self.assertFalse(subscription.send_email)
- def test_email_subscribe(self):
- """middleware subscribes thread with an email"""
- self.user.subscribe_to_started_threads = UserModel.SUBSCRIBE_ALL
- self.user.save()
- response = self.client.post(
- self.api_link,
- data={
- 'category': self.category.id,
- 'title': "This is an test thread!",
- 'post': "This is test response!",
- }
- )
- self.assertEqual(response.status_code, 200)
- # user has subscribed to thread
- thread = self.user.thread_set.order_by('id').last()
- subscription = self.user.subscription_set.get(thread=thread)
- self.assertEqual(subscription.category_id, self.category.id)
- self.assertTrue(subscription.send_email)
- class SubscribeRepliedThreadTests(SubscriptionMiddlewareTestCase):
- def setUp(self):
- super().setUp()
- self.thread = testutils.post_thread(self.category)
- self.api_link = reverse(
- 'misago:api:thread-post-list', kwargs={
- 'thread_pk': self.thread.pk,
- }
- )
- def test_dont_subscribe(self):
- """middleware makes no subscription to thread"""
- self.user.subscribe_to_started_threads = UserModel.SUBSCRIBE_NOTIFY
- self.user.subscribe_to_replied_threads = UserModel.SUBSCRIBE_NONE
- self.user.save()
- response = self.client.post(
- self.api_link, data={
- 'post': "This is test response!",
- }
- )
- self.assertEqual(response.status_code, 200)
- # user has no subscriptions
- self.assertEqual(self.user.subscription_set.count(), 0)
- def test_subscribe(self):
- """middleware subscribes thread"""
- self.user.subscribe_to_replied_threads = UserModel.SUBSCRIBE_NOTIFY
- self.user.save()
- response = self.client.post(
- self.api_link, data={
- 'post': "This is test response!",
- }
- )
- self.assertEqual(response.status_code, 200)
- # user has subscribed to thread
- subscription = self.user.subscription_set.get(thread=self.thread)
- self.assertEqual(subscription.category_id, self.category.id)
- self.assertFalse(subscription.send_email)
- def test_email_subscribe(self):
- """middleware subscribes thread with an email"""
- self.user.subscribe_to_replied_threads = UserModel.SUBSCRIBE_ALL
- self.user.save()
- response = self.client.post(
- self.api_link, data={
- 'post': "This is test response!",
- }
- )
- self.assertEqual(response.status_code, 200)
- # user has subscribed to thread
- subscription = self.user.subscription_set.get(thread=self.thread)
- self.assertEqual(subscription.category_id, self.category.id)
- self.assertTrue(subscription.send_email)
- def test_subscribe_with_events(self):
- """middleware omits events when testing for replied thread"""
- self.user.subscribe_to_replied_threads = UserModel.SUBSCRIBE_ALL
- self.user.save()
- # set event in thread
- testutils.reply_thread(self.thread, self.user, is_event=True)
- # reply thread
- response = self.client.post(
- self.api_link, data={
- 'post': "This is test response!",
- }
- )
- self.assertEqual(response.status_code, 200)
- # user has subscribed to thread
- subscription = self.user.subscription_set.get(thread=self.thread)
- self.assertEqual(subscription.category_id, self.category.id)
- self.assertTrue(subscription.send_email)
- def test_dont_subscribe_replied(self):
- """middleware omits threads user already replied"""
- self.user.subscribe_to_replied_threads = UserModel.SUBSCRIBE_ALL
- self.user.save()
- response = self.client.post(
- self.api_link, data={
- 'post': "This is test response!",
- }
- )
- self.assertEqual(response.status_code, 200)
- # clear subscription
- self.user.subscription_set.all().delete()
- # reply again
- response = self.client.post(
- self.api_link, data={
- 'post': "This is test response!",
- }
- )
- self.assertEqual(response.status_code, 200)
- # user has no subscriptions
- self.assertEqual(self.user.subscription_set.count(), 0)
|