def test_missing_cookie(self): auth = CookieAuth(Response('', Delay()), 'name', 'value') request = IncomingTestRequest( base_url='http://localhost/', full_path='/test', method='GET' ) with pytest.raises(AuthenticationError): auth.authenticate(request)
def test_missing_auth_header(self): auth = BasicAuth(Response('', Delay()), 'username', 'password') request = IncomingTestRequest( base_url='http://localhost/', full_path='/test', method='GET' ) with pytest.raises(AuthenticationError): auth.authenticate(request)
def test_authenticate(self): auth = CookieAuth(Response('', Delay()), 'name', 'value') request = IncomingTestRequest( base_url='http://localhost/', full_path='/test', method='GET', cookies={'name': 'value'} ) auth.authenticate(request)
def test_authenticate(self): auth = BasicAuth(Response('', Delay()), 'username', 'password') request = IncomingTestRequest( base_url='http://localhost/', full_path='/test', method='GET', headers={'Authorization': 'dXNlcm5hbWU6cGFzc3dvcmQ='} ) auth.authenticate(request)
def test_authenticate(self): auth = TokenAuth(Response('', Delay()), 'abcdefghi') request = IncomingTestRequest( base_url='http://localhost/', full_path='/test', method='GET', headers={'Authorization': 'Bearer abcdefghi'} ) auth.authenticate(request)
def test_missing_bearer_token(self): auth = TokenAuth(Response('', Delay()), 'abcdefghi') request = IncomingTestRequest( base_url='http://localhost/', full_path='/test', method='GET', headers={'Authorization': 'Bearer xxxxxxx'} ) with pytest.raises(AuthenticationError): auth.authenticate(request)
def test_invalid_auth_token(self): auth = BasicAuth(Response('', Delay()), 'username', 'password') request = IncomingTestRequest( base_url='http://localhost/', full_path='/test', method='GET', headers={'Authorization': 'xxx'} ) with pytest.raises(AuthenticationError): auth.authenticate(request)
def test_invalid_sign(self): auth = HmacAuth(Response('', Delay()), 'secret') ts = time.time() request = IncomingTestRequest( base_url='http://localhost/', full_path=f'/test?hmac_timestamp={ts}&hmac_sign=invalid', method='GET' ) with pytest.raises(AuthenticationError): auth.authenticate(request)
def test_authenticate(self): auth = HmacAuth(Response('', Delay()), 'secret') ts = time.time() hash_maker = hmac.new('secret'.encode('utf-8'), digestmod=hashlib.sha1) hash_maker.update(f'/test?hmac_timestamp={ts}'.encode('utf-8')) sign = hash_maker.hexdigest() request = IncomingTestRequest( base_url='http://localhost/', full_path=f'/test?hmac_timestamp={ts}&hmac_sign={sign}', method='GET' ) auth.authenticate(request)
def test_serialize(self): auth = TokenAuth(Response('', Delay()), 'abcdefghi') assert auth.serialize() == { 'method': 'token', 'token': 'abcdefghi', 'unauthorized_response': { 'body': '', 'delay': 0.0, 'headers': {}, 'status': 200, 'used_count': 0 } }
def test_serialize(self): auth = HmacAuth(Response('', Delay()), 'secret') assert auth.serialize() == { 'method': 'hmac', 'key': 'secret', 'unauthorized_response': { 'body': '', 'delay': 0.0, 'headers': {}, 'status': 200, 'used_count': 0 } }
def test_serialize(self): auth = BasicAuth(Response('', Delay()), 'username', 'password') assert auth.serialize() == { 'method': 'basic', 'username': '******', 'password': '******', 'unauthorized_response': { 'body': '', 'delay': 0.0, 'headers': {}, 'status': 200, 'used_count': 0 } }
def test_serialize(self): auth = CookieAuth(Response('', Delay()), 'name', 'value') assert auth.serialize() == { 'method': 'cookie', 'name': 'name', 'value': 'value', 'unauthorized_response': { 'body': '', 'delay': 0.0, 'headers': {}, 'status': 200, 'used_count': 0 } }
def deserialize(cls, data: Dict[str, Any]) -> Auth: """Convert json value to Auth.""" if 'method' in data: implementation = cls._find_implementation(data['method']) data.pop('method') if response_data := data.pop('unauthorized_response', None): response = Response.deserialize(response_data) else: response = Response( {'error': 'Unauthorized', 'message': 'Authentication failed.'}, Delay(0.0), status=401 ) return implementation(response, **data)
def test_signature_in_future(self): auth = HmacAuth(Response('', Delay()), 'secret') ts = 13569465661 # 01/01/2400 hash_maker = hmac.new('secret'.encode('utf-8'), digestmod=hashlib.sha1) hash_maker.update(f'/test?hmac_timestamp={ts}'.encode('utf-8')) sign = hash_maker.hexdigest() request = IncomingTestRequest( base_url='http://localhost/', full_path=f'/test?hmac_timestamp={ts}&hmac_sign={sign}', method='GET' ) with pytest.raises(AuthenticationError): auth.authenticate(request)
def test_authenticate(self): auth = FormAuth(Response('', Delay()), { 'field1': 'value1', 'field2': 'value2' }) request = IncomingTestRequest( base_url='http://localhost/', full_path='/test', method='GET', form={ 'field1': 'value1', 'field2': 'value2' } ) auth.authenticate(request)
def test_missing_field(self): auth = FormAuth(Response('', Delay()), { 'field1': 'value1', 'field2': 'value2' }) request = IncomingTestRequest( base_url='http://localhost/', full_path='/test', method='GET', form={ 'field1': 'value1' } ) with pytest.raises(AuthenticationError): auth.authenticate(request)
def test_serialize(self): auth = FormAuth(Response('', Delay()), { 'field1': 'value1', 'field2': 'value2' }) assert auth.serialize() == { 'method': 'form', 'fields': { 'field1': 'value1', 'field2': 'value2' }, 'unauthorized_response': { 'body': '', 'delay': 0.0, 'headers': {}, 'status': 200, 'used_count': 0 } }