def test_create(self):
        # Setup
        hosts = [self.faker.domain_name() for _ in range(self.faker.random_int(0, 10))]
        methods = ["GET", "POST"]
        strip_uri = self.faker.boolean()
        preserve_host = self.faker.boolean()
        retries = self.faker.random_int()
        https_only = self.faker.boolean()
        http_if_terminated = self.faker.boolean()
        upstream_connect_timeout = self.faker.random_int()
        upstream_send_timeout = self.faker.random_int()
        upstream_read_timeout = self.faker.random_int()

        # Exercise
        self.api_data = ApiData(name=self.api_name,
                                upstream_url=self.api_upstream_url,
                                uris=self.api_uris,
                                hosts=hosts,
                                methods=methods,
                                strip_uri=strip_uri,
                                preserve_host=preserve_host,
                                retries=retries,
                                https_only=https_only,
                                http_if_terminated=http_if_terminated,
                                upstream_connect_timeout=upstream_connect_timeout,
                                upstream_send_timeout=upstream_send_timeout,
                                upstream_read_timeout=upstream_read_timeout)

        # Verify
        self.assertEqual(self.api_data['name'], self.api_name)
        self.assertEqual(self.api_data['upstream_url'], self.api_upstream_url)
        self.assertEqual(self.api_data['uris'], self.api_uris)
    def setUp(self):
        self.faker = Faker()
        self.faker.add_provider(ApiDataProvider)
        self.api_name = self.faker.api_name()
        self.api_upstream_url = self.faker.url()
        self.api_uris = self.faker.api_uris()
        self.api_kong_id = self.faker.kong_id()

        hosts = [
            self.faker.domain_name()
            for _ in range(self.faker.random_int(0, 10))
        ]
        methods = ["GET", "POST"]
        strip_uri = self.faker.boolean()
        preserve_host = self.faker.boolean()
        retries = self.faker.random_int()
        https_only = self.faker.boolean()
        http_if_terminated = self.faker.boolean()
        upstream_connect_timeout = self.faker.random_int()
        upstream_send_timeout = self.faker.random_int()
        upstream_read_timeout = self.faker.random_int()

        self.api_data = ApiData(
            name=self.api_name,
            upstream_url=self.api_upstream_url,
            uris=self.api_uris,
            hosts=hosts,
            methods=methods,
            strip_uri=strip_uri,
            preserve_host=preserve_host,
            retries=retries,
            https_only=https_only,
            http_if_terminated=http_if_terminated,
            upstream_connect_timeout=upstream_connect_timeout,
            upstream_send_timeout=upstream_send_timeout,
            upstream_read_timeout=upstream_read_timeout)

        self.requests_mock = MagicMock()
        self.session_mock = MagicMock()
        self.requests_mock.session.return_value = self.session_mock

        data_w_id = {**self.api_data, **{'id': self.api_kong_id}}

        self.session_mock.post.return_value.json.return_value = data_w_id
        self.session_mock.put.return_value.json.return_value = data_w_id
        self.session_mock.patch.return_value.json.return_value = data_w_id

        self.session_mock.get.return_value.status_code = 200
        self.session_mock.post.return_value.status_code = 201
        self.session_mock.patch.return_value.status_code = 200
        self.session_mock.delete.return_value.status_code = 204

        self.kong_admin_url = self.faker.url()
        self.apis_endpoint = self.kong_admin_url + 'apis/'

        self.api_admin_client = ApiAdminClient(
            self.kong_admin_url, _session=self.requests_mock.session())
Ejemplo n.º 3
0
    def test_create_api_data_w_invalid_value_raises_exception(self):
        # Setup
        invalid_value = self.faker

        # Verify
        self.assertRaisesRegex(
            ValueError, r'invalid value',
            lambda: ApiData(name=self.api_name,
                            upstream_url=self.api_upstream_url,
                            uris=self.api_uris,
                            methods=invalid_value))
Ejemplo n.º 4
0
    def test_create_api_data_w_invalid_fields_raises_exception(self):
        # Setup
        invalid_value = ""

        # Verify
        self.assertRaisesRegex(
            SchemaViolation, r'invalid_field',
            lambda: ApiData(name=self.api_name,
                            upstream_url=self.api_upstream_url,
                            uris=self.api_uris,
                            invalid_field=invalid_value))
Ejemplo n.º 5
0
    def test_api_admin_create_using_api_data(self):
        """
            Test: passing a ApiData instance results in the same behaviour
            as normal create
        """
        # Setup
        orig_data = ApiData(name=self.api_name,
                            upstream_url=self.api_upstream_url,
                            uris=self.api_uris)

        # Exercise
        self.api_admin_client.create(api_data=orig_data)

        # Verify
        self.session_mock.post.assert_called_once_with(self.apis_endpoint, json=orig_data.as_dict())
Ejemplo n.º 6
0
    def test_api_admin_create_triggers_http_request_to_kong_server(self):
        """
            Test: ApiAdminClient.create() triggers an http request
            to kong server to create the api in the server.
        """
        # Exercise
        self.api_admin_client.create(
            name=self.api_name, upstream_url=self.api_upstream_url, uris=self.api_uris)

        # Verify
        expected_api_data = ApiData(name=self.api_name,
                                    upstream_url=self.api_upstream_url,
                                    uris=self.api_uris)
        self.session_mock.post.assert_called_once_with(self.apis_endpoint,
                                                       json=expected_api_data.as_dict())
Ejemplo n.º 7
0
    def _perform_create(self, name=None, api_data=None, upstream_url=None, **kwargs):

        if isinstance(name, str):
            api_data = ApiData(name=name, upstream_url=upstream_url, **kwargs)

        elif isinstance(api_data, ApiData):
            pass

        elif upstream_url is None:
            raise ValueError("must provide a upstream_url")

        else:
            raise ValueError("must provide ApiData instance or name to _perform_create a api")

        return self._send_create(api_data.as_dict())
Ejemplo n.º 8
0
    def create(self, api_name_or_data, upstream_url=None, **kwargs):

        if isinstance(api_name_or_data, ApiData):
            api_data = api_name_or_data

        elif upstream_url is None:
            raise ValueError("must provide a upstream_url")

        elif isinstance(api_name_or_data, str):
            api_name = api_name_or_data
            api_data = ApiData(name=api_name, upstream_url=upstream_url, **kwargs)
        else:
            raise ValueError("must provide ApiData instance or name to create a api")

        data = self._send_create(api_data)
        return self.__api_data_from_response(data)
Ejemplo n.º 9
0
 def __api_data_from_response(data):
     validated_data = {}
     for k, val in data.items():
         if k in ApiData.allowed_parameters():
             validated_data[k] = val
     return ApiData(**validated_data)
Ejemplo n.º 10
0
 def update_side_effect(a_id, *args, **kwargs):  # pylint: disable=unused-argument
     return ApiData(name=cfaker.api_name(),
                    upstream_url=cfaker.url(),
                    uris=cfaker.api_path(),
                    id=a_id)
Ejemplo n.º 11
0
 def test_create_api_data_wo_uris_method_or_hosts_raises_exception(self):
     self.assertRaisesRegex(
         SchemaViolation, r'provided',
         lambda: ApiData(name=self.api_name,
                         upstream_url=self.api_upstream_url))