<commit_before>"""
Steps for testing authentication
"""

from django.test.client import Client

from lettuce import before, step, world

from ixdjango.utils import flatten_auth_header
from ixwsauth import auth
from ixwsauth_server.middleware.oauth import ConsumerStore


class ApplicationClient(Client):
    """
    A Django test Client which does IXA WS authentication using
    a consumer from the consumer store.
    """

    consumer_store = ConsumerStore.get_consumer_store()

    def __init__(self, key, secret=None, **defaults):
        self._key = key

        if secret:
            self._secret = secret
        else:
            consumer = self.consumer_store.get_consumer(key)
            self._secret = consumer.secret()

        super(ApplicationClient, self).__init__(**defaults)

    @property
    def key(self):
        """
        The API key
        """

        return self._key

    def secret(self):
        """
        The secret associated with the API key
        """

        return self._secret

    def _base_environ(self, **request):
        """
        Sign the request with key/secret
        """

        environ = super(ApplicationClient, self)._base_environ(**request)

        auth_man = auth.AuthManager()

        method = request['REQUEST_METHOD']
        payload = {
            'method': method,
            'url': 'http://testserver' + request['PATH_INFO'],
            'params': {},
        }
        signed_payload = auth_man.oauth_signed_payload(self, payload)

        environ['HTTP_AUTHORIZATION'] = flatten_auth_header(
            signed_payload['headers']['Authorization'],
            'OAuth'
        )

        return environ


@before.each_scenario  # pylint:disable=no-member
def set_default_client(scenario):
    """<commit_msg>Fix generating OAuth signatures in tests<commit_after>"""
Steps for testing authentication
"""

from urlparse import parse_qs

from django.test.client import Client

from lettuce import before, step, world

from ixdjango.utils import flatten_auth_header
from ixwsauth import auth
from ixwsauth_server.middleware.oauth import ConsumerStore


class ApplicationClient(Client):
    """
    A Django test Client which does IXA WS authentication using
    a consumer from the consumer store.
    """

    consumer_store = ConsumerStore.get_consumer_store()

    def __init__(self, key, secret=None, **defaults):
        self._key = key

        if secret:
            self._secret = secret
        else:
            consumer = self.consumer_store.get_consumer(key)
            self._secret = consumer.secret()

        super(ApplicationClient, self).__init__(**defaults)

    @property
    def key(self):
        """
        The API key
        """

        return self._key

    def secret(self):
        """
        The secret associated with the API key
        """

        return self._secret

    def _base_environ(self, **request):
        """
        Sign the request with key/secret
        """

        environ = super(ApplicationClient, self)._base_environ(**request)

        auth_man = auth.AuthManager()

        method = request['REQUEST_METHOD']
        if method == 'GET':
            params = parse_qs(request['QUERY_STRING'])
        else:
            params = {}
        payload = {
            'method': method,
            'url': 'http://testserver' + request['PATH_INFO'],
            'params': params,
        }
        signed_payload = auth_man.oauth_signed_payload(self, payload)

        environ['HTTP_AUTHORIZATION'] = flatten_auth_header(
            signed_payload['headers']['Authorization'],
            'OAuth'
        )

        return environ


@before.each_scenario  # pylint:disable=no-member
def set_default_client(scenario):
    """
{'fraction_non_alphanumeric': 0.09821190285561783, 'fraction_numerical': 0.0, 'mean_word_length': 2.3826714801444044, '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': '5613923', 'n_tokens_mistral': 1036, 'n_tokens_neox': 990, 'n_words': 255}