From 674d451920bb82716661f3f7405650d2d950fe91 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Przemys=C5=82aw=20Wojtasik?= Date: Thu, 19 May 2022 10:03:33 +0200 Subject: [PATCH] Add tests for old users to the graphql_last_SUITE Remove all tests using mod_last from the graphql_account_SUITE. --- big_tests/tests/graphql_account_SUITE.erl | 136 +----------------- big_tests/tests/graphql_last_SUITE.erl | 164 ++++++++++++++++++++-- 2 files changed, 156 insertions(+), 144 deletions(-) diff --git a/big_tests/tests/graphql_account_SUITE.erl b/big_tests/tests/graphql_account_SUITE.erl index 091e3d8091..d80cbe3f62 100644 --- a/big_tests/tests/graphql_account_SUITE.erl +++ b/big_tests/tests/graphql_account_SUITE.erl @@ -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, @@ -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], @@ -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) -> @@ -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); @@ -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]), @@ -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), @@ -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) -> @@ -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} } }">>, @@ -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 } } }">>, diff --git a/big_tests/tests/graphql_last_SUITE.erl b/big_tests/tests/graphql_last_SUITE.erl index 042ced51a1..7278540bb0 100644 --- a/big_tests/tests/graphql_last_SUITE.erl +++ b/big_tests/tests/graphql_last_SUITE.erl @@ -3,7 +3,7 @@ -compile([export_all, nowarn_export_all]). -import(distributed_helper, [mim/0, require_rpc_nodes/1, rpc/4]). --import(graphql_helper, [execute_user/3, execute_auth/2, user_to_bin/1, +-import(graphql_helper, [execute_user/3, execute_auth/2, user_to_bin/1, user_to_jid/1, get_ok_value/2, get_err_msg/1, get_err_code/1]). -include_lib("eunit/include/eunit.hrl"). @@ -19,11 +19,13 @@ suite() -> all() -> [{group, user_last}, - {group, admin_last}]. + {group, admin_last}, + {group, admin_last_old_users}]. groups() -> [{user_last, [parallel], user_last_handler()}, - {admin_last, [parallel], admin_last_handler()}]. + {admin_last, [parallel], admin_last_handler()}, + {admin_last_old_users, [], admin_old_users_handler()}]. user_last_handler() -> [user_set_last, @@ -39,14 +41,27 @@ admin_last_handler() -> admin_count_active_users, admin_try_count_nonexistent_domain_active_users]. +admin_old_users_handler() -> + [admin_list_old_users_domain, + admin_try_list_old_users_nonexistent_domain, + admin_list_old_users_global, + admin_remove_old_users_domain, + admin_try_remove_old_users_nonexistent_domain, + admin_remove_old_users_global, + admin_user_without_last_info_is_old_user, + admin_logged_user_is_not_old_user]. + init_per_suite(Config) -> - Config1 = escalus:init_per_suite(Config), - Config2 = dynamic_modules:save_modules(domain_helper:host_type(), Config1), HostType = domain_helper:host_type(), + SecHostType = domain_helper:secondary_host_type(), + Config1 = escalus:init_per_suite(Config), + Config2 = dynamic_modules:save_modules(HostType, Config1), + Config3 = dynamic_modules:save_modules(SecHostType, Config2), Backend = mongoose_helper:get_backend_mnesia_rdbms_riak(HostType), + SecBackend = mongoose_helper:get_backend_mnesia_rdbms_riak(SecHostType), dynamic_modules:ensure_modules(HostType, required_modules(Backend)), - escalus:init_per_suite([{backend, Backend} | Config2]). - + dynamic_modules:ensure_modules(SecHostType, required_modules(SecBackend)), + escalus:init_per_suite(Config3). end_per_suite(Config) -> dynamic_modules:restore_modules(Config), @@ -54,17 +69,31 @@ end_per_suite(Config) -> init_per_group(admin_last, Config) -> graphql_helper:init_admin_handler(Config); +init_per_group(admin_last_old_users, Config) -> + graphql_helper:init_admin_handler(Config); init_per_group(user_last, Config) -> [{schema_endpoint, user} | Config]. -end_per_group(admin_last, _Config) -> - escalus_fresh:clean(); -end_per_group(user_last, _Config) -> +end_per_group(_, _Config) -> escalus_fresh:clean(). +init_per_testcase(C, Config) when C =:= admin_remove_old_users_domain; + C =:= admin_remove_old_users_global; + C =:= admin_list_old_users_domain; + C =:= admin_list_old_users_global; + C =:= admin_user_without_last_info_is_old_user -> + Config1 = escalus:create_users(Config, escalus:get_users([alice, bob, alice_bis])), + escalus:init_per_testcase(C, Config1); init_per_testcase(CaseName, Config) -> escalus:init_per_testcase(CaseName, Config). +end_per_testcase(C, Config) when C =:= admin_remove_old_users_domain; + C =:= admin_remove_old_users_global; + C =:= admin_list_old_users_domain; + C =:= admin_list_old_users_global; + C =:= admin_user_without_last_info_is_old_user -> + escalus:delete_users(Config, escalus:get_users([alice, bob, alice_bis])), + escalus:end_per_testcase(C, Config); end_per_testcase(CaseName, Config) -> escalus:end_per_testcase(CaseName, Config). @@ -145,6 +174,96 @@ admin_try_count_nonexistent_domain_active_users(Config) -> ?assertErrMsg(Res, <<"not found">>), ?assertErrCode(Res, domain_not_found). +admin_remove_old_users_domain(Config) -> + jids_with_config(Config, [alice, alice_bis, bob], fun admin_remove_old_users_domain/4). + +admin_remove_old_users_domain(Config, Alice, AliceBis, Bob) -> + Domain = domain_helper:domain(), + ToRemoveDateTime = now_dt_with_offset(100), + + set_last(Bob, ToRemoveDateTime, Config), + set_last(AliceBis, ToRemoveDateTime, Config), + set_last(Alice, now_dt_with_offset(200), Config), + + Resp = execute_auth(admin_remove_old_users_body(Domain, now_dt_with_offset(150)), Config), + [#{<<"jid">> := Bob, <<"timestamp">> := BobDateTimeRes}] = get_ok_value(p(removeOldUsers), Resp), + ?assertEqual(dt_to_unit(ToRemoveDateTime, second), dt_to_unit(BobDateTimeRes, second)), + ?assertMatch({user_does_not_exist, _}, check_account(Bob)), + ?assertMatch({ok, _}, check_account(Alice)), + ?assertMatch({ok, _}, check_account(AliceBis)). + +admin_try_remove_old_users_nonexistent_domain(Config) -> + Res = execute_auth(admin_remove_old_users_body(<<"nonexistent">>, now_dt_with_offset(0)), Config), + ?assertErrMsg(Res, <<"not found">>), + ?assertErrCode(Res, domain_not_found). + +admin_remove_old_users_global(Config) -> + jids_with_config(Config, [alice, alice_bis, bob], fun admin_remove_old_users_global/4). + +admin_remove_old_users_global(Config, Alice, AliceBis, Bob) -> + ToRemoveDateTime = now_dt_with_offset(100), + ToRemoveTimestamp = dt_to_unit(ToRemoveDateTime, second), + + set_last(Bob, ToRemoveDateTime, Config), + set_last(AliceBis, ToRemoveDateTime, Config), + set_last(Alice, now_dt_with_offset(200), Config), + Resp = execute_auth(admin_remove_old_users_body(null, now_dt_with_offset(150)), Config), + [#{<<"jid">> := AliceBis, <<"timestamp">> := AliceBisDateTime}, + #{<<"jid">> := Bob, <<"timestamp">> := BobDateTime}] = + lists:sort(get_ok_value(p(removeOldUsers), Resp)), + ?assertEqual(ToRemoveTimestamp, dt_to_unit(BobDateTime, second)), + ?assertEqual(ToRemoveTimestamp, dt_to_unit(AliceBisDateTime, second)), + ?assertMatch({user_does_not_exist, _}, check_account(Bob)), + ?assertMatch({user_does_not_exist, _}, check_account(AliceBis)), + ?assertMatch({ok, _}, check_account(Alice)). + +admin_list_old_users_domain(Config) -> + jids_with_config(Config, [alice, bob], fun admin_list_old_users_domain/3). + +admin_list_old_users_domain(Config, Alice, Bob) -> + Domain = domain_helper:domain(), + OldDateTime = now_dt_with_offset(100), + + set_last(Bob, OldDateTime, Config), + set_last(Alice, now_dt_with_offset(200), Config), + + Res = execute_auth(admin_list_old_users_body(Domain, now_dt_with_offset(150)), Config), + [#{<<"jid">> := Bob, <<"timestamp">> := BobDateTime}] = get_ok_value(p(listOldUsers), Res), + ?assertEqual(dt_to_unit(OldDateTime, second), dt_to_unit(BobDateTime, second)). + +admin_try_list_old_users_nonexistent_domain(Config) -> + Res = execute_auth(admin_list_old_users_body(<<"nonexistent">>, now_dt_with_offset(0)), Config), + ?assertErrMsg(Res, <<"not found">>), + ?assertErrCode(Res, domain_not_found). + +admin_list_old_users_global(Config) -> + jids_with_config(Config, [alice, alice_bis, bob], fun admin_list_old_users_global/4). + +admin_list_old_users_global(Config, Alice, AliceBis, Bob) -> + OldDateTime = now_dt_with_offset(100), + + set_last(Bob, OldDateTime, Config), + set_last(AliceBis, OldDateTime, Config), + set_last(Alice, now_dt_with_offset(200), Config), + + Res = execute_auth(admin_list_old_users_body(null, now_dt_with_offset(150)), Config), + [#{<<"jid">> := AliceBis, <<"timestamp">> := AliceBisDateTime}, + #{<<"jid">> := Bob, <<"timestamp">> := BobDateTime}] = + lists:sort(get_ok_value(p(listOldUsers), Res)), + ?assertEqual(dt_to_unit(OldDateTime, second), dt_to_unit(BobDateTime, second)), + ?assertEqual(dt_to_unit(OldDateTime, second), dt_to_unit(AliceBisDateTime, second)). + +admin_user_without_last_info_is_old_user(Config) -> + Res = execute_auth(admin_list_old_users_body(null, now_dt_with_offset(150)), Config), + ?assertEqual(3, length(get_ok_value(p(listOldUsers), Res))). + +admin_logged_user_is_not_old_user(Config) -> + escalus:fresh_story_with_config(Config, [{alice, 1}], fun admin_logged_user_is_not_old_user/2). + +admin_logged_user_is_not_old_user(Config, _Alice) -> + Res = execute_auth(admin_list_old_users_body(null, now_dt_with_offset(100)), Config), + ?assertEqual([], get_ok_value(p(listOldUsers), Res)). + %% User test cases user_set_last(Config) -> @@ -188,6 +307,17 @@ user_get_other_user_last(Config, Alice, Bob) -> %% Helpers +jids_with_config(Config, Users, Fun) -> + Args = [escalus_utils:jid_to_lower(escalus_users:get_jid(Config, User)) || User <- Users], + apply(Fun, [Config | Args]). + +set_last(UserJID, DateTime, Config) -> + execute_auth(admin_set_last_body(UserJID, <<>>, DateTime), Config). + +check_account(User) -> + {Username, LServer} = jid:to_lus(user_to_jid(User)), + rpc(mim(), mongoose_account_api, check_account, [Username, LServer]). + assert_err_msg(Contains, Res) -> ?assertNotEqual(nomatch, binary:match(get_err_msg(Res), Contains)). @@ -231,6 +361,20 @@ admin_count_active_users_body(Domain, Timestamp) -> Vars = #{domain => Domain, timestamp => Timestamp}, #{query => Query, operationName => OpName, variables => Vars}. +admin_remove_old_users_body(Domain, Timestamp) -> + Query = <<"mutation M1($domain: String, $timestamp: DateTime!) + { last { removeOldUsers(domain: $domain, timestamp: $timestamp) { jid timestamp } } }">>, + OpName = <<"M1">>, + Vars = #{domain => Domain, timestamp => Timestamp}, + #{query => Query, operationName => OpName, variables => Vars}. + +admin_list_old_users_body(Domain, Timestamp) -> + Query = <<"query Q1($domain: String, $timestamp: DateTime!) + { last { listOldUsers(domain: $domain, timestamp: $timestamp) { jid timestamp } } }">>, + OpName = <<"Q1">>, + Vars = #{domain => Domain, timestamp => Timestamp}, + #{query => Query, operationName => OpName, variables => Vars}. + user_set_last_body(Status, DateTime) -> Query = <<"mutation M1($timestamp: DateTime, $status: String!) { last { setLast (timestamp: $timestamp, status: $status)