<s> try : import simplejson as json <EOL> except ImportError : import json <EOL> from django . contrib . auth . models import User <EOL> from oauth2app . models import Client <EOL> from django . test . client import Client as DjangoTestClient <EOL> from django . utils import unittest <EOL> from base64 import b64encode <EOL> from urlparse import urlparse , parse_qs <EOL> from urllib import urlencode <EOL> USER_USERNAME = "<STR_LIT>" <EOL> USER_PASSWORD = "<STR_LIT>" <EOL> USER_EMAIL = "<STR_LIT>" <EOL> USER_FIRSTNAME = "<STR_LIT>" <EOL> USER_LASTNAME = "<STR_LIT>" <EOL> CLIENT_USERNAME = "<STR_LIT>" <EOL> CLIENT_EMAIL = "<STR_LIT>" <EOL> REDIRECT_URI = "<STR_LIT>" <EOL> class BaseTestCase ( unittest . TestCase ) : <EOL> user = None <EOL> client_holder = None <EOL> client_application = None <EOL> def setUp ( self ) : <EOL> self . user = User . objects . create_user ( <EOL> USER_USERNAME , <EOL> USER_EMAIL , <EOL> USER_PASSWORD ) <EOL> self . user . first_name = USER_FIRSTNAME <EOL> self . user . last_name = USER_LASTNAME <EOL> self . user . save ( ) <EOL> self . client = User . objects . create_user ( CLIENT_USERNAME , CLIENT_EMAIL ) <EOL> self . client_application = Client . objects . create ( <EOL> name = "<STR_LIT>" , <EOL> user = self . client ) <EOL> def tearDown ( self ) : <EOL> self . user . delete ( ) <EOL> self . client . delete ( ) <EOL> self . client_application . delete ( ) <EOL> def get_token ( self ) : <EOL> user = DjangoTestClient ( ) <EOL> user . login ( username = USER_USERNAME , password = USER_PASSWORD ) <EOL> parameters = { <EOL> "<STR_LIT>" : self . client_application . key , <EOL> "<STR_LIT>" : REDIRECT_URI , <EOL> "<STR_LIT>" : "<STR_LIT:code>" } <EOL> response = user . get ( "<STR_LIT>" % urlencode ( parameters ) ) <EOL> qs = parse_qs ( urlparse ( response [ '<STR_LIT:location>' ] ) . query ) <EOL> code = qs [ '<STR_LIT:code>' ] <EOL> client = DjangoTestClient ( ) <EOL> parameters = { <EOL> "<STR_LIT>" : self . client_application . key , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT:code>" : code , <EOL> "<STR_LIT>" : REDIRECT_URI } <EOL> basic_auth = b64encode ( "<STR_LIT>" % ( self . client_application . key , self . client_application . secret ) ) <EOL> response = client . get ( <EOL> "<STR_LIT>" , <EOL> parameters , <EOL> HTTP_AUTHORIZATION = "<STR_LIT>" % basic_auth ) <EOL> return json . loads ( response . content ) [ "<STR_LIT>" ] </s>
