From fb78e510caea5009a083fcee9b339be188c15fa3 Mon Sep 17 00:00:00 2001 From: Shawn <506895667@qq.com> Date: Fri, 9 Jul 2021 15:04:54 +0800 Subject: [PATCH] fix(test): update test cases for emqx_access_control_SUITE --- apps/emqx/src/emqx_config.erl | 5 ++++ apps/emqx/src/emqx_map_lib.erl | 2 ++ apps/emqx/test/emqx_SUITE.erl | 1 + apps/emqx/test/emqx_access_control_SUITE.erl | 24 ++++++-------------- 4 files changed, 15 insertions(+), 17 deletions(-) diff --git a/apps/emqx/src/emqx_config.erl b/apps/emqx/src/emqx_config.erl index 9d5428fdd..18e0d7020 100644 --- a/apps/emqx/src/emqx_config.erl +++ b/apps/emqx/src/emqx_config.erl @@ -27,6 +27,7 @@ -export([ get_listener_conf/3 , get_listener_conf/4 + , put_listener_conf/4 , find_listener_conf/3 ]). @@ -79,6 +80,10 @@ get_listener_conf(Zone, Listener, KeyPath, Default) -> {ok, Data} -> Data end. +-spec put_listener_conf(atom(), atom(), emqx_map_lib:config_key_path(), term()) -> ok. +put_listener_conf(Zone, Listener, KeyPath, Conf) -> + ?MODULE:put([zones, Zone, listeners, Listener | KeyPath], Conf). + -spec find_listener_conf(atom(), atom(), emqx_map_lib:config_key_path()) -> {ok, term()} | {not_foud, emqx_map_lib:config_key_path(), term()}. find_listener_conf(Zone, Listener, KeyPath) -> diff --git a/apps/emqx/src/emqx_map_lib.erl b/apps/emqx/src/emqx_map_lib.erl index 154a3d24f..c0d922de6 100644 --- a/apps/emqx/src/emqx_map_lib.erl +++ b/apps/emqx/src/emqx_map_lib.erl @@ -58,6 +58,8 @@ deep_find(_KeyPath, Data) -> -spec deep_put(config_key_path(), map(), term()) -> map(). deep_put([], Map, Config) when is_map(Map) -> Config; +deep_put([], _Map, Config) -> %% not map, replace it + Config; deep_put([Key | KeyPath], Map, Config) -> SubMap = deep_put(KeyPath, maps:get(Key, Map, #{}), Config), Map#{Key => SubMap}. diff --git a/apps/emqx/test/emqx_SUITE.erl b/apps/emqx/test/emqx_SUITE.erl index 4213a5aac..dca66eca9 100644 --- a/apps/emqx/test/emqx_SUITE.erl +++ b/apps/emqx/test/emqx_SUITE.erl @@ -27,6 +27,7 @@ all() -> emqx_ct:all(?MODULE). init_per_suite(Config) -> emqx_ct_helpers:start_apps([]), + ct:pal("------------config: ~p", [emqx_config:get()]), Config. end_per_suite(_Config) -> diff --git a/apps/emqx/test/emqx_access_control_SUITE.erl b/apps/emqx/test/emqx_access_control_SUITE.erl index b356402fb..054f839a3 100644 --- a/apps/emqx/test/emqx_access_control_SUITE.erl +++ b/apps/emqx/test/emqx_access_control_SUITE.erl @@ -33,36 +33,23 @@ end_per_suite(_Config) -> emqx_ct_helpers:stop_apps([]). t_authenticate(_) -> - emqx_zone:set_env(zone, allow_anonymous, false), + toggle_auth(true), ?assertMatch({error, _}, emqx_access_control:authenticate(clientinfo())), - emqx_zone:set_env(zone, allow_anonymous, true), + toggle_auth(false), ?assertMatch({ok, _}, emqx_access_control:authenticate(clientinfo())). t_authorize(_) -> Publish = ?PUBLISH_PACKET(?QOS_0, <<"t">>, 1, <<"payload">>), ?assertEqual(allow, emqx_access_control:authorize(clientinfo(), Publish, <<"t">>)). -t_bypass_auth_plugins(_) -> - ClientInfo = clientinfo(), - emqx_zone:set_env(bypass_zone, allow_anonymous, true), - emqx_zone:set_env(zone, allow_anonymous, false), - emqx_zone:set_env(bypass_zone, bypass_auth_plugins, true), - emqx:hook('client.authenticate',{?MODULE, auth_fun, []}), - ?assertMatch({ok, _}, emqx_access_control:authenticate(ClientInfo#{zone => bypass_zone})), - ?assertMatch({ok, _}, emqx_access_control:authenticate(ClientInfo)). - %%-------------------------------------------------------------------- %% Helper functions %%-------------------------------------------------------------------- -auth_fun(#{zone := bypass_zone}, AuthRes) -> - {stop, AuthRes#{auth_result => password_error}}; -auth_fun(#{zone := _}, AuthRes) -> - {stop, AuthRes#{auth_result => success}}. - clientinfo() -> clientinfo(#{}). clientinfo(InitProps) -> - maps:merge(#{zone => zone, + maps:merge(#{zone => default, + listener => mqtt_tcp, protocol => mqtt, peerhost => {127,0,0,1}, clientid => <<"clientid">>, @@ -72,3 +59,6 @@ clientinfo(InitProps) -> peercert => undefined, mountpoint => undefined }, InitProps). + +toggle_auth(Bool) when is_boolean(Bool) -> + emqx_config:put_listener_conf(default, mqtt_tcp, [auth, enable], Bool).