class PrivateTopicTest(TestCase): def setUp(self): # scenario - topic1 : # post1 - user1 - unread # post2 - user2 - unread self.profile1 = ProfileFactory() self.profile2 = ProfileFactory() self.topic1 = PrivateTopicFactory(author=self.profile1.user) self.topic1.participants.add(self.profile2.user) self.post1 = PrivatePostFactory(privatetopic=self.topic1, author=self.profile1.user, position_in_topic=1) self.post2 = PrivatePostFactory(privatetopic=self.topic1, author=self.profile2.user, position_in_topic=2) def test_unicode(self): self.assertEqual(self.topic1.__unicode__(), self.topic1.title) def test_absolute_url(self): url = reverse('private-posts-list', args=[self.topic1.pk, self.topic1.slug()]) self.assertEqual(self.topic1.get_absolute_url(), url) def test_post_count(self): self.assertEqual(2, self.topic1.get_post_count()) def test_get_last_answer(self): topic = PrivateTopicFactory(author=self.profile2.user) PrivatePostFactory(privatetopic=topic, author=self.profile2.user, position_in_topic=1) self.assertEqual(self.post2, self.topic1.get_last_answer()) self.assertNotEqual(self.post1, self.topic1.get_last_answer()) self.assertIsNone(topic.get_last_answer()) def test_first_post(self): topic = PrivateTopicFactory(author=self.profile2.user) self.assertEqual(self.post1, self.topic1.first_post()) self.assertIsNone(topic.first_post()) def test_last_read_post(self): # scenario - topic1 : # post1 - user1 - unread # post2 - user2 - unread self.assertEqual(self.post1, self.topic1.last_read_post(self.profile1.user)) # scenario - topic1 : # post1 - user1 - read # post2 - user2 - read mark_read(self.topic1, user=self.profile1.user) self.assertEqual(self.post2, self.topic1.last_read_post(self.profile1.user)) # scenario - topic1 : # post1 - user1 - read # post2 - user2 - read # post3 - user2 - unread PrivatePostFactory(privatetopic=self.topic1, author=self.profile2.user, position_in_topic=3) self.assertEqual(self.post2, self.topic1.last_read_post(self.profile1.user)) def test_first_unread_post(self): # scenario - topic1 : # post1 - user1 - unread # post2 - user2 - unread self.assertEqual(self.post1, self.topic1.first_unread_post(self.profile1.user)) # scenario - topic1 : # post1 - user1 - read # post2 - user2 - read # post3 - user2 - unread mark_read(self.topic1, self.profile1.user) post3 = PrivatePostFactory(privatetopic=self.topic1, author=self.profile2.user, position_in_topic=3) self.assertEqual(post3, self.topic1.first_unread_post(self.profile1.user)) def test_alone(self): topic2 = PrivateTopicFactory(author=self.profile1.user) self.assertFalse(self.topic1.alone()) self.assertTrue(topic2.alone()) def test_never_read(self): # scenario - topic1 : # post1 - user1 - unread # post2 - user2 - unread self.assertTrue(self.topic1.never_read(self.profile1.user)) # scenario - topic1 : # post1 - user1 - read # post2 - user2 - read mark_read(self.topic1, self.profile1.user) self.assertFalse(self.topic1.never_read(self.profile1.user)) # scenario - topic1 : # post1 - user1 - read # post2 - user2 - read # post3 - user2 - unread PrivatePostFactory(privatetopic=self.topic1, author=self.profile2.user, position_in_topic=3) self.assertTrue(self.topic1.never_read(self.profile1.user)) def test_topic_never_read_get_last_read(self): """ Trying to read last message of a never read Private Topic Should return the first message of the Topic """ tester = ProfileFactory() self.topic1.participants.add(tester.user) self.assertEqual(self.topic1.last_read_post(user=tester.user), self.post1)
class AnswerViewTest(TestCase): def setUp(self): self.profile1 = ProfileFactory() self.profile2 = ProfileFactory() self.profile3 = ProfileFactory() self.hat, _ = Hat.objects.get_or_create(name__iexact='A hat', defaults={'name': 'A hat'}) self.profile1.hats.add(self.hat) self.topic1 = PrivateTopicFactory(author=self.profile1.user) self.topic1.participants.add(self.profile2.user) self.post1 = PrivatePostFactory( privatetopic=self.topic1, author=self.profile1.user, position_in_topic=1) self.post2 = PrivatePostFactory( privatetopic=self.topic1, author=self.profile2.user, position_in_topic=2) self.assertTrue( self.client.login( username=self.profile1.user.username, password='******' ) ) def test_denies_anonymous(self): self.client.logout() response = self.client.get(reverse('private-posts-new', args=[1, 'private-topic']), follow=True) self.assertRedirects( response, reverse('member-login') + '?next=' + reverse('private-posts-new', args=[1, 'private-topic'])) def test_fail_answer_not_send_topic_pk(self): response = self.client.post(reverse('private-posts-new', args=[999, 'private-topic'])) self.assertEqual(404, response.status_code) def test_fail_answer_topic_no_exist(self): response = self.client.post(reverse('private-posts-new', args=[156, 'private-topic'])) self.assertEqual(404, response.status_code) def test_fail_cite_post_no_exist(self): response = self.client.get(reverse('private-posts-new', args=[self.topic1.pk, self.topic1.slug]) + '&cite=4864') self.assertEqual(404, response.status_code) def test_fail_cite_post_not_in_current_topic(self): another_topic = PrivateTopicFactory(author=self.profile2.user) another_post = PrivatePostFactory( privatetopic=another_topic, author=self.profile2.user, position_in_topic=1) response = self.client.get(reverse('private-posts-new', args=[self.topic1.pk, self.topic1.slug()]) + '?cite={}'.format(another_post.pk)) self.assertEqual(403, response.status_code) def test_fail_cite_weird_pk(self): response = self.client.get(reverse('private-posts-new', args=[self.topic1.pk, self.topic1.slug()]) + '?cite=abcd') self.assertEqual(404, response.status_code) def test_success_cite_post(self): response = self.client.get(reverse('private-posts-new', args=[self.topic1.pk, self.topic1.slug()]) + '?cite={}'.format(self.post2.pk)) self.assertEqual(200, response.status_code) def test_success_preview_answer(self): response = self.client.post( reverse('private-posts-new', args=[self.topic1.pk, self.topic1.slug]), { 'text': 'answer', 'preview': '', 'last_post': self.topic1.last_message.pk }, follow=True ) self.assertEqual(200, response.status_code) def test_success_answer(self): response = self.client.post( reverse('private-posts-new', args=[self.topic1.pk, self.topic1.slug]), { 'text': 'Luc !?', 'last_post': self.topic1.last_message.pk }, follow=True ) self.assertEqual(200, response.status_code) self.assertEqual(3, PrivatePost.objects.all().count()) self.assertContains(response, 'Luc !?') def test_answer_with_hat(self): response = self.client.post( reverse('private-posts-new', args=[self.topic1.pk, self.topic1.slug]), { 'text': 'Luc !?', 'last_post': self.topic1.last_message.pk, 'with_hat': self.hat.pk, }, follow=False ) self.assertEqual(302, response.status_code) self.assertEqual(PrivatePost.objects.latest('pubdate').hat, self.hat) def test_fail_answer_with_no_right(self): self.client.logout() self.assertTrue( self.client.login( username=self.profile3.user.username, password='******' ) ) response = self.client.post( reverse('private-posts-new', args=[self.topic1.pk, self.topic1.slug]), { 'text': 'answer', 'last_post': self.topic1.last_message.pk }, follow=True ) self.assertEqual(403, response.status_code) self.assertEqual(2, PrivatePost.objects.all().count()) def test_unicode_title_answer(self): """To test unicode title.""" unicode_topic = PrivateTopicFactory(author=self.profile1.user, title='Title with accent àéè') unicode_topic.participants.add(self.profile2.user) unicode_post = PrivatePostFactory( privatetopic=unicode_topic, author=self.profile1.user, position_in_topic=1) response = self.client.post( reverse('private-posts-new', args=[unicode_topic.pk, unicode_topic.slug]), { 'text': 'answer', 'last_post': unicode_post.pk }, follow=True ) self.assertEqual(response.status_code, 200) def test_unicode_subtitle_answer(self): """To test unicode subtitle.""" unicode_topic = PrivateTopicFactory(author=self.profile1.user, subtitle='Subtitle with accent àéè') unicode_topic.participants.add(self.profile2.user) unicode_post = PrivatePostFactory( privatetopic=unicode_topic, author=self.profile1.user, position_in_topic=1) response = self.client.post( reverse('private-posts-new', args=[unicode_topic.pk, unicode_topic.slug]), { 'text': 'answer', 'last_post': unicode_post.pk }, follow=True ) self.assertEqual(response.status_code, 200)
class AnswerViewTest(TestCase): def setUp(self): self.profile1 = ProfileFactory() self.profile2 = ProfileFactory() self.profile3 = ProfileFactory() self.hat, _ = Hat.objects.get_or_create(name__iexact="A hat", defaults={"name": "A hat"}) self.profile1.hats.add(self.hat) self.topic1 = PrivateTopicFactory(author=self.profile1.user) self.topic1.participants.add(self.profile2.user) self.post1 = PrivatePostFactory(privatetopic=self.topic1, author=self.profile1.user, position_in_topic=1) self.post2 = PrivatePostFactory(privatetopic=self.topic1, author=self.profile2.user, position_in_topic=2) self.assertTrue(self.client.login(username=self.profile1.user.username, password="******")) def test_denies_anonymous(self): self.client.logout() response = self.client.get(reverse("private-posts-new", args=[1, "private-topic"]), follow=True) self.assertRedirects( response, reverse("member-login") + "?next=" + reverse("private-posts-new", args=[1, "private-topic"]) ) def test_fail_answer_not_send_topic_pk(self): response = self.client.post(reverse("private-posts-new", args=[999, "private-topic"])) self.assertEqual(404, response.status_code) def test_fail_answer_topic_no_exist(self): response = self.client.post(reverse("private-posts-new", args=[156, "private-topic"])) self.assertEqual(404, response.status_code) def test_fail_cite_post_no_exist(self): response = self.client.get(reverse("private-posts-new", args=[self.topic1.pk, self.topic1.slug]) + "&cite=4864") self.assertEqual(404, response.status_code) def test_fail_cite_post_not_in_current_topic(self): another_topic = PrivateTopicFactory(author=self.profile2.user) another_post = PrivatePostFactory(privatetopic=another_topic, author=self.profile2.user, position_in_topic=1) response = self.client.get( reverse("private-posts-new", args=[self.topic1.pk, self.topic1.slug()]) + "?cite={}".format(another_post.pk) ) self.assertEqual(403, response.status_code) def test_fail_cite_weird_pk(self): response = self.client.get( reverse("private-posts-new", args=[self.topic1.pk, self.topic1.slug()]) + "?cite=abcd" ) self.assertEqual(404, response.status_code) def test_success_cite_post(self): response = self.client.get( reverse("private-posts-new", args=[self.topic1.pk, self.topic1.slug()]) + "?cite={}".format(self.post2.pk) ) self.assertEqual(200, response.status_code) def test_success_preview_answer(self): response = self.client.post( reverse("private-posts-new", args=[self.topic1.pk, self.topic1.slug]), {"text": "answer", "preview": "", "last_post": self.topic1.last_message.pk}, follow=True, ) self.assertEqual(200, response.status_code) def test_success_answer(self): response = self.client.post( reverse("private-posts-new", args=[self.topic1.pk, self.topic1.slug]), {"text": "Bonjour Luc", "last_post": self.topic1.last_message.pk}, follow=True, ) self.assertEqual(200, response.status_code) self.assertEqual(3, PrivatePost.objects.all().count()) self.assertContains(response, "Bonjour Luc") def test_answer_with_hat(self): response = self.client.post( reverse("private-posts-new", args=[self.topic1.pk, self.topic1.slug]), { "text": "Luc !?", "last_post": self.topic1.last_message.pk, "with_hat": self.hat.pk, }, follow=False, ) self.assertEqual(302, response.status_code) self.assertEqual(PrivatePost.objects.latest("pubdate").hat, self.hat) def test_fail_answer_with_no_right(self): self.client.logout() self.assertTrue(self.client.login(username=self.profile3.user.username, password="******")) response = self.client.post( reverse("private-posts-new", args=[self.topic1.pk, self.topic1.slug]), {"text": "answer", "last_post": self.topic1.last_message.pk}, follow=True, ) self.assertEqual(403, response.status_code) self.assertEqual(2, PrivatePost.objects.all().count()) def test_unicode_title_answer(self): """To test unicode title.""" unicode_topic = PrivateTopicFactory(author=self.profile1.user, title="Title with accent àéè") unicode_topic.participants.add(self.profile2.user) unicode_post = PrivatePostFactory(privatetopic=unicode_topic, author=self.profile1.user, position_in_topic=1) response = self.client.post( reverse("private-posts-new", args=[unicode_topic.pk, unicode_topic.slug]), {"text": "answer", "last_post": unicode_post.pk}, follow=True, ) self.assertEqual(response.status_code, 200) def test_unicode_subtitle_answer(self): """To test unicode subtitle.""" unicode_topic = PrivateTopicFactory(author=self.profile1.user, subtitle="Subtitle with accent àéè") unicode_topic.participants.add(self.profile2.user) unicode_post = PrivatePostFactory(privatetopic=unicode_topic, author=self.profile1.user, position_in_topic=1) response = self.client.post( reverse("private-posts-new", args=[unicode_topic.pk, unicode_topic.slug]), {"text": "answer", "last_post": unicode_post.pk}, follow=True, ) self.assertEqual(response.status_code, 200)
class PrivateTopicTest(TestCase): def setUp(self): # scenario - topic1 : # post1 - user1 - unread # post2 - user2 - unread self.user1 = ProfileFactory().user self.user2 = ProfileFactory().user self.outsider = ProfileFactory().user # Create the bot accound and add it to the bot group self.bot = ProfileFactory().user bot_group = Group(name=settings.ZDS_APP["member"]["bot_group"]) bot_group.save() self.bot.groups.add(bot_group) self.topic1 = PrivateTopicFactory(author=self.user1) self.topic1.participants.add(self.user2) self.post1 = PrivatePostFactory(privatetopic=self.topic1, author=self.user1, position_in_topic=1) self.post2 = PrivatePostFactory(privatetopic=self.topic1, author=self.user2, position_in_topic=2) def test_get_absolute_url(self): url = reverse("private-posts-list", args=[self.topic1.pk, self.topic1.slug()]) self.assertEqual(self.topic1.get_absolute_url(), url) def test_get_post_count(self): self.assertEqual(2, self.topic1.get_post_count()) def test_get_last_answer(self): topic = PrivateTopicFactory(author=self.user2) PrivatePostFactory(privatetopic=topic, author=self.user2, position_in_topic=1) self.assertEqual(self.post2, self.topic1.get_last_answer()) self.assertNotEqual(self.post1, self.topic1.get_last_answer()) self.assertIsNone(topic.get_last_answer()) def test_first_post(self): topic = PrivateTopicFactory(author=self.user2) self.assertEqual(self.post1, self.topic1.first_post()) self.assertIsNone(topic.first_post()) def test_last_read_post(self): # scenario - topic1 : # post1 - user1 - unread # post2 - user2 - unread self.assertEqual(self.post1, self.topic1.last_read_post(self.user1)) # scenario - topic1 : # post1 - user1 - read # post2 - user2 - read mark_read(self.topic1, user=self.user1) self.assertEqual(self.post2, self.topic1.last_read_post(self.user1)) # scenario - topic1 : # post1 - user1 - read # post2 - user2 - read # post3 - user2 - unread PrivatePostFactory(privatetopic=self.topic1, author=self.user2, position_in_topic=3) self.assertEqual(self.post2, self.topic1.last_read_post(self.user1)) def test_first_unread_post(self): # scenario - topic1 : # post1 - user1 - unread # post2 - user2 - unread self.assertEqual(self.post1, self.topic1.first_unread_post(self.user1)) # scenario - topic1 : # post1 - user1 - read # post2 - user2 - read # post3 - user2 - unread mark_read(self.topic1, self.user1) post3 = PrivatePostFactory(privatetopic=self.topic1, author=self.user2, position_in_topic=3) self.assertEqual(post3, self.topic1.first_unread_post(self.user1)) def test_one_participant_remaining(self): topic2 = PrivateTopicFactory(author=self.user1) self.assertFalse(self.topic1.one_participant_remaining()) self.assertTrue(topic2.one_participant_remaining()) def test_is_unread(self): # scenario - topic1 : # post1 - user1 - unread # post2 - user2 - unread self.assertTrue(self.topic1.is_unread(self.user1)) # scenario - topic1 : # post1 - user1 - read # post2 - user2 - read mark_read(self.topic1, self.user1) self.assertFalse(self.topic1.is_unread(self.user1)) # scenario - topic1 : # post1 - user1 - read # post2 - user2 - read # post3 - user2 - unread PrivatePostFactory(privatetopic=self.topic1, author=self.user2, position_in_topic=3) self.assertTrue(self.topic1.is_unread(self.user1)) def test_topic_never_read_get_last_read(self): """Trying to read last message of a never read Private Topic Should return the first message of the Topic""" tester = ProfileFactory() self.topic1.participants.add(tester.user) self.assertEqual(self.topic1.last_read_post(user=tester.user), self.post1) def test_is_author(self): self.assertEqual(self.topic1.author, self.user1) self.assertTrue(self.topic1.is_author(self.user1)) self.assertFalse(self.topic1.is_author(self.user2)) def test_set_as_author_same_author(self): self.assertEqual(self.topic1.author, self.user1) self.assertEqual(list(self.topic1.participants.all()), [self.user2]) self.topic1.set_as_author(self.user1) self.assertEqual(self.topic1.author, self.user1) self.assertEqual(list(self.topic1.participants.all()), [self.user2]) def test_set_as_author_new_author(self): self.assertEqual(self.topic1.author, self.user1) self.assertEqual(list(self.topic1.participants.all()), [self.user2]) self.topic1.set_as_author(self.user2) self.assertEqual(self.topic1.author, self.user2) self.assertEqual(list(self.topic1.participants.all()), [self.user1]) def test_set_as_author_outsider(self): self.assertEqual(self.topic1.author, self.user1) self.assertEqual(list(self.topic1.participants.all()), [self.user2]) with self.assertRaises(NotParticipatingError): self.topic1.set_as_author(self.outsider) def test_is_participant(self): self.assertEqual(self.topic1.author, self.user1) self.assertEqual(list(self.topic1.participants.all()), [self.user2]) self.assertTrue(self.topic1.is_participant(self.user1)) self.assertTrue(self.topic1.is_participant(self.user2)) self.assertFalse(self.topic1.is_participant(self.outsider)) @patch("zds.mp.signals.participant_added") def test_add_participant_unreachable_user(self, participant_added): self.assertFalse(is_reachable(self.bot)) with self.assertRaises(NotReachableError): self.topic1.add_participant(self.bot) self.assertFalse(participant_added.send.called) @patch("zds.mp.signals.participant_added") def test_add_participant_already_participating(self, participant_added): self.assertEqual(self.topic1.author, self.user1) self.assertEqual(list(self.topic1.participants.all()), [self.user2]) self.topic1.add_participant(self.user2) self.assertEqual(list(self.topic1.participants.all()), [self.user2]) self.assertFalse(participant_added.send.called) @patch("zds.mp.signals.participant_added") def test_add_participant_normal(self, participant_added): self.assertEqual(self.topic1.author, self.user1) self.assertEqual(list(self.topic1.participants.all()), [self.user2]) self.topic1.add_participant(self.outsider) self.assertEqual(self.topic1.author, self.user1) self.assertEqual(list(self.topic1.participants.all()), [self.user2, self.outsider]) self.assertEqual(participant_added.send.call_count, 1) @patch("zds.mp.signals.participant_removed") def test_remove_participant_not_participating(self, participant_removed): self.assertEqual(self.topic1.author, self.user1) self.assertEqual(list(self.topic1.participants.all()), [self.user2]) self.topic1.remove_participant(self.outsider) self.assertEqual(list(self.topic1.participants.all()), [self.user2]) self.assertFalse(participant_removed.send.called) @patch("zds.mp.signals.participant_removed") def test_remove_participant_author(self, participant_removed): self.assertEqual(self.topic1.author, self.user1) self.assertEqual(list(self.topic1.participants.all()), [self.user2]) self.topic1.remove_participant(self.user1) self.assertEqual(self.topic1.author, self.user2) self.assertEqual(list(self.topic1.participants.all()), []) self.assertEqual(participant_removed.send.call_count, 1) @patch("zds.mp.signals.participant_removed") def test_remove_participant_normal(self, participant_removed): self.assertEqual(self.topic1.author, self.user1) self.assertEqual(list(self.topic1.participants.all()), [self.user2]) self.topic1.remove_participant(self.user2) self.assertEqual(self.topic1.author, self.user1) self.assertEqual(list(self.topic1.participants.all()), []) self.assertEqual(participant_removed.send.call_count, 1)
class PrivateTopicTest(TestCase): def setUp(self): # scenario - topic1 : # post1 - user1 - unread # post2 - user2 - unread self.profile1 = ProfileFactory() self.profile2 = ProfileFactory() self.topic1 = PrivateTopicFactory(author=self.profile1.user) self.topic1.participants.add(self.profile2.user) self.post1 = PrivatePostFactory( privatetopic=self.topic1, author=self.profile1.user, position_in_topic=1) self.post2 = PrivatePostFactory( privatetopic=self.topic1, author=self.profile2.user, position_in_topic=2) def test_absolute_url(self): url = reverse('private-posts-list', args=[self.topic1.pk, self.topic1.slug()]) self.assertEqual(self.topic1.get_absolute_url(), url) def test_post_count(self): self.assertEqual(2, self.topic1.get_post_count()) def test_get_last_answer(self): topic = PrivateTopicFactory(author=self.profile2.user) PrivatePostFactory( privatetopic=topic, author=self.profile2.user, position_in_topic=1) self.assertEqual(self.post2, self.topic1.get_last_answer()) self.assertNotEqual(self.post1, self.topic1.get_last_answer()) self.assertIsNone(topic.get_last_answer()) def test_first_post(self): topic = PrivateTopicFactory(author=self.profile2.user) self.assertEqual(self.post1, self.topic1.first_post()) self.assertIsNone(topic.first_post()) def test_last_read_post(self): # scenario - topic1 : # post1 - user1 - unread # post2 - user2 - unread self.assertEqual( self.post1, self.topic1.last_read_post(self.profile1.user)) # scenario - topic1 : # post1 - user1 - read # post2 - user2 - read mark_read(self.topic1, user=self.profile1.user) self.assertEqual( self.post2, self.topic1.last_read_post(self.profile1.user)) # scenario - topic1 : # post1 - user1 - read # post2 - user2 - read # post3 - user2 - unread PrivatePostFactory( privatetopic=self.topic1, author=self.profile2.user, position_in_topic=3) self.assertEqual( self.post2, self.topic1.last_read_post(self.profile1.user)) def test_first_unread_post(self): # scenario - topic1 : # post1 - user1 - unread # post2 - user2 - unread self.assertEqual( self.post1, self.topic1.first_unread_post(self.profile1.user)) # scenario - topic1 : # post1 - user1 - read # post2 - user2 - read # post3 - user2 - unread mark_read(self.topic1, self.profile1.user) post3 = PrivatePostFactory( privatetopic=self.topic1, author=self.profile2.user, position_in_topic=3) self.assertEqual( post3, self.topic1.first_unread_post(self.profile1.user)) def test_alone(self): topic2 = PrivateTopicFactory(author=self.profile1.user) self.assertFalse(self.topic1.alone()) self.assertTrue(topic2.alone()) def test_never_read(self): # scenario - topic1 : # post1 - user1 - unread # post2 - user2 - unread self.assertTrue(self.topic1.is_unread(self.profile1.user)) # scenario - topic1 : # post1 - user1 - read # post2 - user2 - read mark_read(self.topic1, self.profile1.user) self.assertFalse(self.topic1.is_unread(self.profile1.user)) # scenario - topic1 : # post1 - user1 - read # post2 - user2 - read # post3 - user2 - unread PrivatePostFactory( privatetopic=self.topic1, author=self.profile2.user, position_in_topic=3) self.assertTrue(self.topic1.is_unread(self.profile1.user)) def test_topic_never_read_get_last_read(self): """ Trying to read last message of a never read Private Topic Should return the first message of the Topic """ tester = ProfileFactory() self.topic1.participants.add(tester.user) self.assertEqual(self.topic1.last_read_post(user=tester.user), self.post1)
class AnswerViewTest(TestCase): def setUp(self): self.profile1 = ProfileFactory() self.profile2 = ProfileFactory() self.profile3 = ProfileFactory() self.hat, _ = Hat.objects.get_or_create(name__iexact='A hat', defaults={'name': 'A hat'}) self.profile1.hats.add(self.hat) self.topic1 = PrivateTopicFactory(author=self.profile1.user) self.topic1.participants.add(self.profile2.user) self.post1 = PrivatePostFactory( privatetopic=self.topic1, author=self.profile1.user, position_in_topic=1) self.post2 = PrivatePostFactory( privatetopic=self.topic1, author=self.profile2.user, position_in_topic=2) self.assertTrue( self.client.login( username=self.profile1.user.username, password='******' ) ) def test_denies_anonymous(self): self.client.logout() response = self.client.get(reverse('private-posts-new', args=[1, 'private-topic']), follow=True) self.assertRedirects( response, reverse('member-login') + '?next=' + reverse('private-posts-new', args=[1, 'private-topic'])) def test_fail_answer_not_send_topic_pk(self): response = self.client.post(reverse('private-posts-new', args=[999, 'private-topic'])) self.assertEqual(404, response.status_code) def test_fail_answer_topic_no_exist(self): response = self.client.post(reverse('private-posts-new', args=[156, 'private-topic'])) self.assertEqual(404, response.status_code) def test_fail_cite_post_no_exist(self): response = self.client.get(reverse('private-posts-new', args=[self.topic1.pk, self.topic1.slug]) + '&cite=4864') self.assertEqual(404, response.status_code) def test_fail_cite_post_not_in_current_topic(self): another_topic = PrivateTopicFactory(author=self.profile2.user) another_post = PrivatePostFactory( privatetopic=another_topic, author=self.profile2.user, position_in_topic=1) response = self.client.get(reverse('private-posts-new', args=[self.topic1.pk, self.topic1.slug()]) + '?cite={}'.format(another_post.pk)) self.assertEqual(403, response.status_code) def test_fail_cite_weird_pk(self): response = self.client.get(reverse('private-posts-new', args=[self.topic1.pk, self.topic1.slug()]) + '?cite=abcd') self.assertEqual(404, response.status_code) def test_success_cite_post(self): response = self.client.get(reverse('private-posts-new', args=[self.topic1.pk, self.topic1.slug()]) + '?cite={}'.format(self.post2.pk)) self.assertEqual(200, response.status_code) def test_success_preview_answer(self): response = self.client.post( reverse('private-posts-new', args=[self.topic1.pk, self.topic1.slug]), { 'text': 'answer', 'preview': '', 'last_post': self.topic1.last_message.pk }, follow=True ) self.assertEqual(200, response.status_code) def test_success_answer(self): response = self.client.post( reverse('private-posts-new', args=[self.topic1.pk, self.topic1.slug]), { 'text': 'Bonjour Luc', 'last_post': self.topic1.last_message.pk }, follow=True ) self.assertEqual(200, response.status_code) self.assertEqual(3, PrivatePost.objects.all().count()) self.assertContains(response, 'Bonjour Luc') def test_answer_with_hat(self): response = self.client.post( reverse('private-posts-new', args=[self.topic1.pk, self.topic1.slug]), { 'text': 'Luc !?', 'last_post': self.topic1.last_message.pk, 'with_hat': self.hat.pk, }, follow=False ) self.assertEqual(302, response.status_code) self.assertEqual(PrivatePost.objects.latest('pubdate').hat, self.hat) def test_fail_answer_with_no_right(self): self.client.logout() self.assertTrue( self.client.login( username=self.profile3.user.username, password='******' ) ) response = self.client.post( reverse('private-posts-new', args=[self.topic1.pk, self.topic1.slug]), { 'text': 'answer', 'last_post': self.topic1.last_message.pk }, follow=True ) self.assertEqual(403, response.status_code) self.assertEqual(2, PrivatePost.objects.all().count()) def test_unicode_title_answer(self): """To test unicode title.""" unicode_topic = PrivateTopicFactory(author=self.profile1.user, title='Title with accent àéè') unicode_topic.participants.add(self.profile2.user) unicode_post = PrivatePostFactory( privatetopic=unicode_topic, author=self.profile1.user, position_in_topic=1) response = self.client.post( reverse('private-posts-new', args=[unicode_topic.pk, unicode_topic.slug]), { 'text': 'answer', 'last_post': unicode_post.pk }, follow=True ) self.assertEqual(response.status_code, 200) def test_unicode_subtitle_answer(self): """To test unicode subtitle.""" unicode_topic = PrivateTopicFactory(author=self.profile1.user, subtitle='Subtitle with accent àéè') unicode_topic.participants.add(self.profile2.user) unicode_post = PrivatePostFactory( privatetopic=unicode_topic, author=self.profile1.user, position_in_topic=1) response = self.client.post( reverse('private-posts-new', args=[unicode_topic.pk, unicode_topic.slug]), { 'text': 'answer', 'last_post': unicode_post.pk }, follow=True ) self.assertEqual(response.status_code, 200)