<s> import sys <EOL> import os <EOL> import unittest <EOL> import random <EOL> import json <EOL> import requests <EOL> from urlparse import parse_qs <EOL> path = os . path . dirname ( os . getcwd ( ) ) <EOL> sys . path . insert ( <NUM_LIT:0> , path ) <EOL> from oauth_hook . hook import OAuthHook <EOL> from test_settings import TWITTER_ACCESS_TOKEN , TWITTER_ACCESS_TOKEN_SECRET <EOL> from test_settings import TWITTER_CONSUMER_KEY , TWITTER_CONSUMER_SECRET <EOL> from test_settings import RDIO_API_KEY , RDIO_SHARED_SECRET <EOL> from test_settings import ( <EOL> IMGUR_CONSUMER_KEY , IMGUR_CONSUMER_SECRET , <EOL> IMGUR_ACCESS_TOKEN , IMGUR_ACCESS_TOKEN_SECRET , <EOL> ) <EOL> OAuthHook . consumer_key = TWITTER_CONSUMER_KEY <EOL> OAuthHook . consumer_secret = TWITTER_CONSUMER_SECRET <EOL> oauth_hook = OAuthHook ( TWITTER_ACCESS_TOKEN , TWITTER_ACCESS_TOKEN_SECRET ) <EOL> client = requests . session ( hooks = { '<STR_LIT>' : oauth_hook } ) <EOL> class TwitterOAuthTestSuite ( unittest . TestCase ) : <EOL> def setUp ( self ) : <EOL> oauth_hook . header_auth = True <EOL> def test_rate_limit_GET_urlencoded ( self ) : <EOL> oauth_hook . header_auth = False <EOL> self . test_rate_limit_GET ( ) <EOL> def test_rate_limit_GET ( self ) : <EOL> response = client . get ( '<STR_LIT>' ) <EOL> self . assertEqual ( response . status_code , <NUM_LIT:200> ) <EOL> self . assertEqual ( json . loads ( response . content ) [ '<STR_LIT>' ] , <NUM_LIT> ) <EOL> def test_status_POST_urlencoded ( self ) : <EOL> oauth_hook . header_auth = False <EOL> self . test_status_POST ( ) <EOL> def test_status_POST ( self ) : <EOL> message = "<STR_LIT>" % random . random ( ) <EOL> response = client . post ( '<STR_LIT>' , <EOL> { '<STR_LIT:status>' : message , '<STR_LIT>' : True } ) <EOL> self . assertEqual ( response . status_code , <NUM_LIT:200> ) <EOL> self . assertEqual ( json . loads ( response . content ) [ '<STR_LIT:text>' ] , message ) <EOL> def test_status_GET_with_data_urlencoded ( self ) : <EOL> oauth_hook . header_auth = False <EOL> self . test_status_GET_with_data ( ) <EOL> def test_status_GET_with_data ( self ) : <EOL> response = client . get ( '<STR_LIT>' , data = { '<STR_LIT>' : <NUM_LIT> } ) <EOL> self . assertEqual ( response . status_code , <NUM_LIT:200> ) <EOL> def test_status_GET_with_params_urlencoded ( self ) : <EOL> oauth_hook . header_auth = False <EOL> self . test_status_GET_with_params ( ) <EOL> def test_status_GET_with_params ( self ) : <EOL> response = client . get ( '<STR_LIT>' , params = { '<STR_LIT>' : <NUM_LIT> } ) <EOL> self . assertEqual ( response . status_code , <NUM_LIT:200> ) <EOL> def test_create_delete_list_urlencoded ( self ) : <EOL> oauth_hook . header_auth = False <EOL> self . test_create_delete_list ( ) <EOL> def test_create_delete_list ( self ) : <EOL> screen_name = json . loads ( client . get ( '<STR_LIT>' ) . content ) [ '<STR_LIT>' ] <EOL> user_lists = json . loads ( client . get ( '<STR_LIT>' , data = { '<STR_LIT>' : screen_name } ) . content ) [ '<STR_LIT>' ] <EOL> for list in user_lists : <EOL> if list [ '<STR_LIT:name>' ] == '<STR_LIT>' : <EOL> client . post ( '<STR_LIT>' , data = { '<STR_LIT>' : list [ '<STR_LIT:id>' ] } ) <EOL> created_list = json . loads ( client . post ( '<STR_LIT>' % screen_name , data = { '<STR_LIT:name>' : "<STR_LIT>" } ) . content ) <EOL> list_id = created_list [ '<STR_LIT:id>' ] <EOL> response = client . delete ( '<STR_LIT>' % ( screen_name , list_id ) ) <EOL> self . assertEqual ( response . status_code , <NUM_LIT:200> ) <EOL> self . assertEqual ( json . loads ( response . content ) , created_list ) <EOL> def test_three_legged_auth ( self ) : <EOL> yes_or_no = raw_input ( "<STR_LIT>" ) <EOL> if yes_or_no . lower ( ) in [ '<STR_LIT:y>' , '<STR_LIT:yes>' ] : <EOL> return <EOL> twitter_oauth_hook = OAuthHook ( ) <EOL> for header_auth in ( True , False ) : <EOL> twitter_oauth_hook . header_auth = header_auth <EOL> client = requests . session ( hooks = { '<STR_LIT>' : twitter_oauth_hook } ) <EOL> response = client . post ( '<STR_LIT>' , data = { '<STR_LIT>' : '<STR_LIT>' } ) <EOL> self . assertEqual ( response . status_code , <NUM_LIT:200> ) <EOL> response = parse_qs ( response . content ) <EOL> self . assertTrue ( response [ '<STR_LIT>' ] ) <EOL> self . assertTrue ( response [ '<STR_LIT>' ] ) <EOL> oauth_token = response [ '<STR_LIT>' ] <EOL> oauth_secret = response [ '<STR_LIT>' ] <EOL> print "<STR_LIT>" % oauth_token [ <NUM_LIT:0> ] <EOL> oauth_verifier = raw_input ( '<STR_LIT>' ) <EOL> response = client . post ( '<STR_LIT>' , { '<STR_LIT>' : oauth_verifier , '<STR_LIT>' : oauth_token [ <NUM_LIT:0> ] } ) <EOL> response = parse_qs ( response . content ) <EOL> self . assertTrue ( response [ '<STR_LIT>' ] ) <EOL> self . assertTrue ( response [ '<STR_LIT>' ] ) <EOL> def test_update_profile_image_urlencoded ( self ) : <EOL> oauth_hook . header_auth = False <EOL> self . test_update_profile_image ( ) <EOL> def test_update_profile_image ( self ) : <EOL> files = { '<STR_LIT:image>' : ( '<STR_LIT>' , open ( '<STR_LIT>' , '<STR_LIT:rb>' ) ) } <EOL> response = client . post ( '<STR_LIT>' , files = files ) <EOL> self . assertEqual ( response . status_code , <NUM_LIT:200> ) <EOL> class RdioOAuthTestSuite ( unittest . TestCase ) : <EOL> def setUp ( self ) : <EOL> rdio_oauth_hook = OAuthHook ( consumer_key = RDIO_API_KEY , consumer_secret = RDIO_SHARED_SECRET , header_auth = False ) <EOL> self . client = requests . session ( hooks = { '<STR_LIT>' : rdio_oauth_hook } ) <EOL> def test_rdio_oauth_get_token_data ( self ) : <EOL> response = self . client . post ( '<STR_LIT>' , data = { '<STR_LIT>' : '<STR_LIT>' } ) <EOL> self . assertEqual ( response . status_code , <NUM_LIT:200> ) <EOL> response = parse_qs ( response . content ) <EOL> self . assertTrue ( response [ '<STR_LIT>' ] ) <EOL> self . assertTrue ( response [ '<STR_LIT>' ] ) <EOL> def test_rdio_oauth_get_token_params ( self ) : <EOL> self . client . params = { '<STR_LIT>' : '<STR_LIT>' } <EOL> response = self . client . post ( '<STR_LIT>' ) <EOL> self . assertEqual ( response . status_code , <NUM_LIT:200> ) <EOL> response = parse_qs ( response . content ) <EOL> self . assertTrue ( response [ '<STR_LIT>' ] ) <EOL> self . assertTrue ( response [ '<STR_LIT>' ] ) <EOL> class ImgurOAuthTestSuite ( unittest . TestCase ) : <EOL> def test_three_legged_auth ( self ) : <EOL> yes_or_no = raw_input ( "<STR_LIT>" ) <EOL> if yes_or_no . lower ( ) in [ '<STR_LIT:y>' , '<STR_LIT:yes>' ] : <EOL> return <EOL> for header_auth in ( True , False ) : <EOL> imgur_oauth_hook = OAuthHook ( <EOL> consumer_key = IMGUR_CONSUMER_KEY , <EOL> consumer_secret = IMGUR_CONSUMER_SECRET , <EOL> header_auth = header_auth <EOL> ) <EOL> client = requests . session ( hooks = { '<STR_LIT>' : imgur_oauth_hook } ) <EOL> response = client . post ( '<STR_LIT>' ) <EOL> qs = parse_qs ( response . text ) <EOL> oauth_token = qs [ '<STR_LIT>' ] [ <NUM_LIT:0> ] <EOL> oauth_secret = qs [ '<STR_LIT>' ] [ <NUM_LIT:0> ] <EOL> print "<STR_LIT>" % oauth_token <EOL> oauth_verifier = raw_input ( '<STR_LIT>' ) <EOL> new_imgur_oauth_hook = OAuthHook ( oauth_token , oauth_secret , IMGUR_CONSUMER_KEY , IMGUR_CONSUMER_SECRET , header_auth ) <EOL> new_client = requests . session ( <EOL> hooks = { '<STR_LIT>' : new_imgur_oauth_hook } <EOL> ) <EOL> response = new_client . post ( '<STR_LIT>' , { '<STR_LIT>' : oauth_verifier } ) <EOL> response = parse_qs ( response . content ) <EOL> token = response [ '<STR_LIT>' ] [ <NUM_LIT:0> ] <EOL> token_secret = response [ '<STR_LIT>' ] [ <NUM_LIT:0> ] <EOL> self . assertTrue ( token ) <EOL> self . assertTrue ( token_secret ) <EOL> def test_stats ( self ) : <EOL> imgur_oauth_hook = OAuthHook ( IMGUR_ACCESS_TOKEN , IMGUR_ACCESS_TOKEN_SECRET , IMGUR_CONSUMER_KEY , IMGUR_CONSUMER_SECRET , True ) <EOL> client = requests . session ( hooks = { '<STR_LIT>' : imgur_oauth_hook } ) <EOL> response = client . get ( "<STR_LIT>" ) <EOL> self . assertEqual ( response . status_code , <NUM_LIT:200> ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> unittest . main ( ) </s>
