<s> """<STR_LIT>""" <EOL> from __future__ import unicode_literals <EOL> from mock import MagicMock , patch <EOL> from pyramid . security import Everyone <EOL> from . import MockServerTest <EOL> from pypicloud import auth <EOL> class TestBasicAuth ( MockServerTest ) : <EOL> """<STR_LIT>""" <EOL> def setUp ( self ) : <EOL> super ( TestBasicAuth , self ) . setUp ( ) <EOL> self . request . environ [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> def test_no_headers ( self ) : <EOL> """<STR_LIT>""" <EOL> creds = auth . get_basicauth_credentials ( self . request ) <EOL> self . assertIsNone ( creds ) <EOL> def test_bad_format ( self ) : <EOL> """<STR_LIT>""" <EOL> self . request . environ [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> creds = auth . get_basicauth_credentials ( self . request ) <EOL> self . assertIsNone ( creds ) <EOL> def test_not_basic ( self ) : <EOL> """<STR_LIT>""" <EOL> self . request . environ [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> creds = auth . get_basicauth_credentials ( self . request ) <EOL> self . assertIsNone ( creds ) <EOL> def test_not_base64 ( self ) : <EOL> """<STR_LIT>""" <EOL> self . request . environ [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> creds = auth . get_basicauth_credentials ( self . request ) <EOL> self . assertIsNone ( creds ) <EOL> def test_malformed_user_pass ( self ) : <EOL> """<STR_LIT>""" <EOL> userpass = '<STR_LIT>' . encode ( '<STR_LIT>' ) <EOL> self . request . environ [ '<STR_LIT>' ] = '<STR_LIT>' + userpass <EOL> creds = auth . get_basicauth_credentials ( self . request ) <EOL> self . assertIsNone ( creds ) <EOL> def test_valid ( self ) : <EOL> """<STR_LIT>""" <EOL> username = '<STR_LIT>' <EOL> password = '<STR_LIT>' <EOL> userpass = ( username + '<STR_LIT::>' + password ) . encode ( '<STR_LIT>' ) <EOL> self . request . environ [ '<STR_LIT>' ] = '<STR_LIT>' + userpass <EOL> creds = auth . get_basicauth_credentials ( self . request ) <EOL> self . assertEqual ( creds , { '<STR_LIT>' : username , '<STR_LIT:password>' : password } ) <EOL> def test_forbid ( self ) : <EOL> """<STR_LIT>""" <EOL> ret = auth . _forbid ( self . request ) <EOL> self . assertEqual ( ret . status_code , <NUM_LIT> ) <EOL> def test_forbid_logged_in ( self ) : <EOL> """<STR_LIT>""" <EOL> self . request . userid = '<STR_LIT:abc>' <EOL> ret = auth . _forbid ( self . request ) <EOL> self . assertEqual ( ret . status_code , <NUM_LIT> ) <EOL> class TestBasicAuthPolicy ( MockServerTest ) : <EOL> """<STR_LIT>""" <EOL> def setUp ( self ) : <EOL> super ( TestBasicAuthPolicy , self ) . setUp ( ) <EOL> self . policy = auth . BasicAuthenticationPolicy ( ) <EOL> self . request . access = MagicMock ( ) <EOL> self . get_creds = patch ( <EOL> '<STR_LIT>' ) . start ( ) <EOL> def tearDown ( self ) : <EOL> super ( TestBasicAuthPolicy , self ) . tearDown ( ) <EOL> patch . stopall ( ) <EOL> def test_auth_userid_no_credentials ( self ) : <EOL> """<STR_LIT>""" <EOL> self . get_creds . return_value = None <EOL> userid = self . policy . authenticated_userid ( self . request ) <EOL> self . assertIsNone ( userid ) <EOL> def test_auth_fail_verification ( self ) : <EOL> """<STR_LIT>""" <EOL> self . get_creds . return_value = { '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT:password>' : '<STR_LIT>' } <EOL> self . request . access . verify_user . return_value = False <EOL> userid = self . policy . authenticated_userid ( self . request ) <EOL> self . assertIsNone ( userid ) <EOL> def test_auth ( self ) : <EOL> """<STR_LIT>""" <EOL> self . get_creds . return_value = { '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT:password>' : '<STR_LIT>' } <EOL> self . request . access . verify_user . return_value = True <EOL> userid = self . policy . authenticated_userid ( self . request ) <EOL> self . assertEqual ( userid , '<STR_LIT>' ) <EOL> def test_principals_userid_no_credentials ( self ) : <EOL> """<STR_LIT>""" <EOL> principals = self . policy . effective_principals ( self . request ) <EOL> self . assertItemsEqual ( principals , [ Everyone ] ) <EOL> def test_principals ( self ) : <EOL> """<STR_LIT>""" <EOL> self . request . userid = '<STR_LIT>' <EOL> principals = self . policy . effective_principals ( self . request ) <EOL> self . request . access . user_principals . assert_called_with ( '<STR_LIT>' ) <EOL> self . assertEqual ( principals , self . request . access . user_principals ( ) ) <EOL> def test_remember ( self ) : <EOL> """<STR_LIT>""" <EOL> headers = self . policy . remember ( self . request , '<STR_LIT>' ) <EOL> self . assertEqual ( headers , [ ] ) <EOL> def test_forget ( self ) : <EOL> """<STR_LIT>""" <EOL> headers = self . policy . forget ( self . request ) <EOL> self . assertEqual ( headers , [ ] ) <EOL> class TestSessionAuthPolicy ( MockServerTest ) : <EOL> """<STR_LIT>""" <EOL> def setUp ( self ) : <EOL> super ( TestSessionAuthPolicy , self ) . setUp ( ) <EOL> self . policy = auth . SessionAuthPolicy ( ) <EOL> self . request . access = MagicMock ( ) <EOL> self . request . session = { } <EOL> def test_auth_no_userid ( self ) : <EOL> """<STR_LIT>""" <EOL> userid = self . policy . authenticated_userid ( self . request ) <EOL> self . assertIsNone ( userid ) <EOL> userid = self . policy . unauthenticated_userid ( self . request ) <EOL> self . assertIsNone ( userid ) <EOL> def test_auth_userid ( self ) : <EOL> """<STR_LIT>""" <EOL> self . request . session [ '<STR_LIT:user>' ] = '<STR_LIT>' <EOL> userid = self . policy . authenticated_userid ( self . request ) <EOL> self . assertEqual ( userid , '<STR_LIT>' ) <EOL> def test_unauth_userid ( self ) : <EOL> """<STR_LIT>""" <EOL> self . request . userid = '<STR_LIT>' <EOL> userid = self . policy . unauthenticated_userid ( self . request ) <EOL> self . assertEqual ( userid , '<STR_LIT>' ) <EOL> def test_principals_userid_no_credentials ( self ) : <EOL> """<STR_LIT>""" <EOL> principals = self . policy . effective_principals ( self . request ) <EOL> self . assertItemsEqual ( principals , [ Everyone ] ) <EOL> def test_principals ( self ) : <EOL> """<STR_LIT>""" <EOL> self . request . userid = '<STR_LIT>' <EOL> principals = self . policy . effective_principals ( self . request ) <EOL> self . request . access . user_principals . assert_called_with ( '<STR_LIT>' ) <EOL> self . assertEqual ( principals , self . request . access . user_principals ( ) ) <EOL> def test_remember ( self ) : <EOL> """<STR_LIT>""" <EOL> headers = self . policy . remember ( self . request , '<STR_LIT>' ) <EOL> self . assertEqual ( headers , [ ] ) <EOL> self . assertEqual ( self . request . session , { '<STR_LIT:user>' : '<STR_LIT>' } ) <EOL> def test_forget ( self ) : <EOL> """<STR_LIT>""" <EOL> session = self . request . session = MagicMock ( ) <EOL> headers = self . policy . forget ( self . request ) <EOL> self . assertTrue ( session . delete . called ) <EOL> self . assertEqual ( headers , [ ] ) </s>
