<commit_before>from __future__ import absolute_import

from .unittest import TestCase

from oauthlib.parameters import *


class ParameterTests(TestCase):
    auth_only_params = [
        (u'oauth_consumer_key', u"<KEY>"),
        (u'oauth_token', u"<KEY>"),
        (u'oauth_signature_method', u"HMAC-SHA1"),
        (u'oauth_timestamp', u"137131201"),
        (u'oauth_nonce', u"7d8f3e4a"),
        (u'oauth_signature', u"bYT5CMsGcbgUdFHObYMEfcx6bsw%3D")
    ]
    auth_and_data = list(auth_only_params)
    auth_and_data.append((u'data_param_foo', u'foo'))
    auth_and_data.append((u'data_param_1', u'1'))
    realm = 'testrealm'
    norealm_authorization_header = 'OAuth oauth_consumer_key=9djdj82h48djs9d2,oauth_token=kkk9d7dh3k39sjv7,oauth_signature_method=HMAC-SHA1,oauth_timestamp=137131201,oauth_nonce=7d8f3e4a,oauth_signature=bYT5CMsGcbgUdFHObYMEfcx6bsw%3D'
    withrealm_authorization_header = 'OAuth realm=testrealm,oauth_consumer_key=9djdj82h48djs9d2,oauth_token=kkk9d7dh3k39sjv7,oauth_signature_method=HMAC-SHA1,oauth_timestamp=137131201,oauth_nonce=7d8f3e4a,oauth_signature=bYT5CMsGcbgUdFHObYMEfcx6bsw%3D'

    def test_order_oauth_parameters(self):
        unordered = {
            'oauth_foo': 'foo',
            'oauth_bar': 'bar',
            'lala': 123,
            'oauth_baz': 'baz', }
        expected = [
            ('oauth_bar', 'bar'),
            ('oauth_baz', 'baz'),
            ('oauth_foo', 'foo'),
            ('lala', 123)]
        self.assertEqual(order_oauth_parameters(unordered), expected)

    def test_prepare_authorization_header(self):
        self.assertEqual(
            prepare_authorization_header(self.auth_only_params),
            self.norealm_authorization_header)
        self.assertEqual(
            prepare_authorization_header(self.auth_only_params, realm=self.realm),
            self.withrealm_authorization_header)

    def test_prepare_authorization_header_ignore_data(self):
        self.assertEqual(
            prepare_authorization_header(self.auth_and_data),
            self.norealm_authorization_header)
        self.assertEqual(
            prepare_authorization_header(self.auth_and_data, realm=self.realm),
            self.withrealm_authorization_header)

    def test_prepare_form_encoded_body(self):
        form_encoded_body = 'oauth_signature=bYT5CMsGcbgUdFHObYMEfcx6bsw%253D&oauth_nonce=7d8f3e4a&oauth_timestamp=137131201&oauth_signature_method=HMAC-SHA1&oauth_token=<PASSWORD>9<PASSWORD>&oauth_consumer_key=9djdj82h48djs9d2&data_param_foo=foo&data_param_1=1'
        self.assertEqual(
            prepare_form_encoded_body(self.auth_and_data), form_encoded_body)

    def test_prepare_request_uri_query(self):
        url = u'http://notarealdomain.com/foo/bar/baz?some=args&go=here'
        request_uri_query = u'http://notarealdomain.com/foo/bar/baz?some=args&go=here&data_param_foo=foo&data_param_1=1&oauth_signature=bYT5CMsGcbgUdFHObYMEfcx6bsw%253D&oauth_nonce=7d8f3e4a&oauth_timestamp=137131201&oauth_signature_method=HMAC-SHA1&oauth_token=kkk9d7dh3k39sjv7&oauth_consumer_key=<KEY>'
        self.assertEqual(
            prepare_request_uri_query(self.auth_and_data, url),
            request_uri_query)<commit_msg>Fixing a broken prepare_form_encoded_body test.<commit_after>from __future__ import absolute_import

from .unittest import TestCase

from oauthlib.parameters import *


class ParameterTests(TestCase):
    auth_only_params = [
        (u'oauth_consumer_key', u"<KEY>"),
        (u'oauth_token', u"<KEY>"),
        (u'oauth_signature_method', u"HMAC-SHA1"),
        (u'oauth_timestamp', u"137131201"),
        (u'oauth_nonce', u"7d8f3e4a"),
        (u'oauth_signature', u"bYT5CMsGcbgUdFHObYMEfcx6bsw%3D")
    ]
    auth_and_data = list(auth_only_params)
    auth_and_data.append((u'data_param_foo', u'foo'))
    auth_and_data.append((u'data_param_1', u'1'))
    realm = 'testrealm'
    norealm_authorization_header = 'OAuth oauth_consumer_key=9djdj82h48djs9d2,oauth_token=kkk9d7dh3k39sjv7,oauth_signature_method=HMAC-SHA1,oauth_timestamp=137131201,oauth_nonce=7d8f3e4a,oauth_signature=bYT5CMsGcbgUdFHObYMEfcx6bsw%3D'
    withrealm_authorization_header = 'OAuth realm=testrealm,oauth_consumer_key=9djdj82h48djs9d2,oauth_token=<PASSWORD>9<PASSWORD>,oauth_signature_method=HMAC-SHA1,oauth_timestamp=137131201,oauth_nonce=7d8f3e4a,oauth_signature=bYT5CMsGcbgUdFHObYMEfcx6bsw%3D'

    def test_order_oauth_parameters(self):
        unordered = {
            'oauth_foo': 'foo',
            'oauth_bar': 'bar',
            'lala': 123,
            'oauth_baz': 'baz', }
        expected = [
            ('oauth_bar', 'bar'),
            ('oauth_baz', 'baz'),
            ('oauth_foo', 'foo'),
            ('lala', 123)]
        self.assertEqual(order_oauth_parameters(unordered), expected)

    def test_prepare_authorization_header(self):
        self.assertEqual(
            prepare_authorization_header(self.auth_only_params),
            self.norealm_authorization_header)
        self.assertEqual(
            prepare_authorization_header(self.auth_only_params, realm=self.realm),
            self.withrealm_authorization_header)

    def test_prepare_authorization_header_ignore_data(self):
        self.assertEqual(
            prepare_authorization_header(self.auth_and_data),
            self.norealm_authorization_header)
        self.assertEqual(
            prepare_authorization_header(self.auth_and_data, realm=self.realm),
            self.withrealm_authorization_header)

    def test_prepare_form_encoded_body(self):
        existing_body = u''
        form_encoded_body = 'data_param_foo=foo&data_param_1=1&oauth_signature=bYT5CMsGcbgUdFHObYMEfcx6bsw%253D&oauth_nonce=7d8f3e4a&oauth_timestamp=137131201&oauth_signature_method=HMAC-SHA1&oauth_token=<KEY>&oauth_consumer_key=9djdj82h48djs9d2'
        self.assertEqual(
            prepare_form_encoded_body(self.auth_and_data, existing_body),
            form_encoded_body)

    def test_prepare_request_uri_query(self):
        url = u'http://notarealdomain.com/foo/bar/baz?some=args&go=here'
        request_uri_query = u'http://notarealdomain.com/foo/bar/baz?some=args&go=here&data_param_foo=foo&data_param_1=1&oauth_signature=bYT5CMsGcbgUdFHObYMEfcx6bsw%253D&oauth_nonce=7d8f3e4a&oauth_timestamp=137131201&oauth_signature_method=HMAC-SHA1&oauth_token=<KEY>&oauth_consumer_key=<KEY>'
        self.assertEqual(
            prepare_request_uri_query(self.auth_and_data, url),
            request_uri_query)
{'fraction_non_alphanumeric': 0.102861562258314, 'fraction_numerical': 0.04176334106728538, 'mean_word_length': 4.388333333333334, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, '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': '2726732', 'n_tokens_mistral': 2558, 'n_tokens_neox': 2416, 'n_words': 200}