def test_ask_credentials_when_cached_credentials_are_not_working(self): cached_credentials = ["cached_username", "cached_password"] interactive_credentials = [[ "interactive_username", "interactive_password" ]] expected_token = "test token" exception = xmlrpclib.Fault( 2950, "Either the password or username is incorrect") login_mocked_return_values = [ exception, # raised by the cached credentials expected_token # 1st pair of interactive credentials works fine ] auth = Authenticator(connection=self.mock_connection, user=cached_credentials[0], password=cached_credentials[1], token=None) auth.connection.auth.login.side_effect = lambda username, password: _side_effect_return_from_list( login_mocked_return_values) auth._get_credentials_interactive = MagicMock() auth._get_credentials_interactive.side_effect = lambda: _set_username_and_password( auth, interactive_credentials) self.assertEqual(expected_token, auth.token()) self.assertEqual(1, auth._get_credentials_interactive.call_count) expected_login_calls = [] for c in [cached_credentials] + interactive_credentials: expected_login_calls.append(call.login(c[0], c[1])) auth.connection.auth.assert_has_calls(expected_login_calls) self.assertEqual(2, auth.connection.auth.login.call_count)
def test_middleware(self, postman): conn = Mock() for item in postman.middlewares: item(conn) assert tls_started(conn) assert call.login('username', 'password') in conn.mock_calls
def test_with_password(self, smtp): self.settings['password'] = "******" action = actions.SendEmailAction( ACTION_TYPE, ACTION_NAME, self.params, self.settings) action.run() smtpmock = smtp.return_value calls = [call.ehlo(), call.starttls(), call.ehlo(), call.login(self.settings['from'], self.settings['password'])] smtpmock.assert_has_calls(calls) self.assertTrue(smtpmock.sendmail.called, "should call sendmail")
def test_with_password(self, smtp): self.settings['password'] = "******" action = std.SendEmailAction(self.params, self.settings) action.run() smtpmock = smtp.return_value calls = [ call.ehlo(), call.starttls(), call.ehlo(), call.login(self.settings['from'], self.settings['password']) ] smtpmock.assert_has_calls(calls) self.assertTrue(smtpmock.sendmail.called, "should call sendmail")
def test_should_raise_an_exception_after_the_user_provides_wrong_credentials_three_times_in_a_row( self): interactive_credentials = [ ["interactive_username1", "interactive_password1"], ["interactive_username2", "interactive_password2"], ["interactive_username3", "interactive_password3"] ] cached_credentials = ["cached_username", "cached_password"] exception = xmlrpclib.Fault( 2950, "Either the password or username is incorrect") login_mocked_return_values = [ exception, # raised by the cached credentials exception, # raised by the 1st pair of interactive credentials exception, # raised by the 2nd pair of interactive credentials exception # raised by the 3rd pair of interactive credentials ] auth = Authenticator(connection=self.mock_connection, user=cached_credentials[0], password=cached_credentials[1], token=None) auth.connection.auth.login.side_effect = lambda username, password: _side_effect_return_from_list( login_mocked_return_values) auth._get_credentials_interactive = MagicMock() auth._get_credentials_interactive.side_effect = lambda: _set_username_and_password( auth, interactive_credentials) with self.assertRaises(MaximumNumberOfAuthenticationFailures): auth.token() self.assertEqual(Authenticator.MAX_NUM_OF_CREDENTIAL_FAILURES_ALLOWED, auth._get_credentials_interactive.call_count) expected_login_calls = [] for c in [cached_credentials] + interactive_credentials: expected_login_calls.append(call.login(c[0], c[1])) auth.connection.auth.assert_has_calls(expected_login_calls) self.assertEqual(4, auth.connection.auth.login.call_count)
def test_keep_asking_credentials_if_they_are_wrong(self): interactive_credentials = [ ["interactive_username1", "interactive_password1"], ["interactive_username2", "interactive_password2"], ["interactive_username3", "interactive_password3"] ] expected_token = "test token" exception = xmlrpclib.Fault( 2950, "Either the password or username is incorrect") login_mocked_return_values = [ exception, # raised by the 1st pair of interactive credentials exception, # raised by the 2nd pair of interactive credentials expected_token # 3rd pair of interactive credentials works fine ] # Note well: there are no cached credentials auth = Authenticator(connection=self.mock_connection, user=None, password=None, token=None) auth.connection.auth.login.side_effect = lambda username, password: _side_effect_return_from_list( login_mocked_return_values) auth._get_credentials_interactive = MagicMock() auth._get_credentials_interactive.side_effect = lambda: _set_username_and_password( auth, interactive_credentials) self.assertEqual(expected_token, auth.token()) self.assertEqual(3, auth._get_credentials_interactive.call_count) expected_login_calls = [] for c in interactive_credentials: expected_login_calls.append(call.login(c[0], c[1])) auth.connection.auth.assert_has_calls(expected_login_calls) self.assertEqual(3, auth.connection.auth.login.call_count)
def test_middleware(self, postman): with postman.connection() as conn: assert tls_started(conn) assert call.login('username', 'password') in conn.mock_calls
def test_logs_in_user(self, smtp): auth = Auth(username='******', password='******') auth(smtp) assert call.login('user', 'pass') in smtp.mock_calls
def test_logs_in_user(self, smtp): wrap = auth('user', 'pass') wrap(smtp) assert call.login('user', 'pass') in smtp.mock_calls