Skip to content

Commit

Permalink
tests(plugins) fix plugins after moving all plugin schemas to new DAO
Browse files Browse the repository at this point in the history
  • Loading branch information
kikito committed Sep 20, 2018
1 parent 8cfd127 commit 6675897
Show file tree
Hide file tree
Showing 21 changed files with 347 additions and 301 deletions.
2 changes: 1 addition & 1 deletion kong/plugins/key-auth/schema.lua
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ return {
default = { "apikey" },
}, },
{ hide_credentials = { type = "boolean", default = false }, },
{ anonymous = { type = "string", uuid = true, len_min = 0, default = "", required = true }, },
{ anonymous = { type = "string", uuid = true, len_min = 0, default = "" }, },
{ key_in_body = { type = "boolean", default = false }, },
{ run_on_preflight = { type = "boolean", default = true }, },
},
Expand Down
2 changes: 1 addition & 1 deletion spec/01-unit/000-new-dao/01-schema/06-plugins_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ describe("plugins", function()
assert.same({
key_names = { "apikey" },
hide_credentials = false,
anonymous = ngx.null,
anonymous = "",
key_in_body = false,
run_on_preflight = true,
}, plugin.config)
Expand Down
4 changes: 2 additions & 2 deletions spec/02-integration/03-dao/04-constraints_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,10 @@ for _, strategy in helpers.each_strategy() do
assert.is_table(plugin)
assert.equal(service_fixture.id, plugin.service.id)
assert.same({
anonymous = "",
run_on_preflight = true,
hide_credentials = false,
key_names = {"apikey"},
anonymous = "",
key_in_body = false,
}, plugin.config)
end)
Expand All @@ -55,10 +55,10 @@ for _, strategy in helpers.each_strategy() do
assert.is_table(plugin)
assert.equal(service_fixture.id, plugin.service.id)
assert.same({
anonymous = "",
run_on_preflight = true,
hide_credentials = false,
key_names = {"api-key"},
anonymous = "",
key_in_body = false,
}, plugin.config)
end)
Expand Down
61 changes: 28 additions & 33 deletions spec/03-plugins/08-datadog/02-schema_spec.lua
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
local schemas = require "kong.dao.schemas_validation"
local datadog_schema = require "kong.plugins.datadog.schema"
local validate_entity = schemas.validate_entity
local schema_def = require "kong.plugins.datadog.schema"
local v = require("spec.helpers").validate_plugin_config_schema


describe("Plugin: datadog (schema)", function()
it("accepts empty config #o", function()
local ok, err = validate_entity({}, datadog_schema)
local ok, err = v({}, schema_def)
assert.is_nil(err)
assert.is_true(ok)
assert.is_truthy(ok)
end)
it("accepts empty metrics", function()
local metrics_input = {}
local ok, err = validate_entity({ metrics = metrics_input}, datadog_schema)
local ok, err = v({ metrics = metrics_input }, schema_def)
assert.is_nil(err)
assert.is_true(ok)
assert.is_truthy(ok)
end)
it("accepts just one metrics", function()
local metrics_input = {
Expand All @@ -23,9 +23,9 @@ describe("Plugin: datadog (schema)", function()
tags = {"K1:V1"}
}
}
local ok, err = validate_entity({ metrics = metrics_input}, datadog_schema)
local ok, err = v({ metrics = metrics_input }, schema_def)
assert.is_nil(err)
assert.is_true(ok)
assert.is_truthy(ok)
end)
it("rejects if name or stat not defined", function()
local metrics_input = {
Expand All @@ -34,18 +34,16 @@ describe("Plugin: datadog (schema)", function()
sample_rate = 1
}
}
local _, err = validate_entity({ metrics = metrics_input}, datadog_schema)
assert.not_nil(err)
assert.equal("name and stat_type must be defined for all stats", err.metrics)
local _, err = v({ metrics = metrics_input }, schema_def)
assert.same({ stat_type = "required field missing" }, err.config.metrics)
local metrics_input = {
{
stat_type = "counter",
sample_rate = 1
}
}
_, err = validate_entity({ metrics = metrics_input}, datadog_schema)
assert.not_nil(err)
assert.equal("name and stat_type must be defined for all stats", err.metrics)
_, err = v({ metrics = metrics_input }, schema_def)
assert.same("required field missing", err.config.metrics.name)
end)
it("rejects counters without sample rate", function()
local metrics_input = {
Expand All @@ -54,7 +52,7 @@ describe("Plugin: datadog (schema)", function()
stat_type = "counter",
}
}
local _, err = validate_entity({ metrics = metrics_input}, datadog_schema)
local _, err = v({ metrics = metrics_input }, schema_def)
assert.not_nil(err)
end)
it("rejects invalid metrics name", function()
Expand All @@ -64,9 +62,9 @@ describe("Plugin: datadog (schema)", function()
stat_type = "counter",
}
}
local _, err = validate_entity({ metrics = metrics_input}, datadog_schema)
assert.not_nil(err)
assert.equal("unrecognized metric name: invalid_name", err.metrics)
local _, err = v({ metrics = metrics_input }, schema_def)
assert.match("expected one of: kong_latency", err.config.metrics.name)
assert.equal("required field missing", err.config.metrics.sample_rate)
end)
it("rejects invalid stat type", function()
local metrics_input = {
Expand All @@ -75,9 +73,8 @@ describe("Plugin: datadog (schema)", function()
stat_type = "invalid_stat",
}
}
local _, err = validate_entity({ metrics = metrics_input}, datadog_schema)
assert.not_nil(err)
assert.equal("unrecognized stat_type: invalid_stat", err.metrics)
local _, err = v({ metrics = metrics_input }, schema_def)
assert.match("expected one of: counter", err.config.metrics.stat_type)
end)
it("rejects if customer identifier missing", function()
local metrics_input = {
Expand All @@ -87,9 +84,8 @@ describe("Plugin: datadog (schema)", function()
sample_rate = 1
}
}
local _, err = validate_entity({ metrics = metrics_input}, datadog_schema)
assert.not_nil(err)
assert.equal("consumer_identifier must be defined for metric status_count_per_user", err.metrics)
local _, err = v({ metrics = metrics_input }, schema_def)
assert.equals("required field missing", err.config.metrics.consumer_identifier)
end)
it("rejects if metric has wrong stat type", function()
local metrics_input = {
Expand All @@ -98,19 +94,19 @@ describe("Plugin: datadog (schema)", function()
stat_type = "counter"
}
}
local _, err = validate_entity({ metrics = metrics_input}, datadog_schema)
local _, err = v({ metrics = metrics_input }, schema_def)
assert.not_nil(err)
assert.equal("unique_users metric only works with stat_type 'set'", err.metrics)
assert.equal("value must be counter", err.config.metrics.stat_type)
metrics_input = {
{
name = "status_count",
stat_type = "set",
sample_rate = 1
}
}
_, err = validate_entity({ metrics = metrics_input}, datadog_schema)
_, err = v({ metrics = metrics_input }, schema_def)
assert.not_nil(err)
assert.equal("status_count metric only works with stat_type 'counter'", err.metrics)
assert.equal("value must be set", err.config.metrics.stat_type)
end)
it("rejects if tags malformed", function()
local metrics_input = {
Expand All @@ -121,9 +117,8 @@ describe("Plugin: datadog (schema)", function()
tags = {"T1:"}
}
}
local _, err = validate_entity({ metrics = metrics_input}, datadog_schema)
assert.not_nil(err)
assert.equal("malformed tags: key 'T1:' has no value. Tags must be list of key[:value]", err.metrics)
local _, err = v({ metrics = metrics_input }, schema_def)
assert.same({ tags = "invalid value: T1:" }, err.config.metrics)
end)
it("accept if tags is aempty list", function()
local metrics_input = {
Expand All @@ -134,7 +129,7 @@ describe("Plugin: datadog (schema)", function()
tags = {}
}
}
local _, err = validate_entity({ metrics = metrics_input}, datadog_schema)
local _, err = v({ metrics = metrics_input }, schema_def)
assert.is_nil(err)
end)
end)
7 changes: 3 additions & 4 deletions spec/03-plugins/10-key-auth/01-api_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ local utils = require "kong.tools.utils"


for _, strategy in helpers.each_strategy() do
describe("Plugin: key-auth (API) [" .. strategy .. "]", function()
describe("Plugin: key-auth (API) [#" .. strategy .. "]", function()
local consumer
local admin_client
local bp
Expand Down Expand Up @@ -290,9 +290,8 @@ for _, strategy in helpers.each_strategy() do
})
assert.response(res).has.status(400)
local body = assert.response(res).has.jsonbody()
assert.equal("'hello\\world' is illegal: bad header name " ..
"'hello\\world', allowed characters are A-Z, a-z, 0-9," ..
" '_', and '-'", body.fields.config.key_names)
assert.equal("bad header name 'hello\\world', allowed characters are A-Z, a-z, 0-9, '_', and '-'",
body.fields.config.key_names)
end)
it("succeeds with valid key_names", function()
local key_name = "hello-world"
Expand Down
2 changes: 1 addition & 1 deletion spec/03-plugins/11-basic-auth/04-invalidations_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ for _, strategy in helpers.each_strategy() do
assert(db:truncate("services"))
assert(db:truncate("consumers"))
assert(db:truncate("plugins"))
assert(db:truncate("hmacauth_credentials"))
assert(db:truncate("basicauth_credentials"))

local route = bp.routes:insert {
hosts = { "basic-auth.com" },
Expand Down
30 changes: 15 additions & 15 deletions spec/03-plugins/14-cors/02-schema_spec.lua
Original file line number Diff line number Diff line change
@@ -1,36 +1,36 @@
local validate_entity = require("kong.dao.schemas_validation").validate_entity
local cors_schema = require "kong.plugins.cors.schema"

local schema_def = require "kong.plugins.cors.schema"
local v = require("spec.helpers").validate_plugin_config_schema

describe("cors schema", function()
it("validates '*'", function()
local ok, err = validate_entity({ origins = { "*" } }, cors_schema)
local ok, err = v({ origins = { "*" } }, schema_def)

assert.True(ok)
assert.is_nil(err)
assert.truthy(ok)
assert.falsy(err)
end)

it("validates what looks like a domain", function()
local ok, err = validate_entity({ origins = { "example.com" } }, cors_schema)
local ok, err = v({ origins = { "example.com" } }, schema_def)

assert.True(ok)
assert.is_nil(err)
assert.truthy(ok)
assert.falsy(err)
end)

it("validates what looks like a regex", function()
local ok, err = validate_entity({ origins = { [[.*\.example(?:-foo)?\.com]] } }, cors_schema)
local ok, err = v({ origins = { [[.*\.example(?:-foo)?\.com]] } }, schema_def)

assert.True(ok)
assert.is_nil(err)
assert.truthy(ok)
assert.falsy(err)
end)

describe("errors", function()
it("with invalid regex in origins", function()
local mock_origins = { [[.*.example.com]], [[invalid_**regex]] }
local ok, err = validate_entity({ origins = mock_origins }, cors_schema)
local ok, err = v({ origins = mock_origins }, schema_def)

assert.False(ok)
assert.equals("origin '" .. mock_origins[2] .. "' is not a valid regex", err.origins)
assert.falsy(ok)
assert.equals("'invalid_**regex' is not a valid regex",
err.config.origins)
end)
end)
end)
17 changes: 7 additions & 10 deletions spec/03-plugins/15-request-transformer/01-schema_spec.lua
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
local schemas = require "kong.dao.schemas_validation"
local request_transformer_schema = require "kong.plugins.request-transformer.schema"


local validate_entity = schemas.validate_entity
local schema_def = require "kong.plugins.request-transformer.schema"
local v = require("spec.helpers").validate_plugin_config_schema


describe("Plugin: request-transformer (schema)", function()
it("validates http_method", function()
local ok, err = validate_entity({http_method = "GET"}, request_transformer_schema)
assert.is_nil(err)
local ok, err = v({ http_method = "GET" }, schema_def)
assert.True(ok)
assert.is_nil(err)
end)
it("errors invalid http_method", function()
local ok, err = validate_entity({http_method = "HELLO"}, request_transformer_schema)
assert.equal("HELLO is not supported", err.http_method)
assert.False(ok)
local ok, err = v({ http_method = "HELLO!" }, schema_def)
assert.Falsy(ok)
assert.equal("invalid value: HELLO!", err.config.http_method)
end)
end)
2 changes: 1 addition & 1 deletion spec/03-plugins/16-response-transformer/04-filter_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ local helpers = require "spec.helpers"


for _, strategy in helpers.each_strategy() do
describe("Plugin: response-transformer (filter)", function()
describe("Plugin: response-transformer (filter) [#" .. strategy .. "]", function()
local proxy_client

setup(function()
Expand Down
42 changes: 19 additions & 23 deletions spec/03-plugins/17-jwt/06-schema_spec.lua
Original file line number Diff line number Diff line change
@@ -1,53 +1,49 @@
local validate_entity = require("kong.dao.schemas_validation").validate_entity
local jwt_schema = require "kong.plugins.jwt.schema"
local schema_def = require "kong.plugins.jwt.schema"
local v = require("spec.helpers").validate_plugin_config_schema


describe("Plugin: jwt (schema)", function()
it("validates 'maximum_expiration'", function()
local ok, err = validate_entity({
local ok, err = v({
maximum_expiration = 60,
claims_to_verify = { "exp", "nbf" },
}, jwt_schema)
}, schema_def)

assert.is_nil(err)
assert.is_true(ok)
assert.is_truthy(ok)
end)

describe("errors", function()
it("when 'maximum_expiration' is negative", function()
local ok, err = validate_entity({
local ok, err = v({
maximum_expiration = -1,
claims_to_verify = { "exp", "nbf" },
}, jwt_schema)
}, schema_def)

assert.is_false(ok)
assert.is_falsy(ok)
assert.same({
maximum_expiration = "should be 0 or greater"
}, err)
maximum_expiration = "value should be between 0 and 31536000"
}, err.config)

local ok, err = validate_entity({
local ok, err = v({
maximum_expiration = -1,
claims_to_verify = { "nbf" },
}, jwt_schema)
}, schema_def)

assert.is_false(ok)
assert.is_falsy(ok)
assert.same({
maximum_expiration = "should be 0 or greater"
}, err)
maximum_expiration = "value should be between 0 and 31536000"
}, err.config)
end)

it("when 'maximum_expiration' is specified without 'exp' in 'claims_to_verify'", function()
local ok, err, self_err = validate_entity({
local ok, err = v({
maximum_expiration = 60,
claims_to_verify = { "nbf" },
}, jwt_schema)
}, schema_def)

assert.is_false(ok)
assert.is_nil(err)
assert.same({
message = "claims_to_verify must contain 'exp' when specifying maximum_expiration",
schema = true,
}, self_err)
assert.is_falsy(ok)
assert.equals("expected to contain: exp", err.config.claims_to_verify)
end)
end)
end)
Loading

0 comments on commit 6675897

Please sign in to comment.