Exemple #1
0
class ModerationPermissions(
        TestCase
):  #Test that users without permissions can't access moderation pages
    def setUp(self):
        self.thread = ThreadFactory(reported=True)
        self.post = UserPostFactory(reported=True)

    def test_thread_ban(self):
        resp = self.client.get(self.thread.get_ban_url())
        self.assertEqual(resp.status_code, 403)

    def test_userpost_ban(self):
        resp = self.client.get(self.post.get_ban_url())
        self.assertEqual(resp.status_code, 403)

    def test_thread_reports(self):
        resp = self.client.get(reverse('dj-mod:moderation_thread_report_list'))
        self.assertEqual(resp.status_code, 403)

    def test_userpost_reports(self):
        resp = self.client.get(
            reverse('dj-mod:moderation_userpost_report_list'))
        self.assertEqual(resp.status_code, 403)

    def test_thread_report_dismiss(self):
        resp = self.client.get(self.thread.get_report_dismiss_url())
        self.assertEqual(resp.status_code, 403)

    def test_userpost_report_dismiss(self):
        resp = self.client.get(self.post.get_report_dismiss_url())
        self.assertEqual(resp.status_code, 403)
Exemple #2
0
class BanViewTestCase(TestCase):
    def setUp(self):
        self.ip_addr = '127.0.0.1'
        self.ban_data = {
            'banned_until': '2049-07-22',
            'reason': 'Breaking the rules.'
        }
        self.board = BoardFactory()
        self.thread = ThreadFactory(board=self.board, ip_address=self.ip_addr)
        self.post = UserPostFactory(thread=self.thread,
                                    ip_address=self.ip_addr)
        self.old_THREAD_COOLDOWN = settings.THREAD_COOLDOWN
        settings.THREAD_COOLDOWN = 0
        self.old_POST_COOLDOWN = settings.POST_COOLDOWN
        settings.POST_COOLDOWN = 0
        mod = ModeratorFactory.create_mod()
        self.client.force_login(mod)

    def tearDown(self):
        settings.THREAD_COOLDOWN = self.old_THREAD_COOLDOWN
        settings.POST_COOLDOWN = self.old_POST_COOLDOWN

    def test_thread_ban_get(self):
        resp = self.client.get(
            reverse('dj-mod:moderation_thread_ban',
                    kwargs={'thread_number': self.thread.thread_number}))
        self.assertEqual(resp.status_code, 200)
        self.assertTemplateUsed(resp, 'moderation/transgression_form.html')
        self.assertTrue('form' in resp.context)

    def test_thread_board_ban_post(self):
        resp = self.client.post(
            reverse('dj-mod:moderation_thread_ban',
                    kwargs={'thread_number': self.thread.thread_number}),
            self.ban_data)
        self.assertEqual(resp.status_code, 302)
        new_ban = Transgression.objects.get(reason=self.ban_data['reason'])
        self.assertEqual(new_ban.reason, self.ban_data['reason'])
        self.assertEqual(new_ban.ip_address, '127.0.0.1')
        self.assertEqual(new_ban.banned_from, self.board)

    def test_thread_global_ban_post(self):
        reason = TransgressionFactory.reason
        resp = self.client.post(
            self.thread.get_ban_url(), {
                'reason': reason,
                'banned_until': TransgressionFactory.banned_until,
                'global_ban': True
            })
        self.assertEqual(resp.status_code, 302)
        new_ban = Transgression.objects.get(reason=reason)
        self.assertEqual(new_ban.global_ban, True)
        self.assertEqual(new_ban.banned_from, None)

    def test_userpost_ban_get(self):
        resp = self.client.get(
            reverse('dj-mod:moderation_userpost_ban',
                    kwargs={'post_number': self.post.post_number}))
        self.assertEqual(resp.status_code, 200)
        self.assertTemplateUsed(resp, 'moderation/transgression_form.html')
        self.assertTrue('form' in resp.context)

    def test_userpost_board_ban_post(self):
        resp = self.client.post(
            reverse('dj-mod:moderation_userpost_ban',
                    kwargs={'post_number': self.post.post_number}),
            self.ban_data)
        self.assertEqual(resp.status_code, 302)
        new_ban = Transgression.objects.get(reason=self.ban_data['reason'])
        self.assertEqual(new_ban.reason, self.ban_data['reason'])
        self.assertEqual(new_ban.ip_address, '127.0.0.1')
        self.assertEqual(new_ban.banned_from, self.board)

    def test_userpost_global_ban_post(self):
        reason = TransgressionFactory.reason
        resp = self.client.post(
            self.post.get_ban_url(), {
                'reason': reason,
                'banned_until': TransgressionFactory.banned_until,
                'global_ban': True
            })
        self.assertEqual(resp.status_code, 302)
        new_ban = Transgression.objects.get(reason=reason)
        self.assertEqual(new_ban.global_ban, True)
        self.assertEqual(new_ban.banned_from, None)

    def test_ban_page_redirect_global_ban(
        self
    ):  #Test that banned user redirect to the banned page correctly when user has global ban
        ban = TransgressionFactory(global_ban=True, ip_address=self.ip_addr)
        resp = self.client.post(self.board.get_thread_create_url(), {
            'post': 'Im trying to break the rules!',
            'name': 'Anon'
        })
        self.assertRedirects(
            resp, expected_url=reverse('dj-mod:moderation_ban_page'))
        resp2 = self.client.post(self.thread.get_post_create_url(), {
            'post': 'Im trying to break the rules!',
            'name': 'Anon'
        })
        self.assertRedirects(
            resp2, expected_url=reverse('dj-mod:moderation_ban_page'))

    def test_ban_page_redirect_board_specific(
        self
    ):  #Test that banned user redirects to the banned page correctly when user has board specific ban
        ban = TransgressionFactory(banned_from=self.board,
                                   ip_address=self.ip_addr)
        resp = self.client.post(
            reverse('imageboard_thread_create',
                    kwargs={'board': self.board.slug}), {
                        'post': 'Im trying to break the rules!',
                        'name': 'Anon',
                        'image': ImageFactory()
                    })
        self.assertRedirects(
            resp, expected_url=reverse('dj-mod:moderation_ban_page'))
        resp2 = self.client.post(
            reverse('imageboard_userpost_create',
                    kwargs={
                        'board': self.board.slug,
                        'thread_number': self.thread.thread_number
                    }), {
                        'post': 'Im trying to break the rules!',
                        'name': 'Anon'
                    })
        self.assertRedirects(
            resp2, expected_url=reverse('dj-mod:moderation_ban_page'))

    def test_no_ban_redirect(
        self
    ):  #Test that there is no redirect when user has a ban in another board
        board2 = BoardFactory()
        ban = TransgressionFactory(banned_from=board2, ip_address=self.ip_addr)
        resp = self.client.post(
            reverse('imageboard_thread_create',
                    kwargs={'board': self.board.slug}), {
                        'post': 'Im not trying to break the rules!',
                        'name': 'Anon',
                        'image': ImageFactory(),
                        'captcha_0': 'dummy',
                        'captcha_1': 'PASSED'
                    })
        self.assertRedirects(resp,
                             expected_url=reverse('imageboard_thread_page',
                                                  kwargs={
                                                      'board': self.board.slug,
                                                      'thread_number': 1
                                                  }))
        resp2 = self.client.post(
            reverse('imageboard_userpost_create',
                    kwargs={
                        'board': self.board.slug,
                        'thread_number': self.thread.thread_number
                    }), {
                        'post': 'Im not trying to break the rules!',
                        'name': 'Anon',
                        'captcha_0': 'dummy',
                        'captcha_1': 'PASSED'
                    })
        self.assertRedirects(resp2,
                             expected_url=self.thread.get_absolute_url())

    def test_ban_page(self):  #Test that ban page displays correctly
        bans = TransgressionFactory.create_batch(5, ip_address=self.ip_addr)
        resp = self.client.get(reverse('dj-mod:moderation_ban_page'))
        self.assertEqual(resp.status_code, 200)
        self.assertTemplateUsed(resp, 'moderation/transgression_detail.html')
        self.assertTrue('transgression_list' in resp.context)
        for ban in bans:
            self.assertContains(resp, ban.reason)