# Path: yithlibraryserver/twitter/authorization.py
# def quote(value):
#     return url_quote(value, safe='')
# 
# def nonce(length=8):
#     return ''.join([str(random.randint(0, 9)) for i in range(length)])
# 
# def timestamp():
#     return int(time.time())
# 
# Path: yithlibraryserver/twitter/authorization.py
# def sign(method, url, original_params, consumer_secret, oauth_token):
#     # 1. create the parameter string
#     param_string = '&'.join(['%s=%s' % (key, value)
#                              for key, value in sorted(original_params)])
# 
#     # 2. create signature base string
#     signature_base = '%s&%s&%s' % (
#         method.upper(), quote(url), quote(param_string),
#         )
# 
#     # 3. create the signature key
#     key = '%s&%s' % (quote(consumer_secret), quote(oauth_token))
# 
#     # 4. calculate the signature
#     hashed = hmac.new(key.encode('ascii'),
#                       signature_base.encode('ascii'),
#                       hashlib.sha1)
#     return quote(binascii.b2a_base64(hashed.digest())[:-1])
# 
# def auth_header(method, url, original_params, settings, oauth_token='',
#                 nonce_=None, timestamp_=None):
#     params = list(original_params) + [
#         ('oauth_consumer_key', settings['twitter_consumer_key']),
#         ('oauth_nonce', nonce_ or nonce()),
#         ('oauth_signature_method', 'HMAC-SHA1'),
#         ('oauth_timestamp', str(timestamp_ or timestamp())),
#         ('oauth_version', '1.0'),
#         ]
#     params = [(quote(key), quote(value)) for key, value in params]
# 
#     signature = sign(method, url, params,
#                      settings['twitter_consumer_secret'], oauth_token)
#     params.append(('oauth_signature', signature))
# 
#     header = ", ".join(['%s="%s"' % (key, value) for key, value in params])
# 
#     return 'OAuth %s' % header


import time
import unittest
from yithlibraryserver.twitter.authorization import quote, nonce, timestamp
from yithlibraryserver.twitter.authorization import sign, auth_header
        self.assertNotEqual(nonce(), nonce())

    def test_timestamp(self):
        t1 = timestamp()
        time.sleep(1)
        t2 = timestamp()
        self.assertTrue(t2 > t1)

    def test_sign(self):
        # this example is taken from
        # https://dev.twitter.com/docs/auth/creating-signature
        method = 'post'
        url = 'https://api.twitter.com/1/statuses/update.json'
        params = (
            ('status', quote('Hello Ladies + Gentlemen, a signed OAuth request!')),
            ('include_entities', quote('true')),
            ('oauth_consumer_key', quote('xvz1evFS4wEEPTGEFPHBog')),
            ('oauth_nonce', quote('kYjzVBB8Y0ZFabxSWbWovY3uYSQ2pTgmZeNu2VS4cg')),
            ('oauth_signature_method', quote('HMAC-SHA1')),
            ('oauth_timestamp', quote('1318622958')),
            ('oauth_token', quote('370773112-GmHxMAgYyLbNEtIKZeRNFsMKPR9EyMZeS9weJAEb')),
            ('oauth_version', quote('1.0')),
            )
        consumer_secret = 'kAcSOqF21Fu85e7zjz7ZN2U4ZRhfV3WpwPAoE3Z7kBw'
        oauth_token = 'LswwdoUaIvS8ltyTt5jkRh4J50vUPVVHtR2YPi5kE'

        self.assertEqual(sign(method, url, params,
                              consumer_secret, oauth_token),
                         quote('tnnArxj06cWHq44gCs1OSKk/jLY='))


# Path: yithlibraryserver/twitter/authorization.py
# def quote(value):
#     return url_quote(value, safe='')
# 
# def nonce(length=8):
#     return ''.join([str(random.randint(0, 9)) for i in range(length)])
# 
# def timestamp():
#     return int(time.time())
# 
# Path: yithlibraryserver/twitter/authorization.py
# def sign(method, url, original_params, consumer_secret, oauth_token):
#     # 1. create the parameter string
#     param_string = '&'.join(['%s=%s' % (key, value)
#                              for key, value in sorted(original_params)])
# 
#     # 2. create signature base string
#     signature_base = '%s&%s&%s' % (
#         method.upper(), quote(url), quote(param_string),
#         )
# 
#     # 3. create the signature key
#     key = '%s&%s' % (quote(consumer_secret), quote(oauth_token))
# 
#     # 4. calculate the signature
#     hashed = hmac.new(key.encode('ascii'),
#                       signature_base.encode('ascii'),
#                       hashlib.sha1)
#     return quote(binascii.b2a_base64(hashed.digest())[:-1])
# 
# def auth_header(method, url, original_params, settings, oauth_token='',
#                 nonce_=None, timestamp_=None):
#     params = list(original_params) + [
#         ('oauth_consumer_key', settings['twitter_consumer_key']),
#         ('oauth_nonce', nonce_ or nonce()),
#         ('oauth_signature_method', 'HMAC-SHA1'),
#         ('oauth_timestamp', str(timestamp_ or timestamp())),
#         ('oauth_version', '1.0'),
#         ]
#     params = [(quote(key), quote(value)) for key, value in params]
# 
#     signature = sign(method, url, params,
#                      settings['twitter_consumer_secret'], oauth_token)
#     params.append(('oauth_signature', signature))
# 
#     header = ", ".join(['%s="%s"' % (key, value) for key, value in params])
# 
#     return 'OAuth %s' % header


# Path: yithlibraryserver/twitter/tests/test_authorization.py
import time
import unittest
from yithlibraryserver.twitter.authorization import quote, nonce, timestamp
from yithlibraryserver.twitter.authorization import sign, auth_header

        self.assertNotEqual(nonce(), nonce())

    def test_timestamp(self):
        t1 = timestamp()
        time.sleep(1)
        t2 = timestamp()
        self.assertTrue(t2 > t1)

    def test_sign(self):
        # this example is taken from
        # https://dev.twitter.com/docs/auth/creating-signature
        method = 'post'
        url = 'https://api.twitter.com/1/statuses/update.json'
        params = (
            ('status', quote('Hello Ladies + Gentlemen, a signed OAuth request!')),
            ('include_entities', quote('true')),
            ('oauth_consumer_key', quote('xvz1evFS4wEEPTGEFPHBog')),
            ('oauth_nonce', quote('kYjzVBB8Y0ZFabxSWbWovY3uYSQ2pTgmZeNu2VS4cg')),
            ('oauth_signature_method', quote('HMAC-SHA1')),
            ('oauth_timestamp', quote('1318622958')),
            ('oauth_token', quote('370773112-GmHxMAgYyLbNEtIKZeRNFsMKPR9EyMZeS9weJAEb')),
            ('oauth_version', quote('1.0')),
            )
        consumer_secret = 'kAcSOqF21Fu85e7zjz7ZN2U4ZRhfV3WpwPAoE3Z7kBw'
        oauth_token = 'LswwdoUaIvS8ltyTt5jkRh4J50vUPVVHtR2YPi5kE'

        self.assertEqual(sign(method, url, params,
                              consumer_secret, oauth_token),
                         quote('tnnArxj06cWHq44gCs1OSKk/jLY='))


    def test_auth_header(self):