Skip to content

Commit

Permalink
Update GraphQL tests with the new mongoose_stanza_api
Browse files Browse the repository at this point in the history
- The Id is a stanza Id now.
- It is possible to send a stanza with predefined Id and/or without 'from'.
- Update error messages.
  • Loading branch information
chrzaszcz committed Oct 21, 2022
1 parent 22bf907 commit d1ea3fc
Showing 1 changed file with 46 additions and 35 deletions.
81 changes: 46 additions & 35 deletions big_tests/tests/graphql_stanza_SUITE.erl
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
-module(graphql_stanza_SUITE).

-include_lib("common_test/include/ct.hrl").
-include_lib("eunit/include/eunit.hrl").
-include_lib("exml/include/exml.hrl").

Expand Down Expand Up @@ -62,6 +63,7 @@ user_stanza_cases() ->
user_send_message_headline,
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().

Expand Down Expand Up @@ -111,9 +113,10 @@ init_mam(Config) when is_list(Config) ->
disabled ->
Config;
Backend ->
Mods = [{mod_mam, mam_helper:config_opts(#{backend => Backend, pm => #{}})}],
MAMOpts = mam_helper:config_opts(#{backend => Backend, pm => #{}}),
Mods = [{mod_mam, MAMOpts}],
dynamic_modules:ensure_modules(domain_helper:host_type(), Mods),
[{has_mam, true}|Config]
Config
end;
init_mam(Other) ->
Other.
Expand All @@ -128,8 +131,8 @@ admin_send_message_story(Config, Alice, Bob) ->
From = escalus_client:full_jid(Alice),
To = escalus_client:short_jid(Bob),
Res = send_message(From, To, <<"Hi!">>, Config),
#{<<"id">> := MamID} = get_ok_value([data, stanza, sendMessage], Res),
assert_not_empty(MamID, Config),
#{<<"id">> := StanzaId} = get_ok_value([data, stanza, sendMessage], Res),
assert_not_empty(StanzaId),
escalus:assert(is_message, escalus:wait_for_stanza(Bob)).

user_send_message(Config) ->
Expand All @@ -140,8 +143,8 @@ user_send_message_story(Config, Alice, Bob) ->
From = escalus_client:full_jid(Alice),
To = escalus_client:short_jid(Bob),
Res = user_send_message(Alice, From, To, <<"Hi!">>, Config),
#{<<"id">> := MamID} = get_ok_value([data, stanza, sendMessage], Res),
assert_not_empty(MamID, Config),
#{<<"id">> := StanzaId} = get_ok_value([data, stanza, sendMessage], Res),
assert_not_empty(StanzaId),
escalus:assert(is_message, escalus:wait_for_stanza(Bob)).

user_send_message_without_from(Config) ->
Expand All @@ -151,8 +154,8 @@ user_send_message_without_from(Config) ->
user_send_message_without_from_story(Config, Alice, Bob) ->
To = escalus_client:short_jid(Bob),
Res = user_send_message(Alice, null, To, <<"Hi!">>, Config),
#{<<"id">> := MamID} = get_ok_value([data, stanza, sendMessage], Res),
assert_not_empty(MamID, Config),
#{<<"id">> := StanzaId} = get_ok_value([data, stanza, sendMessage], Res),
assert_not_empty(StanzaId),
escalus:assert(is_message, escalus:wait_for_stanza(Bob)).

user_send_message_with_spoofed_from(Config) ->
Expand Down Expand Up @@ -184,9 +187,8 @@ admin_send_message_headline_story(Config, Alice, Bob) ->
From = escalus_client:full_jid(Alice),
To = escalus_client:short_jid(Bob),
Res = send_message_headline(From, To, <<"Welcome">>, <<"Hi!">>, Config),
#{<<"id">> := MamID} = get_ok_value([data, stanza, sendMessageHeadLine], Res),
% Headlines are not stored in MAM
<<>> = MamID,
#{<<"id">> := StanzaId} = get_ok_value([data, stanza, sendMessageHeadLine], Res),
assert_not_empty(StanzaId),
escalus:assert(is_message, escalus:wait_for_stanza(Bob)).

user_send_message_headline(Config) ->
Expand All @@ -197,9 +199,8 @@ user_send_message_headline_story(Config, Alice, Bob) ->
From = escalus_client:full_jid(Alice),
To = escalus_client:short_jid(Bob),
Res = user_send_message_headline(Alice, From, To, <<"Welcome">>, <<"Hi!">>, Config),
#{<<"id">> := MamID} = get_ok_value([data, stanza, sendMessageHeadLine], Res),
% Headlines are not stored in MAM
<<>> = MamID,
#{<<"id">> := StanzaId} = get_ok_value([data, stanza, sendMessageHeadLine], Res),
assert_not_empty(StanzaId),
escalus:assert(is_message, escalus:wait_for_stanza(Bob)).

user_send_message_headline_with_spoofed_from(Config) ->
Expand Down Expand Up @@ -270,9 +271,11 @@ admin_send_stanza_story(Config, Alice, Bob) ->
Body = <<"Hi!">>,
Stanza = escalus_stanza:from(escalus_stanza:chat_to_short_jid(Bob, Body), Alice),
Res = send_stanza(exml:to_binary(Stanza), Config),
#{<<"id">> := MamID} = get_ok_value([data, stanza, sendStanza], Res),
assert_not_empty(MamID, Config),
escalus:assert(is_message, escalus:wait_for_stanza(Bob)).
#{<<"id">> := StanzaId} = get_ok_value([data, stanza, sendStanza], Res),
assert_not_empty(StanzaId),
StanzaIn = escalus:wait_for_stanza(Bob),
escalus:assert(is_message, StanzaIn),
?assertEqual(StanzaId, exml_query:attr(StanzaIn, <<"id">>)).

user_send_stanza(Config) ->
escalus:fresh_story_with_config(Config, [{alice, 1}, {bob, 1}],
Expand All @@ -282,9 +285,25 @@ user_send_stanza_story(Config, Alice, Bob) ->
Body = <<"Hi!">>,
Stanza = escalus_stanza:from(escalus_stanza:chat_to_short_jid(Bob, Body), Alice),
Res = user_send_stanza(Alice, exml:to_binary(Stanza), Config),
#{<<"id">> := MamID} = get_ok_value([data, stanza, sendStanza], Res),
assert_not_empty(MamID, Config),
escalus:assert(is_message, escalus:wait_for_stanza(Bob)).
#{<<"id">> := StanzaId} = get_ok_value([data, stanza, sendStanza], Res),
assert_not_empty(StanzaId),
StanzaIn = escalus:wait_for_stanza(Bob),
escalus:assert(is_message, StanzaIn),
?assertEqual(StanzaId, exml_query:attr(StanzaIn, <<"id">>)).

user_send_stanza_without_from_with_id(Config) ->
escalus:fresh_story_with_config(Config, [{alice, 1}, {bob, 1}],
fun user_send_stanza_without_from_with_id_story/3).

user_send_stanza_without_from_with_id_story(Config, Alice, Bob) ->
Body = <<"Hi!">>,
StanzaId = base16:encode(crypto:strong_rand_bytes(8)),
Stanza = escalus_stanza:set_id(escalus_stanza:chat_to_short_jid(Bob, Body), StanzaId),
Res = user_send_stanza(Alice, exml:to_binary(Stanza), Config),
?assertEqual(#{<<"id">> => StanzaId}, get_ok_value([data, stanza, sendStanza], Res)),
StanzaIn = escalus:wait_for_stanza(Bob),
escalus:assert(is_message, StanzaIn),
?assertEqual(StanzaId, exml_query:attr(StanzaIn, <<"id">>)).

user_send_stanza_with_spoofed_from(Config) ->
escalus:fresh_story_with_config(Config, [{alice, 1}, {bob, 1}],
Expand Down Expand Up @@ -313,7 +332,7 @@ admin_send_stanza_from_unknown_user_story(Config, Bob) ->
Stanza = escalus_stanza:from(escalus_stanza:chat_to_short_jid(Bob, Body), From),
Res = send_stanza(exml:to_binary(Stanza), Config),
?assertEqual(<<"unknown_user">>, get_err_code(Res)),
?assertEqual(<<"Given user does not exist">>, get_err_msg(Res)).
?assertEqual(<<"User does not exist">>, get_err_msg(Res)).

admin_send_stanza_from_unknown_domain(Config) ->
escalus:fresh_story_with_config(Config, [{bob, 1}],
Expand All @@ -324,8 +343,8 @@ admin_send_stanza_from_unknown_domain_story(Config, Bob) ->
From = <<"YeeeAH@oopsie">>,
Stanza = escalus_stanza:from(escalus_stanza:chat_to_short_jid(Bob, Body), From),
Res = send_stanza(exml:to_binary(Stanza), Config),
?assertEqual(<<"unknown_domain">>, get_err_code(Res)),
?assertEqual(<<"Given domain does not exist">>, get_err_msg(Res)).
?assertEqual(<<"unknown_user">>, get_err_code(Res)),
?assertEqual(<<"User's domain does not exist">>, get_err_msg(Res)).

admin_get_last_messages(Config) ->
escalus:fresh_story_with_config(Config, [{alice, 1}, {bob, 1}],
Expand Down Expand Up @@ -366,7 +385,7 @@ admin_get_last_messages_for_unknown_user(Config) ->
Jid = <<"maybemaybebutnot@", Domain/binary>>,
Res = get_last_messages(Jid, null, null, Config),
?assertEqual(<<"unknown_user">>, get_err_code(Res)),
?assertEqual(<<"Given user does not exist">>, get_err_msg(Res)).
?assertEqual(<<"User does not exist">>, get_err_msg(Res)).

admin_get_last_messages_with(Config) ->
escalus:fresh_story_with_config(Config, [{alice, 1}, {bob, 1}, {kate, 1}],
Expand Down Expand Up @@ -409,7 +428,7 @@ admin_get_last_messages_limit_enforced_story(Config, Alice, Bob) ->
Caller = escalus_client:full_jid(Alice),
Res = get_last_messages(Caller, null, 1000, null, Config),
%% The actual limit is returned
#{<<"stanzas">> := [M1], <<"limit">> := 500} =
#{<<"stanzas">> := [M1], <<"limit">> := 50} =
get_ok_value([data, stanza, getLastMessages], Res),
check_stanza_map(M1, Alice).

Expand Down Expand Up @@ -473,14 +492,6 @@ user_get_last_messages(User, With, Before, Config) ->

%% Helpers

assert_not_empty(Bin, Config) ->
case proplists:get_value(has_mam, Config) of
true ->
assert_not_empty(Bin);
_ ->
skip
end.

assert_not_empty(Bin) when byte_size(Bin) > 0 -> ok.

ok_result(What1, What2, {{<<"200">>, <<"OK">>}, #{<<"data">> := Data}}) ->
Expand All @@ -498,5 +509,5 @@ check_stanza_map(#{<<"sender">> := SenderJID,

spoofed_error(Call, Response) ->
null = graphql_helper:get_err_value([data, stanza, Call], Response),
?assertEqual(<<"Sending from this JID is not allowed">>, get_err_msg(Response)),
?assertEqual(<<"bad_from_jid">>, get_err_code(Response)).
?assertEqual(<<"Sender's JID is different from the user's JID">>, get_err_msg(Response)),
?assertEqual(<<"invalid_sender">>, get_err_code(Response)).

0 comments on commit d1ea3fc

Please sign in to comment.