<commit_before>from __future__ import absolute_import

from .unittest import TestCase

import urllib

from oauthlib.signature import *


class SignatureTests(TestCase):

    uri_query = "b5=%3D%253D&a3=a&c%40=&a2=r%20b"
    authorization_header = """OAuth realm="Example",
    oauth_consumer_key="<KEY>",
    oauth_token="kkk9d<PASSWORD>7",
    oauth_signature_method="HMAC-SHA1",
    oauth_timestamp="137131201",
    oauth_nonce="7d8f3e4a",
    oauth_signature="djosJKDKJSD8743243%2Fjdk33klY%3D" """.strip()
    body = "content=This is being the body of things"


    def test_construct_base_string(self):
        """
        Example text to be turned into a base string::

            POST /request?b5=%3D%253D&a3=a&c%40=&a2=r%20b HTTP/1.1
            Host: example.com
            Content-Type: application/x-www-form-urlencoded
            Authorization: OAuth realm="Example",
                           oauth_consumer_key="<KEY>",
                           oauth_token="kkk9d7dh3k39<PASSWORD>7",
                           oauth_signature_method="HMAC-SHA1",
                           oauth_timestamp="137131201",
                           oauth_nonce="7d8f3e4a",
                           oauth_signature="bYT5CMsGcbgUdFHObYMEfcx6bsw%3D"

        Sample Base string generated and tested against::
            
            POST&http%3A//example.com/request%3Fb5%3D%253D%25253D%26a3%3Da%26c%2540%3D
            %26a2%3Dr%2520b&OAuth%20realm%3D%22Example%22%2Coauth_consumer_key%3D%229d
            jdj82h48djs9d2%22%2Coauth_token%3D%22kkk9d7dh3k39sjv7%22%2Coauth_signature
            _method%3D%22HMAC-SHA1%22%2Coauth_timestamp%3D%22137131201%22%2Coauth_nonc
            e%3D%227d8f3e4a%22%2Coauth_signature%3D%22bYT5CMsGcbgUdFHObYMEfcx6bsw%253D
            %22
        """

        # This is the string we want to match
        control_test_string = "POST&http%3A//example.com/request%3Fb5%3D%253D%25253D%26a3%3Da%26c%2540%3D%26a2%3Dr%2520b&OAuth%20realm%3D%22Example%22%2Coauth_consumer_key%3D%229djdj82h48djs9d2%22%2Coauth_token%3D%22kkk9d7dh3k39sjv7%22%2Coauth_signature_method%3D%22HMAC-SHA1%22%2Coauth_timestamp%3D%22137131201%22%2Coauth_nonce%3D%227d8f3e4a%22%2Coauth_signature%3D%22bYT5CMsGcbgUdFHObYMEfcx6bsw%253D%22"

        # Create test variables
        # Create test variables
        # Create test variables
        http_method = "post"
        base_string_url = urllib.quote("http://example.com/request?b5=%3D%253D&a3=a&c%40=&a2=r%20b")
        normalized_encoded_request_parameters = urllib.quote("""OAuth realm="Example",oauth_consumer_key="<KEY>",oauth_token="kkk9d7dh3k3<PASSWORD>7",oauth_signature_method="HMAC-SHA1",oauth_timestamp="137131201",oauth_nonce="7d8f3e4a",oauth_signature="bYT5CMsGcbgUdFHObYMEfcx6bsw%3D" """.strip())

        base_string = construct_base_string(http_method, base_string_url , normalized_encoded_request_parameters)

        self.assertEqual(control_test_string, base_string)

    def test_normalize_base_string_uri(self):
        """
        Example text to be turned into a normalized base string uri::
            
            GET /?q=1 HTTP/1.1
            Host: www.example.net:8080
        
        Sample string generated::
            
            https://www.example.net:8080/
        """
        
        # test for unicode failure
        uri = "www.example.com:8080"
        self.assertRaises(ValueError, normalize_base_string_uri, uri)

        uri = u"http://www.example.com:80"
        self.assertEquals(normalize_base_string_uri(uri), "http://www.example.com")

    def test_collect_parameters(self):
        """ We check against parameters multiple times in case things change after more
                parameters are added.
        """
        # check against empty parameters
        # check against empty parameters
        # check against empty parameters
        self.assertEquals(collect_parameters(), [])

        # Check against uri_query
        # Check against uri_query
        # Check against uri_query
        
        parameters = collect_parameters(uri_query=self.uri_query)

        self.assertEquals(len(parameters), 3)
        self.assertEquals(parameters[0], ('b5', '=%3D'))
        self.assertEquals(parameters[1], ('a3', 'a'))
        self.assertEquals(parameters[2], ('a2', 'r b'))

        # check against authorization header as well
        # check against authorization header as well
        # check against authorization header as well

        parameters = collect_parameters(uri_query=self.uri_query, authorization_header=self.authorization_header)

        # Redo the checks against all the parameters. Duplicated code but better safety
        self.assertEquals(len(parameters), 8)
        self.assertEquals(parameters[0], ('b5', '=%3D'))
        self.assertEquals(parameters[1], ('a3', 'a'))
        self.assertEquals(parameters[2], ('a2', 'r b'))
        self.assertEquals(parameters[3], ('oauth_nonce', '7d8f3e4a'))
        self.assertEquals(parameters[4], ('oauth_timestamp', '137131201'))
        self.assertEquals(parameters[5], ('oauth_consumer_key', '<KEY>'))
        self.assertEquals(parameters[6], ('oauth_signature_method', 'HMAC-SHA1'))
        self.assertEquals(parameters[7], ('oauth_token', '<PASSWORD>'))

        # Add in the body.
        # TODO - add more valid content for the body. <NAME> 2012/03/12
        # Redo again the checks against all the parameters. Duplicated code but better safety
        parameters = collect_parameters(uri_query=self.uri_query, authorization_header=self.authorization_header, body=self.body)
        self.assertEquals(len(parameters), 9)
        self.assertEquals(parameters[0], ('b5', '=%3D'))
        self.assertEquals(parameters[1], ('a3', 'a'))
        self.assertEquals(parameters[2], ('a2', 'r b'))
        self.assertEquals(parameters[3], ('oauth_nonce', '7d8f3e4a'))
        self.assertEquals(parameters[4], ('oauth_timestamp', '137131201'))
        self.assertEquals(parameters[5], ('oauth_consumer_key', '<KEY>'))
        self.assertEquals(parameters[6], ('oauth_signature_method', 'HMAC-SHA1'))
        self.assertEquals(parameters[7], ('oauth_token', '<PASSWORD>'))
        self.assertEquals(parameters[8], ('content', 'This is being the body of things'))

    def test_normalize_parameters(self):
        """ We copy some of the variables from the test method above."""

        # First we check for unicode issues.
        # We should generate Value Errors
        parameters = collect_parameters(uri_query=self.uri_query, authorization_header=self.authorization_header, body=self.body)
        self.assertRaises(ValueError, normalize_parameters, parameters)
        parameters = collect_parameters(uri_query=unicode(self.uri_query), authorization_header=self.authorization_header, body=self.body)
        self.assertRaises(ValueError, normalize_parameters, parameters)
        parameters = collect_parameters(uri_query=unicode(self.uri_query), authorization_header=unicode(self.authorization_header), body=self.body)
        self.assertRaises(ValueError, normalize_parameters, parameters)

        # Create the parameters
        parameters = collect_parameters(uri_query=unicode(self.uri_query), authorization_header=unicode(self.authorization_header), body=unicode(self.body))
        normalized = normalize_parameters(parameters)

        # check the parameters type
        self.assertTrue(isinstance(normalized, unicode))

        # Lets see if things are in order
        # check to see that querystring keys come in alphanumeric order:
        querystring_keys = ['a2', 'a3', 'b5', 'content', 'oauth_consumer_key', 'oauth_nonce', 'oauth_signature_method', 'oauth_timestamp', 'oauth_token']
        index = -1 # start at -1 because the 'a2' key starts at index 0
        for key in querystring_keys:
            self.assertTrue(normalized.index(key) > index)
            index = normalized.index(key)<commit_msg>Adding in the test_sign_hmac_sha1 method<commit_after>from __future__ import absolute_import

from .unittest import TestCase

import urllib

from oauthlib.signature import *


class SignatureTests(TestCase):

    uri_query = "b5=%3D%253D&a3=a&c%40=&a2=r%20b"
    authorization_header = """OAuth realm="Example",
    oauth_consumer_key="<KEY>",
    oauth_token="<PASSWORD>",
    oauth_signature_method="HMAC-SHA1",
    oauth_timestamp="137131201",
    oauth_nonce="7d8f3e4a",
    oauth_signature="djosJKDKJSD8743243%2Fjdk33klY%3D" """.strip()
    body = "content=This is being the body of things"
    http_method = "post"
    base_string_url = urllib.quote("http://example.com/request?b5=%3D%253D&a3=a&c%40=&a2=r%20b")
    normalized_encoded_request_parameters = urllib.quote("""OAuth realm="Example",oauth_consumer_key="<KEY>",oauth_token="<PASSWORD>",oauth_signature_method="HMAC-SHA1",oauth_timestamp="137131201",oauth_nonce="7d8f3e4a",oauth_signature="bYT5CMsGcbgUdFHObYMEfcx6bsw%3D" """.strip())



    def test_construct_base_string(self):
        """
        Example text to be turned into a base string::

            POST /request?b5=%3D%253D&a3=a&c%40=&a2=r%20b HTTP/1.1
            Host: example.com
            Content-Type: application/x-www-form-urlencoded
            Authorization: OAuth realm="Example",
                           oauth_consumer_key="<KEY>",
                           oauth_token="<PASSWORD>",
                           oauth_signature_method="HMAC-SHA1",
                           oauth_timestamp="137131201",
                           oauth_nonce="7d8f3e4a",
                           oauth_signature="bYT5CMsGcbgUdFHObYMEfcx6bsw%3D"

        Sample Base string generated and tested against::
            
            POST&http%3A//example.com/request%3Fb5%3D%253D%25253D%26a3%3Da%26c%2540%3D
            %26a2%3Dr%2520b&OAuth%20realm%3D%22Example%22%2Coauth_consumer_key%3D%229d
            jdj82h48djs9d2%22%2Coauth_token%3D%22kkk9d7dh3k39sjv7%22%2Coauth_signature
            _method%3D%22HMAC-SHA1%22%2Coauth_timestamp%3D%22137131201%22%2Coauth_nonc
            e%3D%227d8f3e4a%22%2Coauth_signature%3D%22bYT5CMsGcbgUdFHObYMEfcx6bsw%253D
            %22
        """

        # This is the string we want to match
        control_test_string = "POST&http%3A//example.com/request%3Fb5%3D%253D%25253D%26a3%3Da%26c%2540%3D%26a2%3Dr%2520b&OAuth%20realm%3D%22Example%22%2Coauth_consumer_key%3D%229djdj82h48djs9d2%22%2Coauth_token%3D%22kkk9d7dh3k39sjv7%22%2Coauth_signature_method%3D%22HMAC-SHA1%22%2Coauth_timestamp%3D%22137131201%22%2Coauth_nonce%3D%227d8f3e4a%22%2Coauth_signature%3D%22bYT5CMsGcbgUdFHObYMEfcx6bsw%253D%22"

        # Create test variables
        # Create test variables
        # Create test variables

        base_string = construct_base_string(self.http_method, self.base_string_url , self.normalized_encoded_request_parameters)

        self.assertEqual(control_test_string, base_string)

    def test_normalize_base_string_uri(self):
        """
        Example text to be turned into a normalized base string uri::
            
            GET /?q=1 HTTP/1.1
            Host: www.example.net:8080
        
        Sample string generated::
            
            https://www.example.net:8080/
        """
        
        # test for unicode failure
        uri = "www.example.com:8080"
        self.assertRaises(ValueError, normalize_base_string_uri, uri)

        uri = u"http://www.example.com:80"
        self.assertEquals(normalize_base_string_uri(uri), "http://www.example.com")

    def test_collect_parameters(self):
        """ We check against parameters multiple times in case things change after more
                parameters are added.
        """
        # check against empty parameters
        # check against empty parameters
        # check against empty parameters
        self.assertEquals(collect_parameters(), [])

        # Check against uri_query
        # Check against uri_query
        # Check against uri_query
        
        parameters = collect_parameters(uri_query=self.uri_query)

        self.assertEquals(len(parameters), 3)
        self.assertEquals(parameters[0], ('b5', '=%3D'))
        self.assertEquals(parameters[1], ('a3', 'a'))
        self.assertEquals(parameters[2], ('a2', 'r b'))

        # check against authorization header as well
        # check against authorization header as well
        # check against authorization header as well

        parameters = collect_parameters(uri_query=self.uri_query, authorization_header=self.authorization_header)

        # Redo the checks against all the parameters. Duplicated code but better safety
        self.assertEquals(len(parameters), 8)
        self.assertEquals(parameters[0], ('b5', '=%3D'))
        self.assertEquals(parameters[1], ('a3', 'a'))
        self.assertEquals(parameters[2], ('a2', 'r b'))
        self.assertEquals(parameters[3], ('oauth_nonce', '7d8f3e4a'))
        self.assertEquals(parameters[4], ('oauth_timestamp', '137131201'))
        self.assertEquals(parameters[5], ('oauth_consumer_key', '<KEY>'))
        self.assertEquals(parameters[6], ('oauth_signature_method', 'HMAC-SHA1'))
        self.assertEquals(parameters[7], ('oauth_token', '<PASSWORD>'))

        # Add in the body.
        # TODO - add more valid content for the body. <NAME> 2012/03/12
        # Redo again the checks against all the parameters. Duplicated code but better safety
        parameters = collect_parameters(uri_query=self.uri_query, authorization_header=self.authorization_header, body=self.body)
        self.assertEquals(len(parameters), 9)
        self.assertEquals(parameters[0], ('b5', '=%3D'))
        self.assertEquals(parameters[1], ('a3', 'a'))
        self.assertEquals(parameters[2], ('a2', 'r b'))
        self.assertEquals(parameters[3], ('oauth_nonce', '7d8f3e4a'))
        self.assertEquals(parameters[4], ('oauth_timestamp', '137131201'))
        self.assertEquals(parameters[5], ('oauth_consumer_key', '<KEY>'))
        self.assertEquals(parameters[6], ('oauth_signature_method', 'HMAC-SHA1'))
        self.assertEquals(parameters[7], ('oauth_token', '<PASSWORD>'))
        self.assertEquals(parameters[8], ('content', 'This is being the body of things'))

    def test_normalize_parameters(self):
        """ We copy some of the variables from the test method above."""

        # First we check for unicode issues.
        # We should generate Value Errors
        parameters = collect_parameters(uri_query=self.uri_query, authorization_header=self.authorization_header, body=self.body)
        self.assertRaises(ValueError, normalize_parameters, parameters)
        parameters = collect_parameters(uri_query=unicode(self.uri_query), authorization_header=self.authorization_header, body=self.body)
        self.assertRaises(ValueError, normalize_parameters, parameters)
        parameters = collect_parameters(uri_query=unicode(self.uri_query), authorization_header=unicode(self.authorization_header), body=self.body)
        self.assertRaises(ValueError, normalize_parameters, parameters)

        # Create the parameters
        parameters = collect_parameters(uri_query=unicode(self.uri_query), authorization_header=unicode(self.authorization_header), body=unicode(self.body))
        normalized = normalize_parameters(parameters)

        # check the parameters type
        self.assertTrue(isinstance(normalized, unicode))

        # Lets see if things are in order
        # check to see that querystring keys come in alphanumeric order:
        querystring_keys = ['a2', 'a3', 'b5', 'content', 'oauth_consumer_key', 'oauth_nonce', 'oauth_signature_method', 'oauth_timestamp', 'oauth_token']
        index = -1 # start at -1 because the 'a2' key starts at index 0
        for key in querystring_keys:
            self.assertTrue(normalized.index(key) > index)
            index = normalized.index(key)

    def test_sign_hmac_sha1(self):
        """ TODO: Someone make a better test for this."""

        # construct_base_string copied from the test_construct_base_string above
        base_string = construct_base_string(self.http_method, self.base_string_url , self.normalized_encoded_request_parameters)
        client_secret = "<KEY>"
        resource_owner_secret = "just-a-string    asdasd"

        # check for Unicode
        self.assertRaises(ValueError, sign_hmac_sha1, base_string, client_secret, resource_owner_secret)

        sign = sign_hmac_sha1(unicode(base_string), unicode(client_secret), unicode(resource_owner_secret))
        self.assertEquals(len(sign), 28)



{'fraction_non_alphanumeric': 0.11190678480398107, 'fraction_numerical': 0.049824007767933, 'mean_word_length': 3.451377633711507, 'pattern_counts': {'":': 0, '<': 26, '<?xml version=': 0, '>': 28, 'https://': 2, 'lorem ipsum': 0, 'www.': 12, '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': '7116653', 'n_tokens_mistral': 5575, 'n_tokens_neox': 5146, 'n_words': 904}