diff --git a/kong/api/routes/plugins.lua b/kong/api/routes/plugins.lua index dce7638e77d1..90252ec21839 100644 --- a/kong/api/routes/plugins.lua +++ b/kong/api/routes/plugins.lua @@ -52,7 +52,7 @@ return { ["/plugins/:id"] = { before = function(self, dao_factory, helpers) local err - self.plugin_conf, err = dao_factory.plugins:find_by_primary_key({ id = self.params.id }) + self.plugin_conf, err = dao_factory.plugins:find_by_primary_key {id = self.params.id} if err then return helpers.yield_error(err) elseif not self.plugin_conf then @@ -65,17 +65,19 @@ return { end, PATCH = function(self, dao_factory) - crud.patch(self.params, self.plugin_conf, dao_factory.plugins) + crud.patch(self.params, dao_factory.plugins) end, DELETE = function(self, dao_factory) crud.delete(self.plugin_conf, dao_factory.plugins) end - }, ["/plugins/enabled"] = { + }, + + ["/plugins/enabled"] = { GET = function(self, dao_factory, helpers) return helpers.responses.send_HTTP_OK { enabled_plugins = configuration.plugins } end - }, + } } diff --git a/spec/integration/admin_api/plugins_routes_spec.lua b/spec/integration/admin_api/plugins_routes_spec.lua index beb387dc004d..d730fd2827e1 100644 --- a/spec/integration/admin_api/plugins_routes_spec.lua +++ b/spec/integration/admin_api/plugins_routes_spec.lua @@ -5,11 +5,6 @@ local spec_helper = require "spec.spec_helpers" describe("Admin API", function() setup(function() spec_helper.prepare_db() - spec_helper.insert_fixtures { - api = { - {request_host = "test.com", upstream_url = "http://mockbin.com"} - } - } spec_helper.start_kong() end) @@ -24,7 +19,76 @@ describe("Admin API", function() local response, status = http_client.get(BASE_URL) assert.equal(200, status) local body = json.decode(response) - assert.equal("table", type(body.enabled_plugins)) + assert.is_table(body.enabled_plugins) + end) + end) + + describe("/plugins", function() + local BASE_URL = spec_helper.API_URL.."/plugins/" + local fixtures + + setup(function() + fixtures = spec_helper.insert_fixtures { + api = { + {request_host = "test-get.com", upstream_url = "http://mockbin.com"}, + {request_host = "test-patch.com", upstream_url = "http://mockbin.com"}, + {request_host = "test-delete.com", upstream_url = "http://mockbin.com"} + }, + plugin = { + {name = "key-auth", __api = 1}, + {name = "key-auth", __api = 2}, + {name = "key-auth", __api = 3} + } + } + end) + + describe("GET", function() + it("[SUCCESS] should retrieve all the plugins", function() + local response, status = http_client.get(BASE_URL) + assert.equal(200, status) + local body = json.decode(response) + assert.equal(3, body.total) + end) + end) + + describe("/plugins/:id", function() + describe("GET", function() + it("[SUCCESS] should GET a plugin", function() + local response, status = http_client.get(BASE_URL..fixtures.plugin[1].id) + assert.equal(200, status) + local body = json.decode(response) + assert.equal("key-auth", body.name) + assert.equal(fixtures.api[1].id, body.api_id) + end) + it("[FAILURE] should return 404 if not found", function() + local _, status = http_client.get(BASE_URL.."2f49143e-caba-11e5-9d08-03a86066f7a4") + assert.equal(404, status) + end) + end) + + describe("PATCH", function() + it("[SUCCESS] should PATCH a plugin", function() + local response, status = http_client.patch(BASE_URL..fixtures.plugin[2].id, {enabled = false}) + assert.equal(200, status) + local body = json.decode(response) + assert.False(body.enabled) + end) + it("[FAILURE] should return 404 if not found", function() + local _, status = http_client.patch(BASE_URL.."2f49143e-caba-11e5-9d08-03a86066f7a4") + assert.equal(404, status) + end) + end) + + describe("DELETE", function() + it("[SUCCESS] should DELETE a plugin", function() + local _, status = http_client.delete(BASE_URL..fixtures.plugin[3].id) + assert.equal(204, status) + end) + it("[FAILURE] should return 404 if not found", function() + local _, status = http_client.delete(BASE_URL.."2f49143e-caba-11e5-9d08-03a86066f7a4") + assert.equal(404, status) + end) + end) end) end) @@ -35,9 +99,8 @@ describe("Admin API", function() local response, status = http_client.get(BASE_URL) assert.equal(200, status) local body = json.decode(response) - assert.equal("table", type(body.fields)) + assert.is_table(body.fields) end) - it("[FAILURE] should return a descriptive error if schema is not found", function() local response, status = http_client.get(spec_helper.API_URL.."/plugins/schema/foo") assert.equal(404, status)