From 27bcfdd3038c4d31db785508608a09db69f2ec09 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Chrz=C4=85szcz?= Date: Fri, 21 Oct 2022 12:39:59 +0200 Subject: [PATCH] Prevent get_last_messages from crashing when MAM is disabled --- big_tests/tests/graphql_stanza_SUITE.erl | 91 ++++++++++++++++-------- priv/graphql/schemas/admin/stanza.gql | 3 +- priv/graphql/schemas/user/stanza.gql | 3 +- 3 files changed, 65 insertions(+), 32 deletions(-) diff --git a/big_tests/tests/graphql_stanza_SUITE.erl b/big_tests/tests/graphql_stanza_SUITE.erl index e3e76909cb..08d27b26b7 100644 --- a/big_tests/tests/graphql_stanza_SUITE.erl +++ b/big_tests/tests/graphql_stanza_SUITE.erl @@ -9,7 +9,7 @@ -import(distributed_helper, [mim/0, require_rpc_nodes/1]). -import(graphql_helper, [execute_user_command/5, execute_command/4, get_ok_value/2, get_err_code/1, get_err_msg/1, get_coercion_err_msg/1, - get_unauthorized/1]). + get_unauthorized/1, get_not_loaded/1]). suite() -> require_rpc_nodes([mim]) ++ escalus:suite(). @@ -21,10 +21,19 @@ all() -> {group, user_stanza}]. groups() -> - [{admin_stanza_http, [parallel], admin_stanza_cases()}, - {admin_stanza_cli, [], admin_stanza_cases()}, - {domain_admin_stanza, [], domain_admin_stanza_cases()}, - {user_stanza, [parallel], user_stanza_cases()}]. + [{admin_stanza_http, [], [{group, admin_mam}, + {group, admin_no_mam}]}, + {admin_stanza_cli, [], [{group, admin_mam}, + {group, admin_no_mam}]}, + {domain_admin_stanza, [], [{group, admin_mam}, % same as for admin + {group, domain_admin_no_mam}]}, + {user_stanza, [], [{group, user_mam}, + {group, user_no_mam}]}, + {admin_mam, [parallel], admin_mam_cases()}, + {admin_no_mam, [parallel], admin_stanza_cases() ++ admin_no_mam_cases()}, + {domain_admin_no_mam, [parallel], domain_admin_stanza_cases() ++ admin_no_mam_cases()}, + {user_mam, [parallel], user_mam_cases()}, + {user_no_mam, [parallel], user_stanza_cases() ++ user_no_mam_cases()}]. admin_stanza_cases() -> [admin_send_message, @@ -33,10 +42,9 @@ admin_stanza_cases() -> admin_send_stanza, admin_send_unparsable_stanza, admin_send_stanza_from_unknown_user, - admin_send_stanza_from_unknown_domain] - ++ admin_get_last_messages_cases(). + admin_send_stanza_from_unknown_domain]. -admin_get_last_messages_cases() -> +admin_mam_cases() -> [admin_get_last_messages, admin_get_last_messages_for_unknown_user, admin_get_last_messages_with, @@ -44,6 +52,9 @@ admin_get_last_messages_cases() -> admin_get_last_messages_limit_enforced, admin_get_last_messages_before]. +admin_no_mam_cases() -> + [admin_get_last_messages_no_mam]. + domain_admin_stanza_cases() -> [admin_send_message, admin_send_message_to_unparsable_jid, @@ -53,8 +64,7 @@ domain_admin_stanza_cases() -> admin_send_unparsable_stanza, domain_admin_send_stanza_from_unknown_user, domain_admin_send_stanza_from_unknown_domain, - domain_admin_get_last_messages_no_permission] - ++ admin_get_last_messages_cases(). + domain_admin_get_last_messages_no_permission]. user_stanza_cases() -> [user_send_message, @@ -64,17 +74,18 @@ user_stanza_cases() -> user_send_message_headline_with_spoofed_from, user_send_stanza, user_send_stanza_without_from_with_id, - user_send_stanza_with_spoofed_from] - ++ user_get_last_messages_cases(). + user_send_stanza_with_spoofed_from]. -user_get_last_messages_cases() -> +user_mam_cases() -> [user_get_last_messages]. +user_no_mam_cases() -> + [user_get_last_messages_no_mam]. + init_per_suite(Config) -> Config1 = ejabberd_node_utils:init(mim(), Config), Config2 = escalus:init_per_suite(Config1), - dynamic_modules:save_modules(domain_helper:host_type(), Config2), - init_mam(Config2). + dynamic_modules:save_modules(domain_helper:host_type(), Config2). end_per_suite(Config) -> escalus_fresh:clean(), @@ -88,22 +99,28 @@ init_per_group(admin_stanza_cli, Config) -> init_per_group(domain_admin_stanza, Config) -> graphql_helper:init_domain_admin_handler(Config); init_per_group(user_stanza, Config) -> - graphql_helper:init_user(Config). - -end_per_group(_, _Config) -> - graphql_helper:clean(). + graphql_helper:init_user(Config); +init_per_group(GN, Config) when GN =:= admin_mam; + GN =:= domain_admin_mam; + GN =:= user_mam -> + init_mam(Config); +init_per_group(GN, Config) when GN =:= admin_no_mam; + GN =:= domain_admin_no_mam; + GN =:= user_no_mam -> + Mods = [{mod_mam, stopped}], + dynamic_modules:ensure_modules(domain_helper:host_type(), Mods), + Config. + +end_per_group(GN, _Config) when GN =:= admin_stanza_http; + GN =:= admin_stanza_cli; + GN =:= domain_admin_stanza; + GN =:= user_stanza -> + graphql_helper:clean(); +end_per_group(_GN, _Config) -> + ok. init_per_testcase(CaseName, Config) -> - HasMam = proplists:get_value(has_mam, Config, false), - MamOnly = lists:member(CaseName, - user_get_last_messages_cases() - ++ admin_get_last_messages_cases()), - case {HasMam, MamOnly} of - {false, true} -> - {skip, no_mam}; - _ -> - escalus:init_per_testcase(CaseName, Config) - end. + escalus:init_per_testcase(CaseName, Config). end_per_testcase(CaseName, Config) -> escalus:end_per_testcase(CaseName, Config). @@ -111,7 +128,7 @@ end_per_testcase(CaseName, Config) -> init_mam(Config) when is_list(Config) -> case mam_helper:backend() of disabled -> - Config; + {skip, no_mam}; Backend -> MAMOpts = #{max_result_limit := MaxResultLimit} = mam_helper:config_opts(#{backend => Backend, pm => #{}}), @@ -364,6 +381,12 @@ admin_get_last_messages_story(Config, Alice, Bob) -> get_ok_value([data, stanza, getLastMessages], Res2), check_stanza_map(M2, Alice). +admin_get_last_messages_no_mam(Config) -> + FreshConfig = escalus_fresh:create_users(Config, [{alice, 1}]), + AliceJid = escalus_users:get_jid(FreshConfig, alice), + Res = get_last_messages(AliceJid, null, null, Config), + get_not_loaded(Res). + user_get_last_messages(Config) -> escalus:fresh_story_with_config(Config, [{alice, 1}, {bob, 1}], fun user_get_last_messages_story/3). @@ -381,6 +404,14 @@ user_get_last_messages_story(Config, Alice, Bob) -> get_ok_value([data, stanza, getLastMessages], Res2), check_stanza_map(M2, Alice). +user_get_last_messages_no_mam(Config) -> + escalus:fresh_story_with_config(Config, [{alice, 1}], + fun user_get_last_messages_no_mam_story/2). + +user_get_last_messages_no_mam_story(Config, Alice) -> + Res = user_get_last_messages(Alice, null, null, Config), + get_not_loaded(Res). + admin_get_last_messages_for_unknown_user(Config) -> Domain = domain_helper:domain(), Jid = <<"maybemaybebutnot@", Domain/binary>>, diff --git a/priv/graphql/schemas/admin/stanza.gql b/priv/graphql/schemas/admin/stanza.gql index 151c1629b8..460b173370 100644 --- a/priv/graphql/schemas/admin/stanza.gql +++ b/priv/graphql/schemas/admin/stanza.gql @@ -3,8 +3,9 @@ type StanzaAdminQuery @protected{ Get last 50 messages to/from a given contact, optionally you can change the limit, specify a date or select only messages exchanged with a specific contact """ - getLastMessages(caller: JID!, with: JID, limit: Int = 50, before: DateTime): StanzasPayload + getLastMessages(caller: JID!, with: JID, limit: PosInt = 50, before: DateTime): StanzasPayload @protected(type: DOMAIN, args: ["caller"]) + @use(modules: ["mod_mam_pm"], arg: "caller") } type StanzaAdminMutation @protected{ diff --git a/priv/graphql/schemas/user/stanza.gql b/priv/graphql/schemas/user/stanza.gql index 66fba77d51..47e7859028 100644 --- a/priv/graphql/schemas/user/stanza.gql +++ b/priv/graphql/schemas/user/stanza.gql @@ -3,7 +3,8 @@ Allow user to query MAM archive. """ type StanzaUserQuery @protected{ "Get n last messages to/from a given contact (optional) with limit and optional date" - getLastMessages(with: JID, limit: Int = 50, before: DateTime): StanzasPayload + getLastMessages(with: JID, limit: PosInt = 50, before: DateTime): StanzasPayload + @use(modules: ["mod_mam_pm"]) } """