Skip to content

Commit

Permalink
Use the new instrumentation assertions in privacy tests
Browse files Browse the repository at this point in the history
To assert the exact number of events (usually one), assertions need to
check the timestamps, because event in a single test there are
usually multiple events for the same user.
  • Loading branch information
chrzaszcz committed Jul 4, 2024
1 parent f539010 commit b375b7d
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 54 deletions.
38 changes: 23 additions & 15 deletions big_tests/tests/mod_blocking_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -231,12 +231,13 @@ messages_from_blocked_user_dont_arrive(Config) ->
Config, [{alice, 1}, {bob, 1}],
fun(User1, User2) ->
user_blocks(User1, [User2]),
TS = instrument_helper:timestamp(),
message(User2, User1, <<"Hi!">>),
ct:sleep(100),
escalus_assert:has_no_stanzas(User1),
privacy_helper:gets_error(User2, <<"cancel">>, <<"service-unavailable">>),
privacy_helper:assert_privacy_check_packet_event(User2, #{dir => out}),
privacy_helper:assert_privacy_check_packet_event(User1, #{dir => in, blocked_count => 1})
privacy_helper:assert_privacy_check_packet_event(User2, #{dir => out}, TS),
privacy_helper:assert_privacy_check_packet_event(User1, #{dir => in, blocked_count => 1}, TS)
end).

messages_from_unblocked_user_arrive_again(Config) ->
Expand Down Expand Up @@ -437,10 +438,11 @@ notify_blockee(Config) ->

%%
get_blocklist(User) ->
TS = instrument_helper:timestamp(),
IQGet = get_blocklist_stanza(),
escalus_client:send(User, IQGet),
Result = escalus_client:wait_for_stanza(User),
privacy_helper:assert_privacy_get_event(User),
privacy_helper:assert_privacy_get_event(User, TS),
Result.

%%
Expand Down Expand Up @@ -589,14 +591,15 @@ get_blocklist_items(Items) ->
end, Items).

user_blocks(Blocker, Blockees) when is_list(Blockees) ->
TS = instrument_helper:timestamp(),
BlockeeJIDs = [ escalus_utils:jid_to_lower(escalus_client:short_jid(B)) || B <- Blockees ],
AddStanza = block_users_stanza(BlockeeJIDs),
escalus_client:send(Blocker, AddStanza),
Res = escalus:wait_for_stanzas(Blocker, 2),
CheckPush = fun(E) -> is_xep191_push(<<"block">>, BlockeeJIDs, E) end,
Preds = [is_iq_result, CheckPush],
escalus:assert_many(Preds, Res),
privacy_helper:assert_privacy_set_event(Blocker, #{}).
privacy_helper:assert_privacy_set_event(Blocker, #{}, TS).

blocklist_is_empty(BlockList) ->
escalus:assert(is_iq_result, BlockList),
Expand All @@ -607,19 +610,21 @@ blocklist_contains_jid(BlockList, Client) ->
escalus:assert(fun blocklist_result_has/2, [JID], BlockList).

user_unblocks(Unblocker, Unblockees) when is_list(Unblockees) ->
TS = instrument_helper:timestamp(),
UnblockeeJIDs = [ escalus_utils:jid_to_lower(escalus_client:short_jid(B)) || B <- Unblockees ],
AddStanza = unblock_users_stanza(UnblockeeJIDs),
escalus_client:send(Unblocker, AddStanza),
Res = escalus:wait_for_stanzas(Unblocker, 2),
CheckPush = fun(E) -> is_xep191_push(<<"unblock">>, UnblockeeJIDs, E) end,
Preds = [is_iq_result, CheckPush],
escalus:assert_many(Preds, Res),
privacy_helper:assert_privacy_set_event(Unblocker, #{});
privacy_helper:assert_privacy_set_event(Unblocker, #{}, TS);
user_unblocks(Unblocker, Unblockee) ->
TS = instrument_helper:timestamp(),
JID = escalus_utils:jid_to_lower(escalus_client:short_jid(Unblockee)),
escalus_client:send(Unblocker, unblock_user_stanza(JID)),
user_gets_remove_result(Unblocker, [JID]),
privacy_helper:assert_privacy_set_event(Unblocker, #{}).
privacy_helper:assert_privacy_set_event(Unblocker, #{}, TS).

blocklist_doesnt_contain_jid(BlockList, Client) ->
JID = escalus_utils:jid_to_lower(escalus_client:short_jid(Client)),
Expand All @@ -642,32 +647,35 @@ message(From, To, MsgTxt) ->

message_is_delivered(From, [To|_] = Tos, MessageText) ->
BareTo = escalus_utils:jid_to_lower(escalus_client:short_jid(To)),
TS = instrument_helper:timestamp(),
escalus:send(From, escalus_stanza:chat_to(BareTo, MessageText)),
[ escalus:assert(is_chat_message, [MessageText], escalus:wait_for_stanza(C)) ||
C <- Tos ],
privacy_helper:assert_privacy_check_packet_event(From, #{dir => out}),
privacy_helper:assert_privacy_check_packet_event(To, #{dir => in});
privacy_helper:assert_privacy_check_packet_event(From, #{dir => out}, TS),
privacy_helper:assert_privacy_check_packet_event(To, #{dir => in}, TS);
message_is_delivered(From, To, MessageText) ->
BareTo = escalus_utils:jid_to_lower(escalus_client:short_jid(To)),
TS = instrument_helper:timestamp(),
escalus:send(From, escalus_stanza:chat_to(BareTo, MessageText)),
escalus:assert(is_chat_message, [MessageText], escalus:wait_for_stanza(To)),
privacy_helper:assert_privacy_check_packet_event(From, #{dir => out}),
privacy_helper:assert_privacy_check_packet_event(To, #{dir => in}).
privacy_helper:assert_privacy_check_packet_event(From, #{dir => out}, TS),
privacy_helper:assert_privacy_check_packet_event(To, #{dir => in}, TS).

message_is_blocked_by_recipient(From, To) ->
TS = instrument_helper:timestamp(),
message_is_not_delivered(From, [To], <<"You blocked me!">>),
privacy_helper:gets_error(From, <<"cancel">>, <<"service-unavailable">>),
privacy_helper:assert_privacy_check_packet_event(From, #{dir => out}),
privacy_helper:assert_privacy_check_packet_event(To, #{dir => in, blocked_count => 1}).
privacy_helper:assert_privacy_check_packet_event(From, #{dir => out}, TS),
privacy_helper:assert_privacy_check_packet_event(To, #{dir => in, blocked_count => 1}, TS).

message_is_blocked_by_sender(From, To) ->
TS = instrument_helper:timestamp(),
message_is_not_delivered(From, [To], <<"I blocked you!">>),
client_gets_blocking_error(From),
privacy_helper:assert_privacy_check_packet_event(From, #{dir => out, blocked_count => 1}).
privacy_helper:assert_privacy_check_packet_event(From, #{dir => out, blocked_count => 1}, TS).

message_is_not_delivered(From, [To|_] = Tos, MessageText) ->
BareTo = escalus_utils:jid_to_lower(escalus_client:short_jid(To)),
escalus:send(From, escalus_stanza:chat_to(BareTo, MessageText)),
escalus:send(From, escalus_stanza:chat_to(To, MessageText)),
clients_have_no_messages(Tos).

clients_have_no_messages(Cs) when is_list (Cs) -> [ client_has_no_messages(C) || C <- Cs ].
Expand Down
32 changes: 20 additions & 12 deletions big_tests/tests/privacy_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,16 @@
-module(privacy_SUITE).
-compile([export_all, nowarn_export_all]).

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

-define(SLEEP_TIME, 50).

-import(privacy_helper, [assert_privacy_get_event/1,
assert_privacy_get_event/2,
assert_privacy_set_event/2,
assert_privacy_check_packet_event/2,
assert_privacy_push_item_event/2]).
assert_privacy_check_packet_event/3,
assert_privacy_push_item_event/2,
assert_privacy_push_item_event/3]).

%%--------------------------------------------------------------------
%% Suite configuration
Expand Down Expand Up @@ -195,10 +194,11 @@ get_all_lists_with_active(Config) ->

privacy_helper:set_and_activate(Alice, {<<"deny_client">>, Bob}),

TS = instrument_helper:timestamp(),
escalus:send(Alice, escalus_stanza:privacy_get_all()),
escalus:assert(is_privacy_result_with_active, [<<"deny_client">>],
escalus:wait_for_stanza(Alice)),
assert_privacy_get_event(Alice)
assert_privacy_get_event(Alice, TS)

end).

Expand All @@ -209,10 +209,11 @@ get_all_lists_with_default(Config) ->
privacy_helper:set_list(Alice, {<<"allow_client">>, Bob}),
privacy_helper:set_default_list(Alice, <<"allow_client">>),

TS = instrument_helper:timestamp(),
escalus:send(Alice, escalus_stanza:privacy_get_all()),
escalus:assert(is_privacy_result_with_default,
escalus:wait_for_stanza(Alice)),
assert_privacy_get_event(Alice)
assert_privacy_get_event(Alice, TS)

end).

Expand Down Expand Up @@ -243,13 +244,14 @@ get_existing_list(Config) ->

privacy_helper:set_list(Alice, {<<"deny_client">>, Bob}),

TS = instrument_helper:timestamp(),
escalus:send(Alice, escalus_stanza:privacy_get_lists([<<"deny_client">>])),
Response = escalus:wait_for_stanza(Alice),

<<"deny_client">> = exml_query:path(Response, [{element, <<"query">>},
{element, <<"list">>},
{attr, <<"name">>}]),
assert_privacy_get_event(Alice)
assert_privacy_get_event(Alice, TS)

end).

Expand Down Expand Up @@ -391,9 +393,14 @@ remove_list(Config) ->
privacy_helper:send_set_list(Alice, {<<"deny_client">>, Bob}),

%% These are the pushed notification and iq result.
escalus_client:wait_for_stanzas(Alice, 2),
escalus:assert_many([
fun privacy_helper:is_privacy_list_push/1,
is_iq_result
], escalus_client:wait_for_stanzas(Alice, 2)),
assert_privacy_push_item_event(Alice, 1),

%% Request list deletion by sending an empty list.
TS = instrument_helper:timestamp(),
RemoveRequest = escalus_stanza:privacy_set_list(
escalus_stanza:privacy_list(<<"someList">>, [])),
escalus_client:send(Alice, RemoveRequest),
Expand All @@ -403,7 +410,7 @@ remove_list(Config) ->
fun privacy_helper:is_privacy_list_push/1,
is_iq_result
], escalus_client:wait_for_stanzas(Alice, 2)),
assert_privacy_push_item_event(Alice, 1),
assert_privacy_push_item_event(Alice, 1, TS),

escalus_client:send(Alice,
escalus_stanza:privacy_get_lists([<<"someList">>])),
Expand Down Expand Up @@ -432,10 +439,11 @@ block_jid_message(Config) ->
%% Blocking only applies to incoming messages, so Alice can still send
%% Bob a message
%% and Bob gets nothing
TS = instrument_helper:timestamp(),
escalus_client:send(Alice, escalus_stanza:chat_to(Bob, <<"Hi, Bobbb!">>)),
escalus_assert:is_chat_message(<<"Hi, Bobbb!">>, escalus_client:wait_for_stanza(Bob)),
assert_privacy_check_packet_event(Alice, #{dir => out}),
assert_privacy_check_packet_event(Bob, #{dir => in})
assert_privacy_check_packet_event(Alice, #{dir => out}, TS),
assert_privacy_check_packet_event(Bob, #{dir => in}, TS)

end).

Expand Down
82 changes: 55 additions & 27 deletions big_tests/tests/privacy_helper.erl
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
-include_lib("exml/include/exml.hrl").
-include_lib("escalus/include/escalus.hrl").
-include_lib("escalus/include/escalus_xmlns.hrl").
-include_lib("common_test/include/ct.hrl").

gets_error(Who, Type) ->
gets_error(Who, <<"cancel">>, Type).
Expand All @@ -29,31 +28,36 @@ send_set_list(Client, List) ->

%% Sets the list on server.
set_list(Client, {ListName, Target}) ->
TS = instrument_helper:timestamp(),
Stanza = send_set_list(Client, {ListName, Target}),
verify_set_list_response(Client),
verify_set_list_response(Client, TS),
verify_list(Client, ListName, Stanza);
set_list(Client, ListName) ->
TS = instrument_helper:timestamp(),
Stanza = send_set_list(Client, ListName),
verify_set_list_response(Client),
verify_set_list_response(Client, TS),
verify_list(Client, ListName, Stanza).

set_list(Client, ListName, NewItems) ->
TS = instrument_helper:timestamp(),
PrivacyList = escalus_stanza:privacy_list(ListName, NewItems),
Stanza = escalus_stanza:privacy_set_list(PrivacyList),
escalus:send(Client, Stanza),
verify_set_list_response(Client),
verify_set_list_response(Client, TS),
verify_list(Client, ListName, Stanza).

verify_set_list_response(Client) ->
verify_set_list_response(Client, TS) ->
Responses = escalus:wait_for_stanzas(Client, 2),
escalus:assert_many([is_iq_result, is_privacy_set], Responses),
assert_privacy_set_event(Client, #{}).
assert_privacy_set_event(Client, #{}, TS).

verify_list(Client, ListName, Stanza) ->
TS = instrument_helper:timestamp(),
GetStanza = escalus_stanza:privacy_get_lists([ListName]),
escalus:send(Client, GetStanza),
GetResultStanza = escalus:wait_for_stanza(Client),
escalus:assert(fun does_result_match_request/3, [Stanza, GetStanza], GetResultStanza).
escalus:assert(fun does_result_match_request/3, [Stanza, GetStanza], GetResultStanza),
assert_privacy_get_event(Client, TS).

does_result_match_request(SetRequest, GetRequest, Result) ->
escalus_pred:is_iq_result(GetRequest, Result) andalso
Expand Down Expand Up @@ -90,15 +94,17 @@ attr_match(Name, Value, Props) ->

%% Make the list the active one.
activate_list(Client, ListName) ->
TS = instrument_helper:timestamp(),
escalus:send(Client, escalus_stanza:privacy_activate(ListName)),
escalus:assert(is_iq_result, escalus:wait_for_stanza(Client)),
assert_privacy_set_event(Client, #{active_count => 1}).
assert_privacy_set_event(Client, #{active_count => 1}, TS).

%% Make the list the default one.
set_default_list(Client, ListName) ->
TS = instrument_helper:timestamp(),
escalus:send(Client, escalus_stanza:privacy_set_default(ListName)),
escalus:assert(is_iq_result, escalus:wait_for_stanza(Client)),
assert_privacy_set_event(Client, #{default_count => 1}).
assert_privacy_set_event(Client, #{default_count => 1}, TS).

%% Is this iq a notification about a privacy list being changed?
is_privacy_list_push(Iq) ->
Expand Down Expand Up @@ -255,36 +261,58 @@ does_user_process_crash(From, To, Type, Children, Message) ->
escalus:wait_for_stanza(To)).

send_and_check_blocked_message(Sender, Recipient) ->
TS = instrument_helper:timestamp(),
escalus_client:send(Sender, escalus_stanza:chat_to(Recipient, <<"Hi, blocker!">>)),
timer:sleep(100),
escalus_assert:has_no_stanzas(Recipient),
privacy_helper:gets_error(Sender, <<"service-unavailable">>),
assert_privacy_check_packet_event(Sender, #{dir => out}),
assert_privacy_check_packet_event(Recipient, #{dir => in, denied_count => 1}).
assert_privacy_check_packet_event(Sender, #{dir => out}, TS),
assert_privacy_check_packet_event(Recipient, #{dir => in, denied_count => 1}, TS).

%% Intrumentation events
%% Instrumentation events

assert_privacy_get_event(Client) ->
ClientJid = jid:from_binary(escalus_utils:get_jid(Client)),
instrument_helper:assert(
mod_privacy_get, #{host_type => domain_helper:host_type()},
fun(M) -> M =:= #{count => 1, jid => ClientJid} end).
assert_event(mod_privacy_get, measurements(Client)).

assert_privacy_get_event(Client, TS) ->
assert_event(mod_privacy_get, measurements(Client), TS).

assert_privacy_set_event(Client, ExtraM) ->
ClientJid = jid:from_binary(escalus_utils:get_jid(Client)),
instrument_helper:assert(
mod_privacy_set, #{host_type => domain_helper:host_type()},
fun(M) -> M =:= maps:merge(#{jid => ClientJid, count => 1}, ExtraM) end).
assert_event(mod_privacy_set, measurements(Client, ExtraM)).

assert_privacy_set_event(Client, ExtraM, TS) ->
assert_event(mod_privacy_set, measurements(Client, ExtraM), TS).

assert_privacy_check_packet_event(Client, ExtraM) ->
ClientJid = jid:from_binary(escalus_utils:get_jid(Client)),
instrument_helper:assert(
mod_privacy_check_packet, #{host_type => domain_helper:host_type()},
fun(M) -> M =:= maps:merge(#{jid => ClientJid, count => 1}, ExtraM) end).
assert_event(mod_privacy_check_packet, measurements(Client, ExtraM)).

assert_privacy_check_packet_event(Client, ExtraM, TS) ->
assert_event(mod_privacy_check_packet, measurements(Client, ExtraM), TS).

assert_privacy_push_item_event(Client, ExpCount) ->
assert_event(mod_privacy_push_item, push_item_measurements(Client, ExpCount)).

assert_privacy_push_item_event(Client, ExpCount, TS) ->
assert_event(mod_privacy_push_item, push_item_measurements(Client, ExpCount), TS).

measurements(Client, ExtraM) ->
maps:merge(measurements(Client), ExtraM).

measurements(Client) ->
ClientJid = jid:from_binary(escalus_utils:get_jid(Client)),
#{jid => ClientJid, count => 1}.

push_item_measurements(Client, ExpCount) ->
User = escalus_utils:get_username(Client),
Server = escalus_utils:get_server(Client),
instrument_helper:assert(
mod_privacy_push_item, #{host_type => domain_helper:host_type()},
fun(M) -> M =:= #{user => User, server => Server, count => ExpCount} end).
#{user => User, server => Server, count => ExpCount}.

assert_event(Name, Measurements) ->
instrument_helper:assert_one(Name, labels(), fun(M) -> M =:= Measurements end).

assert_event(Name, Measurements, TS) ->
instrument_helper:assert(Name, labels(), fun(M) -> M =:= Measurements end,
#{expected_count => 1, min_timestamp => TS}).

labels() ->
#{host_type => domain_helper:host_type()}.

0 comments on commit b375b7d

Please sign in to comment.