예제 #1
0
class PrivatePostTest(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):
        page = int(
            ceil(
                float(
                    self.post1.position_in_topic) /
                settings.ZDS_APP['forum']['posts_per_page']))

        url = '{0}?page={1}#p{2}'.format(
            self.post1.privatetopic.get_absolute_url(),
            page,
            self.post1.pk)

        self.assertEqual(url, self.post1.get_absolute_url())
예제 #2
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)
예제 #3
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()]) +
            '?cite={}'.format(another_post.pk))

        self.assertEqual(403, response.status_code)
예제 #4
0
class PrivatePostTest(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):
        title = u'<Post pour "{0}", #{1}>'.format(
            self.post1.privatetopic,
            self.post1.pk)
        self.assertEqual(title, self.post1.__unicode__())

    def test_absolute_url(self):
        page = int(
            ceil(
                float(
                    self.post1.position_in_topic) /
                settings.POSTS_PER_PAGE))

        url = '{0}?page={1}#p{2}'.format(
            self.post1.privatetopic.get_absolute_url(),
            page,
            self.post1.pk)

        self.assertEqual(url, self.post1.get_absolute_url())
예제 #5
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)

        self.assertTrue(
            self.client.login(
                username=self.profile1.user.username,
                password='******'
            )
        )
예제 #6
0
 def create_multiple_private_posts_for_member(
         self,
         user,
         private_topic,
         number_of_users=settings.REST_FRAMEWORK['PAGINATE_BY']):
     list = []
     for i in xrange(0, number_of_users):
         private_post = PrivatePostFactory(author=user,
                                           privatetopic=private_topic,
                                           position_in_topic=i)
         private_topic.last_message = private_post
         list.append(private_post)
     return list
예제 #7
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)
예제 #8
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.assertTrue(
            self.client.login(username=self.profile1.user.username,
                              password='******'))
예제 #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)
예제 #10
0
    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))
예제 #11
0
    def test_topic_get_page_too_far(self):
        """ get a page that is too far yet"""

        login_check = self.client.login(username=self.profile1.user.username, password="******")
        self.assertTrue(login_check)

        # 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)
예제 #12
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.assertTrue(
            self.client.login(username=self.profile1.user.username,
                              password='******'))
예제 #13
0
    def test_update_of_private_post_last_one_but_not_the_author(self):
        """
        Tries to update the last message but not the author of this message.
        """
        another_profile = ProfileFactory()
        another_private_post = PrivatePostFactory(
            author=another_profile.user,
            privatetopic=self.private_topic,
            position_in_topic=2)

        response = self.client.put(
            reverse('api-mp-message-detail',
                    args=[self.private_topic.id, another_private_post.id]))
        self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
예제 #14
0
    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))
예제 #15
0
    def setUp(self):
        self.profile = ProfileFactory()
        self.private_topic = PrivateTopicFactory(author=self.profile.user)
        self.private_post = PrivatePostFactory(author=self.profile.user,
                                               privatetopic=self.private_topic,
                                               position_in_topic=1)
        self.client = APIClient()
        client_oauth2 = create_oauth2_client(self.profile.user)
        authenticate_client(self.client, client_oauth2,
                            self.profile.user.username, 'hostel77')

        self.bot_group = Group()
        self.bot_group.name = ZDS_APP["member"]["bot_group"]
        self.bot_group.save()

        get_cache(extensions_api_settings.DEFAULT_USE_CACHE).clear()
예제 #16
0
    def test_detail_of_private_post_not_in_participants(self):
        """
        Gets an error 403 when the member doesn't have permission to display details about the private post.
        """
        another_profile = ProfileFactory()
        another_private_topic = PrivateTopicFactory(
            author=another_profile.user)
        another_private_post = PrivatePostFactory(
            author=self.profile.user,
            privatetopic=another_private_topic,
            position_in_topic=1)

        response = self.client.get(
            reverse('api-mp-message-detail',
                    args=[another_private_topic.id, another_private_post.id]))
        self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
예제 #17
0
    def test_unicode_subtitle_answer(self):
        """To test unicode subtitle."""

        unicodeTopic = PrivateTopicFactory(
            author=self.profile1.user, subtitle=u'Subtitle with accent àéè')
        unicodeTopic.participants.add(self.profile2.user)
        unicodePost = PrivatePostFactory(privatetopic=unicodeTopic,
                                         author=self.profile1.user,
                                         position_in_topic=1)

        response = self.client.post(reverse('zds.mp.views.answer') +
                                    '?sujet=' + str(unicodeTopic.pk), {
                                        'text': 'answer',
                                        'last_post': unicodePost.pk
                                    },
                                    follow=True)
        self.assertEqual(response.status_code, 200)
예제 #18
0
    def test_mark_read(self):
        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))
예제 #19
0
    def test_unicode_title_answer(self):
        """To test unicode title."""

        unicode_topic = PrivateTopicFactory(author=self.profile1.user,
                                            title=u'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)
예제 #20
0
    def setUp(self):
        self.author = ProfileFactory()
        self.user = ProfileFactory()
        self.topic = PrivateTopicFactory(author=self.author.user)
        self.topic.participants.add(self.user.user)
        self.post = PrivatePostFactory(privatetopic=self.topic,
                                       author=self.author.user,
                                       position_in_topic=1)

        # humane_delta test
        periods = ((1, 0), (2, 1), (3, 7), (4, 30), (5, 360))
        cont = dict()
        cont['date_today'] = periods[0][0]
        cont['date_yesterday'] = periods[1][0]
        cont['date_last_week'] = periods[2][0]
        cont['date_last_month'] = periods[3][0]
        cont['date_last_year'] = periods[4][0]
        self.context = Context(cont)
예제 #21
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)
예제 #22
0
    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))
예제 #23
0
class PrivatePostTest(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):
        page = int(ceil(float(self.post1.position_in_topic) / settings.ZDS_APP["forum"]["posts_per_page"]))

        url = "{0}?page={1}#p{2}".format(self.post1.privatetopic.get_absolute_url(), page, self.post1.pk)

        self.assertEqual(url, self.post1.get_absolute_url())
예제 #24
0
    def test_topic_get_page_too_far(self):
        """ get a page that is too far yet"""

        login_check = self.client.login(username=self.profile1.user.username,
                                        password='******')
        self.assertTrue(login_check)

        # 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.post(
            reverse('zds.mp.views.index') + '?page=42', {
                'items': [self.topic1.pk],
            })
        self.assertEqual(response.status_code, 200)
        # will return the last page (2)
        self.assertEqual(response.context['nb'], 2)
예제 #25
0
    def test_more_than_one_message(self):
        """ test get second page """

        login_check = self.client.login(username=self.profile1.user.username,
                                        password='******')
        self.assertTrue(login_check)

        # create many subjects (at least two pages)
        for i in range(1, settings.ZDS_APP['forum']['topics_per_page'] + 5):
            post = PrivatePostFactory(privatetopic=self.topic1,
                                      author=self.profile1.user,
                                      position_in_topic=i + 2)

        response = self.client.get(
            reverse('zds.mp.views.topic',
                    kwargs={
                        'topic_pk': self.topic1.pk,
                        'topic_slug': slugify(self.topic1.title)
                    }) + '?page=2')
        self.assertEqual(response.status_code, 200)
        self.assertEqual(response.context['posts'][-1], post)
        self.assertEqual(response.context['last_post_pk'], post.pk)
예제 #26
0
    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))
예제 #27
0
    def test_more_than_one_message(self):
        """test get second page"""

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

        # create many subjects (at least two pages)
        post = None
        for i in range(1, settings.ZDS_APP["forum"]["topics_per_page"] + 5):
            post = PrivatePostFactory(privatetopic=self.topic1,
                                      author=self.profile1.user,
                                      position_in_topic=i + 2)

        response = self.client.get(
            reverse(
                "private-posts-list",
                kwargs={
                    "pk": self.topic1.pk,
                    "topic_slug": self.topic1.slug,
                },
            ) + "?page=2")
        self.assertEqual(response.status_code, 200)
        self.assertEqual(response.context["posts"][-1], post)
        self.assertEqual(response.context["last_post_pk"], post.pk)
예제 #28
0
    def test_unregister(self):
        """Tests that unregistering user is working"""

        # test not logged user can't unregister
        self.client.logout()
        result = self.client.post(reverse('zds.member.views.unregister'),
                                  follow=False)
        self.assertEqual(result.status_code, 302)
        user = ProfileFactory()
        login_check = self.client.login(username=user.user.username,
                                        password='******')
        self.assertEqual(login_check, True)
        result = self.client.post(reverse('zds.member.views.unregister'),
                                  follow=False)
        self.assertEqual(result.status_code, 302)
        self.assertEqual(
            User.objects.filter(username=user.user.username).count(), 0)
        user = ProfileFactory()
        user2 = ProfileFactory()
        aloneGallery = GalleryFactory()
        UserGalleryFactory(gallery=aloneGallery, user=user.user)
        sharedGallery = GalleryFactory()
        UserGalleryFactory(gallery=sharedGallery, user=user.user)
        UserGalleryFactory(gallery=sharedGallery, user=user2.user)
        # first case : a published tutorial with only one author
        publishedTutorialAlone = MiniTutorialFactory(light=True)
        publishedTutorialAlone.authors.add(user.user)
        publishedTutorialAlone.save()
        # second case : a published tutorial with two authors
        publishedTutorial2 = MiniTutorialFactory(light=True)
        publishedTutorial2.authors.add(user.user)
        publishedTutorial2.authors.add(user2.user)
        publishedTutorial2.save()
        # third case : a private tutorial with only one author
        writingTutorialAlone = MiniTutorialFactory(light=True)
        writingTutorialAlone.authors.add(user.user)
        writingTutorialAlone.save()
        writingTutorialAloneGallerPath = writingTutorialAlone.gallery.get_gallery_path(
        )
        writingTutorialAlonePath = writingTutorialAlone.get_path()
        # fourth case : a private tutorial with at least two authors
        writingTutorial2 = MiniTutorialFactory(light=True)
        writingTutorial2.authors.add(user.user)
        writingTutorial2.authors.add(user2.user)
        writingTutorial2.save()
        self.client.login(username=self.staff.username, password="******")
        pub = self.client.post(reverse('zds.tutorial.views.ask_validation'), {
            'tutorial': publishedTutorialAlone.pk,
            'text': u'Ce tuto est excellent',
            'version': publishedTutorialAlone.sha_draft,
            'source': 'http://zestedesavoir.com',
        },
                               follow=False)
        self.assertEqual(pub.status_code, 302)
        # reserve tutorial
        validation = Validation.objects.get(
            tutorial__pk=publishedTutorialAlone.pk)
        pub = self.client.post(reverse('zds.tutorial.views.reservation',
                                       args=[validation.pk]),
                               follow=False)
        self.assertEqual(pub.status_code, 302)
        # publish tutorial
        pub = self.client.post(reverse('zds.tutorial.views.valid_tutorial'), {
            'tutorial': publishedTutorialAlone.pk,
            'text': u'Ce tuto est excellent',
            'is_major': True,
            'source': 'http://zestedesavoir.com',
        },
                               follow=False)
        pub = self.client.post(reverse('zds.tutorial.views.ask_validation'), {
            'tutorial': publishedTutorial2.pk,
            'text': u'Ce tuto est excellent',
            'version': publishedTutorial2.sha_draft,
            'source': 'http://zestedesavoir.com',
        },
                               follow=False)
        self.assertEqual(pub.status_code, 302)
        # reserve tutorial
        validation = Validation.objects.get(tutorial__pk=publishedTutorial2.pk)
        pub = self.client.post(reverse('zds.tutorial.views.reservation',
                                       args=[validation.pk]),
                               follow=False)
        self.assertEqual(pub.status_code, 302)
        # publish tutorial
        pub = self.client.post(reverse('zds.tutorial.views.valid_tutorial'), {
            'tutorial': publishedTutorial2.pk,
            'text': u'Ce tuto est excellent',
            'is_major': True,
            'source': 'http://zestedesavoir.com',
        },
                               follow=False)
        # same thing for articles
        publishedArticleAlone = ArticleFactory()
        publishedArticleAlone.authors.add(user.user)
        publishedArticleAlone.save()
        publishedArticle2 = ArticleFactory()
        publishedArticle2.authors.add(user.user)
        publishedArticle2.authors.add(user2.user)
        publishedArticle2.save()

        writingArticleAlone = ArticleFactory()
        writingArticleAlone.authors.add(user.user)
        writingArticleAlone.save()
        writingArticle2 = ArticleFactory()
        writingArticle2.authors.add(user.user)
        writingArticle2.authors.add(user2.user)
        writingArticle2.save()
        # ask public article
        pub = self.client.post(reverse('zds.article.views.modify'), {
            'article': publishedArticleAlone.pk,
            'comment': u'Valides moi ce bébé',
            'pending': 'Demander validation',
            'version': publishedArticleAlone.sha_draft,
            'is_major': True
        },
                               follow=False)
        self.assertEqual(pub.status_code, 302)

        login_check = self.client.login(username=self.staff.username,
                                        password='******')
        self.assertEqual(login_check, True)

        # reserve article
        validation = ArticleValidation.objects.get(
            article__pk=publishedArticleAlone.pk)
        pub = self.client.post(reverse('zds.article.views.reservation',
                                       args=[validation.pk]),
                               follow=False)
        self.assertEqual(pub.status_code, 302)

        # publish article
        pub = self.client.post(reverse('zds.article.views.modify'), {
            'article': publishedArticleAlone.pk,
            'comment-v': u'Cet article est excellent',
            'valid-article': 'Demander validation',
            'is_major': True
        },
                               follow=False)
        self.assertEqual(pub.status_code, 302)
        # ask public article
        pub = self.client.post(reverse('zds.article.views.modify'), {
            'article': publishedArticle2.pk,
            'comment': u'Valides moi ce bébé',
            'pending': 'Demander validation',
            'version': publishedArticle2.sha_draft,
            'is_major': True
        },
                               follow=False)
        self.assertEqual(pub.status_code, 302)

        login_check = self.client.login(username=self.staff.username,
                                        password='******')
        self.assertEqual(login_check, True)

        # reserve article
        validation = ArticleValidation.objects.get(
            article__pk=publishedArticle2.pk)
        pub = self.client.post(reverse('zds.article.views.reservation',
                                       args=[validation.pk]),
                               follow=False)
        self.assertEqual(pub.status_code, 302)

        # publish article
        pub = self.client.post(reverse('zds.article.views.modify'), {
            'article': publishedArticle2.pk,
            'comment-v': u'Cet article est excellent',
            'valid-article': 'Demander validation',
            'is_major': True
        },
                               follow=False)
        self.assertEqual(pub.status_code, 302)
        # about posts and topics
        authoredTopic = TopicFactory(author=user.user, forum=self.forum11)
        answeredTopic = TopicFactory(author=user2.user, forum=self.forum11)
        PostFactory(topic=answeredTopic, author=user.user, position=2)
        editedAnswer = PostFactory(topic=answeredTopic,
                                   author=user.user,
                                   position=3)
        editedAnswer.editor = user.user
        editedAnswer.save()
        privateTopic = PrivateTopicFactory(author=user.user)
        privateTopic.participants.add(user2.user)
        privateTopic.save()
        PrivatePostFactory(author=user.user,
                           privatetopic=privateTopic,
                           position_in_topic=1)
        login_check = self.client.login(username=user.user.username,
                                        password='******')
        self.assertEqual(login_check, True)
        result = self.client.post(reverse('zds.member.views.unregister'),
                                  follow=False)
        self.assertEqual(result.status_code, 302)
        self.assertEqual(publishedTutorialAlone.authors.count(), 1)
        self.assertEqual(publishedTutorialAlone.authors.first().username,
                         settings.ZDS_APP["member"]["external_account"])
        self.assertFalse(os.path.exists(writingTutorialAloneGallerPath))
        self.assertEqual(publishedTutorial2.authors.count(), 1)
        self.assertEqual(
            publishedTutorial2.authors.filter(
                username=settings.ZDS_APP["member"]
                ["external_account"]).count(), 0)
        self.assertIsNotNone(publishedTutorial2.get_prod_path())
        self.assertTrue(os.path.exists(publishedTutorial2.get_prod_path()))
        self.assertIsNotNone(publishedTutorialAlone.get_prod_path())
        self.assertTrue(os.path.exists(publishedTutorialAlone.get_prod_path()))
        self.assertEqual(
            self.client.get(reverse(
                'zds.tutorial.views.view_tutorial_online',
                args=[publishedTutorialAlone.pk, publishedTutorialAlone.slug]),
                            follow=False).status_code, 200)
        self.assertEqual(
            self.client.get(reverse(
                'zds.tutorial.views.view_tutorial_online',
                args=[publishedTutorial2.pk, publishedTutorial2.slug]),
                            follow=False).status_code, 200)
        self.assertTrue(os.path.exists(publishedArticleAlone.get_path()))
        self.assertEqual(
            self.client.get(reverse(
                'zds.article.views.view_online',
                args=[publishedArticleAlone.pk, publishedArticleAlone.slug]),
                            follow=True).status_code, 200)
        self.assertEqual(
            self.client.get(reverse(
                'zds.article.views.view_online',
                args=[publishedArticle2.pk, publishedArticle2.slug]),
                            follow=True).status_code, 200)
        self.assertEqual(
            Tutorial.objects.filter(pk=writingTutorialAlone.pk).count(), 0)
        self.assertEqual(writingTutorial2.authors.count(), 1)
        self.assertEqual(
            writingTutorial2.authors.filter(username=settings.ZDS_APP["member"]
                                            ["external_account"]).count(), 0)
        self.assertEqual(publishedArticleAlone.authors.count(), 1)
        self.assertEqual(publishedArticleAlone.authors.first().username,
                         settings.ZDS_APP["member"]["external_account"])
        self.assertEqual(publishedArticle2.authors.count(), 1)
        self.assertEqual(
            publishedArticle2.authors.filter(
                username=settings.ZDS_APP["member"]
                ["external_account"]).count(), 0)
        self.assertEqual(
            Article.objects.filter(pk=writingArticleAlone.pk).count(), 0)
        self.assertEqual(writingArticle2.authors.count(), 1)
        self.assertEqual(
            writingArticle2.authors.filter(username=settings.ZDS_APP["member"]
                                           ["external_account"]).count(), 0)
        self.assertEqual(
            Topic.objects.filter(author__username=user.user.username).count(),
            0)
        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.assertFalse(os.path.exists(writingTutorialAlonePath))
        self.assertIsNotNone(Topic.objects.get(pk=authoredTopic.pk))
        self.assertIsNotNone(PrivateTopic.objects.get(pk=privateTopic.pk))
        self.assertIsNotNone(Gallery.objects.get(pk=aloneGallery.pk))
        self.assertEquals(aloneGallery.get_users().count(), 1)
        self.assertEquals(sharedGallery.get_users().count(), 1)
        self.assertEquals(
            UserGallery.objects.filter(user=user.user).count(), 0)
예제 #29
0
파일: tests.py 프로젝트: istandre/zds-site
    def test_edit_mp_post(self):
        """To test all aspects of the edition of simple mp post by member."""

        ptopic1 = PrivateTopicFactory(author=self.user1)
        ppost1 = PrivatePostFactory(privatetopic=ptopic1,
                                    author=self.user1,
                                    position_in_topic=1)
        ppost2 = PrivatePostFactory(privatetopic=ptopic1,
                                    author=self.user1,
                                    position_in_topic=2)
        ppost3 = PrivatePostFactory(privatetopic=ptopic1,
                                    author=self.user1,
                                    position_in_topic=3)

        result = self.client.post(reverse(
            'zds.mp.views.edit_post'
        ) + '?message={0}'.format(ppost3.pk), {
            'text':
            u'C\'est tout simplement l\'histoire de la ville de Paris que je voudrais vous conter '
        },
                                  follow=False)

        self.assertEqual(result.status_code, 302)

        # check topic's number
        self.assertEqual(PrivateTopic.objects.all().count(), 1)

        # check post's number
        self.assertEqual(PrivatePost.objects.all().count(), 3)

        # check topic and post
        self.assertEqual(ppost1.privatetopic, ptopic1)
        self.assertEqual(ppost2.privatetopic, ptopic1)
        self.assertEqual(ppost3.privatetopic, ptopic1)

        # check values
        self.assertEqual(
            PrivatePost.objects.get(pk=ppost3.pk).text,
            u"C\'est tout simplement l\'histoire de la ville de Paris que je voudrais vous conter "
        )

        # check no email has been sent
        self.assertEquals(len(mail.outbox), 0)

        # i can edit a mp if it's not last
        result = self.client.post(reverse(
            'zds.mp.views.edit_post'
        ) + '?message={0}'.format(ppost2.pk), {
            'text':
            u"C\'est tout simplement l\'histoire de la ville de Paris que je voudrais vous conter "
        },
                                  follow=False)

        self.assertEqual(result.status_code, 403)

        # staff can't edit mp if he's not author
        staff = StaffProfileFactory().user
        log = self.client.login(username=staff.username, password='******')
        self.assertEqual(log, True)

        result = self.client.post(reverse(
            'zds.mp.views.edit_post'
        ) + '?message={0}'.format(ppost3.pk), {
            'text':
            u'C\'est tout simplement l\'histoire de la ville de Paris que je voudrais vous conter '
        },
                                  follow=False)

        self.assertEqual(result.status_code, 403)
예제 #30
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 register.
        user = ProfileFactory()
        login_check = self.client.login(username=user.user.username,
                                        password='******')
        self.assertEqual(login_check, True)
        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.login(username=self.staff.username, password="******")
        # 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=CategoryFactory())
        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)
        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)

        # login and unregister:
        login_check = self.client.login(username=user.user.username,
                                        password='******')
        self.assertEqual(login_check, True)
        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(
            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.assertEquals(alone_gallery.get_linked_users().count(), 1)
        self.assertEquals(shared_gallery.get_linked_users().count(), 1)
        self.assertEquals(
            UserGallery.objects.filter(user=user.user).count(), 0)
        self.assertEquals(
            CommentVote.objects.filter(user=user.user, positive=True).count(),
            0)
        self.assertEquals(
            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)