-
Notifications
You must be signed in to change notification settings - Fork 426
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
298 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
-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)). | ||
|
||
-define(NONEXISTENT_JID, <<"[email protected]">>). | ||
-define(DEFAULT_DT, <<"2022-04-17T12:58:30.000000Z">>). | ||
|
||
suite() -> | ||
require_rpc_nodes([mim]) ++ escalus:suite(). | ||
|
||
all() -> | ||
[{group, user_inbox}, | ||
{group, admin_inbox}]. | ||
|
||
groups() -> | ||
[{user_inbox, [parallel], user_inbox_handler()}, | ||
{admin_last, [parallel], 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(), | ||
Config1 = dynamic_modules:save_modules([HostType], Config), | ||
InboxOptions = inbox_helper:inbox_opts(regular), | ||
ok = dynamic_modules:ensure_modules(HostType, [{mod_inbox, InboxOptions}]), | ||
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}], | ||
fun admin_flush_user_bin/2). | ||
|
||
admin_flush_user_bin(Config, Alice) -> | ||
%% TODO add test body | ||
?assert(false). | ||
|
||
admin_flush_domain_bin(Config) -> | ||
escalus:fresh_story_with_config(Config, [{alice, 1}], | ||
fun admin_flush_domain_bin/2). | ||
|
||
admin_flush_domain_bin(Config, Alice) -> | ||
%% TODO add test body | ||
?assert(false). | ||
|
||
admin_flush_global_bin(Config) -> | ||
escalus:fresh_story_with_config(Config, [{alice, 1}], | ||
fun admin_flush_global_bin/2). | ||
|
||
admin_flush_global_bin(Config, Alice) -> | ||
%% TODO add test body | ||
?assert(false). | ||
|
||
admin_try_flush_nonexistent_user_bin(Config) -> | ||
%% TODO add test body | ||
?assert(false). | ||
|
||
admin_try_flush_nonexistent_domain_bin(Config) -> | ||
%% TODO add test body | ||
?assert(false). | ||
|
||
admin_try_flush_nonexistent_host_type_bin(Config) -> | ||
%% TODO add test body | ||
?assert(false). | ||
|
||
|
||
%% User test cases | ||
|
||
user_flush_own_bin(Config) -> | ||
escalus:fresh_story_with_config(Config, [{alice, 1}], | ||
fun user_flush_own_bin/2). | ||
|
||
user_flush_own_bin(Config, Alice) -> | ||
%% TODO add test body | ||
?assert(false). | ||
|
||
%% 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 { flushDomainBin(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: Days) { inbox { flushBin(days: Int!) } }">>, | ||
OpName = <<"M1">>, | ||
Vars = #{days => Days}, | ||
#{query => Query, operationName => OpName, variables => Vars}. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
""" | ||
Allow admin to flush the inbox bin". | ||
""" | ||
type InboxAdminMutation @protected{ | ||
"Flush the user's bin and return deleted rows number" | ||
flushUserBin(user: JID!, days: Int!): Int | ||
@protected(type: DOMAIN, args: ["user"]) | ||
"Flush the whole domain bin and return deleted rows number" | ||
flushDomainBin(domain: String!, days: Int!): Int | ||
@protected(type: Domain, args: ["domain"]) | ||
"Flush the whole host type bin and return deleted rows number" | ||
flushGlobalBin(hostType: String!, days: Int!): Int | ||
@protected(type: GLOBAL) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
""" | ||
Allow user to flush own inbox bin". | ||
""" | ||
type InboxUserMutation @protected{ | ||
"Flush the user's bin and return deleted rows number" | ||
flushBin(Days: Int!): Int | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
src/graphql/admin/mongoose_graphql_inbox_admin_mutation.erl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
-module(mongoose_graphql_inbox_admin_mutation). | ||
|
||
-behaviour(mongoose_graphql). | ||
|
||
-export([execute/4]). | ||
|
||
-import(mongoose_graphql_helper, [format_result/2]). | ||
|
||
-ignore_xref([execute/4]). | ||
|
||
execute(_Ctx, inbox, <<"flushUserBin">>, #{<<"user">> := UserJID, <<"days">> := Days}) -> | ||
Res = mod_inbox_api:flush_user_bin(UserJID, Days), | ||
format_result(Res, #{user => jid:to_binary(UserJID)}); | ||
execute(_Ctx, inbox, <<"flushDomainBin">>, #{<<"domain">> := Domain, <<"days">> := Days}) -> | ||
Res = mod_offline_api:delete_expired_messages(Domain, Days), | ||
format_result(Res, #{domain => Domain}); | ||
execute(_Ctx, inbox, <<"flushGlobalBin">>, #{<<"hostType">> := HostType, <<"days">> := Days}) -> | ||
Res = mod_inbox_api:flush_global_bin(HostType, Days), | ||
format_result(Res, #{host_type => HostType}). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
-module(mongoose_graphql_inbox_user_mutation). | ||
|
||
-behaviour(mongoose_graphql). | ||
|
||
-export([execute/4]). | ||
|
||
-import(mongoose_graphql_helper, [format_result/2]). | ||
|
||
-ignore_xref([execute/4]). | ||
|
||
execute(#{user := UserJID}, inbox, <<"flushBin">>, #{<<"days">> := Days}) -> | ||
Res = mod_inbox_api:flush_user_bin(UserJID, Days), | ||
format_result(Res, #{user => jid:to_binary(UserJID)}). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
-module(mod_inbox_api). | ||
|
||
-export([flush_user_bin/2, flush_domain_bin/2, flush_global_bin/2]). | ||
|
||
-include_lib("jid/include/jid.hrl"). | ||
|
||
-spec flush_user_bin(jid:jid(), Days :: integer()) -> | ||
{ok, integer()} | {domain_not_found, binary()}. | ||
flush_user_bin(#jid{luser = LU, lserver = LS} = JID, Days) -> | ||
case mongoose_domain_api:get_host_type(LS) of | ||
{ok, HostType} -> | ||
case ejabberd_auth:does_user_exist(JID) of | ||
true -> | ||
FromTS = days_to_timestamp(Days), | ||
Count = mod_inbox_backend:empty_user_bin(HostType, LS, LU, FromTS), | ||
{ok, Count}; | ||
false -> | ||
{user_not_found, <<"User does not exists">>} | ||
end; | ||
{error, not_found} -> | ||
{domain_not_found, <<"Domain not found">>} | ||
end. | ||
|
||
-spec flush_domain_bin(jid:server(), Days :: integer()) -> | ||
{ok, integer()} | {domain_not_found, iodata()}. | ||
flush_domain_bin(Domain, Days) -> | ||
LDomain = jid:nodeprep(Domain), | ||
case mongoose_domain_api:get_host_type(LDomain) of | ||
{ok, HostType} -> | ||
FromTS = days_to_timestamp(Days), | ||
Count = mod_inbox_backend:empty_domain_bin(HostType, Domain, FromTS), | ||
{ok, Count}; | ||
{host_type_not_found, _} = Error -> | ||
Error | ||
end. | ||
|
||
-spec flush_global_bin(mongooseim:host_type(), Days :: integer()) -> | ||
{ok, integer()} | {host_type_not_found, binary()}. | ||
flush_global_bin(HostType, Days) -> | ||
case validate_host_type(HostType) of | ||
ok -> | ||
FromTS = days_to_timestamp(Days), | ||
Count = mod_inbox_backend:empty_global_bin(HostType, FromTS), | ||
{ok, Count}; | ||
{host_type_not_found, _} = Error -> | ||
Error | ||
end. | ||
|
||
%% Internal | ||
|
||
validate_host_type(HostType) -> | ||
HostTypes = mongoose_config:get_opt(host_types), | ||
case lists:member(HostType, HostTypes) of | ||
true -> | ||
ok; | ||
false -> | ||
{host_type_not_found, <<"Host type not found">>} | ||
end. | ||
|
||
days_to_timestamp(Days) -> | ||
Now = erlang:system_time(microsecond), | ||
mod_inbox_utils:calculate_ts_from(Now, Days). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.