<s> from django . conf import settings <EOL> from django . contrib . auth import get_user_model <EOL> from django . core . urlresolvers import reverse <EOL> from django . test import TestCase , Client , override_settings <EOL> from mock import patch <EOL> from buildservice . models import OAuthToken , Repository <EOL> @ override_settings ( GITHUB_CLIENT_SECRET = '<STR_LIT>' ) <EOL> class OAuthViewsTestCase ( TestCase ) : <EOL> login_url = reverse ( '<STR_LIT>' ) <EOL> callback_url = reverse ( '<STR_LIT>' ) <EOL> def setUp ( self ) : <EOL> self . client = Client ( ) <EOL> self . user = get_user_model ( ) . objects . create_user ( '<STR_LIT:user>' , password = '<STR_LIT>' ) <EOL> def test_get_login_anonymous ( self ) : <EOL> resp = self . client . get ( self . login_url ) <EOL> self . assertEqual ( resp . status_code , <NUM_LIT> ) <EOL> self . assertEqual ( resp . url , reverse ( '<STR_LIT>' ) + '<STR_LIT>' + self . login_url ) <EOL> @ patch ( <EOL> '<STR_LIT>' , <EOL> return_value = ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> ) <EOL> def test_get_login_ok ( self , authorization_url ) : <EOL> self . assertTrue ( self . client . login ( username = '<STR_LIT:user>' , password = '<STR_LIT>' ) ) <EOL> resp = self . client . get ( self . login_url ) <EOL> self . assertEqual ( resp . status_code , <NUM_LIT> ) <EOL> authorization_url . assert_called_with ( settings . GITHUB_AUTHORIZATION_BASE_URL ) <EOL> self . assertEqual ( resp . url , '<STR_LIT>' ) <EOL> self . assertEqual ( resp . wsgi_request . session [ '<STR_LIT>' ] , '<STR_LIT>' ) <EOL> def test_callback_anonymous ( self ) : <EOL> resp = self . client . get ( self . callback_url ) <EOL> self . assertEqual ( resp . status_code , <NUM_LIT> ) <EOL> self . assertEqual ( resp . url , reverse ( '<STR_LIT>' ) + '<STR_LIT>' + self . callback_url ) <EOL> def test_callback_token_missing_oauth_state ( self ) : <EOL> self . assertTrue ( self . client . login ( username = '<STR_LIT:user>' , password = '<STR_LIT>' ) ) <EOL> resp = self . client . get ( self . callback_url ) <EOL> self . assertEqual ( resp . status_code , <NUM_LIT> ) <EOL> self . assertEqual ( resp . content , '<STR_LIT>' ) <EOL> @ patch ( '<STR_LIT>' , return_value = '<STR_LIT>' ) <EOL> def test_callback_token_malformatted ( self , fetch_token ) : <EOL> self . assertTrue ( self . client . login ( username = '<STR_LIT:user>' , password = '<STR_LIT>' ) ) <EOL> session = self . client . session <EOL> session [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> session . save ( ) <EOL> resp = self . client . get ( self . callback_url ) <EOL> fetch_token . assert_called_with ( <EOL> settings . GITHUB_TOKEN_URL , <EOL> client_secret = '<STR_LIT>' , <EOL> authorization_response = resp . wsgi_request . build_absolute_uri ( ) <EOL> ) <EOL> self . assertEqual ( resp . status_code , <NUM_LIT> ) <EOL> self . assertEqual ( resp . content , '<STR_LIT>' ) <EOL> @ patch ( '<STR_LIT>' , return_value = { } ) <EOL> def test_callback_token_malformatted_bis ( self , fetch_token ) : <EOL> self . assertTrue ( self . client . login ( username = '<STR_LIT:user>' , password = '<STR_LIT>' ) ) <EOL> session = self . client . session <EOL> session [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> session . save ( ) <EOL> resp = self . client . get ( self . callback_url ) <EOL> fetch_token . assert_called_with ( <EOL> settings . GITHUB_TOKEN_URL , <EOL> client_secret = '<STR_LIT>' , <EOL> authorization_response = resp . wsgi_request . build_absolute_uri ( ) <EOL> ) <EOL> self . assertEqual ( resp . status_code , <NUM_LIT> ) <EOL> self . assertEqual ( resp . content , '<STR_LIT>' ) <EOL> @ patch . object ( Repository , '<STR_LIT>' ) <EOL> @ patch ( '<STR_LIT>' , return_value = { '<STR_LIT>' : '<STR_LIT:abc>' } ) <EOL> def test_callback_ok ( self , fetch_token , add_repos ) : <EOL> self . assertTrue ( self . client . login ( username = '<STR_LIT:user>' , password = '<STR_LIT>' ) ) <EOL> session = self . client . session <EOL> session [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> session . save ( ) <EOL> resp = self . client . get ( self . callback_url ) <EOL> fetch_token . assert_called_with ( <EOL> settings . GITHUB_TOKEN_URL , <EOL> client_secret = '<STR_LIT>' , <EOL> authorization_response = resp . wsgi_request . build_absolute_uri ( ) <EOL> ) <EOL> add_repos . assert_called_with ( self . user ) <EOL> self . assertEqual ( resp . status_code , <NUM_LIT> ) <EOL> self . assertEqual ( resp . url , '<STR_LIT:/>' ) <EOL> token = OAuthToken . objects . get ( user = self . user ) <EOL> self . assertEqual ( token . value , '<STR_LIT:abc>' ) </s>
