Skip to content

Commit

Permalink
Closes Kong#48
Browse files Browse the repository at this point in the history
Former-commit-id: 23b2b60031acf7d41edef4e68bb6c55990ab4c61
  • Loading branch information
subnetmarco committed Jun 27, 2015
1 parent 876174f commit 2cdc619
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
11 changes: 10 additions & 1 deletion kong/plugins/keyauth/daos.lua
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
local utils = require "kong.tools.utils"
local stringy = require "stringy"
local BaseDao = require "kong.dao.cassandra.base_dao"

local function generate_if_missing(v, t, column)
if not v or stringy.strip(v) == "" then
return true, nil, { [column] = utils.random_string()}
end
return true
end

local SCHEMA = {
primary_key = {"id"},
fields = {
id = { type = "id", dao_insert_value = true },
created_at = { type = "timestamp", dao_insert_value = true },
consumer_id = { type = "id", required = true, foreign = "consumers:id" },
key = { type = "string", required = true, unique = true, queryable = true }
key = { type = "string", required = false, unique = true, queryable = true, func = generate_if_missing }
}
}

Expand Down
15 changes: 15 additions & 0 deletions spec/plugins/keyauth/daos_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,21 @@ describe("DAO keyauth Credentials", function()
local cred_t = {key = "apikey123", consumer_id = consumer.id}
local app, err = dao_factory.keyauth_credentials:insert(cred_t)
assert.falsy(err)
assert.truthy(app.key)
assert.are.same("apikey123", app.key)
assert.truthy(app.id)
assert.truthy(app.created_at)
end)

it("should insert an autogenerated key", function()
local consumer_t = faker:fake_entity("consumer")
local consumer, err = dao_factory.consumers:insert(consumer_t)
assert.falsy(err)

local cred_t = {consumer_id = consumer.id}
local app, err = dao_factory.keyauth_credentials:insert(cred_t)
assert.falsy(err)
assert.truthy(app.key)
assert.truthy(app.id)
assert.truthy(app.created_at)
end)
Expand Down

0 comments on commit 2cdc619

Please sign in to comment.