Ejemplo n.º 1
0
    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())
Ejemplo n.º 2
0
    def setUp(self):
        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)
Ejemplo n.º 3
0
    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)
Ejemplo n.º 4
0
class FunctionTest(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_never_privateread(self):
        self.assertTrue(is_privatetopic_unread(self.topic1,
                                               self.profile1.user))
        mark_read(self.topic1, self.profile1.user)
        self.assertFalse(
            is_privatetopic_unread(self.topic1, self.profile1.user))

    @patch("zds.mp.signals.topic_read")
    def test_mark_read(self, topic_read):
        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))
        self.assertEqual(topic_read.send.call_count, 1)

        # 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))
Ejemplo n.º 5
0
    def test_fail_delete_topic_not_belong_to_user(self):
        topic = PrivateTopicFactory(author=self.profile1.user)

        self.assertEqual(1, PrivateTopic.objects.filter(pk=topic.pk).count())

        self.client.force_login(self.profile2.user)

        self.client.post(reverse("mp-list-delete"), {"items": [topic.pk]})

        self.assertEqual(1, PrivateTopic.objects.filter(pk=topic.pk).count())
Ejemplo n.º 6
0
    def test_success_delete_topic_no_participants(self):
        topic = PrivateTopicFactory(author=self.profile1.user)
        self.client.force_login(self.profile1.user)
        self.assertEqual(1, PrivateTopic.objects.filter(pk=topic.pk).count())

        response = self.client.post(reverse("mp-list-delete"),
                                    {"items": [topic.pk]})

        self.assertEqual(302, response.status_code)
        self.assertEqual(0, PrivateTopic.objects.filter(pk=topic.pk).count())
Ejemplo n.º 7
0
    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.client.force_login(self.profile1.user)
Ejemplo n.º 8
0
    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()]) +
            f"?cite={another_post.pk}")

        self.assertEqual(403, response.status_code)
Ejemplo n.º 9
0
    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)
Ejemplo n.º 10
0
    def setUp(self):
        self.profile1 = ProfileFactory()
        self.profile2 = ProfileFactory()

        self.anonymous_account = UserFactory(
            username=settings.ZDS_APP["member"]["anonymous_account"])
        self.bot_group = Group()
        self.bot_group.name = settings.ZDS_APP["member"]["bot_group"]
        self.bot_group.save()
        self.anonymous_account.groups.add(self.bot_group)
        self.anonymous_account.save()

        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.client.force_login(self.profile1.user)
Ejemplo n.º 11
0
    def test_topic_get_page_too_far(self):
        """get a page that is too far yet"""

        self.client.force_login(self.profile1.user)

        # create many subjects (at least two pages)
        for i in range(1, settings.ZDS_APP["forum"]["topics_per_page"] + 5):
            topic = PrivateTopicFactory(author=self.profile1.user)
            topic.participants.add(self.profile2.user)
            PrivatePostFactory(privatetopic=topic,
                               author=self.profile1.user,
                               position_in_topic=1)

        response = self.client.get(reverse("mp-list") + "?page=42")
        self.assertEqual(response.status_code, 404)
Ejemplo n.º 12
0
    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)
Ejemplo n.º 13
0
 def create_notification_for_pm(self, sender, target):
     topic = PrivateTopicFactory(author=sender)
     topic.add_participant(target, silent=True)
     send_message_mp(author=sender, n_topic=topic, text="Testing")
     return topic
Ejemplo n.º 14
0
class PrivateTopicEditTest(TestCase):
    def setUp(self):
        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_denies_anonymous(self):

        self.client.logout()
        self.topic1.title = "super title"
        self.topic1.subtitle = "super subtitle"
        self.topic1.save()

        # get
        response = self.client.get(reverse(
            "mp-edit-topic", args=[self.topic1.pk, "private-topic"]),
                                   follow=True)

        self.assertRedirects(
            response,
            reverse("member-login") + "?next=" +
            reverse("mp-edit-topic", args=[self.topic1.pk, "private-topic"]),
        )

        # post
        response = self.client.post(
            reverse("mp-edit-topic", args=[self.topic1.pk, "private-topic"]),
            {
                "title": "test",
                "subtitle": "subtest"
            },
            follow=True,
        )

        self.assertRedirects(
            response,
            reverse("member-login") + "?next=" +
            reverse("mp-edit-topic", args=[self.topic1.pk, "private-topic"]),
        )

        topic = PrivateTopic.objects.get(pk=self.topic1.pk)
        self.assertEqual("super title", topic.title)
        self.assertEqual("super subtitle", topic.subtitle)

    def test_success_edit_topic(self):
        self.client.force_login(self.profile1.user)

        response = self.client.post(
            reverse("mp-edit-topic", args=[self.topic1.pk, "private-topic"]),
            {
                "title": "test",
                "subtitle": "subtest"
            },
            follow=True,
        )

        self.assertEqual(200, response.status_code)

        topic = PrivateTopic.objects.get(pk=self.topic1.pk)
        self.assertEqual("test", topic.title)
        self.assertEqual("subtest", topic.subtitle)

    def test_fail_user_is_not_author(self):

        self.topic1.title = "super title"
        self.topic1.subtitle = "super subtitle"
        self.topic1.save()

        self.client.force_login(self.profile2.user)

        response = self.client.get(reverse(
            "mp-edit-topic", args=[self.topic1.pk, "private-topic"]),
                                   follow=True)
        self.assertEqual(403, response.status_code)

        response = self.client.post(
            reverse("mp-edit-topic", args=[self.topic1.pk, "private-topic"]),
            {
                "title": "test",
                "subtitle": "subtest"
            },
            follow=True,
        )

        self.assertEqual(403, response.status_code)

        topic = PrivateTopic.objects.get(pk=self.topic1.pk)
        self.assertEqual("super title", topic.title)
        self.assertEqual("super subtitle", topic.subtitle)

    def test_fail_topic_doesnt_exist(self):
        self.client.force_login(self.profile1.user)

        response = self.client.get(reverse("mp-edit-topic",
                                           args=[91, "private-topic"]),
                                   follow=True)
        self.assertEqual(404, response.status_code)

        response = self.client.post(reverse("mp-edit-topic",
                                            args=[91, "private-topic"]), {
                                                "title": "test",
                                                "subtitle": "subtest"
                                            },
                                    follow=True)
        self.assertEqual(404, response.status_code)

    def test_fail_blank_title(self):

        self.topic1.title = "super title"
        self.topic1.subtitle = "super subtitle"
        self.topic1.save()

        self.client.force_login(self.profile1.user)

        response = self.client.post(
            reverse("mp-edit-topic", args=[self.topic1.pk, "private-topic"]),
            {
                "title": "",
                "subtitle": "subtest"
            },
            follow=True,
        )

        self.assertEqual(200, response.status_code)

        topic = PrivateTopic.objects.get(pk=self.topic1.pk)
        self.assertEqual("super title", topic.title)
        self.assertEqual("super subtitle", topic.subtitle)
Ejemplo n.º 15
0
class LeaveViewTest(TestCase):
    def setUp(self):
        self.profile1 = ProfileFactory()
        self.profile2 = ProfileFactory()

        self.anonymous_account = UserFactory(
            username=settings.ZDS_APP["member"]["anonymous_account"])
        self.bot_group = Group()
        self.bot_group.name = settings.ZDS_APP["member"]["bot_group"]
        self.bot_group.save()
        self.anonymous_account.groups.add(self.bot_group)
        self.anonymous_account.save()

        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.client.force_login(self.profile1.user)

    def test_denies_anonymous(self):

        self.client.logout()
        response = self.client.get(reverse("mp-delete",
                                           args=[1, "private-topic"]),
                                   follow=True)

        self.assertRedirects(
            response,
            reverse("member-login") + "?next=" +
            reverse("mp-delete", args=[1, "private-topic"]))

    def test_fail_leave_topic_no_exist(self):

        response = self.client.post(
            reverse("mp-delete", args=[999, "private-topic"]))

        self.assertEqual(404, response.status_code)

    def test_success_leave_topic_as_author_no_participants(self):

        self.topic1.participants.clear()
        self.topic1.save()

        response = self.client.post(reverse(
            "mp-delete", args=[self.topic1.pk, self.topic1.slug]),
                                    follow=True)

        self.assertEqual(200, response.status_code)
        self.assertEqual(
            0,
            PrivateTopic.objects.filter(pk=self.topic1.pk).all().count())

    def test_success_leave_topic_as_author(self):

        response = self.client.post(reverse(
            "mp-delete", args=[self.topic1.pk, self.topic1.slug]),
                                    follow=True)

        self.assertEqual(200, response.status_code)
        self.assertEqual(
            1,
            PrivateTopic.objects.filter(pk=self.topic1.pk).all().count())

        self.assertEqual(self.profile2.user,
                         PrivateTopic.objects.get(pk=self.topic1.pk).author)

    def test_success_leave_topic_as_participant(self):

        self.client.logout()
        self.client.force_login(self.profile2.user)

        response = self.client.post(reverse(
            "mp-delete", args=[self.topic1.pk, self.topic1.slug]),
                                    follow=True)

        self.assertEqual(200, response.status_code)

        self.assertNotIn(
            self.profile2.user,
            PrivateTopic.objects.get(pk=self.topic1.pk).participants.all())

        self.assertNotEqual(self.profile2.user,
                            PrivateTopic.objects.get(pk=self.topic1.pk).author)
Ejemplo n.º 16
0
 def test_first_post(self):
     topic = PrivateTopicFactory(author=self.user2)
     self.assertEqual(self.post1, self.topic1.first_post())
     self.assertIsNone(topic.first_post())
Ejemplo n.º 17
0
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)
Ejemplo n.º 18
0
    def test_unregister(self):
        """
        To test that unregistering user is working.
        """

        # test not logged user can't unregister.
        self.client.logout()
        result = self.client.post(reverse("member-unregister"), follow=False)
        self.assertEqual(result.status_code, 302)

        # test logged user can unregister.
        user = ProfileFactory()
        self.client.force_login(user.user)
        result = self.client.post(reverse("member-unregister"), follow=False)
        self.assertEqual(result.status_code, 302)
        self.assertEqual(User.objects.filter(username=user.user.username).count(), 0)

        # Attach a user at tutorials, articles, topics and private topics. After that,
        # unregister this user and check that he is well removed in all contents.
        user = ProfileFactory()
        user2 = ProfileFactory()
        alone_gallery = GalleryFactory()
        UserGalleryFactory(gallery=alone_gallery, user=user.user)
        shared_gallery = GalleryFactory()
        UserGalleryFactory(gallery=shared_gallery, user=user.user)
        UserGalleryFactory(gallery=shared_gallery, user=user2.user)
        # first case : a published tutorial with only one author
        published_tutorial_alone = PublishedContentFactory(type="TUTORIAL")
        published_tutorial_alone.authors.add(user.user)
        published_tutorial_alone.save()
        # second case : a published tutorial with two authors
        published_tutorial_2 = PublishedContentFactory(type="TUTORIAL")
        published_tutorial_2.authors.add(user.user)
        published_tutorial_2.authors.add(user2.user)
        published_tutorial_2.save()
        # third case : a private tutorial with only one author
        writing_tutorial_alone = PublishableContentFactory(type="TUTORIAL")
        writing_tutorial_alone.authors.add(user.user)
        writing_tutorial_alone.save()
        writing_tutorial_alone_galler_path = writing_tutorial_alone.gallery.get_gallery_path()
        # fourth case : a private tutorial with at least two authors
        writing_tutorial_2 = PublishableContentFactory(type="TUTORIAL")
        writing_tutorial_2.authors.add(user.user)
        writing_tutorial_2.authors.add(user2.user)
        writing_tutorial_2.save()
        self.client.force_login(self.staff)
        # same thing for articles
        published_article_alone = PublishedContentFactory(type="ARTICLE")
        published_article_alone.authors.add(user.user)
        published_article_alone.save()
        published_article_2 = PublishedContentFactory(type="ARTICLE")
        published_article_2.authors.add(user.user)
        published_article_2.authors.add(user2.user)
        published_article_2.save()
        writing_article_alone = PublishableContentFactory(type="ARTICLE")
        writing_article_alone.authors.add(user.user)
        writing_article_alone.save()
        writing_article_2 = PublishableContentFactory(type="ARTICLE")
        writing_article_2.authors.add(user.user)
        writing_article_2.authors.add(user2.user)
        writing_article_2.save()
        # beta content
        beta_forum = ForumFactory(category=ForumCategoryFactory())
        beta_content = BetaContentFactory(author_list=[user.user], forum=beta_forum)
        beta_content_2 = BetaContentFactory(author_list=[user.user, user2.user], forum=beta_forum)
        # about posts and topics
        authored_topic = TopicFactory(author=user.user, forum=self.forum11, solved_by=user.user)
        answered_topic = TopicFactory(author=user2.user, forum=self.forum11)
        PostFactory(topic=answered_topic, author=user.user, position=2)
        edited_answer = PostFactory(topic=answered_topic, author=user.user, position=3)
        edited_answer.editor = user.user
        edited_answer.save()

        upvoted_answer = PostFactory(topic=answered_topic, author=user2.user, position=4)
        upvoted_answer.like += 1
        upvoted_answer.save()
        CommentVote.objects.create(user=user.user, comment=upvoted_answer, positive=True)

        private_topic = PrivateTopicFactory(author=user.user)
        private_topic.participants.add(user2.user)
        private_topic.save()
        PrivatePostFactory(author=user.user, privatetopic=private_topic, position_in_topic=1)

        # add API key
        self.assertEqual(Application.objects.count(), 0)
        self.assertEqual(AccessToken.objects.count(), 0)
        api_application = Application()
        api_application.client_id = "foobar"
        api_application.user = user.user
        api_application.client_type = "confidential"
        api_application.authorization_grant_type = "password"
        api_application.client_secret = "42"
        api_application.save()
        token = AccessToken()
        token.user = user.user
        token.token = "r@d0m"
        token.application = api_application
        token.expires = datetime.now()
        token.save()
        self.assertEqual(Application.objects.count(), 1)
        self.assertEqual(AccessToken.objects.count(), 1)

        # add a karma note and a sanction with this user
        note = KarmaNote(moderator=user.user, user=user2.user, note="Good!", karma=5)
        note.save()
        ban = Ban(moderator=user.user, user=user2.user, type="Ban définitif", note="Test")
        ban.save()

        # login and unregister:
        self.client.force_login(user.user)
        result = self.client.post(reverse("member-unregister"), follow=False)
        self.assertEqual(result.status_code, 302)

        # check that the bot have taken authorship of tutorial:
        self.assertEqual(published_tutorial_alone.authors.count(), 1)
        self.assertEqual(
            published_tutorial_alone.authors.first().username, settings.ZDS_APP["member"]["external_account"]
        )
        self.assertFalse(os.path.exists(writing_tutorial_alone_galler_path))
        self.assertEqual(published_tutorial_2.authors.count(), 1)
        self.assertEqual(
            published_tutorial_2.authors.filter(username=settings.ZDS_APP["member"]["external_account"]).count(), 0
        )

        # check that published tutorials remain published and accessible
        self.assertIsNotNone(published_tutorial_2.public_version.get_prod_path())
        self.assertTrue(os.path.exists(published_tutorial_2.public_version.get_prod_path()))
        self.assertIsNotNone(published_tutorial_alone.public_version.get_prod_path())
        self.assertTrue(os.path.exists(published_tutorial_alone.public_version.get_prod_path()))
        self.assertEqual(
            self.client.get(
                reverse("tutorial:view", args=[published_tutorial_alone.pk, published_tutorial_alone.slug]),
                follow=False,
            ).status_code,
            200,
        )
        self.assertEqual(
            self.client.get(
                reverse("tutorial:view", args=[published_tutorial_2.pk, published_tutorial_2.slug]), follow=False
            ).status_code,
            200,
        )

        # test that published articles remain accessible
        self.assertTrue(os.path.exists(published_article_alone.public_version.get_prod_path()))
        self.assertEqual(
            self.client.get(
                reverse("article:view", args=[published_article_alone.pk, published_article_alone.slug]), follow=True
            ).status_code,
            200,
        )
        self.assertEqual(
            self.client.get(
                reverse("article:view", args=[published_article_2.pk, published_article_2.slug]), follow=True
            ).status_code,
            200,
        )

        # check that the tutorial for which the author was alone does not exists anymore
        self.assertEqual(PublishableContent.objects.filter(pk=writing_tutorial_alone.pk).count(), 0)
        self.assertFalse(os.path.exists(writing_tutorial_alone.get_repo_path()))

        # check that bot haven't take the authorship of the tuto with more than one author
        self.assertEqual(writing_tutorial_2.authors.count(), 1)
        self.assertEqual(
            writing_tutorial_2.authors.filter(username=settings.ZDS_APP["member"]["external_account"]).count(), 0
        )

        # authorship for the article for which user was the only author
        self.assertEqual(published_article_alone.authors.count(), 1)
        self.assertEqual(
            published_article_alone.authors.first().username, settings.ZDS_APP["member"]["external_account"]
        )
        self.assertEqual(published_article_2.authors.count(), 1)

        self.assertEqual(PublishableContent.objects.filter(pk=writing_article_alone.pk).count(), 0)
        self.assertFalse(os.path.exists(writing_article_alone.get_repo_path()))

        # not bot if another author:
        self.assertEqual(
            published_article_2.authors.filter(username=settings.ZDS_APP["member"]["external_account"]).count(), 0
        )
        self.assertEqual(writing_article_2.authors.count(), 1)
        self.assertEqual(
            writing_article_2.authors.filter(username=settings.ZDS_APP["member"]["external_account"]).count(), 0
        )

        # topics, gallery and PMs:
        self.assertEqual(Topic.objects.filter(author__username=user.user.username).count(), 0)
        self.assertEqual(Topic.objects.filter(solved_by=user.user).count(), 0)
        self.assertEqual(Topic.objects.filter(solved_by=self.anonymous).count(), 1)
        self.assertEqual(Post.objects.filter(author__username=user.user.username).count(), 0)
        self.assertEqual(Post.objects.filter(editor__username=user.user.username).count(), 0)
        self.assertEqual(PrivatePost.objects.filter(author__username=user.user.username).count(), 0)
        self.assertEqual(PrivateTopic.objects.filter(author__username=user.user.username).count(), 0)

        self.assertIsNotNone(Topic.objects.get(pk=authored_topic.pk))
        self.assertIsNotNone(PrivateTopic.objects.get(pk=private_topic.pk))
        self.assertIsNotNone(Gallery.objects.get(pk=alone_gallery.pk))
        self.assertEqual(alone_gallery.get_linked_users().count(), 1)
        self.assertEqual(shared_gallery.get_linked_users().count(), 1)
        self.assertEqual(UserGallery.objects.filter(user=user.user).count(), 0)
        self.assertEqual(CommentVote.objects.filter(user=user.user, positive=True).count(), 0)
        self.assertEqual(Post.objects.filter(pk=upvoted_answer.id).first().like, 0)

        # zep 12, published contents and beta
        self.assertIsNotNone(PublishedContent.objects.filter(content__pk=published_tutorial_alone.pk).first())
        self.assertIsNotNone(PublishedContent.objects.filter(content__pk=published_tutorial_2.pk).first())
        self.assertTrue(Topic.objects.get(pk=beta_content.beta_topic.pk).is_locked)
        self.assertFalse(Topic.objects.get(pk=beta_content_2.beta_topic.pk).is_locked)

        # check API
        self.assertEqual(Application.objects.count(), 0)
        self.assertEqual(AccessToken.objects.count(), 0)

        # check that the karma note and the sanction were kept
        self.assertTrue(KarmaNote.objects.filter(pk=note.pk).exists())
        self.assertTrue(Ban.objects.filter(pk=ban.pk).exists())
Ejemplo n.º 19
0
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.client.force_login(self.profile1.user)

    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()]) +
            f"?cite={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()]) +
            f"?cite={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.client.force_login(self.profile3.user)

        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)
Ejemplo n.º 20
0
 def setUp(self):
     self.profile = ProfileFactory()
     self.topic = PrivateTopicFactory(author=self.profile.user)
Ejemplo n.º 21
0
 def test_one_participant_remaining(self):
     topic2 = PrivateTopicFactory(author=self.user1)
     self.assertFalse(self.topic1.one_participant_remaining())
     self.assertTrue(topic2.one_participant_remaining())