<s> import sys <EOL> import unittest <EOL> from os . path import dirname <EOL> try : <EOL> from urlparse import urlparse , parse_qsl <EOL> except ImportError : <EOL> from urllib . parse import urlparse , parse_qsl <EOL> from django . conf import settings <EOL> try : <EOL> from django . conf . urls . defaults import patterns , include , url <EOL> except ImportError : <EOL> from django . conf . urls import patterns , include , url <EOL> from django . http import HttpResponseForbidden <EOL> from sanction . client import Client as SanctionClient <EOL> def _request_token ( self , ** kwargs ) : <EOL> if '<STR_LIT:code>' in kwargs : <EOL> self . access_token = '<STR_LIT>' <EOL> SanctionClient . request_token = _request_token <EOL> class TestViews ( unittest . TestCase ) : <EOL> def test_login ( self ) : <EOL> c = TestClient ( ) <EOL> provider_key = '<STR_LIT>' <EOL> resp = c . get ( '<STR_LIT>' . format ( provider_key ) ) <EOL> self . assertEquals ( resp . status_code , <NUM_LIT> ) <EOL> rloc = urlparse ( resp . _headers [ '<STR_LIT:location>' ] [ <NUM_LIT:1> ] ) <EOL> sloc = urlparse ( settings . SANCTION_PROVIDERS [ provider_key ] [ <EOL> '<STR_LIT>' ] ) <EOL> self . assertEquals ( rloc . scheme , sloc . scheme ) <EOL> self . assertEquals ( rloc . netloc , sloc . netloc ) <EOL> self . assertEquals ( rloc . path , sloc . path ) <EOL> def test_invalid_csrf ( self ) : <EOL> c = TestClient ( ) <EOL> resp = c . get ( '<STR_LIT>' ) <EOL> self . assertEquals ( type ( resp ) , HttpResponseForbidden ) <EOL> resp = c . get ( '<STR_LIT>' ) <EOL> self . assertEquals ( type ( resp ) , HttpResponseForbidden ) <EOL> def test_login_logout ( self ) : <EOL> c = TestClient ( ) <EOL> provider_key = '<STR_LIT>' <EOL> provider = settings . SANCTION_PROVIDERS [ provider_key ] <EOL> resp = c . get ( '<STR_LIT>' . format ( provider_key ) ) <EOL> rloc = urlparse ( resp . _headers [ '<STR_LIT:location>' ] [ <NUM_LIT:1> ] ) <EOL> query = dict ( parse_qsl ( rloc . query ) ) <EOL> self . assertTrue ( '<STR_LIT:state>' in query ) <EOL> self . assertEquals ( '<STR_LIT>' . format ( rloc . scheme , rloc . netloc , <EOL> rloc . path ) , provider [ '<STR_LIT>' ] ) <EOL> resp = c . get ( '<STR_LIT>' . format ( provider_key , <EOL> query [ '<STR_LIT:state>' ] ) ) <EOL> self . assertEquals ( resp . status_code , <NUM_LIT> ) <EOL> self . assertTrue ( '<STR_LIT>' in resp . cookies ) <EOL> resp = c . get ( '<STR_LIT>' . format ( provider_key ) ) <EOL> self . assertEquals ( resp . status_code , <NUM_LIT> ) <EOL> rloc = urlparse ( resp . _headers [ '<STR_LIT:location>' ] [ <NUM_LIT:1> ] ) <EOL> self . assertEquals ( rloc . path , settings . LOGIN_REDIRECT_URL ) <EOL> resp = c . get ( '<STR_LIT>' ) <EOL> self . assertEquals ( resp . status_code , <NUM_LIT> ) <EOL> rloc = urlparse ( resp . _headers [ '<STR_LIT:location>' ] [ <NUM_LIT:1> ] ) <EOL> self . assertEquals ( rloc . path , settings . LOGIN_URL ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> settings . configure ( <EOL> DEBUG = True , <EOL> SECRET_KEY = '<STR_LIT>' , <EOL> URL_CONF = '<STR_LIT>' , <EOL> DATABASES = { <EOL> '<STR_LIT:default>' : { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } <EOL> } , <EOL> AUTHENTICATION_BACKENDS = ( <EOL> '<STR_LIT>' , <EOL> ) , <EOL> INSTALLED_APPS = ( <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ) , <EOL> MIDDLEWARE_CLASSES = ( <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ) , <EOL> SESSION_ENGINE = '<STR_LIT>' , <EOL> ROOT_URLCONF = '<STR_LIT>' , <EOL> LOGIN_URL = '<STR_LIT:/>' , <EOL> LOGIN_REDIRECT_URL = '<STR_LIT>' , <EOL> LOGOUT_URL = '<STR_LIT>' , <EOL> SANCTION_PROVIDERS = { <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : ( '<STR_LIT:email>' , ) , <EOL> } , <EOL> } , <EOL> ) <EOL> sys . path . append ( dirname ( dirname ( __file__ ) ) ) <EOL> from django . core . management import call_command <EOL> from django . test . client import Client as TestClient <EOL> from django . db import models <EOL> class User ( models . Model ) : <EOL> class Meta : <EOL> app_label = '<STR_LIT>' <EOL> id = models . CharField ( max_length = <NUM_LIT:50> , unique = True , primary_key = True ) <EOL> access_token = models . CharField ( max_length = <NUM_LIT:50> , null = True ) <EOL> token_expires = models . FloatField ( default = <NUM_LIT:0> ) <EOL> provider = models . CharField ( max_length = <NUM_LIT> ) <EOL> last_login = models . DateTimeField ( null = True ) <EOL> USERNAME_FIELD = '<STR_LIT:id>' <EOL> REQUIRED_FIELDS = [ '<STR_LIT>' ] <EOL> def is_authenticated ( self ) : <EOL> return True <EOL> @ staticmethod <EOL> def fetch_user ( provider , access_token ) : <EOL> user , new = User . objects . get_or_create ( id = '<STR_LIT>' ) <EOL> return user <EOL> @ staticmethod <EOL> def get_user ( user_id ) : <EOL> return User . objects . get ( id = user_id ) <EOL> setattr ( settings , '<STR_LIT>' , '<STR_LIT>' ) <EOL> call_command ( '<STR_LIT>' , interactive = False ) <EOL> unittest . main ( ) <EOL> urlpatterns = patterns ( '<STR_LIT>' , <EOL> url ( r'<STR_LIT>' , include ( '<STR_LIT>' ) ) , <EOL> ) </s>
