<commit_before>from urlparse import urlparse
import random
import json
import hmac
import hashlib
import math

import mohawk
from requests.auth import AuthBase

from loads.case import TestCase


class TestLoop(TestCase):

    def test_all(self):
        self.register()
        token = self.generate_token()
        call_id, session, caller_token, api_key = self.initiate_call(token)
        calls = self.list_pending_calls()
        for call in calls:
            # We want to reject 30% of the calls.
            status = 200
            if random.randint(0, 100) <= 30:
                self.discard_call(call['callId'])
                status = 404
            self.get_call_status(call['callId'], status)

    def _get_json(self, resp):
        try:
            return resp.json()
        except Exception:
            print resp.text
            raise

    def register(self):
        resp = self.session.post(
            self.server_url + '/registration',
            data={'simple_push_url': 'http://localhost/push'})
        self.assertEquals(200, resp.status_code)

        try:
            self.hawk_auth = HawkAuth(
                self.server_url,
                resp.headers['hawk-session-token'])
        except KeyError:
            print resp
            raise

    def generate_token(self):
        resp = self.session.post(
            self.server_url + '/call-url',
            data=json.dumps({'callerId': '<EMAIL>'}),
            headers={'Content-type': 'application/json'},
            auth=self.hawk_auth
        )
        self.assertEquals(resp.status_code, 200)
        data = self._get_json(resp)
        call_url = data['call_url']
        return call_url.split('/').pop()

    def initiate_call(self, token):
        # This happens when not authenticated.
        resp = self.session.post(self.server_url + '/calls/%s' % token)
        self.assertEquals(resp.status_code, 200)

        data = self._get_json(resp)
        return (data['callId'], data['sessionId'], data['sessionToken'],
                data['apiKey'])

    def list_pending_calls(self):
        resp = self.session.get(
            self.server_url + '/calls?version=200',
            auth=self.hawk_auth)
        data = self._get_json(resp)
        return data['calls']

    def revoke_token(self, token):
        # You don't need to be authenticated to revoke a token.
        self.session.delete(self.server_url + '/call-url/%s' % token)

    def discard_call(self, call_id):
        self.session.delete(self.server_url + '/calls/id/%s' % call_id)

    def get_call_status(self, call_id, status):
        resp = self.session.get(self.server_url + '/calls/id/%s' % call_id)
        self.assertEqual(resp.status_code, status)


def HKDF_extract(salt, IKM, hashmod=hashlib.sha256):
    """HKDF-Extract; see RFC-5869 for the details."""
    if salt is None:
        salt = b"\x00" * hashmod().digest_size
    return hmac.new(salt, IKM, hashmod).digest()

<commit_msg>Improve loadtest debugging.<commit_after>from urlparse import urlparse
import random
import json
import hmac
import hashlib
import math

import mohawk
from requests.auth import AuthBase

from loads.case import TestCase


class TestLoop(TestCase):

    def test_all(self):
        self.register()
        token = self.generate_token()
        call_id, session, caller_token, api_key = self.initiate_call(token)
        calls = self.list_pending_calls()
        for call in calls:
            # We want to reject 30% of the calls.
            status = 200
            if random.randint(0, 100) <= 30:
                self.discard_call(call['callId'])
                status = 404
            self.get_call_status(call['callId'], status)

    def _get_json(self, resp):
        try:
            return resp.json()
        except Exception:
            print resp.text
            raise

    def register(self):
        resp = self.session.post(
            self.server_url + '/registration',
            data={'simple_push_url': 'http://httpbin.org/deny'})
        self.assertEquals(200, resp.status_code,
                          "Registration failed: %s" % resp.content)

        try:
            self.hawk_auth = HawkAuth(
                self.server_url,
                resp.headers['hawk-session-token'])
        except KeyError:
            print resp
            raise

    def generate_token(self):
        resp = self.session.post(
            self.server_url + '/call-url',
            data=json.dumps({'callerId': '<EMAIL>'}),
            headers={'Content-type': 'application/json'},
            auth=self.hawk_auth
        )
        self.assertEquals(resp.status_code, 200,
                          "Call-Url creation failed: %s" % resp.content)
        data = self._get_json(resp)
        call_url = data.get('callUrl', data.get('call_url'))
        return call_url.split('/').pop()

    def initiate_call(self, token):
        # This happens when not authenticated.
        resp = self.session.post(self.server_url + '/calls/%s' % token,
                                 {"callType": "audio-video"})
        self.assertEquals(resp.status_code, 200,
                          "Call Initialization failed: %s" % resp.content)

        data = self._get_json(resp)
        return (data['callId'], data['sessionId'], data['sessionToken'],
                data['apiKey'])

    def list_pending_calls(self):
        resp = self.session.get(
            self.server_url + '/calls?version=200',
            auth=self.hawk_auth)
        data = self._get_json(resp)
        return data['calls']

    def revoke_token(self, token):
        # You don't need to be authenticated to revoke a token.
        self.session.delete(self.server_url + '/call-url/%s' % token)

    def discard_call(self, call_id):
        self.session.delete(self.server_url + '/calls/id/%s' % call_id)

    def get_call_status(self, call_id, status):
        resp = self.session.get(self.server_url + '/calls/id/%s' % call_id)
        self.assertEqual(resp.status_code, status,
                         "Call status retrieval failed %s" % resp.content)


def HKDF_extract(salt, IKM, hashmod=hashlib.sha256):
    """HKDF-Extract; see RFC-5869 for the details."""
    if salt is None:
        salt = b"\x00" * hashmod().digest_size
    return hmac.new(salt, IKM, hashmod).digest()


{'fraction_non_alphanumeric': 0.11740506329113924, 'fraction_numerical': 0.011075949367088608, 'mean_word_length': 2.1230237154150196, 'pattern_counts': {'":': 1, '<': 7, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5134556', 'n_tokens_mistral': 1964, 'n_tokens_neox': 1857, 'n_words': 417}