test(exhook): refine property-tests

This commit is contained in:
JianBo He 2021-07-31 14:12:57 +08:00
parent 7c46c8712b
commit 922aa7aae5
1 changed files with 23 additions and 36 deletions

View File

@ -30,12 +30,17 @@
, subopts/0 , subopts/0
]). ]).
-define(CONF_DEFAULT, <<"
exhook: { server.default: { url: \"http://127.0.0.1:9000\" } }
">>).
-define(ALL(Vars, Types, Exprs), -define(ALL(Vars, Types, Exprs),
?SETUP(fun() -> ?SETUP(fun() ->
State = do_setup(), State = do_setup(),
fun() -> do_teardown(State) end fun() -> do_teardown(State) end
end, ?FORALL(Vars, Types, Exprs))). end, ?FORALL(Vars, Types, Exprs))).
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% Properties %% Properties
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
@ -76,27 +81,13 @@ prop_client_authenticate() ->
ClientInfo = inject_magic_into(username, ClientInfo0), ClientInfo = inject_magic_into(username, ClientInfo0),
OutAuthResult = emqx_hooks:run_fold('client.authenticate', [ClientInfo], AuthResult), OutAuthResult = emqx_hooks:run_fold('client.authenticate', [ClientInfo], AuthResult),
ExpectedAuthResult = case maps:get(username, ClientInfo) of ExpectedAuthResult = case maps:get(username, ClientInfo) of
<<"baduser">> -> <<"baduser">> -> {error, not_authorized};
AuthResult#{ <<"gooduser">> -> ok;
auth_result => not_authorized, <<"normaluser">> -> ok;
anonymous => false}; _ -> case AuthResult of
<<"gooduser">> -> ok -> ok;
AuthResult#{ _ -> {error, not_authorized}
auth_result => success, end
anonymous => false};
<<"normaluser">> ->
AuthResult#{
auth_result => success,
anonymous => false};
_ ->
case maps:get(auth_result, AuthResult) of
success ->
#{auth_result => success,
anonymous => false};
_ ->
#{auth_result => not_authorized,
anonymous => false}
end
end, end,
?assertEqual(ExpectedAuthResult, OutAuthResult), ?assertEqual(ExpectedAuthResult, OutAuthResult),
@ -109,14 +100,14 @@ prop_client_authenticate() ->
true true
end). end).
prop_client_check_acl() -> prop_client_authorize() ->
?ALL({ClientInfo0, PubSub, Topic, Result}, ?ALL({ClientInfo0, PubSub, Topic, Result},
{clientinfo(), oneof([publish, subscribe]), {clientinfo(), oneof([publish, subscribe]),
topic(), oneof([allow, deny])}, topic(), oneof([allow, deny])},
begin begin
ClientInfo = inject_magic_into(username, ClientInfo0), ClientInfo = inject_magic_into(username, ClientInfo0),
OutResult = emqx_hooks:run_fold( OutResult = emqx_hooks:run_fold(
'client.check_acl', 'client.authorize',
[ClientInfo, PubSub, Topic], [ClientInfo, PubSub, Topic],
Result), Result),
ExpectedOutResult = case maps:get(username, ClientInfo) of ExpectedOutResult = case maps:get(username, ClientInfo) of
@ -127,7 +118,7 @@ prop_client_check_acl() ->
end, end,
?assertEqual(ExpectedOutResult, OutResult), ?assertEqual(ExpectedOutResult, OutResult),
{'on_client_check_acl', Resp} = emqx_exhook_demo_svr:take(), {'on_client_authorize', Resp} = emqx_exhook_demo_svr:take(),
Expected = Expected =
#{result => aclresult_to_bool(Result), #{result => aclresult_to_bool(Result),
type => pubsub_to_enum(PubSub), type => pubsub_to_enum(PubSub),
@ -423,7 +414,7 @@ subopts(SubOpts) ->
}. }.
authresult_to_bool(AuthResult) -> authresult_to_bool(AuthResult) ->
maps:get(auth_result, AuthResult, undefined) == success. AuthResult == ok.
aclresult_to_bool(Result) -> aclresult_to_bool(Result) ->
Result == allow. Result == allow.
@ -474,7 +465,8 @@ from_message(Msg) ->
do_setup() -> do_setup() ->
logger:set_primary_config(#{level => warning}), logger:set_primary_config(#{level => warning}),
_ = emqx_exhook_demo_svr:start(), _ = emqx_exhook_demo_svr:start(),
emqx_ct_helpers:start_apps([emqx_exhook], fun set_special_cfgs/1), ok = emqx_config:init_load(emqx_exhook_schema, ?CONF_DEFAULT),
emqx_ct_helpers:start_apps([emqx_exhook]),
%% waiting first loaded event %% waiting first loaded event
{'on_provider_loaded', _} = emqx_exhook_demo_svr:take(), {'on_provider_loaded', _} = emqx_exhook_demo_svr:take(),
ok. ok.
@ -488,15 +480,6 @@ do_teardown(_) ->
timer:sleep(2000), timer:sleep(2000),
ok. ok.
set_special_cfgs(emqx) ->
application:set_env(emqx, allow_anonymous, false),
application:set_env(emqx, enable_acl_cache, false),
application:set_env(emqx, modules_loaded_file, undefined),
application:set_env(emqx, plugins_loaded_file,
emqx_ct_helpers:deps_path(emqx, "test/emqx_SUITE_data/loaded_plugins"));
set_special_cfgs(emqx_exhook) ->
ok.
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% Generators %% Generators
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
@ -517,7 +500,11 @@ shutdown_reason() ->
oneof([utf8(), {shutdown, emqx_ct_proper_types:limited_atom()}]). oneof([utf8(), {shutdown, emqx_ct_proper_types:limited_atom()}]).
authresult() -> authresult() ->
?LET(RC, connack_return_code(), #{auth_result => RC}). ?LET(RC, connack_return_code(),
case RC of
success -> ok;
_ -> {error, RC}
end).
inject_magic_into(Key, Object) -> inject_magic_into(Key, Object) ->
case castspell() of case castspell() of