Skip to content

Commit 6920523

Browse files
nb-99chrisingenhaag
authored andcommitted
fix: adjust tests to match variable usage in plugin
1 parent adc4a0c commit 6920523

File tree

2 files changed

+45
-77
lines changed

2 files changed

+45
-77
lines changed

spec/01-unit/gateway/securitylog_spec.lua

Lines changed: 26 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -5,113 +5,75 @@
55
local helpers = require "spec.helpers"
66

77
describe("Plugin: jwt-keycloak (security logging)", function()
8-
98
before_each(function()
109
helpers.setup_kong_mock()
1110
helpers.setup_socket_mocks()
1211
end)
13-
12+
1413
after_each(function()
1514
helpers.teardown_kong_mock()
1615
helpers.teardown_socket_mocks()
1716
end)
1817

1918
describe("security_event", function()
20-
it("should log security events and set ngx vars", function()
21-
local securitylog = require("kong.plugins.jwt-keycloak.gateway.securitylog")
22-
23-
-- Mock ngx.var to capture the set values
24-
local captured_vars = {}
25-
ngx.var = setmetatable({}, {
26-
__newindex = function(t, k, v)
27-
captured_vars[k] = v
28-
end
29-
})
30-
19+
it("should log security events and set kong.ctx.shared vars", function()
20+
local securitylog = require("gateway.securitylog")
21+
3122
-- Test security_event function
3223
securitylog.security_event('ua200', 'test event details')
33-
34-
-- Verify that the ngx vars were set correctly
35-
assert.equals('ua200', captured_vars.sec_event_code)
36-
assert.equals('test event details', captured_vars.sec_event_details)
24+
25+
-- Verify that the ctx.shared vars were set correctly
26+
assert.equals('ua200', kong.ctx.shared.sec_event_code)
27+
assert.equals('test event details', kong.ctx.shared.sec_event_details)
3728
end)
3829
end)
3930

4031
describe("collect_gateway_data", function()
4132
it("should set gateway_consumer to anonymous when jwt is nil", function()
42-
local securitylog = require("kong.plugins.jwt-keycloak.gateway.securitylog")
43-
44-
-- Mock ngx.var to capture the set values
45-
local captured_vars = {}
46-
ngx.var = setmetatable({}, {
47-
__newindex = function(t, k, v)
48-
captured_vars[k] = v
49-
end
50-
})
51-
33+
local securitylog = require("gateway.securitylog")
34+
5235
-- Test with nil jwt
5336
securitylog.collect_gateway_data(nil)
54-
55-
assert.equals("anonymous", captured_vars.gateway_consumer)
37+
38+
assert.equals("anonymous", kong.ctx.shared.gateway_consumer)
5639
end)
5740

5841
it("should set gateway_consumer to anonymous when jwt.claims is nil", function()
59-
local securitylog = require("kong.plugins.jwt-keycloak.gateway.securitylog")
60-
61-
local captured_vars = {}
62-
ngx.var = setmetatable({}, {
63-
__newindex = function(t, k, v)
64-
captured_vars[k] = v
65-
end
66-
})
67-
42+
local securitylog = require("gateway.securitylog")
43+
6844
-- Test with jwt but no claims
6945
local jwt = {}
7046
securitylog.collect_gateway_data(jwt)
71-
72-
assert.equals("anonymous", captured_vars.gateway_consumer)
47+
48+
assert.equals("anonymous", kong.ctx.shared.gateway_consumer)
7349
end)
7450

7551
it("should set gateway_consumer to clientId when available", function()
76-
local securitylog = require("kong.plugins.jwt-keycloak.gateway.securitylog")
77-
78-
local captured_vars = {}
79-
ngx.var = setmetatable({}, {
80-
__newindex = function(t, k, v)
81-
captured_vars[k] = v
82-
end
83-
})
84-
52+
local securitylog = require("gateway.securitylog")
53+
8554
-- Test with jwt containing clientId
8655
local jwt = {
8756
claims = {
8857
clientId = "test-client-123"
8958
}
9059
}
9160
securitylog.collect_gateway_data(jwt)
92-
93-
assert.equals("test-client-123", captured_vars.gateway_consumer)
61+
62+
assert.equals("test-client-123", kong.ctx.shared.gateway_consumer)
9463
end)
9564

9665
it("should set gateway_consumer to anonymous when clientId is not a string", function()
97-
local securitylog = require("kong.plugins.jwt-keycloak.gateway.securitylog")
98-
99-
local captured_vars = {}
100-
ngx.var = setmetatable({}, {
101-
__newindex = function(t, k, v)
102-
captured_vars[k] = v
103-
end
104-
})
105-
66+
local securitylog = require("gateway.securitylog")
67+
10668
-- Test with jwt containing non-string clientId
10769
local jwt = {
10870
claims = {
109-
clientId = 123 -- number instead of string
71+
clientId = 123 -- number instead of string
11072
}
11173
}
11274
securitylog.collect_gateway_data(jwt)
113-
114-
assert.equals("anonymous", captured_vars.gateway_consumer)
75+
76+
assert.equals("anonymous", kong.ctx.shared.gateway_consumer)
11577
end)
11678
end)
117-
end)
79+
end)

spec/helpers.lua

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ local mock_kong = {
1313
err = function(...) end,
1414
info = function(...) end,
1515
warn = function(...) end
16+
},
17+
-- ctx.shared is used by gateway/securitylog.lua
18+
ctx = {
19+
shared = {}
1620
}
1721
}
1822

@@ -22,51 +26,53 @@ local mock_ngx = {
2226
-- Simple base64 encoding for tests (this is a minimal implementation)
2327
local chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'
2428
local result = ''
25-
local bytes = {s:byte(1, -1)}
26-
29+
local bytes = { s:byte(1, -1) }
30+
2731
for i = 1, #bytes, 3 do
28-
local b1, b2, b3 = bytes[i], bytes[i+1], bytes[i+2]
32+
local b1, b2, b3 = bytes[i], bytes[i + 1], bytes[i + 2]
2933
local n = (b1 or 0) * 65536 + (b2 or 0) * 256 + (b3 or 0)
3034
result = result .. chars:sub(math.floor(n / 262144) + 1, math.floor(n / 262144) + 1)
3135
result = result .. chars:sub((math.floor(n / 4096) % 64) + 1, (math.floor(n / 4096) % 64) + 1)
3236
result = result .. (b2 and chars:sub((math.floor(n / 64) % 64) + 1, (math.floor(n / 64) % 64) + 1) or '=')
3337
result = result .. (b3 and chars:sub((n % 64) + 1, (n % 64) + 1) or '=')
3438
end
35-
39+
3640
return result
3741
end,
38-
42+
3943
decode_base64 = function(s)
4044
-- Simple base64 decoding for tests
4145
local chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'
4246
local lookup = {}
4347
for i = 1, #chars do
4448
lookup[chars:byte(i)] = i - 1
4549
end
46-
50+
4751
s = s:gsub('=+$', '')
4852
local result = ''
49-
53+
5054
for i = 1, #s, 4 do
51-
local a, b, c, d = s:byte(i, i+3)
55+
local a, b, c, d = s:byte(i, i + 3)
5256
local n = (lookup[a] or 0) * 262144 + (lookup[b] or 0) * 4096 + (lookup[c] or 0) * 64 + (lookup[d] or 0)
5357
result = result .. string.char(math.floor(n / 65536) % 256)
5458
if c then result = result .. string.char(math.floor(n / 256) % 256) end
5559
if d then result = result .. string.char(n % 256) end
5660
end
57-
61+
5862
return result
5963
end,
60-
64+
6165
-- Mock ngx.var for security logging
6266
var = {},
63-
67+
6468
-- Mock ngx.DEBUG constant
6569
DEBUG = 7
6670
}
6771

6872
-- Mock functions for testing
6973
function helpers.setup_kong_mock()
74+
-- reset ctx.shared for each test to ensure isolation
75+
mock_kong.ctx = { shared = {} }
7076
_G.kong = mock_kong
7177
_G.ngx = mock_ngx
7278
end
@@ -81,7 +87,7 @@ local mock_cjson_safe = {
8187
decode = function(data)
8288
-- Simple JSON decoder for tests
8389
if data == '{"test": "data"}' then
84-
return {test = "data"}
90+
return { test = "data" }
8591
end
8692
return nil, "parse error"
8793
end
@@ -148,4 +154,4 @@ function helpers.teardown_socket_mocks()
148154
package.loaded["ngx.errlog"] = nil
149155
end
150156

151-
return helpers
157+
return helpers

0 commit comments

Comments
 (0)