-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: entities schemas structure and tests
- Loading branch information
1 parent
256e0a5
commit dc8cbdd
Showing
14 changed files
with
131 additions
and
129 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
local url = require "socket.url" | ||
local constants = require "kong.constants" | ||
|
||
local function validate_target_url(value) | ||
local parsed_url = url.parse(value) | ||
if parsed_url.scheme and parsed_url.host then | ||
parsed_url.scheme = parsed_url.scheme:lower() | ||
if parsed_url.scheme == "http" or parsed_url.scheme == "https" then | ||
parsed_url.path = parsed_url.path or "/" | ||
return true, nil, { target_url = url.build(parsed_url)} | ||
else | ||
return false, "Supported protocols are HTTP and HTTPS" | ||
end | ||
end | ||
|
||
return false, "Invalid target URL" | ||
end | ||
|
||
return { | ||
id = { type = constants.DATABASE_TYPES.ID }, | ||
name = { type = "string", unique = true, queryable = true, default = function(api_t) return api_t.public_dns end }, | ||
public_dns = { type = "string", required = true, unique = true, queryable = true, | ||
regex = "([a-zA-Z0-9-]+(\\.[a-zA-Z0-9-]+)*)" }, | ||
target_url = { type = "string", required = true, func = validate_target_url }, | ||
created_at = { type = constants.DATABASE_TYPES.TIMESTAMP } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
local stringy = require "stringy" | ||
local constants = require "kong.constants" | ||
|
||
local function check_custom_id_and_username(value, consumer_t) | ||
if (consumer_t.custom_id == nil or stringy.strip(consumer_t.custom_id) == "") | ||
and (consumer_t.username == nil or stringy.strip(consumer_t.username) == "") then | ||
return false, "At least a 'custom_id' or a 'username' must be specified" | ||
end | ||
return true | ||
end | ||
|
||
return { | ||
id = { type = constants.DATABASE_TYPES.ID }, | ||
custom_id = { type = "string", unique = true, queryable = true, func = check_custom_id_and_username }, | ||
username = { type = "string", unique = true, queryable = true, func = check_custom_id_and_username }, | ||
created_at = { type = constants.DATABASE_TYPES.TIMESTAMP } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
local utils = require "kong.tools.utils" | ||
local constants = require "kong.constants" | ||
|
||
local function load_value_schema(plugin_t) | ||
if plugin_t.name then | ||
local loaded, plugin_schema = utils.load_module_if_exists("kong.plugins."..plugin_t.name..".schema") | ||
if loaded then | ||
return plugin_schema | ||
else | ||
return nil, "Plugin \""..(plugin_t.name and plugin_t.name or "").."\" not found" | ||
end | ||
end | ||
end | ||
|
||
return { | ||
id = { type = constants.DATABASE_TYPES.ID }, | ||
api_id = { type = constants.DATABASE_TYPES.ID, required = true, foreign = true, queryable = true }, | ||
consumer_id = { type = constants.DATABASE_TYPES.ID, foreign = true, queryable = true, default = constants.DATABASE_NULL_ID }, | ||
name = { type = "string", required = true, queryable = true, immutable = true }, | ||
value = { type = "table", schema = load_value_schema }, | ||
enabled = { type = "boolean", default = true }, | ||
created_at = { type = constants.DATABASE_TYPES.TIMESTAMP } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
local validate = require("kong.dao.schemas_validation").validate | ||
local api_schema = require "kong.dao.schemas.apis" | ||
|
||
require "kong.tools.ngx_stub" | ||
|
||
describe("Entities Schemas", function() | ||
describe("APIs", function() | ||
|
||
it("should return error with wrong target_url", function() | ||
local valid, errors = validate({ | ||
public_dns = "hello.com", | ||
target_url = "asdasd" | ||
}, api_schema) | ||
assert.False(valid) | ||
assert.same("Invalid target URL", errors.target_url) | ||
end) | ||
|
||
it("should return error with wrong target_url protocol", function() | ||
local valid, errors = validate({ | ||
public_dns = "hello.com", | ||
target_url = "wot://hello.com/" | ||
}, api_schema) | ||
assert.False(valid) | ||
assert.same("Supported protocols are HTTP and HTTPS", errors.target_url) | ||
end) | ||
|
||
it("should work without a path", function() | ||
local valid, errors = validate({ | ||
public_dns = "hello.com", | ||
target_url = "http://hello.com" | ||
}, api_schema) | ||
assert.True(valid) | ||
assert.falsy(errors) | ||
end) | ||
|
||
it("should work without upper case protocol", function() | ||
local valid, errors = validate({ | ||
public_dns = "hello2.com", | ||
target_url = "HTTP://hello.com/world" | ||
}, api_schema) | ||
assert.True(valid) | ||
assert.falsy(errors) | ||
end) | ||
|
||
end) | ||
end) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters