Skip to content

Commit b63901e

Browse files
AlinsRanCopilot
andcommitted
fix: use server.lua mock functions for feishu-auth tests
- Add feishu-auth to default plugin list in apisix/cli/config.lua - Add _M.feishu_token and _M.feishu_userinfo to t/lib/server.lua - Rewrite test to use /feishu/token and /feishu/userinfo paths instead of extra_init_by_lua dynamic assignment Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 1a1ba10 commit b63901e

3 files changed

Lines changed: 24 additions & 67 deletions

File tree

apisix/cli/config.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,7 @@ local _M = {
221221
"jwt-auth",
222222
"jwe-decrypt",
223223
"key-auth",
224+
"feishu-auth",
224225
"consumer-restriction",
225226
"attach-consumer-label",
226227
"forward-auth",

t/lib/server.lua

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1242,4 +1242,23 @@ function _M.mock_compressed_upstream_response()
12421242
end
12431243

12441244

1245+
function _M.feishu_token()
1246+
local json = require("cjson")
1247+
ngx.req.read_body()
1248+
local data = ngx.req.get_body_data()
1249+
local payload = json.decode(data)
1250+
if payload.code ~= "passed" then
1251+
ngx.status = 400
1252+
ngx.say([[{"code": 20051, "error_description": "Unauthorized"}]])
1253+
return
1254+
end
1255+
ngx.say([[{"code": 0, "expires_in": 7200, "access_token": "85b8b7665c4c3bc5bd91d8e6cb6594b7", "token_type": "Bearer"}]])
1256+
end
1257+
1258+
1259+
function _M.feishu_userinfo()
1260+
ngx.say([[{"code": 0, "data": {"avatar_big": "https://example.com/big.jpg", "avatar_middle": "https://example.com/mid.jpg", "avatar_thumb": "https://example.com/thumb.jpg", "avatar_url": "https://example.com/url.jpg", "en_name": "jack", "name": "jack", "open_id": "ou_8fc70d9ea27111749a71eb", "tenant_key": "1224d18e8d", "union_id": "on_c249ec29c9d6"}, "msg": "success"}]])
1261+
end
1262+
1263+
12451264
return _M

t/plugin/feishu-auth.t

Lines changed: 4 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -24,69 +24,6 @@ no_root_location();
2424
add_block_preprocessor(sub {
2525
my ($block) = @_;
2626

27-
my $extra_init_by_lua = <<_EOC_;
28-
local server = require("lib.server")
29-
server.token = function()
30-
local json = require("cjson")
31-
local headers = ngx.req.get_headers()
32-
ngx.log(ngx.INFO, ngx.var.request_uri, " receive headers: ", json.encode(headers))
33-
34-
ngx.req.read_body()
35-
local data = ngx.req.get_body_data()
36-
ngx.log(ngx.INFO, ngx.var.request_uri, " payload: ", data)
37-
38-
local payload = json.decode(data)
39-
40-
if payload.code ~= "passed" then
41-
ngx.status = 400
42-
ngx.say([[{"code": 20051, "error_description": "Unauthorized"}]])
43-
return
44-
end
45-
46-
ngx.log(ngx.INFO, ngx.var.request_uri, " payload: ", data)
47-
48-
local resp_payload = [[
49-
{
50-
"code": 0,
51-
"expires_in": 7200,
52-
"access_token": "85b8b7665c4c3bc5bd91d8e6cb6594b7",
53-
"token_type": "Bearer"
54-
}
55-
]]
56-
57-
ngx.say(resp_payload)
58-
end
59-
60-
server.userinfo = function()
61-
local json = require("cjson")
62-
local headers = ngx.req.get_headers()
63-
ngx.log(ngx.INFO, ngx.var.request_uri, " receive headers: ", json.encode(headers))
64-
65-
local resp_payload = [[
66-
{
67-
"code": 0,
68-
"data": {
69-
"avatar_big": "https://s3-imfile.feishucdn.com/static-resource/v1/v2_d8ffef5f-bb1b-4ba0-bf05-1487b4be",
70-
"avatar_middle": "https://s1-imfile.feishucdn.com/static-resource/v1/v2_d8ffef5f-bb1b-4ba0-bf05-1487b4beba4",
71-
"avatar_thumb": "https://s3-imfile.feishucdn.com/static-resource/v1/v2_d8ffef5f-bb1b-4ba0-bf05-1487b4beba",
72-
"avatar_url": "https://s3-imfile.feishucdn.com/static-resource/v1/v2_d8ffef5f-bb1b-4ba0-bf05-1487b4beba4g",
73-
"en_name": "jack",
74-
"name": "jack",
75-
"open_id": "ou_8fc70d9ea27111749a71eb",
76-
"tenant_key": "1224d18e8d",
77-
"union_id": "on_c249ec29c9d6"
78-
},
79-
"msg": "success"
80-
}
81-
]]
82-
83-
ngx.say(resp_payload)
84-
end
85-
_EOC_
86-
87-
$block->set_value("extra_init_by_lua", $extra_init_by_lua);
88-
89-
9027
if (!$block->request) {
9128
if (!$block->stream_request) {
9229
$block->set_value("request", "GET /t");
@@ -123,8 +60,8 @@ __DATA__
12360
"app_secret": "456",
12461
"secret": "my-secret",
12562
"auth_redirect_uri": "https://example.com",
126-
"access_token_url": "http://127.0.0.1:1980/token",
127-
"userinfo_url": "http://127.0.0.1:1980/userinfo",
63+
"access_token_url": "http://127.0.0.1:1980/feishu/token",
64+
"userinfo_url": "http://127.0.0.1:1980/feishu/userinfo",
12865
"cookie_expires_in": 2,
12966
"redirect_uri": "/echo"
13067
}
@@ -317,8 +254,8 @@ passed
317254
"app_secret": "456",
318255
"secret": "my-secret",
319256
"auth_redirect_uri": "https://example.com",
320-
"access_token_url": "http://127.0.0.1:1980/token",
321-
"userinfo_url": "http://127.0.0.1:1980/userinfo",
257+
"access_token_url": "http://127.0.0.1:1980/feishu/token",
258+
"userinfo_url": "http://127.0.0.1:1980/feishu/userinfo",
322259
"code_query": "custom_code",
323260
"code_header": "Custom-feishu-Code",
324261
"redirect_uri": "/echo"

0 commit comments

Comments
 (0)