예제 #1
0
    def test_authenticate_success(self):
        cs = client.Client("username", "password", "project_id", "auth_url")
        management_url = 'https://localhost/v2.1/443470'
        auth_response = utils.TestResponse({
            'status_code': 204,
            'headers': {
                'x-server-management-url': management_url,
                'x-auth-token': '1b751d74-de0c-46ae-84f0-915744b582d1',
            },
        })
        mock_request = mock.Mock(return_value=(auth_response))

        @mock.patch.object(requests, "request", mock_request)
        def test_auth_call():
            cs.client.authenticate()
            headers = {
                'Accept': 'application/json',
                'X-Auth-User': '******',
                'X-Auth-Key': 'password',
                'X-Auth-Project-Id': 'project_id',
                'User-Agent': cs.client.USER_AGENT
            }
            mock_request.assert_called_with("GET",
                                            cs.client.auth_url,
                                            headers=headers,
                                            **self.TEST_REQUEST_BASE)

            self.assertEqual(auth_response.headers['x-server-management-url'],
                             cs.client.management_url)
            self.assertEqual(auth_response.headers['x-auth-token'],
                             cs.client.auth_token)

        test_auth_call()
예제 #2
0
    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):
                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: [
            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("username",
                           "password",
                           "project_id",
                           "auth_url/v2.0",
                           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)
예제 #3
0
    def test_auth_manual(self):
        cs = client.Client("username", "password", "project_id", "auth_url")

        @mock.patch.object(cs.client, 'authenticate')
        def test_auth_call(m):
            cs.authenticate()
            m.assert_called()

        test_auth_call()
예제 #4
0
 def test_auth_call():
     plugin = auth_plugin.DeprecatedAuthPlugin("fakewithauthurl")
     cs = client.Client("username",
                        "password",
                        "project_id",
                        auth_system="fakewithauthurl",
                        auth_plugin=plugin)
     cs.client.authenticate()
     self.assertEqual(cs.client.auth_url, "http://faked/v2.0")
예제 #5
0
    def test_authenticate_failure(self):
        cs = client.Client("username", "password", "project_id", "auth_url")
        auth_response = utils.TestResponse({"status_code": 401})
        mock_request = mock.Mock(return_value=(auth_response))

        @mock.patch.object(requests, "request", mock_request)
        def test_auth_call():
            self.assertRaises(exceptions.Unauthorized, cs.client.authenticate)

        test_auth_call()
예제 #6
0
 def test_auth_call():
     auth_plugin.discover_auth_systems()
     plugin = auth_plugin.DeprecatedAuthPlugin("notexists")
     cs = client.Client("username",
                        "password",
                        "project_id",
                        "auth_url/v2.0",
                        auth_system="notexists",
                        auth_plugin=plugin)
     self.assertRaises(exceptions.AuthSystemNotFound,
                       cs.client.authenticate)
예제 #7
0
    def test_auth_automatic(self):
        cs = client.Client("username", "password", "project_id", "auth_url")
        http_client = cs.client
        http_client.management_url = ''
        mock_request = mock.Mock(return_value=(None, None))

        @mock.patch.object(http_client, 'request', mock_request)
        @mock.patch.object(http_client, 'authenticate')
        def test_auth_call(m):
            http_client.get('/')
            m.assert_called()
            mock_request.assert_called()

        test_auth_call()
예제 #8
0
    def test_authenticate_failure(self):
        cs = client.Client("username", "password", "project_id",
                           "http://*****:*****@mock.patch.object(requests, "request", mock_request)
        def test_auth_call():
            self.assertRaises(exceptions.Unauthorized, cs.client.authenticate)

        test_auth_call()
예제 #9
0
        def test_auth_call():
            plugin = auth_plugin.DeprecatedAuthPlugin("fake")
            cs = client.Client("username",
                               "password",
                               "project_id",
                               "auth_url/v2.0",
                               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)
예제 #10
0
    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):
                return FakePlugin

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

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

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

        cs = client.Client("username",
                           "password",
                           "project_id",
                           auth_system="fakewithauthurl",
                           auth_plugin=plugin)
        self.assertEqual(cs.client.auth_url, "http://faked/v2.0")
예제 #11
0
    def test_authenticate_tenant_id(self):
        cs = client.Client("username",
                           "password",
                           auth_url="http://*****:*****@mock.patch.object(requests, "request", mock_request)
        def test_auth_call():
            cs.client.authenticate()
            headers = {
                'User-Agent': cs.client.USER_AGENT,
                'Content-Type': 'application/json',
                'Accept': 'application/json',
            }
            body = {
                'auth': {
                    'passwordCredentials': {
                        'username': cs.client.user,
                        'password': cs.client.password,
                    },
                    'tenantId': cs.client.tenant_id,
                },
            }

            token_url = cs.client.auth_url + "/tokens"
            mock_request.assert_called_with("POST",
                                            token_url,
                                            headers=headers,
                                            data=json.dumps(body),
                                            allow_redirects=True,
                                            **self.TEST_REQUEST_BASE)

            endpoints = resp["access"]["serviceCatalog"][0]['endpoints']
            public_url = endpoints[0]["publicURL"].rstrip('/')
            self.assertEqual(public_url, cs.client.management_url)
            token_id = resp["access"]["token"]["id"]
            self.assertEqual(token_id, cs.client.auth_token)
            tenant_id = resp["access"]["token"]["tenant"]["id"]
            self.assertEqual(tenant_id, cs.client.tenant_id)

        test_auth_call()
예제 #12
0
    def test_auth_redirect(self):
        cs = client.Client("username",
                           "password",
                           "project_id",
                           "http://*****:*****@mock.patch.object(requests, "request", mock_request)
        def test_auth_call():
            cs.client.authenticate()
            headers = {
                'User-Agent': cs.client.USER_AGENT,
                'Content-Type': 'application/json',
                'Accept': 'application/json',
            }
            body = {
                'auth': {
                    'passwordCredentials': {
                        'username': cs.client.user,
                        'password': cs.client.password,
                    },
                    'tenantName': cs.client.projectid,
                },
            }

            token_url = cs.client.auth_url + "/tokens"
            mock_request.assert_called_with("POST",
                                            token_url,
                                            headers=headers,
                                            data=json.dumps(body),
                                            allow_redirects=True,
                                            **self.TEST_REQUEST_BASE)

            resp = dict_correct_response
            endpoints = resp["access"]["serviceCatalog"][0]['endpoints']
            public_url = endpoints[0]["publicURL"].rstrip('/')
            self.assertEqual(public_url, cs.client.management_url)
            token_id = resp["access"]["token"]["id"]
            self.assertEqual(token_id, cs.client.auth_token)

        test_auth_call()