Пример #1
0
class KongPluginTestCase(unittest.TestCase):
    def setUp(self):
        self.api = KongPlugin(mock_kong_admin_url, "mockbin")

    @responses.activate
    def test_plugin_add_new(self):

        no_plugin_response = {"data": []}
        expected_url = '{}/apis/mockbin/plugins'.format(mock_kong_admin_url)
        responses.add(responses.GET,
                      expected_url,
                      status=201,
                      body=json.dumps(no_plugin_response))

        expected_url = "{}/apis/mockbin/plugins".format(mock_kong_admin_url)
        responses.add(responses.POST, expected_url, status=201)

        example_data = {
            "name": "request-transformer",
            "config": {
                "config.add.headers":
                "x-new-header:some_value, x-another-header:some_value",
                "config.add.querystring":
                "new-param:some_value, another-param:some_value",
                "config.add.form":
                "new-form-param:some_value, another-form-param:some_value",
                "config.remove.headers": "x-toremove, x-another-one",
                "config.remove.querystring":
                "param-toremove, param-another-one",
                "config.remove.form":
                "formparam-toremove, formparam-another-one"
            }
        }
        response = self.api.add_or_update(**example_data)

        assert response.status_code == 201, \
         "Expect 201 Created, got: {}: {}" . format (response.status_code, response.content)

    def test__get_plugin_id(self):

        plugins_list = [{
            "name": "needle",
            "id": 123
        }, {
            "name": "request-transformer"
        }]

        plugin_id = self.api._get_plugin_id("needle", plugins_list)
        assert plugin_id == 123, \
         'Expect the correct plugin_id to be returned. Expected 123. Got: {}' . format (plugin_id)

    def test__get_plugin_id_plugin_doesnt_exist(self):

        plugins_list = [{"name": "haystack"}, {"name": "request-transformer"}]

        plugin = self.api._get_plugin_id("needle", plugins_list)
        assert plugin is None, \
         'Expect it to return None if no plugin is found. Expected None. Got: {}' . format (plugin)

    @responses.activate
    def test_plugin_update(self):
        example_response = {
            "data": [{
                "id": "1",
                "name": "basic-auth"
            }, {
                "id": "2",
                "name": "request-transformer"
            }]
        }

        expected_url = "{}/apis/mockbin/plugins".format(mock_kong_admin_url)
        responses.add(responses.GET,
                      expected_url,
                      status=200,
                      body=json.dumps(example_response))

        expected_url = "{}/apis/mockbin/plugins/1".format(mock_kong_admin_url)
        responses.add(responses.PATCH, expected_url, status=200)

        response = self.api.add_or_update("basic-auth")

        assert response.status_code == 200

    @responses.activate
    def test_plugin_delete(self):

        id = "123"
        expected_url = "{}/apis/mockbin/plugins/{}".format(
            mock_kong_admin_url, id)
        responses.add(responses.DELETE, expected_url, status=204)

        self.api.delete(id)
 def setUp(self):
     self.api = KongPlugin(mock_kong_admin_url, "mockbin")
Пример #3
0
 def setUp(self):
     self.api = KongPlugin(mock_kong_admin_url, "mockbin")
class KongPluginTestCase(unittest.TestCase):
    def setUp(self):
        self.api = KongPlugin(mock_kong_admin_url, "mockbin")

    @responses.activate
    def test_plugin_add_new(self):

        no_plugin_response = {"data": []}
        expected_url = "{}/apis/mockbin/plugins".format(mock_kong_admin_url)
        responses.add(responses.GET, expected_url, status=201, body=json.dumps(no_plugin_response))

        expected_url = "{}/apis/mockbin/plugins".format(mock_kong_admin_url)
        responses.add(responses.POST, expected_url, status=201)

        example_data = {
            "name": "request-transformer",
            "config": {
                "config.add.headers": "x-new-header:some_value, x-another-header:some_value",
                "config.add.querystring": "new-param:some_value, another-param:some_value",
                "config.add.form": "new-form-param:some_value, another-form-param:some_value",
                "config.remove.headers": "x-toremove, x-another-one",
                "config.remove.querystring": "param-toremove, param-another-one",
                "config.remove.form": "formparam-toremove, formparam-another-one",
            },
        }
        response = self.api.add_or_update(**example_data)

        assert response.status_code == 201, "Expect 201 Created, got: {}: {}".format(
            response.status_code, response.content
        )

    def test__get_plugin_id(self):

        plugins_list = [{"name": "needle", "id": 123}, {"name": "request-transformer"}]

        plugin_id = self.api._get_plugin_id("needle", plugins_list)
        assert plugin_id == 123, "Expect the correct plugin_id to be returned. Expected 123. Got: {}".format(plugin_id)

    def test__get_plugin_id_plugin_doesnt_exist(self):

        plugins_list = [{"name": "haystack"}, {"name": "request-transformer"}]

        plugin = self.api._get_plugin_id("needle", plugins_list)
        assert plugin is None, "Expect it to return None if no plugin is found. Expected None. Got: {}".format(plugin)

    @responses.activate
    def test_plugin_update(self):
        example_response = {"data": [{"id": "1", "name": "basic-auth"}, {"id": "2", "name": "request-transformer"}]}

        expected_url = "{}/apis/mockbin/plugins".format(mock_kong_admin_url)
        responses.add(responses.GET, expected_url, status=200, body=json.dumps(example_response))

        expected_url = "{}/apis/mockbin/plugins/1".format(mock_kong_admin_url)
        responses.add(responses.PATCH, expected_url, status=200)

        response = self.api.add_or_update("basic-auth")

        assert response.status_code == 200

    @responses.activate
    def test_plugin_delete(self):

        id = "123"
        expected_url = "{}/apis/mockbin/plugins/{}".format(mock_kong_admin_url, id)
        responses.add(responses.DELETE, expected_url, status=204)

        self.api.delete(id)