-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding UnitTests coverage for filter/TokenVerificationFilter.lua
Added GHA WF for running unit tests as part of CI pipeline Improvements to Readme with instructions on how to run tests Signed-off-by: Alfredo Gutierrez <[email protected]>
- Loading branch information
1 parent
88a1836
commit dded2c5
Showing
13 changed files
with
703 additions
and
13 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
name: Tests | ||
|
||
on: | ||
pull_request: | ||
branches: [ main, release/**] | ||
push: | ||
branches: [ main, release/*] | ||
tags: [ v* ] | ||
|
||
env: | ||
ACTIONS_RUNTIME_TOKEN: fake-token | ||
|
||
|
||
jobs: | ||
proxy-tests: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- name: Checkout repository | ||
uses: actions/checkout@v3 | ||
|
||
- name: Install Lua | ||
uses: leafo/gh-actions-lua@v8 | ||
with: | ||
luaVersion: '5.3' | ||
|
||
- name: Install LuaRocks | ||
uses: leafo/gh-actions-luarocks@v4 | ||
|
||
- name: Install lunatest | ||
run: luarocks install lunatest | ||
|
||
- name: Install luacov | ||
run: luarocks install luacov | ||
|
||
- name: Install luacov-console | ||
run: luarocks install luacov-console | ||
|
||
- name: Install cjson | ||
run: luarocks install lua-cjson | ||
|
||
- name: Install luasocket | ||
run: luarocks install luasocket | ||
|
||
- name: Print Current Directory | ||
run: pwd && ls -la | ||
working-directory: auth-layer-proxy/tests | ||
|
||
- name: Run tests | ||
run: lua test.lua | ||
working-directory: auth-layer-proxy/tests | ||
|
||
- name: Generate coverage report | ||
run: luacov | ||
working-directory: auth-layer-proxy/tests | ||
|
||
- name: Generate Console Report | ||
run: luacov-console ../filters/ && luacov-console ../filters/ -s && luacov-console ../filters/ -s > coverage.txt | ||
working-directory: auth-layer-proxy/tests |
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,3 @@ | ||
modules = { | ||
["TokenVerificationFilter"] = "../filters/TokenVerificationFilter.lua", | ||
} |
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 @@ | ||
pcall(require, "luacov") --measure code coverage, if luacov is present | ||
local lunatest = require "lunatest" | ||
|
||
|
||
print '==============================' | ||
print('Tests of auth-layer-proxy') | ||
print("Tests are being executed on current directory: " .. os.getenv("PWD")) | ||
print '==============================' | ||
|
||
lunatest.suite("test_checkTokenPermissions") | ||
lunatest.suite("test_extractToken") | ||
lunatest.suite("test_parseJsonBody") | ||
lunatest.suite("test_verifyValidMethod") | ||
lunatest.suite("test_envoy_on_request") | ||
|
||
|
||
lunatest.run() |
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,76 @@ | ||
local cjson = require("cjson") | ||
|
||
-- Mock for HTTP request | ||
local function mockHttpRequest(params) | ||
|
||
local requestBody = params.source() | ||
local token, clientId, clientSecret = requestBody:match("token=(.*)&client_id=(.*)&client_secret=(.*)") | ||
|
||
-- You can adjust the response based on the input parameters for different test cases | ||
if token == "validToken" then | ||
statusCode = 200 | ||
response = cjson.encode({ | ||
active = true, | ||
subgraph_access = "subgraph1,subgraph2", | ||
email = "[email protected]", | ||
email_verified = true, | ||
["realm_access"] = { -- Use square brackets and quotes for the key | ||
roles = { -- Use equals sign for assignment | ||
"subgraph_create", | ||
"subgraph_deploy", | ||
"subgraph_resume", | ||
"subgraph_pause" | ||
} | ||
} | ||
}) | ||
params.sink(response) | ||
return 1, statusCode, nil, nil | ||
elseif token == "emailNotVerified" then | ||
statusCode = 200 | ||
response = cjson.encode({ | ||
active = true, | ||
subgraph_access = "subgraph1,subgraph2", | ||
email = "[email protected]", | ||
email_verified = false, | ||
["realm_access"] = { -- Use square brackets and quotes for the key | ||
roles = { -- Use equals sign for assignment | ||
"subgraph_remove", | ||
"subgraph_create", | ||
"subgraph_deploy", | ||
"subgraph_resume", | ||
"subgraph_pause" | ||
} | ||
} | ||
}) | ||
params.sink(response) | ||
return 1, statusCode, nil, nil | ||
|
||
elseif token == "invalidToken" then | ||
statusCode = 200 | ||
response = cjson.encode({ | ||
active = false | ||
}) | ||
params.sink(response) | ||
return 1, statusCode, nil, nil | ||
else | ||
-- Simulate a failed HTTP request | ||
statusCode = 500 | ||
statusText = "Internal Server Error" | ||
return 1, statusCode, statusText, nil | ||
end | ||
end | ||
|
||
local function mockGetEnv(envVarName) | ||
local envVars = { | ||
CLIENT_ID = "htg-auth_layer", | ||
CLIENT_SECRET = "wEGsZafep01LKNPkJhiOMQSmgAGAMWUi", | ||
TOKEN_INTROSPECTION_URL = "http://host.docker.internal:8080/realms/HederaTheGraph/protocol/openid-connect/token/introspect" | ||
} | ||
return envVars[envVarName] | ||
end | ||
|
||
-- export module | ||
local testMocks = {} | ||
testMocks.mockHttpRequest = mockHttpRequest | ||
testMocks.mockGetEnv = mockGetEnv | ||
return testMocks |
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,22 @@ | ||
testUtils = {} | ||
|
||
function printGreen(text) | ||
print("\27[32m" .. text .. "\27[0m") | ||
end | ||
|
||
-- Function to print text in red | ||
function printRed(text) | ||
print("\27[31m" .. text .. "\27[0m") | ||
end | ||
|
||
function wrapTextInRed(text) | ||
return "\27[31m" .. text .. "\27[0m" | ||
end | ||
|
||
|
||
-- export functions | ||
testUtils.printGreen = printGreen | ||
testUtils.printRed = printRed | ||
testUtils.wrapTextInRed = wrapTextInRed | ||
|
||
return testUtils |
Oops, something went wrong.