<commit_before># -*- coding: utf-8 -*-
"""Tests for the squarelet integration app"""

# Django
from django.conf import settings
from django.core.urlresolvers import reverse
from django.test import TestCase
from django.test.client import RequestFactory

# Standard Library
import hashlib
import hmac
import time
import uuid

# Third Party
from mock import patch
from nose.tools import eq_

# MuckRock
from muckrock.squarelet.views import webhook


class ViewsTest(TestCase):
    """Test the squarelet webhook view"""

    def setUp(self):
        self.request_factory = RequestFactory()
        self.view = webhook
        self.url = reverse('squarelet-webhook')
        self.data = {
            'type': 'user',
            'uuids': [
                str(uuid.uuid4()),
                str(uuid.uuid4()),
            ],
            'timestamp': int(time.time()),
        }
        self.data['signature'] = self._calc_signature(
            self.data['timestamp'],
            self.data['type'],
            self.data['uuids'],
        )

    def _calc_signature(self, timestamp, type_, uuids):
        """Calculate the webhook signature"""
        return hmac.new(
            key=settings.SQUARELET_SECRET.encode('utf8'),
            msg='{}{}{}'.format(
                timestamp,
                type_,
                ''.join(uuids),
            ).encode('utf8'),
            digestmod=hashlib.sha256,
        ).hexdigest()

    @patch('muckrock.squarelet.tasks.pull_data.delay')
    def test_webhook_success(self, mock):
        """Test a succesful webhook"""
        request = self.request_factory.post(self.url, self.data)
        response = self.view(request)

        eq_(response.status_code, 200)
        eq_(response.content, 'OK')
        for uuid_ in self.data['uuids']:
            mock.assert_any_call(self.data['type'], uuid_)

    @patch('muckrock.squarelet.tasks.pull_data.delay')
    def test_webhook_signature_error(self, mock):
        """Test a webhook with an incorrect signature"""
        self.data['signature'] = 'foobar'
        request = self.request_factory.post(self.url, self.data)
        response = self.view(request)
<commit_msg>strings/bytes<commit_after># -*- coding: utf-8 -*-
"""Tests for the squarelet integration app"""

# Django
from django.conf import settings
from django.core.urlresolvers import reverse
from django.test import TestCase
from django.test.client import RequestFactory

# Standard Library
import hashlib
import hmac
import time
import uuid

# Third Party
from mock import patch
from nose.tools import eq_

# MuckRock
from muckrock.squarelet.views import webhook


class ViewsTest(TestCase):
    """Test the squarelet webhook view"""

    def setUp(self):
        self.request_factory = RequestFactory()
        self.view = webhook
        self.url = reverse('squarelet-webhook')
        self.data = {
            'type': 'user',
            'uuids': [
                str(uuid.uuid4()),
                str(uuid.uuid4()),
            ],
            'timestamp': int(time.time()),
        }
        self.data['signature'] = self._calc_signature(
            self.data['timestamp'],
            self.data['type'],
            self.data['uuids'],
        )

    def _calc_signature(self, timestamp, type_, uuids):
        """Calculate the webhook signature"""
        return hmac.new(
            key=settings.SQUARELET_SECRET.encode('utf8'),
            msg='{}{}{}'.format(
                timestamp,
                type_,
                ''.join(uuids),
            ).encode('utf8'),
            digestmod=hashlib.sha256,
        ).hexdigest()

    @patch('muckrock.squarelet.tasks.pull_data.delay')
    def test_webhook_success(self, mock):
        """Test a succesful webhook"""
        request = self.request_factory.post(self.url, self.data)
        response = self.view(request)

        eq_(response.status_code, 200)
        eq_(response.content, b'OK')
        for uuid_ in self.data['uuids']:
            mock.assert_any_call(self.data['type'], uuid_)

    @patch('muckrock.squarelet.tasks.pull_data.delay')
    def test_webhook_signature_error(self, mock):
        """Test a webhook with an incorrect signature"""
        self.data['signature'] = 'foobar'
        request = self.request_factory.post(self.url, self.data)
        response = self.view(request)

{'fraction_non_alphanumeric': 0.12497112497112497, 'fraction_numerical': 0.005082005082005082, 'mean_word_length': 2.3670295489891133, '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': '3072906', 'n_tokens_mistral': 1272, 'n_tokens_neox': 1235, 'n_words': 278}