Skip to content

Commit 548d225

Browse files
authored
Add tests
1 parent 9c281b1 commit 548d225

File tree

10 files changed

+253
-6
lines changed

10 files changed

+253
-6
lines changed

.busted

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
return {
2+
default = {
3+
verbose = true,
4+
coverage = false,
5+
output = "gtest",
6+
},
7+
}

.circleci/config.yml

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
version: 2.1
2+
3+
jobs:
4+
test:
5+
machine:
6+
image: ubuntu-2004:202010-01
7+
docker_layer_caching: true
8+
steps:
9+
- checkout
10+
- run:
11+
name: Install pongo
12+
command: |
13+
git clone --single-branch https://github.com/Kong/kong-pongo ../kong-pongo
14+
../kong-pongo/pongo.sh up
15+
../kong-pongo/pongo.sh build
16+
- run:
17+
name: Lint
18+
command: |
19+
../kong-pongo/pongo.sh lint
20+
- run:
21+
name: Test
22+
command: |
23+
../kong-pongo/pongo.sh run
24+
25+
workflows:
26+
test:
27+
jobs:
28+
- test

.editorconfig

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
root = true
2+
3+
[*]
4+
end_of_line = lf
5+
insert_final_newline = true
6+
trim_trailing_whitespace = true
7+
charset = utf-8
8+
9+
[*.lua]
10+
indent_style = space
11+
indent_size = 2
12+
13+
[kong/templates/nginx*]
14+
indent_style = space
15+
indent_size = 4
16+
17+
[*.template]
18+
indent_style = space
19+
indent_size = 4
20+
21+
[Makefile]
22+
indent_style = tab

.gitignore

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# servroot typically is the Kong working directory for tests
2+
servroot
3+
# exclude generated packed rocks
4+
*.rock

.luacheckrc

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
std = "ngx_lua"
2+
unused_args = false
3+
redefined = false
4+
max_line_length = false
5+
6+
7+
include_files = {
8+
"**/*.lua",
9+
"*.rockspec",
10+
".busted",
11+
".luacheckrc",
12+
}
13+
14+
15+
globals = {
16+
"_KONG",
17+
"kong",
18+
"ngx.IS_CLI",
19+
}
20+
21+
22+
not_globals = {
23+
"string.len",
24+
"table.getn",
25+
}
26+
27+
28+
ignore = {
29+
"6.", -- ignore whitespace warnings
30+
}
31+
32+
33+
files["spec/**/*.lua"] = {
34+
std = "ngx_lua+busted",
35+
}

.pongo/pongorc

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
--postgres
2+
--cassandra

kong/plugins/access-token-introspection/access.lua

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ end
1414
function _M.introspect_access_token_req(access_token)
1515
local httpc = http:new()
1616

17-
local res, err = httpc:request_uri(_M.conf.introspection_endpoint, {
17+
local res, _ = httpc:request_uri(_M.conf.introspection_endpoint, {
1818
method = "POST",
1919
ssl_verify = false,
2020
body = "token=" .. access_token .. "&client_id=" .. _M.conf.client_id .. "&client_secret=" .. _M.conf.client_secret,

kong/plugins/access-token-introspection/schema.lua

+5-5
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@ local typedefs = require "kong.db.schema.typedefs"
33

44
local function validate_url(value)
55
local parsed_url = url.parse(value)
6-
if parsed_url.scheme and parsed_url.host then
7-
parsed_url.scheme = parsed_url.scheme:lower()
8-
if not (parsed_url.scheme == "http" or parsed_url.scheme == "https") then
9-
return false, "Supported protocols are HTTP and HTTPS"
10-
end
6+
if parsed_url.scheme == nil or parsed_url.host == nil then
7+
return nil, "Invalid URL"
118
end
129

10+
if not (parsed_url.scheme:lower() == "http" or parsed_url.scheme:lower() ==
11+
"https") then return nil, "Supported protocols are HTTP and HTTPS" end
12+
1313
return true
1414
end
1515

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
local PLUGIN_NAME = "access-token-introspection"
2+
3+
-- helper function to validate data against a schema
4+
local validate
5+
do
6+
local validate_entity =
7+
require("spec.helpers").validate_plugin_config_schema
8+
local plugin_schema = require("kong.plugins." .. PLUGIN_NAME .. ".schema")
9+
10+
function validate(data) return validate_entity(data, plugin_schema) end
11+
end
12+
13+
describe(PLUGIN_NAME .. ": (schema)", function()
14+
15+
it("requires distinct client_id, client_secret, and introspection_endpoint",
16+
function()
17+
local ok, err = validate({
18+
client_id = "CLIENT_ID",
19+
client_secret = "CLIENT_SECRET",
20+
introspection_endpoint = "http://localhost:8080"
21+
})
22+
assert.is_nil(err)
23+
assert.is_truthy(ok)
24+
end)
25+
26+
it("validates introspection_endpoint is a valid URL", function()
27+
local ok, err = validate({
28+
client_id = "CLIENT_ID",
29+
client_secret = "CLIENT_SECRET",
30+
introspection_endpoint = "xyz"
31+
})
32+
assert.falsy(ok)
33+
assert.same("Invalid URL", err.config.introspection_endpoint)
34+
end)
35+
36+
it("validates token_cache_time is an integer", function()
37+
local ok, err = validate({
38+
client_id = "CLIENT_ID",
39+
client_secret = "CLIENT_SECRET",
40+
introspection_endpoint = "http://localhost:8080",
41+
token_cache_time = "60"
42+
})
43+
assert.falsy(ok)
44+
assert.same("expected a number", err.config.token_cache_time)
45+
end)
46+
47+
it("validates introspection_map supports body, headers, and static",
48+
function()
49+
local ok, err = validate({
50+
client_id = "CLIENT_ID",
51+
client_secret = "CLIENT_SECRET",
52+
introspection_endpoint = "http://localhost:8080",
53+
introspection_map = {
54+
body = {Header1 = "index-1"},
55+
headers = {Header2 = "X-User"},
56+
static = {Header3 = "zyx"}
57+
}
58+
})
59+
assert.is_nil(err)
60+
assert.is_truthy(ok)
61+
end)
62+
63+
it("validates introspection_map only supports body, headers, and static",
64+
function()
65+
local ok, err = validate({
66+
client_id = "CLIENT_ID",
67+
client_secret = "CLIENT_SECRET",
68+
introspection_endpoint = "http://localhost:8080",
69+
introspection_map = {xyz = "zyx"}
70+
})
71+
assert.falsy(ok)
72+
assert.same("unknown field", err.config.introspection_map.xyz)
73+
end)
74+
75+
end)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
local PLUGIN_NAME = "access-token-introspection"
2+
local helpers = require "spec.helpers"
3+
4+
for _, strategy in helpers.each_strategy() do
5+
describe(PLUGIN_NAME .. ": (access) [#" .. strategy .. "]", function()
6+
local client
7+
8+
lazy_setup(function()
9+
10+
local bp = helpers.get_db_utils(strategy, nil, {PLUGIN_NAME})
11+
12+
-- Inject a test route. No need to create a service, there is a default
13+
-- service which will echo the request.
14+
local route1 = bp.routes:insert({hosts = {"test1.com"}})
15+
local route2 = bp.routes:insert({hosts = {"test2.com"}})
16+
-- add the plugin to test to the route we created
17+
bp.plugins:insert{
18+
name = PLUGIN_NAME,
19+
route = {id = route1.id},
20+
config = {
21+
client_id = "CLIENT_ID",
22+
client_secret = "CLIENT_SECRET",
23+
introspection_endpoint = "http://auth.com",
24+
}
25+
}
26+
27+
bp.plugins:insert{
28+
name = PLUGIN_NAME,
29+
route = {id = route2.id},
30+
config = {
31+
client_id = "CLIENT_ID",
32+
client_secret = "CLIENT_SECRET",
33+
introspection_endpoint = "http://auth.com",
34+
require_success = false
35+
}
36+
}
37+
38+
-- start kong
39+
assert(helpers.start_kong({
40+
-- set the strategy
41+
database = strategy,
42+
-- use the custom test template to create a local mock server
43+
nginx_conf = "spec/fixtures/custom_nginx.template",
44+
-- make sure our plugin gets loaded
45+
plugins = "bundled," .. PLUGIN_NAME
46+
}))
47+
end)
48+
49+
lazy_teardown(function() helpers.stop_kong(nil, true) end)
50+
51+
before_each(function() client = helpers.proxy_client() end)
52+
53+
after_each(function() if client then client:close() end end)
54+
55+
describe("with default config", function()
56+
it("responds with unauthorized", function()
57+
local r = client:get("/request",
58+
{headers = {host = "test1.com"}})
59+
-- validate that the request fails, response status 401
60+
assert.response(r).has.status(401)
61+
end)
62+
end)
63+
64+
describe("with require_success = false", function()
65+
it("responds with ok", function()
66+
local r = client:get("/request",
67+
{headers = {host = "test2.com"}})
68+
-- validate that the request succeeded, response status 200
69+
assert.response(r).has.status(200)
70+
end)
71+
end)
72+
73+
end)
74+
end

0 commit comments

Comments
 (0)