Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move helpers into big_tests/tests/domain_rest_helper.erl #3232

Merged
merged 1 commit into from
Aug 27, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
126 changes: 126 additions & 0 deletions big_tests/tests/domain_rest_helper.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
-module(domain_rest_helper).

%% Config helpers
-export([set_invalid_creds/1,
set_no_creds/1,
set_valid_creds/1]).

%% Requests API
-export([rest_patch_enabled/3,
rest_put_domain/3,
putt_domain_with_custom_body/2,
rest_select_domain/2,
rest_delete_domain/3,
delete_custom/4,
patch_custom/4]).

%% Handler
-export([start_listener/0,
start_listener/1,
stop_listener/0]).

-import(distributed_helper, [mim/0, mim2/0, require_rpc_nodes/1, rpc/4]).

-define(TEST_PORT, 8866).

set_invalid_creds(Config) ->
[{auth_creds, invalid}|Config].

set_no_creds(Config) ->
[{auth_creds, false}|Config].

set_valid_creds(Config) ->
[{auth_creds, valid}|Config].

domain_path(Domain) ->
<<"/domains/", Domain/binary>>.

make_creds(Config) ->
case proplists:get_value(auth_creds, Config) of
valid ->
{<<"admin">>, <<"secret">>};
invalid ->
{<<"admin">>, <<"badsecret">>};
_ ->
false
end.

%% Requests API
rest_patch_enabled(Config, Domain, Enabled) ->
Params = #{enabled => Enabled},
rest_helper:make_request(#{ role => admin, method => <<"PATCH">>,
port => ?TEST_PORT,
path => domain_path(Domain),
creds => make_creds(Config),
body => Params }).

rest_put_domain(Config, Domain, Type) ->
Params = #{host_type => Type},
rest_helper:make_request(#{ role => admin, method => <<"PUT">>,
port => ?TEST_PORT,
path => domain_path(Domain),
creds => make_creds(Config),
body => Params }).

putt_domain_with_custom_body(Config, Body) ->
rest_helper:make_request(#{ role => admin, method => <<"PUT">>,
port => ?TEST_PORT,
path => <<"/domains/example.db">>,
creds => make_creds(Config),
body => Body }).

rest_select_domain(Config, Domain) ->
Params = #{},
rest_helper:make_request(#{ role => admin, method => <<"GET">>,
port => ?TEST_PORT,
path => domain_path(Domain),
creds => make_creds(Config),
body => Params }).

rest_delete_domain(Config, Domain, HostType) ->
Params = #{<<"host_type">> => HostType},
rest_helper:make_request(#{ role => admin, method => <<"DELETE">>,
port => ?TEST_PORT,
path => domain_path(Domain),
creds => make_creds(Config),
body => Params }).

delete_custom(Config, Role, Path, Body) ->
rest_helper:make_request(#{ role => Role, method => <<"DELETE">>,
port => ?TEST_PORT,
path => Path, creds => make_creds(Config),
body => Body }).

patch_custom(Config, Role, Path, Body) ->
rest_helper:make_request(#{ role => Role, method => <<"PATCH">>,
port => ?TEST_PORT,
path => Path, creds => make_creds(Config),
body => Body }).

%% REST handler setup
start_listener() ->
start_listener(#{}).

stop_listener() ->
PortIpNet = {?TEST_PORT, {127,0,0,1}, tcp},
ListenerModule = ejabberd_cowboy,
rpc(mim(), ejabberd_listener, stop_listener, [PortIpNet, ListenerModule]).

start_listener(Params) ->
PortIpNet = {?TEST_PORT, {127,0,0,1}, tcp},
ListenerModule = ejabberd_cowboy,
ListenerOpts = [{modules, [domain_handler(Params)]},
{transport_options, transport_options()}],
rpc(mim(), ejabberd_listener, start_listener,
[PortIpNet, ListenerModule, ListenerOpts]).

transport_options() ->
[{max_connections, 1024}, {num_acceptors, 10}].

domain_handler(Params) ->
{"localhost", "/api", mongoose_domain_handler, handler_opts(Params)}.

handler_opts(#{skip_auth := true}) ->
[];
handler_opts(_Params) ->
[{password, <<"secret">>}, {username, <<"admin">>}].
120 changes: 19 additions & 101 deletions big_tests/tests/service_domain_db_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,25 @@
-import(distributed_helper, [mim/0, mim2/0, require_rpc_nodes/1, rpc/4]).
-import(ejabberdctl_helper, [ejabberdctl/3]).

-define(TEST_PORT, 8866).
-import(domain_rest_helper,
[set_invalid_creds/1,
set_no_creds/1,
set_valid_creds/1]).

-import(domain_rest_helper,
[rest_patch_enabled/3,
rest_put_domain/3,
putt_domain_with_custom_body/2,
rest_select_domain/2,
rest_delete_domain/3,
delete_custom/4,
patch_custom/4]).

-import(domain_rest_helper,
[start_listener/0,
start_listener/1,
stop_listener/0]).

-define(INV_PWD, <<"basic auth provided, invalid password">>).
-define(NO_PWD, <<"basic auth is required">>).
-define(UNWANTED_PWD, <<"basic auth provided, but not configured">>).
Expand Down Expand Up @@ -1028,106 +1046,6 @@ maybe_teardown_meck(_) ->
%% running unload meck makes no harm even if nothing is mocked
rpc(mim(), meck, unload, []).

make_creds(Config) ->
case proplists:get_value(auth_creds, Config) of
valid ->
{<<"admin">>, <<"secret">>};
invalid ->
{<<"admin">>, <<"badsecret">>};
_ ->
false
end.

set_invalid_creds(Config) ->
[{auth_creds, invalid}|Config].

set_no_creds(Config) ->
[{auth_creds, false}|Config].

set_valid_creds(Config) ->
[{auth_creds, valid}|Config].

domain_path(Domain) ->
<<"/domains/", Domain/binary>>.

rest_patch_enabled(Config, Domain, Enabled) ->
Params = #{enabled => Enabled},
rest_helper:make_request(#{ role => admin, method => <<"PATCH">>,
port => ?TEST_PORT,
path => domain_path(Domain),
creds => make_creds(Config),
body => Params }).

rest_put_domain(Config, Domain, Type) ->
Params = #{host_type => Type},
rest_helper:make_request(#{ role => admin, method => <<"PUT">>,
port => ?TEST_PORT,
path => domain_path(Domain),
creds => make_creds(Config),
body => Params }).

putt_domain_with_custom_body(Config, Body) ->
rest_helper:make_request(#{ role => admin, method => <<"PUT">>,
port => ?TEST_PORT,
path => <<"/domains/example.db">>,
creds => make_creds(Config),
body => Body }).

rest_select_domain(Config, Domain) ->
Params = #{},
rest_helper:make_request(#{ role => admin, method => <<"GET">>,
port => ?TEST_PORT,
path => domain_path(Domain),
creds => make_creds(Config),
body => Params }).

rest_delete_domain(Config, Domain, HostType) ->
Params = #{<<"host_type">> => HostType},
rest_helper:make_request(#{ role => admin, method => <<"DELETE">>,
port => ?TEST_PORT,
path => domain_path(Domain),
creds => make_creds(Config),
body => Params }).

delete_custom(Config, Role, Path, Body) ->
rest_helper:make_request(#{ role => Role, method => <<"DELETE">>,
port => ?TEST_PORT,
path => Path, creds => make_creds(Config),
body => Body }).

patch_custom(Config, Role, Path, Body) ->
rest_helper:make_request(#{ role => Role, method => <<"PATCH">>,
port => ?TEST_PORT,
path => Path, creds => make_creds(Config),
body => Body }).

start_listener() ->
start_listener(#{}).

stop_listener() ->
PortIpNet = {?TEST_PORT, {127,0,0,1}, tcp},
ListenerModule = ejabberd_cowboy,
rpc(mim(), ejabberd_listener, stop_listener, [PortIpNet, ListenerModule]).

start_listener(Params) ->
PortIpNet = {?TEST_PORT, {127,0,0,1}, tcp},
ListenerModule = ejabberd_cowboy,
ListenerOpts = [{modules, [domain_handler(Params)]},
{transport_options, transport_options()}],
rpc(mim(), ejabberd_listener, start_listener,
[PortIpNet, ListenerModule, ListenerOpts]).

transport_options() ->
[{max_connections, 1024}, {num_acceptors, 10}].

domain_handler(Params) ->
{"localhost", "/api", mongoose_domain_handler, handler_opts(Params)}.

handler_opts(#{skip_auth := true}) ->
[];
handler_opts(_Params) ->
[{password, <<"secret">>}, {username, <<"admin">>}].

leave_cluster(Config) ->
Cmd = "leave_cluster",
#{node := Node} = distributed_helper:mim(),
Expand Down