Skip to content

Commit

Permalink
new Admin API tests suite
Browse files Browse the repository at this point in the history
Former-commit-id: 08d499e597f311688702a7100b450f31c7b1f499
  • Loading branch information
thibaultcha committed May 28, 2015
1 parent 7db809e commit f546c36
Show file tree
Hide file tree
Showing 10 changed files with 685 additions and 12 deletions.
4 changes: 3 additions & 1 deletion kong/api/crud_helpers.lua
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,10 @@ function _M.put(self, dao_collection)
local new_entity, err
if self.params.id then
new_entity, err = dao_collection:update(self.params)
if not err then
if not err and new_entity then
return responses.send_HTTP_OK(new_entity)
elseif not new_entity then
return responses.send_HTTP_NOT_FOUND()
end
else
new_entity, err = dao_collection:insert(self.params)
Expand Down
46 changes: 43 additions & 3 deletions kong/api/routes/apis.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
local validations = require "kong.dao.schemas"
local crud = require "kong.api.crud_helpers"

local function find_by_name_or_id(self, dao_factory, helpers)
local function find_api_by_name_or_id(self, dao_factory, helpers)
local fetch_keys = {
[validations.is_valid_uuid(self.params.name_or_id) and "id" or "name"] = self.params.name_or_id
}
Expand All @@ -20,6 +20,24 @@ local function find_by_name_or_id(self, dao_factory, helpers)
end
end

local function find_plugin_by_name_or_id(self, dao_factory, helpers)
local fetch_keys = {
api_id = self.api.id,
[validations.is_valid_uuid(self.params.plugin_name_or_id) and "id" or "name"] = self.params.plugin_name_or_id
}
self.params.plugin_name_or_id = nil

local data, err = dao_factory.plugins_configurations:find_by_keys(fetch_keys)
if err then
return helpers.yield_error(err)
end

self.plugin = data[1]
if not self.plugin then
return helpers.responses.send_HTTP_NOT_FOUND()
end
end

return {
["/apis/"] = {
GET = function(self, dao_factory)
Expand All @@ -36,7 +54,7 @@ return {
},

["/apis/:name_or_id"] = {
before = find_by_name_or_id,
before = find_api_by_name_or_id,

GET = function(self, dao_factory, helpers)
return helpers.responses.send_HTTP_OK(self.api)
Expand All @@ -54,7 +72,7 @@ return {

["/apis/:name_or_id/plugins/"] = {
before = function(self, dao_factory, helpers)
find_by_name_or_id(self, dao_factory, helpers)
find_api_by_name_or_id(self, dao_factory, helpers)
self.params.api_id = self.api.id
end,

Expand All @@ -69,5 +87,27 @@ return {
PUT = function(self, dao_factory, helpers)
crud.put(self, dao_factory.plugins_configurations)
end
},

["/apis/:name_or_id/plugins/:plugin_name_or_id"] = {
before = function(self, dao_factory, helpers)
find_api_by_name_or_id(self, dao_factory, helpers)
self.params.api_id = self.api.id

find_plugin_by_name_or_id(self, dao_factory, helpers)
end,

GET = function(self, dao_factory, helpers)
return helpers.responses.send_HTTP_OK(self.plugin)
end,

PATCH = function(self, dao_factory, helpers)
self.params.id = self.plugin.id
crud.patch(self.params, dao_factory.plugins_configurations)
end,

DELETE = function(self, dao_factory)
crud.delete(self.plugin.id, dao_factory.plugins_configurations)
end
}
}
2 changes: 1 addition & 1 deletion kong/dao/cassandra/base_dao.lua
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ function BaseDao:_check_all_unique(t, is_updating)
elseif not unique and k == "self" then
return false, nil, self._entity.." already exists"
elseif not unique then
errors = utils.add_error(errors, k, k.." already exists with value "..t[k])
errors = utils.add_error(errors, k, k.." already exists with value '"..t[k].."'")
end
end
end
Expand Down
7 changes: 5 additions & 2 deletions kong/dao/schemas/consumers.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@ local stringy = require "stringy"
local constants = require "kong.constants"

local function check_custom_id_and_username(value, consumer_t)
if (consumer_t.custom_id == nil or stringy.strip(consumer_t.custom_id) == "")
and (consumer_t.username == nil or stringy.strip(consumer_t.username) == "") then
local custom_id = consumer_t.custom_id
local username = consumer_t.username

if (custom_id == nil or type(custom_id) == "string" and stringy.strip(custom_id) == "")
and (username == nil or type(username) == "string" and stringy.strip(username) == "") then
return false, "At least a 'custom_id' or a 'username' must be specified"
end
return true
Expand Down
Loading

0 comments on commit f546c36

Please sign in to comment.