Esempio n. 1
0
    def test_auth_system_success(self, mock_mgr_map, mock_request):
        """Test that we can authenticate using the auth system."""
        class FakePlugin(BaseFakePlugin):
            def authenticate(self, cls):
                cls.request("POST",
                            "http://auth/tokens",
                            json={"fake": "me"},
                            allow_redirects=True)

        mock_mgr_map.side_effect = (
            lambda func: func(MockEntrypoint("fake", FakePlugin)))

        mock_request.side_effect = mock_http_request()

        auth.discover_auth_systems()
        plugin = auth.load_plugin("fake")
        cs = client.HTTPClient(auth_plugin=plugin)
        cs.authenticate()

        headers = requested_headers(cs)

        mock_request.assert_called_with("POST",
                                        "http://auth/tokens",
                                        headers=headers,
                                        data='{"fake": "me"}',
                                        allow_redirects=True,
                                        **TEST_REQUEST_BASE)
Esempio n. 2
0
    def test_client_with_no_token_and_no_endpoint(self):
        with mock.patch('%s.FakeAuthPlugin.token_and_endpoint' % __name__,
                        mock.MagicMock()) as mocked_token_and_endpoint:
            mocked_token_and_endpoint.return_value = (None, None)

            http_client = client.HTTPClient(FakeAuthPlugin())
            self.assertRaises(
                exceptions.AuthorizationFailure, http_client.client_request,
                TestClient(http_client), "GET", "/resource", json={"1": "2"})
Esempio n. 3
0
 def test_client_raising_unauthorized_with_equal_token_and_endpoint(self):
     with mock.patch('%s.FakeAuthPlugin.token_and_endpoint' % __name__,
                     mock.MagicMock()) as mocked_token_and_endpoint:
         mocked_token_and_endpoint.return_value = ('token-0', '/endpoint-0')
         http_client = client.HTTPClient(FakeAuthPlugin())
         http_client.request = mock.MagicMock(
             side_effect=exceptions.Unauthorized())
         self.assertRaises(
             exceptions.Unauthorized, http_client.client_request,
             TestClient(http_client), "GET", "/resource", json={"1": "2"})
Esempio n. 4
0
 def test_client_with_response_400_status_code(self):
     http_client = client.HTTPClient(FakeAuthPlugin())
     mock_request = mock.Mock()
     mock_request.return_value = requests.Response()
     mock_request.return_value.status_code = 400
     with mock.patch("requests.Session.request", mock_request):
         self.assertRaises(
             exceptions.BadRequest, http_client.client_request,
             TestClient(http_client), "GET", "/resource",
             json={"bad": "request"})
Esempio n. 5
0
 def test_client_with_timeout(self):
     http_client = client.HTTPClient(None, timeout=2)
     self.assertEqual(http_client.timeout, 2)
     mock_request = mock.Mock()
     mock_request.return_value = requests.Response()
     mock_request.return_value.status_code = 200
     with mock.patch("requests.Session.request", mock_request):
         http_client.request("GET", "/", json={"1": "2"})
         requests.Session.request.assert_called_with(
             "GET",
             "/",
             timeout=2,
             headers=mock.ANY,
             verify=mock.ANY,
             data=mock.ANY)
Esempio n. 6
0
 def test_client_request(self):
     http_client = client.HTTPClient(FakeAuthPlugin())
     mock_request = mock.Mock()
     mock_request.return_value = requests.Response()
     mock_request.return_value.status_code = 200
     with mock.patch("requests.Session.request", mock_request):
         http_client.client_request(
             TestClient(http_client), "GET", "/resource", json={"1": "2"})
         requests.Session.request.assert_called_with(
             "GET",
             "/endpoint-0/resource",
             headers={
                 "User-Agent": http_client.user_agent,
                 "Content-Type": "application/json",
                 "X-Auth-Token": "token-0"
             },
             data='{"1": "2"}',
             verify=True)
Esempio n. 7
0
    def test_client_raising_unauthorized_with_just_authenticated(self):
        side_effect_rv = [True, False, True]

        def side_effect(*args, **kwargs):
            if side_effect_rv.pop():
                raise exceptions.EndpointException()
            return ("token-%s" % len(side_effect_rv),
                    "/endpoint-%s" % len(side_effect_rv))

        with mock.patch('%s.FakeAuthPlugin.token_and_endpoint' % __name__,
                        mock.MagicMock()) as mocked_token_and_endpoint:
            mocked_token_and_endpoint.side_effect = side_effect

            http_client = client.HTTPClient(FakeAuthPlugin())
            http_client.request = mock.MagicMock(
                side_effect=exceptions.Unauthorized())
            self.assertRaises(
                exceptions.Unauthorized, http_client.client_request,
                TestClient(http_client), "GET", "/resource", json={"1": "2"})
Esempio n. 8
0
    def test_client_request_reissue(self):
        reject_token = None

        def fake_request(method, url, **kwargs):
            if kwargs["headers"]["X-Auth-Token"] == reject_token:
                raise exceptions.Unauthorized(method=method, url=url)
            return "%s %s" % (method, url)

        http_client = client.HTTPClient(FakeAuthPlugin())
        test_client = TestClient(http_client)
        http_client.request = fake_request

        self.assertEqual(
            http_client.client_request(
                test_client, "GET", "/resource"),
            "GET /endpoint-0/resource")
        reject_token = "token-0"
        self.assertEqual(
            http_client.client_request(
                test_client, "GET", "/resource"),
            "GET /endpoint-1/resource")