Skip to content

Commit

Permalink
Use mongoose_config in small tests
Browse files Browse the repository at this point in the history
  • Loading branch information
chrzaszcz committed Oct 19, 2021
1 parent 4f0e6ee commit 95bdf67
Show file tree
Hide file tree
Showing 26 changed files with 132 additions and 279 deletions.
26 changes: 8 additions & 18 deletions test/acl_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,11 @@ host_type_test_cases() ->
].

init_per_suite(Config) ->
ok = mnesia:create_schema([node()]),
ok = mnesia:start(),
{ok, _} = application:ensure_all_started(jid),
Config.

end_per_suite(_Config) ->
mongoose_domain_api:stop(),
mnesia:stop(),
mnesia:delete_schema([node()]),
meck:unload(),
ok.

Expand Down Expand Up @@ -318,16 +314,10 @@ different_specs_matching_the_same_user(Config) ->
ok.

set_acl(HostType, ACLName, ACLSpec) ->
ejabberd_config:add_local_option({acl, ACLName, HostType}, [ACLSpec]).
mongoose_config:set_opt({acl, ACLName, HostType}, [ACLSpec]).

given_clean_config() ->
meck:unload(),
%% skip loading part
meck:new(ejabberd_config, [no_link, unstick, passthrough]),
meck:expect(ejabberd_config, load_file, fun(_File) -> ok end),
ejabberd_config:start(),
mnesia:clear_table(config),
mnesia:clear_table(local_config),
[persistent_term:erase(Key) || {Key = {mongoose_config, _}, _Value} <- persistent_term:get()],
ok.

given_registered_domains(Config, DomainsList) ->
Expand All @@ -339,24 +329,24 @@ given_registered_domains(Config, DomainsList) ->
end.

register_static_domains(DomainsList) ->
ejabberd_config:add_local_option(hosts, DomainsList),
ejabberd_config:add_local_option(host_types, []),
mongoose_config:set_opt(hosts, DomainsList),
mongoose_config:set_opt(host_types, []),
mongoose_domain_api:stop(),
mongoose_domain_api:init().

register_dynamic_domains(DomainsList) ->
ejabberd_config:add_local_option(hosts, []),
ejabberd_config:add_local_option(host_types, [<<"test type">>, <<"empty type">>]),
mongoose_config:set_opt(hosts, []),
mongoose_config:set_opt(host_types, [<<"test type">>, <<"empty type">>]),
mongoose_domain_api:stop(),
mongoose_domain_api:init(),
[mongoose_domain_core:insert(Domain, <<"test type">>, test) || Domain <- DomainsList].

%% ACLs might be an empty list
set_host_rule(Rule, Host, ACLs) ->
ejabberd_config:add_local_option({access, Rule, Host}, ACLs),
mongoose_config:set_opt({access, Rule, Host}, ACLs),
ok.

%% ACLs might be an empty list
set_global_rule(Rule, ACLs) ->
ejabberd_config:add_local_option({access, Rule, global}, ACLs),
mongoose_config:set_opt({access, Rule, global}, ACLs),
ok.
16 changes: 6 additions & 10 deletions test/auth_dummy_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -35,18 +35,14 @@ all() -> [

init_per_suite(C) ->
{ok, _} = application:ensure_all_started(jid),
meck:new(ejabberd_config, [no_link]),
meck:expect(ejabberd_config, get_local_option,
fun({auth_method, ?HOST_TYPE}) ->
dummy;
({auth_opts, ?HOST_TYPE}) ->
[{dummy_base_timeout, 5}, {dummy_variance, 10}]
end),
mongoose_config:set_opt({auth_method, ?HOST_TYPE}, [dummy]),
mongoose_config:set_opt({auth_opts, ?HOST_TYPE}, [{dummy_base_timeout, 5},
{dummy_variance, 10}]),
C.

end_per_suite(C) ->
meck:unload(),
C.
end_per_suite(_C) ->
mongoose_config:unset_opt({auth_method, ?HOST_TYPE}),
mongoose_config:unset_opt({auth_opts, ?HOST_TYPE}).

%%--------------------------------------------------------------------
%% Authentication tests
Expand Down
27 changes: 10 additions & 17 deletions test/auth_external_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,14 @@ init_per_suite(C) ->
end_per_suite(C) ->
C.

init_per_group(G, Config) ->
setup_meck(G, Config),
init_per_group(_G, Config) ->
setup_config(Config),
ejabberd_auth_external:start(host_type()),
Config.

end_per_group(G, Config) ->
end_per_group(_G, Config) ->
ejabberd_auth_external:stop(host_type()),
unload_meck(G),
teardown_config(),
Config.

try_register_ok(_C) ->
Expand Down Expand Up @@ -77,20 +77,13 @@ given_user_registered() ->
ok = ejabberd_auth_external:try_register(host_type(), U, domain(), P),
UP.

setup_meck(_G, Config) ->
setup_config(Config) ->
DataDir = ?config(data_dir, Config),
meck:new(ejabberd_config, [no_link]),
meck:expect(ejabberd_config, get_local_option,
fun({auth_opts, ?HOST_TYPE}) ->
[{extauth_program, DataDir ++ "sample_external_auth.py"}];
({extauth_instances, ?HOST_TYPE}) ->
undefined;
({extauth_cache, ?HOST_TYPE}) ->
undefined
end).

unload_meck(_G) ->
meck:unload(ejabberd_config).
mongoose_config:set_opt({auth_opts, ?HOST_TYPE},
[{extauth_program, DataDir ++ "sample_external_auth.py"}]).

teardown_config() ->
mongoose_config:unset_opt({auth_opts, ?HOST_TYPE}).

gen_user() ->
U = random_binary(5),
Expand Down
37 changes: 10 additions & 27 deletions test/auth_http_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ suite() ->

init_per_suite(Config) ->
{ok, _} = application:ensure_all_started(jid),
meck_config(Config),
setup_config(Config),
mim_ct_rest:start(?BASIC_AUTH, Config),
% Separate process needs to do this, because this one will terminate
% so will supervisor and children and ETS tables
Expand All @@ -83,12 +83,12 @@ init_per_suite(Config) ->
mongoose_wpool_http:init(),
ejabberd_auth_http:start(?HOST_TYPE)
end),
meck_cleanup(),
Config.

end_per_suite(Config) ->
ejabberd_auth_http:stop(?HOST_TYPE),
ok = mim_ct_rest:stop(),
teardown_config(),
Config.

init_per_group(cert_auth, Config) ->
Expand All @@ -106,10 +106,9 @@ init_per_group(cert_auth, Config) ->
init_per_group(GroupName, Config) ->
Config2 = lists:keystore(scram_group, 1, Config,
{scram_group, GroupName == auth_requests_scram}),
meck_config(Config2),
setup_config(Config2),
mim_ct_rest:register(<<"alice">>, ?DOMAIN, do_scram(<<"makota">>, Config2)),
mim_ct_rest:register(<<"bob">>, ?DOMAIN, do_scram(<<"niema5klepki">>, Config2)),
meck_cleanup(),
Config2.

end_per_group(cert_auth, Config) ->
Expand All @@ -120,45 +119,36 @@ end_per_group(_GroupName, Config) ->
Config.

init_per_testcase(remove_user, Config) ->
meck_config(Config),
mim_ct_rest:register(<<"toremove1">>, ?DOMAIN, do_scram(<<"pass">>, Config)),
mim_ct_rest:register(<<"toremove2">>, ?DOMAIN, do_scram(<<"pass">>, Config)),
Config;
init_per_testcase(cert_auth_fail, Config) ->
meck_config(Config),
Cert = proplists:get_value(pem_cert1, Config),
mim_ct_rest:register(<<"cert_user">>, ?DOMAIN, Cert),
Config;
init_per_testcase(cert_auth_success, Config) ->
meck_config(Config),
Cert1 = proplists:get_value(pem_cert1, Config),
Cert2 = proplists:get_value(pem_cert2, Config),
SeveralCerts = <<Cert1/bitstring, Cert2/bitstring>>,
mim_ct_rest:register(<<"cert_user">>, ?DOMAIN, SeveralCerts),
Config;
init_per_testcase(_CaseName, Config) ->
meck_config(Config),
Config.

end_per_testcase(try_register, Config) ->
mim_ct_rest:remove_user(<<"nonexistent">>, ?DOMAIN),
meck_cleanup(),
Config;
end_per_testcase(remove_user, Config) ->
mim_ct_rest:remove_user(<<"toremove1">>, ?DOMAIN),
mim_ct_rest:remove_user(<<"toremove2">>, ?DOMAIN),
meck_cleanup(),
Config;
end_per_testcase(cert_auth_fail, Config) ->
mim_ct_rest:remove_user(<<"cert_user">>, ?DOMAIN),
meck_cleanup(),
Config;
end_per_testcase(cert_auth_success, Config) ->
mim_ct_rest:remove_user(<<"cert_user">>, ?DOMAIN),
meck_cleanup(),
Config;
end_per_testcase(_CaseName, Config) ->
meck_cleanup(),
Config.

%%--------------------------------------------------------------------
Expand Down Expand Up @@ -245,25 +235,18 @@ creds_with_cert(Config, Username) ->
mongoose_credentials:extend(NewCreds, [{der_cert, Cert},
{username, Username}]).

meck_config(Config) ->
meck:unload(),
setup_config(Config) ->
ScramOpts = case lists:keyfind(scram_group, 1, Config) of
{_, false} -> [{password_format, plain}];
_ -> []
end,
meck:new(ejabberd_config),
meck:expect(ejabberd_config, get_local_option,
fun({auth_opts, ?HOST_TYPE}) ->
[
{host, ?AUTH_HOST},
{path_prefix, "/auth/"},
{basic_auth, ?BASIC_AUTH}
] ++ ScramOpts
end).
mongoose_config:set_opt({auth_opts, ?HOST_TYPE},
[{host, ?AUTH_HOST},
{path_prefix, "/auth/"},
{basic_auth, ?BASIC_AUTH}] ++ ScramOpts).

meck_cleanup() ->
meck:validate(ejabberd_config),
meck:unload(ejabberd_config).
teardown_config() ->
mongoose_config:unset_opt({auth_opts, ?HOST_TYPE}).

do_scram(Pass, Config) ->
case lists:keyfind(scram_group, 1, Config) of
Expand Down
50 changes: 10 additions & 40 deletions test/auth_jwt_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,10 @@ suite() ->

init_per_suite(Config) ->
application:ensure_all_started(jid),
Self = self(),
ETSProcess = spawn(fun() -> ets_owner(Self) end),
wait_for_ets(),
meck_config(Config),
[{ets_process, ETSProcess} | Config].
Config.

end_per_suite(Config) ->
meck_cleanup(),
stop_ets(proplists:get_value(ets_process, Config)),
unset_auth_opts(),
Config.

init_per_group(public_key, Config) ->
Expand Down Expand Up @@ -124,40 +119,15 @@ check_password_succeeds_for_pubkey_signed_token(C) ->
%% Helpers
%%--------------------------------------------------------------------

ets_owner(Parent) ->
ets:new(jwt_meck, [public, named_table, set]),
Parent ! ets_ready,
receive stop -> ok end.

wait_for_ets() ->
receive
ets_ready -> ok
after
5000 -> ct:fail(cant_prepare_ets)
end.

stop_ets(undefined) -> ok;
stop_ets(Pid) -> Pid ! stop.

set_auth_opts(SecretSource, Secret, Algorithm, Key) ->
ets:insert(jwt_meck, {auth_opts, [
{jwt_secret_source, SecretSource},
{jwt_secret, Secret},
{jwt_algorithm, Algorithm},
{jwt_username_key, Key}
]}).

meck_config(_Config) ->
meck:new(ejabberd_config, [no_link]),
meck:expect(ejabberd_config, get_local_option,
fun({Key, ?HOST_TYPE}) -> [{_, Data}] = ets:lookup(jwt_meck, Key), Data end),
meck:expect(ejabberd_config, add_local_option,
fun({Key, _}, Val) -> ets:insert(jwt_meck, {Key, Val}) end).

meck_cleanup() ->
meck:validate(ejabberd_config),
meck:unload(ejabberd_config),
ets:delete(jwt_meck).
mongoose_config:set_opt({auth_opts, ?HOST_TYPE},
[{jwt_secret_source, SecretSource},
{jwt_secret, Secret},
{jwt_algorithm, Algorithm},
{jwt_username_key, Key}]).

unset_auth_opts() ->
mongoose_config:unset_opt({auth_opts, ?HOST_TYPE}).

generate_token(Alg, NbfDelta, Key) ->
Now = erlang:system_time(second),
Expand Down
22 changes: 8 additions & 14 deletions test/commands_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -70,27 +70,21 @@ stop_helper_proc(C) ->
Pid ! stop.

init_per_testcase(_, C) ->
meck:new(ejabberd_config),
meck:expect(ejabberd_config, get_local_option, fun get_opt/1),
meck:expect(ejabberd_config, get_local_option_or_default, fun(K, _) -> get_opt(K) end),
[mongoose_config:set_opt(Key, Value) || {Key, Value} <- opts()],
meck:new(ejabberd_auth_dummy, [non_strict]),
meck:expect(ejabberd_auth_dummy, get_password_s, fun(_, _) -> <<"">> end),
meck:new(mongoose_domain_api),
meck:expect(mongoose_domain_api, get_domain_host_type, fun(H) -> {ok, H} end),
C.

end_per_testcase(_, C) ->
meck:unload(),
C.
end_per_testcase(_, _C) ->
[mongoose_config:unset_opt(Key) || {Key, _Value} <- opts()],
meck:unload().

get_opt({auth_method, _}) ->
dummy;
get_opt({access, experts_only, _}) ->
[{allow, coder}, {allow, manager}, {deny, all}];
get_opt({access, _, _}) ->
[];
get_opt({acl, coder, _}) ->
[{user, <<"zenek">>}].
opts() ->
[{{auth_method, <<"localhost">>}, [dummy]},
{{access, experts_only, <<"localhost">>}, [{allow, coder}, {allow, manager}, {deny, all}]},
{{acl, coder, <<"localhost">>}, [{user, <<"zenek">>}]}].

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%% test methods
Expand Down
2 changes: 0 additions & 2 deletions test/commands_backend_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,6 @@ setup(Module) ->
%% you have to meck some stuff to get it working....
meck:expect(gen_hook, add_handler, fun(_, _, _, _, _) -> ok end),
meck:expect(gen_hook, run_fold, fun(_, _, _, _) -> {ok, ok} end),
meck:expect(ejabberd_config, get_local_option, fun(_) -> undefined end),
spawn(fun mc_holder/0),
meck:expect(supervisor, start_child,
fun(ejabberd_listeners, {_, {_, start_link, [_]}, transient,
Expand All @@ -118,7 +117,6 @@ setup(Module) ->
teardown() ->
cowboy:stop_listener(ejabberd_cowboy:ref({?PORT, ?IP, tcp})),
mongoose_commands:unregister(commands_new()),
meck:unload(ejabberd_config),
meck:unload(ejabberd_auth),
meck:unload(gen_hook),
meck:unload(supervisor),
Expand Down
9 changes: 2 additions & 7 deletions test/component_reg_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,7 @@ init_per_suite(C) ->
{ok, _} = application:ensure_all_started(jid),
ok = mnesia:create_schema([node()]),
ok = mnesia:start(),
meck:new(ejabberd_config),
meck:expect(ejabberd_config, get_local_option,
fun(routing_modules) ->
[xmpp_router_a, xmpp_router_b, xmpp_router_c];
(_) ->
undefined
end),
mongoose_config:set_opt(routing_modules, [xmpp_router_a, xmpp_router_b, xmpp_router_c]),
meck:new(mongoose_domain_api, [no_link]),
meck:expect(mongoose_domain_api, get_host_type,
fun(_) -> {error, not_found} end),
Expand All @@ -36,6 +30,7 @@ end_per_suite(_C) ->
mnesia:stop(),
mnesia:delete_schema([node()]),
meck:unload(),
mongoose_config:unset_opt(routing_modules),
ok.

registering(_C) ->
Expand Down
Loading

0 comments on commit 95bdf67

Please sign in to comment.