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

GraphQL - Implement last category #3651

Merged
merged 16 commits into from
May 25, 2022
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
1 change: 1 addition & 0 deletions big_tests/default.spec
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
{suites, "tests", graphql_SUITE}.
{suites, "tests", graphql_account_SUITE}.
{suites, "tests", graphql_domain_SUITE}.
{suites, "tests", graphql_last_SUITE}.
{suites, "tests", graphql_muc_SUITE}.
{suites, "tests", graphql_muc_light_SUITE}.
{suites, "tests", graphql_private_SUITE}.
Expand Down
1 change: 1 addition & 0 deletions big_tests/dynamic_domains.spec
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
{suites, "tests", graphql_SUITE}.
{suites, "tests", graphql_account_SUITE}.
{suites, "tests", graphql_domain_SUITE}.
{suites, "tests", graphql_last_SUITE}.
{suites, "tests", graphql_muc_SUITE}.
{suites, "tests", graphql_muc_light_SUITE}.
{suites, "tests", graphql_private_SUITE}.
Expand Down
136 changes: 2 additions & 134 deletions big_tests/tests/graphql_account_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ user_account_handler() ->
admin_account_handler() ->
[admin_list_users,
admin_count_users,
admin_get_active_users_number,
admin_check_password,
admin_check_password_hash,
admin_check_plain_password_hash,
Expand All @@ -39,11 +38,7 @@ admin_account_handler() ->
admin_remove_non_existing_user,
admin_remove_existing_user,
admin_ban_user,
admin_change_user_password,
admin_list_old_users_domain,
admin_list_old_users_all,
admin_remove_old_users_domain,
admin_remove_old_users_all].
admin_change_user_password].

init_per_suite(Config) ->
Config1 = [{ctl_auth_mods, mongoose_helper:auth_modules()} | Config],
Expand All @@ -55,9 +50,6 @@ end_per_suite(Config) ->
escalus:end_per_suite(Config).

init_per_group(admin_account_handler, Config) ->
Mods = [{mod_last, config_parser_helper:default_mod_config(mod_last)}],
dynamic_modules:ensure_modules(domain_helper:host_type(), Mods),
dynamic_modules:ensure_modules(domain_helper:secondary_host_type(), Mods),
Config1 = escalus:create_users(Config, escalus:get_users([alice])),
graphql_helper:init_admin_handler(Config1);
init_per_group(user_account_handler, Config) ->
Expand All @@ -67,24 +59,12 @@ init_per_group(_, Config) ->

end_per_group(admin_account_handler, Config) ->
escalus_fresh:clean(),
escalus:delete_users(Config, escalus:get_users([alice])),
dynamic_modules:restore_modules(Config);
escalus:delete_users(Config, escalus:get_users([alice]));
end_per_group(user_account_handler, _Config) ->
escalus_fresh:clean();
end_per_group(_, _Config) ->
ok.

init_per_testcase(C, Config) when C =:= admin_list_old_users_all;
C =:= admin_list_old_users_domain;
C =:= admin_remove_old_users_all;
C =:= admin_remove_old_users_domain ->
{_, AuthMods} = lists:keyfind(ctl_auth_mods, 1, Config),
case lists:member(ejabberd_auth_ldap, AuthMods) of
true -> {skip, not_fully_supported_with_ldap};
false ->
Config1 = escalus:create_users(Config, escalus:get_users([alice, bob, alice_bis])),
escalus:init_per_testcase(C, Config1)
end;
init_per_testcase(admin_register_user = C, Config) ->
Config1 = [{user, {<<"gql_admin_registration_test">>, domain_helper:domain()}} | Config],
escalus:init_per_testcase(C, Config1);
Expand All @@ -103,11 +83,6 @@ init_per_testcase(admin_check_plain_password_hash = C, Config) ->
init_per_testcase(CaseName, Config) ->
escalus:init_per_testcase(CaseName, Config).

end_per_testcase(C, Config) when C =:= admin_list_old_users_all;
C =:= admin_list_old_users_domain;
C =:= admin_remove_old_users_all;
C =:= admin_remove_old_users_domain ->
escalus:delete_users(Config, escalus:get_users([alice, bob, alice_bis]));
end_per_testcase(admin_register_user = C, Config) ->
{Username, Domain} = proplists:get_value(user, Config),
rpc(mim(), mongoose_account_api, unregister_user, [Username, Domain]),
Expand Down Expand Up @@ -172,14 +147,6 @@ admin_count_users(Config) ->
Resp2 = execute_auth(count_users_body(Domain), Config),
?assert(0 < get_ok_value([data, account, countUsers], Resp2)).

admin_get_active_users_number(Config) ->
% Check non-existing domain
Resp = execute_auth(get_active_users_number_body(<<"unknown-domain">>, 5), Config),
?assertNotEqual(nomatch, binary:match(get_err_msg(Resp), <<"Cannot count">>)),
% Check an existing domain without active users
Resp2 = execute_auth(get_active_users_number_body(domain_helper:domain(), 5), Config),
?assertEqual(0, get_ok_value([data, account, countActiveUsers], Resp2)).

admin_check_password(Config) ->
Password = lists:last(escalus_users:get_usp(Config, alice)),
BinJID = escalus_users:get_jid(Config, alice),
Expand Down Expand Up @@ -297,90 +264,12 @@ admin_change_user_password(Config) ->
?assertNotEqual(nomatch, binary:match(get_ok_value(Path, Resp3), <<"Password changed">>))
end).

admin_remove_old_users_domain(Config) ->
[AliceName, Domain, _] = escalus_users:get_usp(Config, alice),
[BobName, Domain, _] = escalus_users:get_usp(Config, bob),
[AliceBisName, BisDomain, _] = escalus_users:get_usp(Config, alice_bis),

Now = erlang:system_time(seconds),
set_last(AliceName, Domain, Now),
set_last(BobName, Domain, Now - 86400 * 30),

Path = [data, account, removeOldUsers, users],
Resp = execute_auth(remove_old_users_body(Domain, 10), Config),
?assertEqual(1, length(get_ok_value(Path, Resp))),
?assertMatch({user_does_not_exist, _}, check_account(BobName, Domain)),
?assertMatch({ok, _}, check_account(AliceName, Domain)),
?assertMatch({ok, _}, check_account(AliceBisName, BisDomain)).

admin_remove_old_users_all(Config) ->
[AliceName, Domain, _] = escalus_users:get_usp(Config, alice),
[BobName, Domain, _] = escalus_users:get_usp(Config, bob),
[AliceBisName, BisDomain, _] = escalus_users:get_usp(Config, alice_bis),

Now = erlang:system_time(seconds),
OldTime = Now - 86400 * 30,
set_last(AliceName, Domain, Now),
set_last(BobName, Domain, OldTime),
set_last(AliceBisName, BisDomain, OldTime),

Path = [data, account, removeOldUsers, users],
Resp = execute_auth(remove_old_users_body(null, 10), Config),
?assertEqual(2, length(get_ok_value(Path, Resp))),
?assertMatch({user_does_not_exist, _}, check_account(BobName, Domain)),
?assertMatch({ok, _}, check_account(AliceName, Domain)),
?assertMatch({user_does_not_exist, _}, check_account(AliceBisName, BisDomain)).

admin_list_old_users_domain(Config) ->
[AliceName, Domain, _] = escalus_users:get_usp(Config, alice),
[BobName, Domain, _] = escalus_users:get_usp(Config, bob),

Now = erlang:system_time(seconds),
set_last(AliceName, Domain, Now),
set_last(BobName, Domain, Now - 86400 * 30),

LBob = escalus_utils:jid_to_lower(escalus_users:get_jid(Config, bob)),

Path = [data, account, listOldUsers],
Resp = execute_auth(list_old_users_body(Domain, 10), Config),
Users = get_ok_value(Path, Resp),
?assertEqual(1, length(Users)),
?assert(lists:member(LBob, Users)).

admin_list_old_users_all(Config) ->
[AliceName, Domain, _] = escalus_users:get_usp(Config, alice),
[BobName, Domain, _] = escalus_users:get_usp(Config, bob),
[AliceBisName, BisDomain, _] = escalus_users:get_usp(Config, alice_bis),

Now = erlang:system_time(seconds),
OldTime = Now - 86400 * 30,
set_last(AliceName, Domain, Now),
set_last(BobName, Domain, OldTime),
set_last(AliceBisName, BisDomain, OldTime),

LBob = escalus_utils:jid_to_lower(escalus_users:get_jid(Config, bob)),
LAliceBis = escalus_utils:jid_to_lower(escalus_users:get_jid(Config, alice_bis)),

Path = [data, account, listOldUsers],
Resp = execute_auth(list_old_users_body(null, 10), Config),
Users = get_ok_value(Path, Resp),
?assertEqual(2, length(Users)),
?assert(lists:member(LBob, Users)),
?assert(lists:member(LAliceBis, Users)).

%% Helpers

get_md5(AccountPass) ->
lists:flatten([io_lib:format("~.16B", [X])
|| X <- binary_to_list(crypto:hash(md5, AccountPass))]).

set_last(User, Domain, TStamp) ->
rpc(mim(), mod_last, store_last_info,
[domain_helper:host_type(), escalus_utils:jid_to_lower(User), Domain, TStamp, <<>>]).

check_account(Username, Domain) ->
rpc(mim(), mongoose_account_api, check_account, [Username, Domain]).

%% Request bodies

list_users_body(Domain) ->
Expand All @@ -395,13 +284,6 @@ count_users_body(Domain) ->
Vars = #{<<"domain">> => Domain},
#{query => Query, operationName => OpName, variables => Vars}.

get_active_users_number_body(Domain, Days) ->
Query = <<"query Q1($domain: String!, $days: Int!)
{ account { countActiveUsers(domain: $domain, days: $days) } }">>,
OpName = <<"Q1">>,
Vars = #{<<"domain">> => Domain, <<"days">> => Days},
#{query => Query, operationName => OpName, variables => Vars}.

check_password_body(User, Password) ->
Query = <<"query Q1($user: JID!, $password: String!)
{ account { checkPassword(user: $user, password: $password) {correct message} } }">>,
Expand Down Expand Up @@ -439,20 +321,6 @@ remove_user_body(User) ->
Vars = #{<<"user">> => User},
#{query => Query, operationName => OpName, variables => Vars}.

remove_old_users_body(Domain, Days) ->
Query = <<"mutation M1($domain: String, $days: Int!)
{ account { removeOldUsers(domain: $domain, days: $days) { message users } } }">>,
OpName = <<"M1">>,
Vars = #{<<"domain">> => Domain, <<"days">> => Days},
#{query => Query, operationName => OpName, variables => Vars}.

list_old_users_body(Domain, Days) ->
Query = <<"query Q1($domain: String, $days: Int!)
{ account { listOldUsers(domain: $domain, days: $days) } }">>,
OpName = <<"Q1">>,
Vars = #{<<"domain">> => Domain, <<"days">> => Days},
#{query => Query, operationName => OpName, variables => Vars}.

ban_user_body(JID, Reason) ->
Query = <<"mutation M1($user: JID!, $reason: String!)
{ account { banUser(user: $user, reason: $reason) { jid message } } }">>,
Expand Down
8 changes: 6 additions & 2 deletions big_tests/tests/graphql_helper.erl
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
-export([execute/3, execute_auth/2, execute_domain_auth/2, execute_user/3]).
-export([init_admin_handler/1, init_domain_admin_handler/1, end_domain_admin_handler/1]).
-export([get_listener_port/1, get_listener_config/1]).
-export([get_ok_value/2, get_err_msg/1, get_err_msg/2, make_creds/1,
-export([get_ok_value/2, get_err_msg/1, get_err_msg/2, get_err_code/1, make_creds/1,
user_to_bin/1, user_to_full_bin/1, user_to_jid/1, user_to_lower_jid/1]).

-include_lib("common_test/include/ct.hrl").
Expand Down Expand Up @@ -77,6 +77,9 @@ get_listener_opts(EpName) ->
#{handlers := [Opts]} = get_listener_config(EpName),
Opts.

get_err_code(Resp) ->
get_ok_value([errors, 1, extensions, code], Resp).

-spec get_err_msg(#{errors := [#{message := binary()}]}) -> binary().
get_err_msg(Resp) ->
get_ok_value([errors, 1, message], Resp).
Expand All @@ -100,7 +103,8 @@ user_to_full_bin(#client{} = Client) -> escalus_client:full_jid(Client);
user_to_full_bin(Bin) when is_binary(Bin) -> Bin.

user_to_bin(#client{} = Client) -> escalus_client:short_jid(Client);
user_to_bin(Bin) when is_binary(Bin) -> Bin.
user_to_bin(Bin) when is_binary(Bin) -> Bin;
user_to_bin(null) -> null.

user_to_jid(#client{jid = JID}) -> jid:to_bare(jid:from_binary(JID));
user_to_jid(Bin) when is_binary(Bin) -> jid:to_bare(jid:from_binary(Bin)).
Expand Down
Loading