def test_should_authenticate_if_credentials_are_valid(self): """ Should accept valid credentials """ # Mock user dto from core. authenticated_user_dto = mock.Mock() # Mock user object authenticated_user = mock.Mock() authenticated_user.serialize = mock.Mock(return_value={}) # Mock User.authenticate method # to return mock user User = mock.Mock() User.authenticate = mock.Mock(return_value=authenticated_user_dto) # Mock the ReturnUser ReturnUser = mock.Mock() ReturnUser.build = mock.Mock(return_value=authenticated_user) with mock.patch.multiple('caliopen.web.authentication.validation', User=User, ReturnUser=ReturnUser): auth = validate(username="******", password="******") # Assert we called user.to_api() authenticated_user.to_api.assert_call_once() # assert mock was called User.authenticate.assert_call_once_with("John", "Doe") ReturnUser.build.assert_call_once_with(authenticated_user_dto) authenticated_user.serialize.assert_call_once() self.assertTrue(auth.success, 'User should be authenticated')
def test_should_reject_empty_password(self): """ Should reject empty password. """ for val in [None, ""]: auth = validate(username="******", password=val) self.assertFalse(auth.success) self.assertIsNone(auth.user) self.assertHasError(auth, fieldName='password')
def test_should_reject_empty_username(self): """ Should reject empty username. """ for val in [None, ""]: auth = validate(username=val, password="******") self.assertFalse(auth.success) self.assertIsNone(auth.user) self.assertHasError(auth, fieldName='username')
def test_should_reject_empty_password(self): """ Should reject empty password. """ for val in [ None, ""]: auth = validate(username="******", password=val) self.assertFalse(auth.success) self.assertIsNone(auth.user) self.assertHasError(auth, fieldName='password')
def test_should_reject_empty_username(self): """ Should reject empty username. """ for val in [ None, ""]: auth = validate(username=val, password="******") self.assertFalse(auth.success) self.assertIsNone(auth.user) self.assertHasError(auth, fieldName='username')
def handle_signin(self): """Validate the user request""" # Validate request authentication = validate(self.username, self.password) # Handle successful request if authentication.success is True: authenticate_user(self.request, authentication.user) return redirect(self.request, 'user.redirect_after_signin') # Handle error request self.request.status_int = 400 return {'username': self.username, 'errors': authentication.errors}
def test_should_reject_if_credentials_are_invalid(self): """ Should reject invalid credentials """ # Mock User.authenticate method # to raise a ValidationError User = mock.Mock() User.authenticate = mock.Mock(side_effect=CredentialException('A credential Error')) with mock.patch.multiple('caliopen.web.authentication.validation', User=User): auth = validate(username="******", password="******") # ensure mock has been called User.authenticate.assert_call_once_with("John", "Doe") self.assertFalse(auth.success, 'User should not be authenticated')
def test_should_reject_if_credentials_are_invalid(self): """ Should reject invalid credentials """ # Mock User.authenticate method # to raise a ValidationError User = mock.Mock() User.authenticate = mock.Mock( side_effect=CredentialException('A credential Error')) with mock.patch.multiple('caliopen.web.authentication.validation', User=User): auth = validate(username="******", password="******") # ensure mock has been called User.authenticate.assert_call_once_with("John", "Doe") self.assertFalse(auth.success, 'User should not be authenticated')
def test_should_rethrow_if_internal_errors(self): """ Should rethrow if internal error during validation """ # Mock User.authenticate method # to raise an un expected Exception User = mock.Mock() User.authenticate = mock.Mock(side_effect=Exception('Internal error')) with mock.patch.multiple('caliopen.web.authentication.validation', User=User): with self.assertRaises(Exception) as context: auth = validate(username="******", password="******") # ensure mock has been called User.authenticate.assert_call_once_with("John", "Doe") self.assertTrue('Internal error' in context.exception, 'Erorr is raised again')
def handle_signin(self): """Validate the user request""" # Validate request authentication = validate(self.username, self.password) # Handle successful request if authentication.success is True: authenticate_user(self.request, authentication.user) return redirect(self.request, 'user.redirect_after_signin') # Handle error request self.request.status_int = 400 return { 'username': self.username, 'errors': authentication.errors }