fix(test): update test cases for emqx_access_control_SUITE
This commit is contained in:
parent
0ac2492b36
commit
fb78e510ca
|
@ -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) ->
|
||||
|
|
|
@ -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}.
|
||||
|
|
|
@ -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) ->
|
||||
|
|
|
@ -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).
|
||||
|
|
Loading…
Reference in New Issue