Ejemplo n.º 1
0
    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 = xmlrpc_client.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)
Ejemplo n.º 2
0
    def test_use_cached_credentials_first(self):
        expected_user = "******"
        expected_password = "******"
        expected_token = "test token"

        auth = Authenticator(connection=self.mock_connection,
                             user=expected_user,
                             password=expected_password,
                             token=None)
        auth.connection.auth.login = MagicMock(return_value=expected_token)

        auth._get_credentials_interactive = MagicMock()

        self.assertEqual(expected_token, auth.token())
        self.assertEqual(0, auth._get_credentials_interactive.call_count)
        auth.connection.auth.login.assert_called_once_with(
            expected_user, expected_password)
Ejemplo n.º 3
0
    def test_ask_credentials_when_nothing_is_cached(self):
        expected_user = "******"
        expected_password = "******"
        expected_token = "test token"
        auth = Authenticator(connection=self.mock_connection,
                             user=None,
                             password=None,
                             token=None)
        auth.connection.auth.login = MagicMock(return_value=expected_token)

        auth._get_credentials_interactive = MagicMock()
        interactive_credentials = [[expected_user, expected_password]]
        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)
        auth.connection.auth.login.assert_called_once_with(
            expected_user, expected_password)
Ejemplo n.º 4
0
    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 = xmlrpc_client.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)
Ejemplo n.º 5
0
    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 = xmlrpc_client.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)