<s> import re <EOL> import unittest <EOL> from django . conf import settings <EOL> from django . contrib . auth . models import AbstractBaseUser <EOL> from django . test . utils import override_settings <EOL> import mock <EOL> from nose . tools import eq_ <EOL> from rest_framework . exceptions import AuthenticationFailed <EOL> from hawkrest import HawkAuthenticatedUser , HawkAuthentication <EOL> from . base import BaseTest <EOL> ALTERNATIVE_CREDS = { <EOL> '<STR_LIT:id>' : '<STR_LIT>' , <EOL> '<STR_LIT:key>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' <EOL> } <EOL> def alternative_lookup ( cr_id ) : <EOL> return ALTERNATIVE_CREDS <EOL> class AuthTest ( BaseTest ) : <EOL> def setUp ( self ) : <EOL> super ( AuthTest , self ) . setUp ( ) <EOL> self . auth = HawkAuthentication ( ) <EOL> p = mock . patch ( '<STR_LIT>' ) <EOL> self . mock_log = p . start ( ) <EOL> self . addCleanup ( p . stop ) <EOL> def assert_log_regex ( self , method , pattern ) : <EOL> log_call = getattr ( self . mock_log , method ) . call_args [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] <EOL> assert re . search ( pattern , log_call ) , ( <EOL> '<STR_LIT>' . format ( method , log_call ) ) <EOL> class TestAuthentication ( AuthTest ) : <EOL> def test_www_authenticate_header ( self ) : <EOL> req = self . factory . get ( '<STR_LIT:/>' ) <EOL> eq_ ( self . auth . authenticate_header ( req ) , '<STR_LIT>' ) <EOL> def test_no_auth_for_missing_header ( self ) : <EOL> req = self . factory . get ( '<STR_LIT:/>' ) <EOL> eq_ ( self . auth . authenticate ( req ) , None ) <EOL> def test_no_auth_for_alternate_auth_scheme ( self ) : <EOL> req = self . factory . get ( '<STR_LIT:/>' , HTTP_AUTHORIZATION = '<STR_LIT>' ) <EOL> eq_ ( self . auth . authenticate ( req ) , None ) <EOL> def test_hawk_get ( self ) : <EOL> sender = self . _sender ( ) <EOL> req = self . _request ( sender ) <EOL> assert isinstance ( self . auth . authenticate ( req ) [ <NUM_LIT:0> ] , <EOL> HawkAuthenticatedUser ) , ( <EOL> '<STR_LIT>' ) <EOL> def test_hawk_post ( self ) : <EOL> post_data = '<STR_LIT>' <EOL> content_type = '<STR_LIT>' <EOL> method = '<STR_LIT:POST>' <EOL> sender = self . _sender ( content = post_data , <EOL> content_type = content_type , <EOL> method = method ) <EOL> req = self . _request ( sender , <EOL> content_type = content_type , <EOL> data = post_data , <EOL> method = method ) <EOL> assert isinstance ( self . auth . authenticate ( req ) [ <NUM_LIT:0> ] , <EOL> HawkAuthenticatedUser ) , ( <EOL> '<STR_LIT>' ) <EOL> def test_hawk_post_wrong_sig ( self ) : <EOL> post_data = '<STR_LIT>' <EOL> content_type = '<STR_LIT>' <EOL> method = '<STR_LIT:POST>' <EOL> sender = self . _sender ( content = post_data , <EOL> content_type = content_type , <EOL> method = method ) <EOL> post_data = '<STR_LIT>' . format ( post_data ) <EOL> req = self . _request ( sender , <EOL> content_type = content_type , <EOL> data = post_data , <EOL> method = method ) <EOL> self . assertRaisesRegexp ( AuthenticationFailed , <EOL> '<STR_LIT>' , <EOL> lambda : self . auth . authenticate ( req ) ) <EOL> self . assert_log_regex ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> def test_hawk_get_wrong_sig ( self ) : <EOL> sender = self . _sender ( url = '<STR_LIT>' ) <EOL> req = self . _request ( sender , url = '<STR_LIT>' ) <EOL> with self . assertRaises ( AuthenticationFailed ) : <EOL> self . auth . authenticate ( req ) <EOL> def test_bad_header ( self ) : <EOL> sender = self . _sender ( ) <EOL> sender . request_header += '<STR_LIT>' <EOL> req = self . _request ( sender ) <EOL> self . assertRaisesRegexp ( AuthenticationFailed , <EOL> '<STR_LIT>' , <EOL> lambda : self . auth . authenticate ( req ) ) <EOL> self . assert_log_regex ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> def test_unknown_creds ( self ) : <EOL> wrong_creds = { '<STR_LIT:id>' : '<STR_LIT>' , <EOL> '<STR_LIT:key>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' } <EOL> sender = self . _sender ( credentials = wrong_creds ) <EOL> req = self . _request ( sender ) <EOL> self . assertRaisesRegexp ( AuthenticationFailed , <EOL> '<STR_LIT>' , <EOL> lambda : self . auth . authenticate ( req ) ) <EOL> self . assert_log_regex ( '<STR_LIT>' , <EOL> '<STR_LIT>' ) <EOL> def test_alternative_credential_lookup ( self ) : <EOL> sender = self . _sender ( credentials = ALTERNATIVE_CREDS ) <EOL> req = self . _request ( sender ) <EOL> lookup_path = "<STR_LIT>" % __name__ <EOL> with self . settings ( HAWK_CREDENTIALS_LOOKUP = lookup_path ) : <EOL> assert isinstance ( self . auth . authenticate ( req ) [ <NUM_LIT:0> ] , <EOL> HawkAuthenticatedUser ) , ( <EOL> '<STR_LIT>' ) <EOL> def test_expired_token ( self ) : <EOL> sender = self . _sender ( _timestamp = "<STR_LIT>" ) <EOL> req = self . _request ( sender ) <EOL> self . assertRaisesRegexp ( AuthenticationFailed , <EOL> '<STR_LIT>' , <EOL> lambda : self . auth . authenticate ( req ) ) <EOL> self . assert_log_regex ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> class TestNonce ( AuthTest ) : <EOL> def setUp ( self ) : <EOL> super ( TestNonce , self ) . setUp ( ) <EOL> p = mock . patch ( '<STR_LIT>' ) <EOL> self . cache = p . start ( ) <EOL> self . addCleanup ( p . stop ) <EOL> def auth_request ( self ) : <EOL> sender = self . _sender ( ) <EOL> req = self . _request ( sender ) <EOL> self . auth . authenticate ( req ) <EOL> def test_check_nonce_ok ( self ) : <EOL> self . cache . get . return_value = False <EOL> self . auth_request ( ) <EOL> assert self . cache . get . called , '<STR_LIT>' <EOL> def test_store_nonce ( self ) : <EOL> self . cache . get . return_value = False <EOL> self . auth_request ( ) <EOL> assert self . cache . set . called , '<STR_LIT>' <EOL> def test_nonce_exists ( self ) : <EOL> self . cache . get . return_value = True <EOL> self . assertRaisesRegexp ( AuthenticationFailed , <EOL> '<STR_LIT>' , <EOL> self . auth_request ) <EOL> self . assert_log_regex ( '<STR_LIT>' , <EOL> '<STR_LIT>' ) <EOL> def test_disabled ( self ) : <EOL> with self . settings ( USE_CACHE_FOR_HAWK_NONCE = False ) : <EOL> self . auth_request ( ) <EOL> assert not self . cache . get . called , '<STR_LIT>' <EOL> class TestHawkAuthenticatedUser ( unittest . TestCase ) : <EOL> def setUp ( self ) : <EOL> self . user = HawkAuthenticatedUser ( ) <EOL> def test_method_compliance ( self ) : <EOL> for name , attr in AbstractBaseUser . __dict__ . items ( ) : <EOL> if ( name . startswith ( '<STR_LIT:_>' ) or <EOL> name [ <NUM_LIT:0> ] == name [ <NUM_LIT:0> ] . upper ( ) or <EOL> not callable ( attr ) ) : <EOL> continue <EOL> if not getattr ( self . user , name , None ) : <EOL> raise AssertionError ( <EOL> '<STR_LIT>' <EOL> . format ( name ) ) <EOL> def test_is_anonymous ( self ) : <EOL> eq_ ( self . user . is_anonymous ( ) , False ) <EOL> def test_is_authenticated ( self ) : <EOL> eq_ ( self . user . is_authenticated ( ) , True ) <EOL> def test_is_active ( self ) : <EOL> eq_ ( self . user . is_active , True ) <EOL> def test_has_usable_password ( self ) : <EOL> eq_ ( self . user . has_usable_password ( ) , False ) </s>
