fix(test): update test cases for emqx_access_control_SUITE

This commit is contained in:
Shawn 2021-07-09 15:04:54 +08:00
parent 0ac2492b36
commit fb78e510ca
4 changed files with 15 additions and 17 deletions

View File

@ -27,6 +27,7 @@
-export([ get_listener_conf/3 -export([ get_listener_conf/3
, get_listener_conf/4 , get_listener_conf/4
, put_listener_conf/4
, find_listener_conf/3 , find_listener_conf/3
]). ]).
@ -79,6 +80,10 @@ get_listener_conf(Zone, Listener, KeyPath, Default) ->
{ok, Data} -> Data {ok, Data} -> Data
end. 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()) -> -spec find_listener_conf(atom(), atom(), emqx_map_lib:config_key_path()) ->
{ok, term()} | {not_foud, emqx_map_lib:config_key_path(), term()}. {ok, term()} | {not_foud, emqx_map_lib:config_key_path(), term()}.
find_listener_conf(Zone, Listener, KeyPath) -> find_listener_conf(Zone, Listener, KeyPath) ->

View File

@ -58,6 +58,8 @@ deep_find(_KeyPath, Data) ->
-spec deep_put(config_key_path(), map(), term()) -> map(). -spec deep_put(config_key_path(), map(), term()) -> map().
deep_put([], Map, Config) when is_map(Map) -> deep_put([], Map, Config) when is_map(Map) ->
Config; Config;
deep_put([], _Map, Config) -> %% not map, replace it
Config;
deep_put([Key | KeyPath], Map, Config) -> deep_put([Key | KeyPath], Map, Config) ->
SubMap = deep_put(KeyPath, maps:get(Key, Map, #{}), Config), SubMap = deep_put(KeyPath, maps:get(Key, Map, #{}), Config),
Map#{Key => SubMap}. Map#{Key => SubMap}.

View File

@ -27,6 +27,7 @@ all() -> emqx_ct:all(?MODULE).
init_per_suite(Config) -> init_per_suite(Config) ->
emqx_ct_helpers:start_apps([]), emqx_ct_helpers:start_apps([]),
ct:pal("------------config: ~p", [emqx_config:get()]),
Config. Config.
end_per_suite(_Config) -> end_per_suite(_Config) ->

View File

@ -33,36 +33,23 @@ end_per_suite(_Config) ->
emqx_ct_helpers:stop_apps([]). emqx_ct_helpers:stop_apps([]).
t_authenticate(_) -> t_authenticate(_) ->
emqx_zone:set_env(zone, allow_anonymous, false), toggle_auth(true),
?assertMatch({error, _}, emqx_access_control:authenticate(clientinfo())), ?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())). ?assertMatch({ok, _}, emqx_access_control:authenticate(clientinfo())).
t_authorize(_) -> t_authorize(_) ->
Publish = ?PUBLISH_PACKET(?QOS_0, <<"t">>, 1, <<"payload">>), Publish = ?PUBLISH_PACKET(?QOS_0, <<"t">>, 1, <<"payload">>),
?assertEqual(allow, emqx_access_control:authorize(clientinfo(), Publish, <<"t">>)). ?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 %% 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() -> clientinfo(#{}).
clientinfo(InitProps) -> clientinfo(InitProps) ->
maps:merge(#{zone => zone, maps:merge(#{zone => default,
listener => mqtt_tcp,
protocol => mqtt, protocol => mqtt,
peerhost => {127,0,0,1}, peerhost => {127,0,0,1},
clientid => <<"clientid">>, clientid => <<"clientid">>,
@ -72,3 +59,6 @@ clientinfo(InitProps) ->
peercert => undefined, peercert => undefined,
mountpoint => undefined mountpoint => undefined
}, InitProps). }, InitProps).
toggle_auth(Bool) when is_boolean(Bool) ->
emqx_config:put_listener_conf(default, mqtt_tcp, [auth, enable], Bool).