Skip to content

Commit

Permalink
feat(acl) extended API to query by group name (#1544)
Browse files Browse the repository at this point in the history
  • Loading branch information
subnetmarco authored Aug 30, 2016
1 parent e1a8da0 commit 2614119
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 8 deletions.
10 changes: 7 additions & 3 deletions kong/plugins/acl/api.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
local crud = require "kong.api.crud_helpers"
local utils = require "kong.tools.utils"

return {
["/consumers/:username_or_id/acls/"] = {
Expand All @@ -20,15 +21,18 @@ return {
end
},

["/consumers/:username_or_id/acls/:id"] = {
["/consumers/:username_or_id/acls/:group_or_id"] = {
before = function(self, dao_factory, helpers)
crud.find_consumer_by_username_or_id(self, dao_factory, helpers)
self.params.consumer_id = self.consumer.id

local acls, err = dao_factory.acls:find_all {
local filter_keys = {
[utils.is_valid_uuid(self.params.group_or_id) and "id" or "group"] = self.params.group_or_id,
consumer_id = self.params.consumer_id,
id = self.params.id
}
self.params.group_or_id = nil

local acls, err = dao_factory.acls:find_all(filter_keys)
if err then
return helpers.yield_error(err)
elseif #acls == 0 then
Expand Down
60 changes: 55 additions & 5 deletions spec/03-plugins/10-acl/01-api_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -116,13 +116,17 @@ describe("Plugin: acl (API)", function()
end)

describe("/consumers/:consumer/acls/:id", function()
local acl
local acl, acl2
before_each(function()
helpers.dao:truncate_table("acls")
acl = assert(helpers.dao.acls:insert {
group = "hello",
consumer_id = consumer.id
})
acl2 = assert(helpers.dao.acls:insert {
group = "hello2",
consumer_id = consumer.id
})
end)
describe("GET", function()
it("retrieves by id", function()
Expand All @@ -134,6 +138,15 @@ describe("Plugin: acl (API)", function()
local json = cjson.decode(body)
assert.equal(acl.id, json.id)
end)
it("retrieves by group", function()
local res = assert(admin_client:send {
method = "GET",
path = "/consumers/bob/acls/"..acl.group
})
local body = assert.res_status(200, res)
local json = cjson.decode(body)
assert.equal(acl.id, json.id)
end)
it("retrieves ACL by id only if the ACL belongs to the specified consumer", function()
assert(helpers.dao.consumers:insert {
username = "alice"
Expand All @@ -151,10 +164,23 @@ describe("Plugin: acl (API)", function()
})
assert.res_status(404, res)
end)
it("retrieves ACL by group only if the ACL belongs to the specified consumer", function()
local res = assert(admin_client:send {
method = "GET",
path = "/consumers/bob/acls/"..acl.group
})
assert.res_status(200, res)

res = assert(admin_client:send {
method = "GET",
path = "/consumers/alice/acls/"..acl.group
})
assert.res_status(404, res)
end)
end)

describe("PATCH", function()
it("updates an ACL group", function()
it("updates an ACL group by id", function()
local previous_group = acl.group

local res = assert(admin_client:send {
Expand All @@ -171,6 +197,23 @@ describe("Plugin: acl (API)", function()
local json = cjson.decode(body)
assert.not_equal(previous_group, json.group)
end)
it("updates an ACL group by group", function()
local previous_group = acl.group

local res = assert(admin_client:send {
method = "PATCH",
path = "/consumers/bob/acls/"..acl.group,
body = {
group = "updatedGroup2"
},
headers = {
["Content-Type"] = "application/json"
}
})
local body = assert.res_status(200, res)
local json = cjson.decode(body)
assert.not_equal(previous_group, json.group)
end)
describe("errors", function()
it("handles invalid input", function()
local res = assert(admin_client:send {
Expand All @@ -188,20 +231,27 @@ describe("Plugin: acl (API)", function()
end)

describe("DELETE", function()
it("deletes an ACL group", function()
it("deletes an ACL group by id", function()
local res = assert(admin_client:send {
method = "DELETE",
path = "/consumers/bob/acls/"..acl.id,
})
assert.res_status(204, res)
end)
it("deletes an ACL group by group", function()
local res = assert(admin_client:send {
method = "DELETE",
path = "/consumers/bob/acls/"..acl2.group,
})
assert.res_status(204, res)
end)
describe("errors", function()
it("returns 400 on invalid input", function()
it("returns 404 on missing group", function()
local res = assert(admin_client:send {
method = "DELETE",
path = "/consumers/bob/acls/blah"
})
assert.res_status(400, res)
assert.res_status(404, res)
end)
it("returns 404 if not found", function()
local res = assert(admin_client:send {
Expand Down

0 comments on commit 2614119

Please sign in to comment.