def test_given_not_registered_user__when_calling_login_then_UnrecognisedUserException_is_returned( ): # given mock_user_collection = Mock() mock_whitelisted_collection = Mock() mock_user_collection.find_one.return_value = False auth_service = AuthService(mock_user_collection, mock_whitelisted_collection, mock_private_key, jwt) # then with raises(UnrecognisedUserException): auth_service.login({'username': '******', "password": '******'})
def test_given_valid_auth_header_when_calling_login_then_token_is_returned(): # given mock_user_collection = Mock() mock_whitelisted_collection = Mock() mock_user_collection.find_one.return_value = { # hashed version of 'test' with salt 'password': "******", 'name': 'dummy_name', '_id': 'some_id' } auth_service = AuthService(mock_user_collection, mock_whitelisted_collection, mock_private_key, jwt) # when token = auth_service.login({"username": '******', 'password': '******'}) # then data = jwt.decode(token, mock_public_key, algorithm='RS256') assert 'some_id' == data['public_id'] assert data['exp'] is not None # hashed version, no salt for 'test' print data['key'] == "xzwXZ2CoOI8Z/2QH" mock_user_collection.find_one.assert_called_once_with({'name': 'test'})
def test_given_invalid_password_when_calling_login_then_WrongPasswordException_is_returned( ): # given mock_user_collection = Mock() mock_whitelisted_collection = Mock() mock_user_collection.find_one.return_value = { # hashed version of 'test' with salt 'password': "******", '_id': 'some_id' } auth_service = AuthService(mock_user_collection, mock_whitelisted_collection, mock_private_key, jwt) # then with raises(WrongPasswordException): auth_service.login({'username': '******', "password": '******'})
def test_given_invalid_header_when_calling_login_then_MissingAuthHeaderException_is_returned( ): # given mock_user_collection = Mock() mock_whitelisted_collection = Mock() auth_service = AuthService(mock_user_collection, mock_whitelisted_collection, mock_private_key, jwt) # then with raises(MissingAuthHeaderException): auth_service.login({"password": '******'}) with raises(MissingAuthHeaderException): auth_service.login({"username": '******'}) with raises(MissingAuthHeaderException): auth_service.login(None)