Skip to content

Commit

Permalink
Update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
chrzaszcz committed Oct 21, 2022
1 parent 4bcc1d3 commit f78be93
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 26 deletions.
59 changes: 35 additions & 24 deletions big_tests/tests/graphql_stanza_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,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_mam_cases() ->
Expand Down Expand Up @@ -148,8 +149,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 @@ -160,8 +161,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 @@ -171,8 +172,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 @@ -204,8 +205,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),
assert_not_empty(MamID, Config),
#{<<"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 @@ -216,8 +217,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),
assert_not_empty(MamID, Config),
#{<<"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 @@ -288,9 +289,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 @@ -300,9 +303,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 @@ -506,14 +525,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 Down
3 changes: 3 additions & 0 deletions big_tests/tests/rest_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -347,10 +347,13 @@ messages_are_archived(Config) ->
message_archive_errors(Config) ->
Config1 = escalus_fresh:create_users(Config, [{alice, 1}]),
User = binary_to_list(escalus_users:get_username(Config1, alice)),
Domain = binary_to_list(domain_helper:domain()),
{?NOT_FOUND, <<"Missing owner JID">>} =
gett(admin, "/messages"),
{?BAD_REQUEST, <<"Invalid owner JID">>} =
gett(admin, "/messages/@invalid"),
{?BAD_REQUEST, <<"User does not exist">>} =
gett(admin, "/messages/baduser@" ++ Domain),
{?BAD_REQUEST, <<"Invalid interlocutor JID">>} =
gett(admin, "/messages/" ++ User ++ "/@invalid"),
{?BAD_REQUEST, <<"Invalid limit">>} =
Expand Down
5 changes: 3 additions & 2 deletions src/mongoose_stanza_api.erl
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,9 @@ check_user(M = #{check_user := true, user := UserJid, host_type := HostType}) ->
end.

send(#{host_type := HostType, from := From, to := To, stanza := Stanza}) ->
Acc = mongoose_acc:new(#{location => ?LOCATION,
Acc = mongoose_acc:new(#{from_jid => From,
to_jid => To,
location => ?LOCATION,
host_type => HostType,
lserver => From#jid.lserver,
element => Stanza}),
Expand Down Expand Up @@ -165,7 +167,6 @@ ensure_id(Stanza) ->
end.

maybe_cdata_elem(_, undefined) -> [];
maybe_cdata_elem(_, <<>>) -> [];
maybe_cdata_elem(Name, Text) when is_binary(Text) ->
[cdata_elem(Name, Text)].

Expand Down

0 comments on commit f78be93

Please sign in to comment.