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

Rework domain API module #3851

Merged
merged 13 commits into from
Nov 15, 2022
3 changes: 2 additions & 1 deletion big_tests/dynamic_domains.config
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
{mim2, [{node, mongooseim2@localhost},
{domain, <<"domain.example.com">>},
{host_type, <<"test type">>},
{dynamic_domains, [{<<"test type">>, [<<"domain.example.com">>]}]},
{dynamic_domains, []}, % domains already inserted on mim
{vars, "mim2"},
{cluster, mim},
{c2s_port, 5232},
Expand All @@ -41,6 +41,7 @@
{mim3, [{node, mongooseim3@localhost},
{domain, <<"domain.example.com">>},
{host_type, <<"test type">>},
{dynamic_domains, []}, % domains already inserted on mim
{vars, "mim3"},
{c2s_tls_port, 5263},
{cluster, mim}]},
Expand Down
15 changes: 8 additions & 7 deletions big_tests/tests/domain_helper.erl
Original file line number Diff line number Diff line change
Expand Up @@ -51,28 +51,29 @@ delete_domain(Node, Domain) ->
ok = rpc(Node, mongoose_domain_core, delete, [Domain]).

insert_persistent_domain(Node, Domain, HostType) ->
ok = rpc(Node, mongoose_domain_api, insert_domain, [Domain, HostType]).
{ok, _} = rpc(Node, mongoose_domain_api, insert_domain, [Domain, HostType]).

delete_persistent_domain(Node, Domain, HostType) ->
ok = rpc(Node, mongoose_domain_api, delete_domain, [Domain, HostType]).
{ok, _} = rpc(Node, mongoose_domain_api, delete_domain, [Domain, HostType]).

set_domain_password(Node, Domain, Password) ->
ok = rpc(Node, mongoose_domain_api, set_domain_password, [Domain, Password]).
{ok, _} = rpc(Node, mongoose_domain_api, set_domain_password, [Domain, Password]).

delete_domain_password(Node, Domain) ->
ok = rpc(Node, mongoose_domain_api, delete_domain_password, [Domain]).
{ok, _} = rpc(Node, mongoose_domain_api, delete_domain_password, [Domain]).

for_each_configured_domain(F) ->
[for_each_configured_domain(F, Opts) || {_, Opts} <- ct:get_config(hosts)],
ok.

for_each_configured_domain(F, Opts) ->
case proplists:get_value(dynamic_domains, Opts, []) of
[] ->
ok;
case proplists:get_value(dynamic_domains, Opts) of
undefined ->
ok; % no dynamic domains required
DomainsByHostType ->
Node = #{node => proplists:get_value(node, Opts)},
[F(Node, Domain, HostType) || {HostType, Domains} <- DomainsByHostType,
Domain <- Domains],
rpc(Node, service_domain_db, force_check_for_updates, []),
rpc(Node, service_domain_db, sync_local, [])
end.
69 changes: 45 additions & 24 deletions big_tests/tests/graphql_domain_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

-import(distributed_helper, [mim/0, require_rpc_nodes/1, rpc/4]).
-import(graphql_helper, [execute_command/4, get_ok_value/2, get_err_msg/1, skip_null_fields/1,
execute_domain_admin_command/4, get_unauthorized/1]).
execute_domain_admin_command/4, get_unauthorized/1, get_coercion_err_msg/1]).

-define(HOST_TYPE, <<"dummy auth">>).
-define(SECOND_HOST_TYPE, <<"test type">>).
Expand Down Expand Up @@ -36,6 +36,7 @@ domain_tests() ->
domain_not_found_error_formatting_after_mutation_enable_domain,
domain_not_found_error_formatting_after_query,
wrong_host_type_error_formatting,
invalid_domain_name_error,
disable_domain,
enable_domain,
get_domains_by_host_type,
Expand All @@ -45,7 +46,8 @@ domain_tests() ->
get_domains_after_deletion,
set_domain_password,
set_nonexistent_domain_password,
delete_domain_password
delete_domain_password,
delete_nonexistent_domain_password
].

domain_admin_tests() ->
Expand Down Expand Up @@ -103,24 +105,26 @@ create_domain(DomainName, Config) ->
Result = add_domain(DomainName, ?HOST_TYPE, Config),
ParsedResult = get_ok_value([data, domain, addDomain], Result),
?assertEqual(#{<<"domain">> => DomainName,
<<"hostType">> => ?HOST_TYPE,
<<"status">> => null}, ParsedResult).
<<"hostType">> => ?HOST_TYPE,
<<"status">> => null}, ParsedResult).

unknown_host_type_error_formatting(Config) ->
DomainName = ?EXAMPLE_DOMAIN,
HostType = <<"NonExistingHostType">>,
Result = add_domain(DomainName, HostType, Config),
?assertEqual(<<"Unknown host type">>, get_err_msg(Result)).
?assertEqual(<<"Unknown host type">>, get_err_msg(Result)),
Result2 = get_domains_by_host_type(HostType, Config),
?assertEqual(<<"Unknown host type">>, get_err_msg(Result2)).

static_domain_error_formatting(Config) ->
DomainName = <<"localhost">>,
Result = add_domain(DomainName, ?HOST_TYPE, Config),
?assertEqual(<<"Domain static">>, get_err_msg(Result)).
?assertEqual(<<"Domain is static">>, get_err_msg(Result)).

domain_duplicate_error_formatting(Config) ->
DomainName = ?EXAMPLE_DOMAIN,
Result = add_domain(DomainName, ?SECOND_HOST_TYPE, Config),
?assertEqual(<<"Domain already exists">>, get_err_msg(Result)).
?assertMatch(<<"Domain already exists">>, get_err_msg(Result)).

domain_not_found_error_formatting_after_mutation_enable_domain(Config) ->
DomainName = <<"NonExistingDomain">>,
Expand All @@ -139,7 +143,15 @@ domain_not_found_error_formatting_after_query(Config) ->

wrong_host_type_error_formatting(Config) ->
Result = remove_domain(?EXAMPLE_DOMAIN, ?SECOND_HOST_TYPE, Config),
?assertEqual(<<"Wrong host type">>, get_err_msg(Result)).
?assertEqual(<<"Wrong host type was provided">>, get_err_msg(Result)).

invalid_domain_name_error(Config) ->
%% One operation tested, because they all use the DomainName type
Result1 = add_domain(<<>>, ?HOST_TYPE, Config),
get_coercion_err_msg(Result1),
TooLong = binary:copy(<<$a>>, 1024),
Result2 = add_domain(TooLong, ?HOST_TYPE, Config),
get_coercion_err_msg(Result2).

disable_domain(Config) ->
Result = disable_domain(?EXAMPLE_DOMAIN, Config),
Expand Down Expand Up @@ -169,28 +181,27 @@ get_domain_details(Config) ->
delete_domain(Config) ->
Result1 = remove_domain(?EXAMPLE_DOMAIN, ?HOST_TYPE, Config),
ParsedResult1 = get_ok_value([data, domain, removeDomain], Result1),
?assertMatch(#{<<"msg">> := <<"Domain removed!">>,
<<"domain">> := #{<<"domain">> := ?EXAMPLE_DOMAIN}},
?assertEqual(#{<<"domain">> => ?EXAMPLE_DOMAIN,
<<"hostType">> => ?HOST_TYPE,
<<"status">> => <<"DELETED">>},
ParsedResult1),
Result2 = remove_domain(?SECOND_EXAMPLE_DOMAIN, ?HOST_TYPE, Config),
ParsedResult2 = get_ok_value([data, domain, removeDomain], Result2),
?assertMatch(#{<<"msg">> := <<"Domain removed!">>,
<<"domain">> := #{<<"domain">> := ?SECOND_EXAMPLE_DOMAIN}},
ParsedResult2).
Result2 = remove_domain(?EXAMPLE_DOMAIN, ?HOST_TYPE, Config),
domain_not_found_error_formatting(Result2).

request_delete_domain(Config) ->
Domain = <<"exampleDomain">>,
Result1 = request_remove_domain(Domain, ?HOST_TYPE, Config),
Result1 = request_remove_domain(?SECOND_EXAMPLE_DOMAIN, ?HOST_TYPE, Config),
ParsedResult1 = get_ok_value([data, domain, requestRemoveDomain], Result1),
?assertMatch(#{<<"msg">> := <<"Domain disabled and enqueued for deletion">>,
<<"domain">> := #{<<"domain">> := Domain,
<<"status">> := <<"DELETING">>}},
?assertEqual(#{<<"domain">> => ?SECOND_EXAMPLE_DOMAIN,
<<"hostType">> => ?HOST_TYPE,
<<"status">> => <<"DELETING">>},
ParsedResult1),
F = fun() ->
Result = get_domain_details(Domain, Config),
Result = get_domain_details(?EXAMPLE_DOMAIN, Config),
domain_not_found_error_formatting(Result)
end,
mongoose_helper:wait_until(F, ok, #{time_left => timer:seconds(5)}).
mongoose_helper:wait_until(F, ok, #{time_left => timer:seconds(5)}),
Result2 = request_remove_domain(?EXAMPLE_DOMAIN, ?HOST_TYPE, Config),
domain_not_found_error_formatting(Result2).

get_domains_after_deletion(Config) ->
Result = get_domains_by_host_type(?HOST_TYPE, Config),
Expand All @@ -210,7 +221,14 @@ set_nonexistent_domain_password(Config) ->
delete_domain_password(Config) ->
Result = delete_domain_password(domain_helper:domain(), Config),
ParsedResult = get_ok_value([data, domain, deleteDomainPassword], Result),
?assertNotEqual(nomatch, binary:match(ParsedResult, <<"successfully">>)).
?assertNotEqual(nomatch, binary:match(ParsedResult, <<"successfully">>)),
Result2 = delete_domain_password(domain_helper:domain(), Config),
domain_password_not_found_error_formatting(Result2).

delete_nonexistent_domain_password(Config) ->
Domain = <<"unknown-domain.com">>,
Result = delete_domain_password(Domain, Config),
domain_password_not_found_error_formatting(Result).

domain_admin_get_domain_details(Config) ->
Result = get_domain_details(?DOMAIN_ADMIN_EXAMPLE_DOMAIN, Config),
Expand Down Expand Up @@ -297,4 +315,7 @@ delete_domain_password(Domain, Config) ->
%% Helpers

domain_not_found_error_formatting(Result) ->
?assertEqual(<<"Given domain does not exist">>, get_err_msg(Result)).
?assertMatch(<<"Given domain does not exist", _/binary>>, get_err_msg(Result)).

domain_password_not_found_error_formatting(Result) ->
?assertEqual(<<"Domain password does not exist">>, get_err_msg(Result)).
2 changes: 1 addition & 1 deletion big_tests/tests/graphql_helper.erl
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ init_domain_admin_handler(Config, Domain) ->
true ->
Password = base16:encode(crypto:strong_rand_bytes(8)),
Creds = {<<"admin@", Domain/binary>>, Password},
ok = domain_helper:set_domain_password(mim(), Domain, Password),
domain_helper:set_domain_password(mim(), Domain, Password),
add_specs([{protocol, http}, {domain_admin, Creds}, {schema_endpoint, domain_admin}
| Config]);
false -> {skip, require_rdbms}
Expand Down
Loading