Skip to content

Commit

Permalink
Merge pull request Kong#1321 from Mashape/refactor/hmac-auth
Browse files Browse the repository at this point in the history
tests(hmac-auth) update to new testing tools
  • Loading branch information
thibaultcha authored Jun 24, 2016
2 parents 1ccb8a5 + 25c7576 commit 9983566
Show file tree
Hide file tree
Showing 9 changed files with 1,205 additions and 676 deletions.
2 changes: 1 addition & 1 deletion spec/03-plugins/basic-auth/02-api_spec.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
local helpers = require "spec.helpers"
local cjson = require "cjson"

describe("Basic Auth Credentials API", function()
describe("Plugin: basic-auth (API)", function()
local consumer, admin_client
setup(function()
helpers.dao:truncate_tables()
Expand Down
21 changes: 21 additions & 0 deletions spec/03-plugins/hmac-auth/01-schema_spec.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
local schemas = require "kong.dao.schemas_validation"
local hmac_auth_schema = require "kong.plugins.hmac-auth.schema"
local validate_entity = schemas.validate_entity

describe("Plugin: hmac-auth (schema)", function()
it("accepts empty config", function()
local ok, err = validate_entity({}, hmac_auth_schema)
assert.is_nil(err)
assert.True(ok)
end)
it("accepts correct clock skew", function()
local ok, err = validate_entity({clock_skew = 10}, hmac_auth_schema)
assert.is_nil(err)
assert.True(ok)
end)
it("errors with negative clock skew", function()
local ok, err = validate_entity({clock_skew = -10}, hmac_auth_schema)
assert.equal("Clock Skew should be positive", err.clock_skew)
assert.False(ok)
end)
end)
167 changes: 167 additions & 0 deletions spec/03-plugins/hmac-auth/02-api_spec.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
local helpers = require "spec.helpers"
local cjson = require "cjson"

describe("Plugin: hmac-auth (API)", function()
local client, credential, consumer
setup(function()
helpers.dao:truncate_tables()
assert(helpers.prepare_prefix())
assert(helpers.start_kong())

client = assert(helpers.http_client("127.0.0.1", helpers.test_conf.admin_port))
end)

teardown(function()
if client then
client:close()
end
helpers.stop_kong()
end)

describe("/consumers/:consumer/hmac-auth/", function()
describe("POST", function()
before_each(function()
helpers.dao:truncate_tables()
consumer = assert(helpers.dao.consumers:insert {
username = "bob",
custom_id = "1234"
})
end)
it("[SUCCESS] should create a hmac-auth credential", function()
local res = assert(client:send {
method = "POST",
path = "/consumers/bob/hmac-auth/",
body = {
username = "bob",
secret = "1234"
},
headers = {["Content-Type"] = "application/json"}
})

local body = assert.res_status(201, res)
credential = cjson.decode(body)
assert.equal(consumer.id, credential.consumer_id)
end)
it("[FAILURE] should return proper errors", function()
local res = assert(client:send {
method = "POST",
path = "/consumers/bob/hmac-auth/",
body = {},
headers = {["Content-Type"] = "application/json"}
})
local body = assert.res_status(400, res)
assert.equal('{"username":"username is required"}', body)
end)
end)

describe("PUT", function()
it("[SUCCESS] should create and update", function()
local res = assert(client:send {
method = "PUT",
path = "/consumers/bob/hmac-auth/",
body = {
username = "bob",
secret = "1234"
},
headers = {["Content-Type"] = "application/json"}
})
local body = assert.res_status(201, res)
credential = cjson.decode(body)
assert.equal(consumer.id, credential.consumer_id)
end)
it("[FAILURE] should return proper errors", function()
local res = assert(client:send {
method = "PUT",
path = "/consumers/bob/hmac-auth/",
body = {},
headers = {["Content-Type"] = "application/json"}
})
local body = assert.res_status(400, res)
assert.equal('{"username":"username is required"}', body)
end)
end)

describe("GET", function()
it("should retrieve all", function()
local res = assert(client:send {
method = "GET",
path = "/consumers/bob/hmac-auth/",
body = {},
headers = {["Content-Type"] = "application/json"}
})
local body_json = assert.res_status(200, res)
local body = cjson.decode(body_json)
assert.equal(1, #(body.data))
end)
end)
end)

describe("/consumers/:consumer/hmac-auth/:id", function()
describe("GET", function()
it("should retrieve by id", function()
local res = assert(client:send {
method = "GET",
path = "/consumers/bob/hmac-auth/"..credential.id,
body = {},
headers = {["Content-Type"] = "application/json"}
})
local body_json = assert.res_status(200, res)
local body = cjson.decode(body_json)
assert.equals(credential.id, body.id)
end)
end)

describe("PATCH", function()
it("[SUCCESS] should update a credential", function()
local res = assert(client:send {
method = "PATCH",
path = "/consumers/bob/hmac-auth/"..credential.id,
body = {username = "alice"},
headers = {["Content-Type"] = "application/json"}
})
local body_json = assert.res_status(200, res)
credential = cjson.decode(body_json)
assert.equals("alice", credential.username)
end)
it("[FAILURE] should return proper errors", function()
local res = assert(client:send {
method = "PATCH",
path = "/consumers/bob/hmac-auth/"..credential.id,
body = {username = ""},
headers = {["Content-Type"] = "application/json"}
})
local response = assert.res_status(400, res)
assert.equal('{"username":"username is required"}', response)
end)
end)

describe("DELETE", function()
it("[FAILURE] should return proper errors", function()
local res = assert(client:send {
method = "DELETE",
path = "/consumers/bob/hmac-auth/alice",
body = {},
headers = {["Content-Type"] = "application/json"}
})
assert.res_status(400, res)

local res = assert(client:send {
method = "DELETE",
path = "/consumers/bob/hmac-auth/00000000-0000-0000-0000-000000000000",
body = {},
headers = {["Content-Type"] = "application/json"}
})
assert.res_status(404, res)
end)
it("[SUCCESS] should delete a credential", function()
local res = assert(client:send {
method = "DELETE",
path = "/consumers/bob/hmac-auth/"..credential.id,
body = {},
headers = {["Content-Type"] = "application/json"}
})
assert.res_status(204, res)
end)
end)
end)
end)
Loading

0 comments on commit 9983566

Please sign in to comment.