def test_client_success(self):
        factory = RequestFactory()
        request = factory.post('/clientstats/a', {'data': json.dumps({})})
        request.META['HTTP_USER_AGENT'] = 'Testing'
        request.META['REMOTE_ADDR'] = '127.0.0.1'

        user = views.user_key(request, salt='abc123')
        request.COOKIES['user'] = user

        response = views.client_stats(request, 'a', _now=self.NOW)

        content = self.assertJSON(response)
        # Should be a success
        self.assertEqual(content['code'], 0)
        # Give us more stats at a later stage
        self.assertGreater(content['next'], 0)

        # Assert some stuff is in the database
        stats = models.ClientStats.objects.get(created_by=user,
                                               created_on=self.NOW)

        # FIXME: This changes everytime the list of default
        # saved values changes.
        self.assertListEqual(['ip', 'user-agent'],
                             list(str(x) for x in stats.name_and_values.all()))
Пример #2
0
    def test_client_success(self):
        factory = RequestFactory()
        request = factory.post('/clientstats/a', {'data': json.dumps({})})
        request.META['HTTP_USER_AGENT'] = 'Testing'
        request.META['REMOTE_ADDR'] = '127.0.0.1'

        user = views.user_key(request, salt='abc123')
        request.COOKIES['user'] = user

        response = views.client_stats(request, 'a', _now=self.NOW)

        content = self.assertJSON(response)
        # Should be a success
        self.assertEqual(content['code'], 0)
        # Give us more stats at a later stage
        self.assertGreater(content['next'], 0)

        # Assert some stuff is in the database
        stats = models.ClientStats.objects.get(created_by=user,
                                               created_on=self.NOW)

        # FIXME: This changes everytime the list of default
        # saved values changes.
        self.assertListEqual(
            ['ip', 'user-agent'],
            list(str(x) for x in stats.name_and_values.all()))
    def test_user_key(self):
        factory = RequestFactory()
        request = factory.post('/clientstats', {})
        request.META['HTTP_USER_AGENT'] = 'Testing'
        request.META['REMOTE_ADDR'] = '127.0.0.1'

        keya = views.user_key(request)
        keyb = views.user_key(request)
        self.assertNotEqual(
            keya, keyb,
            'user_key should generate *different* keys for same input when not'
            ' given a salt.')

        keya = views.user_key(request, salt='abc123')
        keyb = views.user_key(request, salt='abc123')
        self.assertEqual(
            keya, keyb,
            'user_key should generate thhe *same* keys for same input when'
            ' a salt is given.')
    def test_user_key(self):
        factory = RequestFactory()
        request = factory.post('/clientstats', {})
        request.META['HTTP_USER_AGENT'] = 'Testing'
        request.META['REMOTE_ADDR'] = '127.0.0.1'

        keya = views.user_key(request)
        keyb = views.user_key(request)
        self.assertNotEqual(
            keya, keyb,
            'user_key should generate *different* keys for same input when not'
            ' given a salt.')

        keya = views.user_key(request, salt='abc123')
        keyb = views.user_key(request, salt='abc123')
        self.assertEqual(
            keya, keyb,
            'user_key should generate thhe *same* keys for same input when'
            ' a salt is given.')
    def test_client_common_error_on_broken_cookie(self):
        factory = RequestFactory()
        request = factory.post('/clientstats', {})
        request.META['HTTP_USER_AGENT'] = 'Testing'
        request.META['REMOTE_ADDR'] = '127.0.0.1'
        request.COOKIES['user'] = views.user_key(request, salt='abc123')

        # Change this header so the user_key shouldn't match anymore
        request.META['HTTP_USER_AGENT'] = 'Testing Modified'

        response, group, key = views.client_common(request, 'a')
        self.assertNotEqual(response, None)
        self.assertEqual(response.status_code, 200)

        content = self.assertJSON(response)
        self.assertWarningCode(content)
        self.assertRetry(content)
        self.assertCookie(response, 'user', '')
    def test_client_common_error_on_broken_cookie(self):
        factory = RequestFactory()
        request = factory.post('/clientstats', {})
        request.META['HTTP_USER_AGENT'] = 'Testing'
        request.META['REMOTE_ADDR'] = '127.0.0.1'
        request.COOKIES['user'] = views.user_key(request, salt='abc123')

        # Change this header so the user_key shouldn't match anymore
        request.META['HTTP_USER_AGENT'] = 'Testing Modified'

        response, group, key = views.client_common(request, 'a')
        self.assertNotEqual(response, None)
        self.assertEqual(response.status_code, 200)

        content = self.assertJSON(response)
        self.assertWarningCode(content)
        self.assertRetry(content)
        self.assertCookie(response, 'user', '')
    def test_client_badjson(self):
        factory = RequestFactory()
        request = factory.post('/clientstats/a', {'data': '{'})
        request.META['HTTP_USER_AGENT'] = 'Testing'
        request.META['REMOTE_ADDR'] = '127.0.0.1'

        user = views.user_key(request, salt='abc123')
        request.COOKIES['user'] = user

        response = views.client_stats(request, 'a', _now=self.NOW)

        content = self.assertJSON(response)
        self.assertErrorCode(content)

        # Assert no stats where added..
        self.assertRaises(
            ObjectDoesNotExist,
            models.ClientStats.objects.get,
            created_by=user, created_on=self.NOW,
            )
    def test_client_badjson(self):
        factory = RequestFactory()
        request = factory.post('/clientstats/a', {'data': '{'})
        request.META['HTTP_USER_AGENT'] = 'Testing'
        request.META['REMOTE_ADDR'] = '127.0.0.1'

        user = views.user_key(request, salt='abc123')
        request.COOKIES['user'] = user

        response = views.client_stats(request, 'a', _now=self.NOW)

        content = self.assertJSON(response)
        self.assertErrorCode(content)

        # Assert no stats where added..
        self.assertRaises(
            ObjectDoesNotExist,
            models.ClientStats.objects.get,
            created_by=user,
            created_on=self.NOW,
        )