예제 #1
0
    def test_reset_abuse(self):
        from django.conf import settings

        settings.ABUSE_LIMIT = 3

        from middleware import AbuseMiddleware
        from middleware import to_string

        today = datetime.date.today()
        request = mock.MagicMock()
        request.session = {"abuse_date": to_string(today - datetime.timedelta(days=10))}
        mid = AbuseMiddleware()

        abuse_reported = mid._abuse_reported(request)
        can_report_abuse = mid._can_report_abuse(request)

        abuse_reported()  # if abuse_date is old enugh it should be reseted
        self.assertEqual(request.session["abuse_date"], to_string(today - datetime.timedelta(days=2)))
        self.assertTrue(can_report_abuse())

        request.session = {"abuse_date": to_string(today - datetime.timedelta(days=3))}

        abuse_reported()
        self.assertEqual(request.session["abuse_date"], to_string(today - datetime.timedelta(days=2)))
        self.assertTrue(can_report_abuse())

        request.session = {"abuse_date": to_string(today - datetime.timedelta(days=2))}

        abuse_reported()
        self.assertEqual(request.session["abuse_date"], to_string(today - datetime.timedelta(days=1)))
        self.assertTrue(can_report_abuse())

        abuse_reported()
        self.assertEqual(request.session["abuse_date"], to_string(today))
        self.assertFalse(can_report_abuse())
예제 #2
0
    def test_set_abuse(self):
        from django.conf import settings
        settings.ABUSE_LIMIT = 3  # 3 times !

        from middleware import AbuseMiddleware
        from middleware import to_string
        today = datetime.date.today()
        request = mock.MagicMock()
        request.session = {}
        mid = AbuseMiddleware()

        abuse_reported = mid._abuse_reported(request)
        can_report_abuse = mid._can_report_abuse(request)

        abuse_reported()  # one
        self.assertEqual(
            request.session['abuse_date'],
            to_string(today - datetime.timedelta(days=2)),
        )
        self.assertTrue(can_report_abuse())

        abuse_reported()  # two
        self.assertEqual(
            request.session['abuse_date'],
            to_string(today - datetime.timedelta(days=1)),
        )
        self.assertTrue(can_report_abuse())

        abuse_reported()  # tree
        self.assertEqual(
            request.session['abuse_date'],
            to_string(today),
        )
        self.assertFalse(can_report_abuse())  # game is over !
예제 #3
0
    def test_set_abuse(self):
        from django.conf import settings

        settings.ABUSE_LIMIT = 3  # 3 times !

        from middleware import AbuseMiddleware
        from middleware import to_string

        today = datetime.date.today()
        request = mock.MagicMock()
        request.session = {}
        mid = AbuseMiddleware()

        abuse_reported = mid._abuse_reported(request)
        can_report_abuse = mid._can_report_abuse(request)

        abuse_reported()  # one
        self.assertEqual(request.session["abuse_date"], to_string(today - datetime.timedelta(days=2)))
        self.assertTrue(can_report_abuse())

        abuse_reported()  # two
        self.assertEqual(request.session["abuse_date"], to_string(today - datetime.timedelta(days=1)))
        self.assertTrue(can_report_abuse())

        abuse_reported()  # tree
        self.assertEqual(request.session["abuse_date"], to_string(today))
        self.assertFalse(can_report_abuse())  # game is over !
예제 #4
0
    def test_reset_abuse(self):
        from django.conf import settings
        settings.ABUSE_LIMIT = 3

        from middleware import AbuseMiddleware
        from middleware import to_string
        today = datetime.date.today()
        request = mock.MagicMock()
        request.session = {
            'abuse_date': to_string(today-datetime.timedelta(days=10))}
        mid = AbuseMiddleware()

        abuse_reported = mid._abuse_reported(request)
        can_report_abuse = mid._can_report_abuse(request)

        abuse_reported()  # if abuse_date is old enugh it should be reseted
        self.assertEqual(
            request.session['abuse_date'],
            to_string(today - datetime.timedelta(days=2)),
        )
        self.assertTrue(can_report_abuse())

        request.session = {
            'abuse_date': to_string(today-datetime.timedelta(days=3))}

        abuse_reported()
        self.assertEqual(
            request.session['abuse_date'],
            to_string(today - datetime.timedelta(days=2)),
        )
        self.assertTrue(can_report_abuse())

        request.session = {
            'abuse_date': to_string(today-datetime.timedelta(days=2))}

        abuse_reported()
        self.assertEqual(
            request.session['abuse_date'],
            to_string(today - datetime.timedelta(days=1)),
        )
        self.assertTrue(can_report_abuse())

        abuse_reported()
        self.assertEqual(
            request.session['abuse_date'],
            to_string(today),
        )
        self.assertFalse(can_report_abuse())