Skip to content

Commit

Permalink
Add tests for both admin and user
Browse files Browse the repository at this point in the history
  • Loading branch information
Premwoik committed Jun 28, 2022
1 parent a1e9173 commit de87049
Show file tree
Hide file tree
Showing 3 changed files with 182 additions and 0 deletions.
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_inbox_SUITE}.
{suites, "tests", graphql_last_SUITE}.
{suites, "tests", graphql_muc_SUITE}.
{suites, "tests", graphql_muc_light_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_inbox_SUITE}.
{suites, "tests", graphql_last_SUITE}.
{suites, "tests", graphql_muc_SUITE}.
{suites, "tests", graphql_muc_light_SUITE}.
Expand Down
180 changes: 180 additions & 0 deletions big_tests/tests/graphql_inbox_SUITE.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
-module(graphql_inbox_SUITE).

-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, user_to_jid/1,
get_ok_value/2, get_err_msg/1, get_err_code/1]).

-include_lib("eunit/include/eunit.hrl").

-define(assertErrMsg(Res, ContainsPart), assert_err_msg(ContainsPart, Res)).
-define(assertErrCode(Res, Code), assert_err_code(Code, Res)).

suite() ->
require_rpc_nodes([mim]) ++ escalus:suite().

all() ->
inbox_helper:skip_or_run_inbox_tests(tests()).

tests() ->
[{group, user_inbox},
{group, admin_inbox}].

groups() ->
[{user_inbox, [], user_inbox_handler()},
{admin_inbox, [], admin_inbox_handler()}].

user_inbox_handler() ->
[user_flush_own_bin].

admin_inbox_handler() ->
[admin_flush_user_bin,
admin_try_flush_nonexistent_user_bin,
admin_flush_domain_bin,
admin_try_flush_nonexistent_domain_bin,
admin_flush_global_bin,
admin_try_flush_nonexistent_host_type_bin].

init_per_suite(Config) ->
HostType = domain_helper:host_type(),
SecHostType = domain_helper:secondary_host_type(),
Config1 = dynamic_modules:save_modules([HostType, SecHostType], Config),
Modules = inbox_helper:inbox_modules(async_pools) ++ inbox_helper:muclight_modules(),
ok = dynamic_modules:ensure_modules(HostType, Modules),
ok = dynamic_modules:ensure_modules(SecHostType, Modules),
escalus:init_per_suite(Config1).

end_per_suite(Config) ->
dynamic_modules:restore_modules(Config),
escalus:end_per_suite(Config).

init_per_group(admin_inbox, Config) ->
graphql_helper:init_admin_handler(Config);
init_per_group(user_inbox, Config) ->
[{schema_endpoint, user} | Config].

end_per_group(_, _Config) ->
escalus_fresh:clean().

init_per_testcase(CaseName, Config) ->
escalus:init_per_testcase(CaseName, Config).

end_per_testcase(CaseName, Config) ->
escalus:end_per_testcase(CaseName, Config).

%% Admin test cases

admin_flush_user_bin(Config) ->
escalus:fresh_story_with_config(Config, [{alice, 1}, {bob, 1}, {kate, 1}],
fun admin_flush_user_bin/4).

admin_flush_user_bin(Config, Alice, Bob, Kate) ->
inbox_SUITE:create_room_and_make_users_leave(Alice, Bob, Kate),
Res = execute_auth(admin_flush_user_bin_body(Bob, 0), Config),
NumOfRows = get_ok_value(p(flushUserBin), Res),
?assertEqual(1, NumOfRows),
inbox_helper:check_inbox(Bob, [], #{box => bin}).

admin_flush_domain_bin(Config) ->
escalus:fresh_story_with_config(Config, [{alice, 1}, {alice_bis, 1}, {kate, 1}],
fun admin_flush_domain_bin/4).

admin_flush_domain_bin(Config, Alice, AliceBis, Kate) ->
Domain = domain_helper:domain(),
inbox_SUITE:create_room_and_make_users_leave(Alice, AliceBis, Kate),
Res = execute_auth(admin_flush_domain_bin_body(Domain, 0), Config),
NumOfRows = get_ok_value(p(flushDomainBin), Res),
?assertEqual(2, NumOfRows),
inbox_helper:check_inbox(Alice, [], #{box => bin}),
inbox_helper:check_inbox(Kate, [], #{box => bin}).

admin_flush_global_bin(Config) ->
escalus:fresh_story_with_config(Config, [{alice, 1}, {alice_bis, 1}, {kate, 1}],
fun admin_flush_global_bin/4).

admin_flush_global_bin(Config, Alice, AliceBis, Kate) ->
SecHostType = domain_helper:host_type(),
inbox_SUITE:create_room_and_make_users_leave(Alice, AliceBis, Kate),
Res = execute_auth(admin_flush_global_bin_body(SecHostType, 0), Config),
NumOfRows = get_ok_value(p(flushGlobalBin), Res),
?assertEqual(3, NumOfRows),
inbox_helper:check_inbox(Alice, [], #{box => bin}),
inbox_helper:check_inbox(AliceBis, [], #{box => bin}),
inbox_helper:check_inbox(Kate, [], #{box => bin}).

admin_try_flush_nonexistent_user_bin(Config) ->
%% Check nonexistent domain error
Res = execute_auth(admin_flush_user_bin_body(<<"[email protected]">>, 0), Config),
?assertErrMsg(Res, <<"not found">>),
?assertErrCode(Res, domain_not_found),
%% Check nonexistent user error
User = <<"nonexistent-user@", (domain_helper:domain())/binary>>,
Res2 = execute_auth(admin_flush_user_bin_body(User, 0), Config),
?assertErrMsg(Res2, <<"does not exist">>),
?assertErrCode(Res2, user_does_not_exist).

admin_try_flush_nonexistent_domain_bin(Config) ->
Res = execute_auth(admin_flush_domain_bin_body(<<"unknown-domain">>, 0), Config),
?assertErrMsg(Res, <<"not found">>),
?assertErrCode(Res, domain_not_found).

admin_try_flush_nonexistent_host_type_bin(Config) ->
Res = execute_auth(admin_flush_global_bin_body(<<"nonexistent host type">>, 0), Config),
?assertErrMsg(Res, <<"not found">>),
?assertErrCode(Res, host_type_not_found).

%% User test cases

user_flush_own_bin(Config) ->
escalus:fresh_story_with_config(Config, [{alice, 1}, {bob, 1}, {kate, 1}],
fun user_flush_own_bin/4).

user_flush_own_bin(Config, Alice, Bob, Kate) ->
inbox_SUITE:create_room_and_make_users_leave(Alice, Bob, Kate),
Res = execute_user(user_flush_own_bin_body(0), Bob, Config),
NumOfRows = get_ok_value(p(flushBin), Res),
?assertEqual(1, NumOfRows),
inbox_helper:check_inbox(Bob, [], #{box => bin}).

%% Helpers

assert_err_msg(Contains, Res) ->
?assertNotEqual(nomatch, binary:match(get_err_msg(Res), Contains)).

assert_err_code(Code, Res) ->
?assertEqual(atom_to_binary(Code), get_err_code(Res)).

p(Cmd) when is_atom(Cmd) ->
[data, inbox, Cmd];
p(Path) when is_list(Path) ->
[data, inbox] ++ Path.

%% Request bodies

admin_flush_user_bin_body(User, Days) ->
Query = <<"mutation M1($user: JID!, $days: Int!)
{ inbox { flushUserBin(user: $user, days: $days) } }">>,
OpName = <<"M1">>,
Vars = #{user => user_to_bin(User), days => Days},
#{query => Query, operationName => OpName, variables => Vars}.

admin_flush_domain_bin_body(Domain, Days) ->
Query = <<"mutation M1($domain: String!, $days: Int!)
{ inbox { flushDomainBin(domain: $domain, days: $days) } }">>,
OpName = <<"M1">>,
Vars = #{domain => Domain, days => Days},
#{query => Query, operationName => OpName, variables => Vars}.

admin_flush_global_bin_body(HostType, Days) ->
Query = <<"mutation M1($hostType: String!, $days: Int!)
{ inbox { flushGlobalBin(hostType: $hostType, days: $days) } }">>,
OpName = <<"M1">>,
Vars = #{hostType => HostType, days => Days},
#{query => Query, operationName => OpName, variables => Vars}.

user_flush_own_bin_body(Days) ->
Query = <<"mutation M1($days: Int!) { inbox { flushBin(days: $days) } }">>,
OpName = <<"M1">>,
Vars = #{days => Days},
#{query => Query, operationName => OpName, variables => Vars}.

0 comments on commit de87049

Please sign in to comment.