janiussyafiq commented on code in PR #13038: URL: https://github.com/apache/apisix/pull/13038#discussion_r4022286051
########## t/plugin/openid-connect-consumer-selector.t: ########## @@ -0,0 +1,574 @@ +# +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. +# The ASF licenses this file to You under the Apache License, Version 2.0 +# (the "License"); you may not use this file except in compliance with +# the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +use t::APISIX 'no_plan'; + +log_level('debug'); +repeat_each(1); +no_long_string(); +no_root_location(); +no_shuffle(); + +add_block_preprocessor(sub { + my ($block) = @_; + + if ((!defined $block->error_log) && (!defined $block->no_error_log)) { + $block->set_value("no_error_log", "[error]"); + } + + if (!defined $block->request) { + $block->set_value("request", "GET /t"); + } +}); +run_tests(); + +__DATA__ + +=== TEST 1: Sanity check with minimal valid configuration. +--- config + location /t { + content_by_lua_block { + local plugin = require("apisix.plugins.openid-connect-consumer-selector") + local ok, err = plugin.check_schema({ + match_var = "http_x_tenant_id", + configs = { + {key = "acme", discovery = "a", client_id = "b", client_secret = "c"}, + } + }) + if not ok then + ngx.say(err) + end + + ngx.say("done") + } + } +--- response_body +done + + + +=== TEST 2: Missing `match_var`. +--- config + location /t { + content_by_lua_block { + local plugin = require("apisix.plugins.openid-connect-consumer-selector") + local ok, err = plugin.check_schema({ + configs = { + {key = "acme", discovery = "a", client_id = "b", client_secret = "c"}, + } + }) + if not ok then + ngx.say(err) + end + + ngx.say("done") + } + } +--- response_body +then clause did not match +done + + + +=== TEST 3: Missing `configs`. +--- config + location /t { + content_by_lua_block { + local plugin = require("apisix.plugins.openid-connect-consumer-selector") + local ok, err = plugin.check_schema({ + match_var = "http_x_tenant_id", + }) + if not ok then + ngx.say(err) + end + + ngx.say("done") + } + } +--- response_body +property "configs" is required +done + + + +=== TEST 4: A `configs` entry missing a required field is rejected. +--- config + location /t { + content_by_lua_block { + local plugin = require("apisix.plugins.openid-connect-consumer-selector") + local ok, err = plugin.check_schema({ + match_var = "http_x_tenant_id", + configs = { + {key = "acme", discovery = "a", client_id = "b"}, + } + }) + if not ok then + ngx.say(err) + end + + ngx.say("done") + } + } +--- response_body_like +property "configs" validation failed:.*"client_secret" is required +done + + + +=== TEST 5: rewrite() sets the oidc_* ctx vars when the match_var value hits a config. +--- config + location /t { + content_by_lua_block { + local plugin = require("apisix.plugins.openid-connect-consumer-selector") + local conf = { + match_var = "http_x_tenant_id", + configs = { + {key = "acme", discovery = "https://acme.example.com/.well-known/openid-configuration", + client_id = "acme-client", client_secret = "acme-secret"}, + {key = "globex", discovery = "https://globex.example.com/.well-known/openid-configuration", + client_id = "globex-client", client_secret = "globex-secret"}, + } + } + local ctx = {var = {http_x_tenant_id = "globex"}} + + plugin.rewrite(conf, ctx) + + ngx.say(ctx.var.oidc_discovery) + ngx.say(ctx.var.oidc_client_id) + ngx.say(ctx.var.oidc_client_secret) + } + } +--- response_body +https://globex.example.com/.well-known/openid-configuration +globex-client +globex-secret + + + +=== TEST 6: rewrite() sets nothing when the match_var value hits no config. +--- config + location /t { + content_by_lua_block { + local plugin = require("apisix.plugins.openid-connect-consumer-selector") + local conf = { + match_var = "http_x_tenant_id", + configs = { + {key = "acme", discovery = "https://acme.example.com/.well-known/openid-configuration", + client_id = "acme-client", client_secret = "acme-secret"}, + } + } + local ctx = {var = {http_x_tenant_id = "unknown-tenant"}} + + plugin.rewrite(conf, ctx) + + ngx.say(ctx.var.oidc_discovery == nil and "nil" or ctx.var.oidc_discovery) + ngx.say(ctx.var.oidc_client_id == nil and "nil" or ctx.var.oidc_client_id) + ngx.say(ctx.var.oidc_client_secret == nil and "nil" or ctx.var.oidc_client_secret) + } + } +--- response_body +nil +nil +nil + + + +=== TEST 7: rewrite() sets nothing when match_var itself is absent from the request. +--- config + location /t { + content_by_lua_block { + local plugin = require("apisix.plugins.openid-connect-consumer-selector") + local conf = { + match_var = "http_x_tenant_id", + configs = { + {key = "acme", discovery = "https://acme.example.com/.well-known/openid-configuration", + client_id = "acme-client", client_secret = "acme-secret"}, + } + } + local ctx = {var = {}} + + plugin.rewrite(conf, ctx) + + ngx.say(ctx.var.oidc_discovery == nil and "nil" or ctx.var.oidc_discovery) + } + } +--- response_body +nil + + + +=== TEST 8b: rewrite() does not crash when reading match_var raises (e.g. an unrecognized nginx var name). Review Comment: ```suggestion === TEST 8: rewrite() does not crash when reading match_var raises (e.g. an unrecognized nginx var name). ``` Small changes -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
