<commit_before>from hawkrest import HawkAuthentication
from rest_framework.response import Response
from rest_framework.test import APIRequestFactory
from rest_framework.views import APIView

from treeherder.webapp.api import throttling


class HawkClient1SecRateThrottle(throttling.HawkClientThrottle):
    THROTTLE_RATES = {'foo': '1/sec'}


class MockView(APIView):
    throttle_classes = (HawkClient1SecRateThrottle,)
    throttle_scope = 'foo'

    def get(self, request):
        return Response('foo')


class MockReceiver(object):
    parsed_header = {'id': 'my-client-id'}


def mock_authenticate(authentication_class, request):
    request.META['hawk.receiver'] = MockReceiver()


factory = APIRequestFactory()


def test_no_throttle():
    request = factory.get('/')

    response = MockView.as_view()(request)
    # first request ok
    response.status_code == 200

    for i in range(1):
        response = MockView.as_view()(request)
    # subsequent requests still ok
    response.status_code == 200


def test_hawk_client_throttle(monkeypatch):

    monkeypatch.setattr(HawkAuthentication, 'authenticate', mock_authenticate)

    request = factory.get('/')
    response = MockView.as_view()(request)

    # first request, everything ok
    response.status_code == 200

    for i in range(1):
        response = MockView.as_view()(request)
    # subsequent requests should get throttled
    assert response.status_code == 429<commit_msg>Bug 1271256 - Fix always passing tests in test_throttling.py<commit_after>from hawkrest import HawkAuthentication
from rest_framework.response import Response
from rest_framework.test import APIRequestFactory
from rest_framework.views import APIView

from treeherder.webapp.api import throttling


class HawkClient1SecRateThrottle(throttling.HawkClientThrottle):
    THROTTLE_RATES = {'foo': '1/sec'}


class MockView(APIView):
    throttle_classes = (HawkClient1SecRateThrottle,)
    throttle_scope = 'foo'

    def get(self, request):
        return Response('foo')


class MockReceiver(object):
    parsed_header = {'id': 'my-client-id'}


def mock_authenticate(authentication_class, request):
    request.META['hawk.receiver'] = MockReceiver()


factory = APIRequestFactory()


def test_no_throttle():
    request = factory.get('/')

    response = MockView.as_view()(request)
    # first request ok
    assert response.status_code == 200

    for i in range(1):
        response = MockView.as_view()(request)
    # subsequent requests still ok
    assert response.status_code == 200


def test_hawk_client_throttle(monkeypatch):

    monkeypatch.setattr(HawkAuthentication, 'authenticate', mock_authenticate)

    request = factory.get('/')
    response = MockView.as_view()(request)

    # first request, everything ok
    assert response.status_code == 200

    for i in range(1):
        response = MockView.as_view()(request)
    # subsequent requests should get throttled
    assert response.status_code == 429
{'fraction_non_alphanumeric': 0.09623149394347241, 'fraction_numerical': 0.01379542395693136, 'mean_word_length': 4.308928571428571, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '348304', 'n_tokens_mistral': 958, 'n_tokens_neox': 885, 'n_words': 209}