def test_exception_if_no_url(self, mock_iter_entry_points):
        """Test that no auth_url at all raises exception."""
        class MockEntrypoint(pkg_resources.EntryPoint):
            def load(self, require=False):
                return FakePlugin

            def resolve(self):
                return FakePlugin

        class FakePlugin(auth_plugin.BaseAuthPlugin):
            pass

        mock_iter_entry_points.side_effect = lambda _t, name=None: [
            MockEntrypoint("fake", "fake", ["FakePlugin"])
        ]

        auth_plugin.discover_auth_systems()
        plugin = auth_plugin.load_plugin("fake")

        self.assertRaises(exceptions.EndpointNotFound,
                          client.Client,
                          "2",
                          "username",
                          "password",
                          "project_id",
                          auth_system="fake",
                          auth_plugin=plugin)
    def test_parse_auth_system_options(self, mock_iter_entry_points):
        """Test that we can parse the auth system options."""
        class MockEntrypoint(pkg_resources.EntryPoint):
            def load(self, require=False):
                return FakePlugin

            def resolve(self):
                return FakePlugin

        class FakePlugin(auth_plugin.BaseAuthPlugin):
            def __init__(self):
                self.opts = {"fake_argument": True}

            def parse_opts(self, args):
                return self.opts

        mock_iter_entry_points.side_effect = lambda _t, name=None: [
            MockEntrypoint("fake", "fake", ["FakePlugin"])
        ]

        auth_plugin.discover_auth_systems()
        plugin = auth_plugin.load_plugin("fake")

        plugin.parse_opts([])
        self.assertIn("fake_argument", plugin.opts)
    def test_auth_system_defining_url(self, mock_iter_entry_points):
        """Test the auth_system defining an url."""
        class MockEntrypoint(pkg_resources.EntryPoint):
            def load(self, require=False):
                return FakePlugin

            def resolve(self):
                return FakePlugin

        class FakePlugin(auth_plugin.BaseAuthPlugin):
            def get_auth_url(self):
                return "http://faked/v1.0"

        mock_iter_entry_points.side_effect = lambda _t, name=None: [
            MockEntrypoint("fake", "fake", ["FakePlugin"])
        ]

        auth_plugin.discover_auth_systems()
        plugin = auth_plugin.load_plugin("fake")

        cs = client.Client("2",
                           "username",
                           "password",
                           "project_id",
                           auth_system="fakewithauthurl",
                           auth_plugin=plugin)
        self.assertEqual("http://faked/v1.0", cs.client.auth_url)
    def test_auth_system_success(self, mock_iter_entry_points, mock_request):
        """Test that we can authenticate using the auth system."""
        class MockEntrypoint(pkg_resources.EntryPoint):
            def load(self, require=False):
                return FakePlugin

            def resolve(self):
                return FakePlugin

        class FakePlugin(auth_plugin.BaseAuthPlugin):
            def authenticate(self, cls, auth_url):
                cls._authenticate(auth_url, {"fake": "me"})

        mock_iter_entry_points.side_effect = lambda _t, name=None: [
            MockEntrypoint("fake", "fake", ["FakePlugin"])
        ]

        mock_request.side_effect = mock_http_request()

        auth_plugin.discover_auth_systems()
        plugin = auth_plugin.load_plugin("fake")
        cs = client.Client("2",
                           "username",
                           "password",
                           "project_id",
                           utils.AUTH_URL_V2,
                           auth_system="fake",
                           auth_plugin=plugin)
        cs.client.authenticate()

        headers = requested_headers(cs)
        token_url = cs.client.auth_url + "/tokens"

        mock_request.assert_called_with("POST",
                                        token_url,
                                        headers=headers,
                                        data='{"fake": "me"}',
                                        allow_redirects=True,
                                        **self.TEST_REQUEST_BASE)