Skip to content

Commit

Permalink
reset
Browse files Browse the repository at this point in the history
  • Loading branch information
nawaz1991 committed Nov 18, 2023
1 parent 3fc5e6c commit e63d2a6
Show file tree
Hide file tree
Showing 6 changed files with 309 additions and 13 deletions.
2 changes: 1 addition & 1 deletion .busted
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
return {
default = {
verbose = true,
coverage = false,
coverage = true,
output = "gtest",
},
}
11 changes: 3 additions & 8 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ jobs:
fail-fast: false
matrix:
kongVersion:
- "2.3.x"
- "2.8.x"
- "3.0.x"
- "dev"
Expand Down Expand Up @@ -49,19 +50,13 @@ jobs:
# rewrite those to the local location in GHA
if [ -f .luacov ]; then
cp .luacov .luacov_backup
cat .luacov_backup | sed 's/\/kong-plugin-oasvalidator\/luacov./luacov./' > .luacov
else
cat .luacov_backup | sed 's/\/kong-plugin\/luacov./luacov./' > .luacov
fi
echo "No .luacov file found"
fi
echo "Uploading luacov to coveralls.io"
if [ -d /home/runner/work/kong-plugin/kong-plugin-oasvalidator]; then
echo "File present"
else
echo "File not present"
fi
luacov-coveralls
#luacov-coveralls --output coveralls.out
# undo the hack
Expand Down
8 changes: 4 additions & 4 deletions .luacov
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
include = {
"%/kong%-plugin%-oasvalidator%/kong%/.+$",
"%/kong%-plugin%/kong%/.+$",
}

statsfile = "/kong-plugin-oasvalidator/luacov.stats.out"
reportfile = "/kong-plugin-oasvalidator/luacov.report.out"
runreport = true
statsfile = "/kong-plugin/luacov.stats.out"
reportfile = "/kong-plugin/luacov.report.out"
runreport = true
106 changes: 106 additions & 0 deletions spec/oasvalidator/02-path_param_validator_spec.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
-- Copyright (c) 2023 Muhammad Nawaz
-- Licensed under the MIT License. See LICENSE file for more information.
-- [ END OF LICENSE e2f4afc94de80d2c104519cd5b5e65ca0f4d5b62 ]

local helpers = require "spec.helpers"
local cjson = require "cjson"


local PLUGIN_NAME = "oasvalidator"


for _, strategy in helpers.all_strategies() do if strategy ~= "cassandra" then
describe(PLUGIN_NAME .. ": (access) [#" .. strategy .. "]", function()
local client

lazy_setup(function()

local bp = helpers.get_db_utils(strategy == "off" and "postgres" or strategy, nil, { PLUGIN_NAME })

-- Inject a test route. No need to create a service, there is a default
-- service which will echo the request.
local route1 = bp.routes:insert({
hosts = { "test1.com" },
})
-- add the plugin to test to the route we created
bp.plugins:insert {
name = PLUGIN_NAME,
route = { id = route1.id },
config = { oas_spec_path = "/data/openAPI_example.json",
validate_request = false,
validate_path_params = true,},
}

-- start kong
assert(helpers.start_kong({
-- set the strategy
database = strategy,
-- use the custom test template to create a local mock server
nginx_conf = "spec/fixtures/custom_nginx.template",
-- make sure our plugin gets loaded
plugins = "bundled," .. PLUGIN_NAME,
-- write & load declarative config, only if 'strategy=off'
declarative_config = strategy == "off" and helpers.make_yaml_file() or nil,
}))
end)

lazy_teardown(function()
helpers.stop_kong(nil, true)
end)

before_each(function()
client = helpers.proxy_client()
end)

after_each(function()
if client then client:close() end
end)

describe("Invalid:", function()
it("Path param", function()
local r = client:get("/test/integer_simple_true/not_an_integer", {
headers = {
host = "test1.com"
}
})
-- validate that the request is unsuccessful, response status 400
assert.response(r).has.status(400)
local body = assert.res_status(400, r)
local json = cjson.decode(body)
assert.equal("INVALID_PATH_PARAM", json["errorCode"])
end)
end)

describe("Invalid:", function()
it("Path param", function()
local r = client:get("/test/integer_simple_false/not_an_integer", {
headers = {
host = "test1.com"
}
})
-- validate that the request is unsuccessful, response status 400
assert.response(r).has.status(400)
local body = assert.res_status(400, r)
local json = cjson.decode(body)
assert.equal("INVALID_PATH_PARAM", json["errorCode"])
end)
end)

describe("Invalid:", function()
it("Path param", function()
local r = client:get("/test/integer_label_true/123", {
headers = {
host = "test1.com"
}
})
-- validate that the request is unsuccessful, response status 400
assert.response(r).has.status(400)
local body = assert.res_status(400, r)
local json = cjson.decode(body)
assert.equal("INVALID_PATH_PARAM", json["errorCode"])
end)
end)

end)

end end
91 changes: 91 additions & 0 deletions spec/oasvalidator/02-query_param_validator_spec.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
-- Copyright (c) 2023 Muhammad Nawaz
-- Licensed under the MIT License. See LICENSE file for more information.
-- [ END OF LICENSE e2f4afc94de80d2c104519cd5b5e65ca0f4d5b62 ]

local helpers = require "spec.helpers"
local cjson = require "cjson"


local PLUGIN_NAME = "oasvalidator"


for _, strategy in helpers.all_strategies() do if strategy ~= "cassandra" then
describe(PLUGIN_NAME .. ": (access) [#" .. strategy .. "]", function()
local client

lazy_setup(function()

local bp = helpers.get_db_utils(strategy == "off" and "postgres" or strategy, nil, { PLUGIN_NAME })

-- Inject a test route. No need to create a service, there is a default
-- service which will echo the request.
local route1 = bp.routes:insert({
hosts = { "test1.com" },
})
-- add the plugin to test to the route we created
bp.plugins:insert {
name = PLUGIN_NAME,
route = { id = route1.id },
config = { oas_spec_path = "/data/openAPI_example.json",
validate_request = false,
validate_query_params = true,},
}

-- start kong
assert(helpers.start_kong({
-- set the strategy
database = strategy,
-- use the custom test template to create a local mock server
nginx_conf = "spec/fixtures/custom_nginx.template",
-- make sure our plugin gets loaded
plugins = "bundled," .. PLUGIN_NAME,
-- write & load declarative config, only if 'strategy=off'
declarative_config = strategy == "off" and helpers.make_yaml_file() or nil,
}))
end)

lazy_teardown(function()
helpers.stop_kong(nil, true)
end)

before_each(function()
client = helpers.proxy_client()
end)

after_each(function()
if client then client:close() end
end)

describe("Invalid:", function()
it("Query param", function()
local r = client:get("/test/query_array_integer_form_true?124.4565", {
headers = {
host = "test1.com"
}
})
-- validate that the request is unsuccessful, response status 400
assert.response(r).has.status(400)
local body = assert.res_status(400, r)
local json = cjson.decode(body)
assert.equal("INVALID_QUERY_PARAM", json["errorCode"])
end)
end)

describe("Invalid:", function()
it("Query param", function()
local r = client:get("/test/query_array_integer_form_true?not_a_number", {
headers = {
host = "test1.com"
}
})
-- validate that the request is unsuccessful, response status 400
assert.response(r).has.status(400)
local body = assert.res_status(400, r)
local json = cjson.decode(body)
assert.equal("INVALID_QUERY_PARAM", json["errorCode"])
end)
end)

end)

end end
104 changes: 104 additions & 0 deletions spec/oasvalidator/02-request_validator_spec.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
-- Copyright (c) 2023 Muhammad Nawaz
-- Licensed under the MIT License. See LICENSE file for more information.
-- [ END OF LICENSE e2f4afc94de80d2c104519cd5b5e65ca0f4d5b62 ]

local helpers = require "spec.helpers"
local cjson = require "cjson"


local PLUGIN_NAME = "oasvalidator"


for _, strategy in helpers.all_strategies() do if strategy ~= "cassandra" then
describe(PLUGIN_NAME .. ": (access) [#" .. strategy .. "]", function()
local client

lazy_setup(function()

local bp = helpers.get_db_utils(strategy == "off" and "postgres" or strategy, nil, { PLUGIN_NAME })

-- Inject a test route. No need to create a service, there is a default
-- service which will echo the request.
local route1 = bp.routes:insert({
hosts = { "test1.com" },
})
-- add the plugin to test to the route we created
bp.plugins:insert {
name = PLUGIN_NAME,
route = { id = route1.id },
config = { oas_spec_path = "/data/openAPI_example.json",
validate_request = true },
}

-- start kong
assert(helpers.start_kong({
-- set the strategy
database = strategy,
-- use the custom test template to create a local mock server
nginx_conf = "spec/fixtures/custom_nginx.template",
-- make sure our plugin gets loaded
plugins = "bundled," .. PLUGIN_NAME,
-- write & load declarative config, only if 'strategy=off'
declarative_config = strategy == "off" and helpers.make_yaml_file() or nil,
}))
end)

lazy_teardown(function()
helpers.stop_kong(nil, true)
end)

before_each(function()
client = helpers.proxy_client()
end)

after_each(function()
if client then client:close() end
end)

-- Valid request
describe("valid request", function()
it("A test valid request", function()
local r = client:get("/test/query_two_integer_form_mixed?param1=123&param2=6", {
headers = {
host = "test1.com"
}
})
-- validate that the request succeeded, response status 404 as no valid upstream server present
assert.response(r).has.status(404)
end)
end)

describe("invalid path", function()
it("An invalid test request", function()
local r = client:get("/invalid/path", {
headers = {
host = "test1.com"
}
})
-- validate that the request is unsuccessful, response status 400
assert.response(r).has.status(400)
local body = assert.res_status(400, r)
local json = cjson.decode(body)
assert.equal("INVALID_ROUTE", json["errorCode"])
end)
end)

-- Invalid query param
describe("invalid query param", function()
it("An invalid test request", function()
local r = client:get("/test/query_two_integer_form_mixed?param1=123&param2=not_a_num", {
headers = {
host = "test1.com"
}
})
-- validate that the request is unsuccessful, response status 400
assert.response(r).has.status(400)
local body = assert.res_status(400, r)
local json = cjson.decode(body)
assert.equal("INVALID_QUERY_PARAM", json["errorCode"])
end)
end)

end)

end end

0 comments on commit e63d2a6

Please sign in to comment.