Skip to content

Commit

Permalink
hotfix(api) fix /plugins/:id PATCH
Browse files Browse the repository at this point in the history
- fix PATCH method triggering a 500 error
- new test cases for /plugins and /plugins/:id
  • Loading branch information
thibaultcha committed Feb 3, 2016
1 parent 282481b commit b83875c
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 12 deletions.
10 changes: 6 additions & 4 deletions kong/api/routes/plugins.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
},
}
}
79 changes: 71 additions & 8 deletions spec/integration/admin_api/plugins_routes_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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)

Expand All @@ -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)
Expand Down

0 comments on commit b83875c

Please sign in to comment.