Skip to content

Commit

Permalink
fixing unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
DenysGonchar committed Apr 20, 2021
1 parent 38088e0 commit a22f2e4
Show file tree
Hide file tree
Showing 9 changed files with 116 additions and 87 deletions.
119 changes: 83 additions & 36 deletions test/acc_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
-include("mongoose.hrl").

-define(PRT(X, Y), ct:pal("~p: ~p", [X, Y])).
-define(ACC_PARAMS, #{location => ?LOCATION,
host_type => <<"local host">>,
lserver => <<"localhost">>}).

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%% suite configuration
Expand All @@ -28,7 +31,8 @@ groups() ->
init_from_element,
produce_iq_meta_automatically,
strip,
parse_with_cdata
parse_with_cdata,
tries_to_get_host_type
]
}
].
Expand All @@ -43,10 +47,9 @@ end_per_suite(C) ->
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%% test methods
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

store_retrieve_and_delete(_C) ->
Acc = mongoose_acc:new(#{ location => ?LOCATION,
lserver => <<"localhost">>,
element => undefined }),
Acc = mongoose_acc:new(?ACC_PARAMS),
Acc2 = mongoose_acc:set(ns, check, 1, Acc),
?assertEqual(1, mongoose_acc:get(ns, check, Acc2)),
Acc3 = mongoose_acc:set(ns, check, 2, Acc2),
Expand All @@ -56,9 +59,7 @@ store_retrieve_and_delete(_C) ->
ok.

store_permanent_retrieve_and_delete(_C) ->
Acc = mongoose_acc:new(#{ location => ?LOCATION,
lserver => <<"localhost">>,
element => undefined }),
Acc = mongoose_acc:new(?ACC_PARAMS),
Acc2 = mongoose_acc:set_permanent(ns, check, 1, Acc),
?assertEqual(1, mongoose_acc:get(ns, check, Acc2)),
Acc3 = mongoose_acc:set_permanent(ns, check, 2, Acc2),
Expand All @@ -70,9 +71,7 @@ store_permanent_retrieve_and_delete(_C) ->
ok.

store_retrieve_and_delete_many(_C) ->
Acc = mongoose_acc:new(#{ location => ?LOCATION,
lserver => <<"localhost">>,
element => undefined}),
Acc = mongoose_acc:new(?ACC_PARAMS),
KV = [{check, 1}, {check2, 2}, {check3, 3}],
Acc2 = mongoose_acc:set(ns, [{check3, 0} | KV], Acc),
[?assertEqual(V, mongoose_acc:get(ns, K, Acc2)) || {K, V} <- KV],
Expand All @@ -87,9 +86,7 @@ store_retrieve_and_delete_many(_C) ->
ok.

store_permanent_retrieve_and_delete_many(_C) ->
Acc = mongoose_acc:new(#{ location => ?LOCATION,
lserver => <<"localhost">>,
element => undefined}),
Acc = mongoose_acc:new(?ACC_PARAMS),
KV = [{check, 1}, {check2, 2}, {check3, 3}],
NK = [{ns, K} || {K, _} <- KV],
Acc2 = mongoose_acc:set_permanent(ns, [{check3, 0} | KV], Acc),
Expand All @@ -108,19 +105,15 @@ store_permanent_retrieve_and_delete_many(_C) ->
ok.

init_from_element(_C) ->
Acc = mongoose_acc:new(#{ location => ?LOCATION,
lserver => <<"localhost">>,
element => sample_stanza() }),
Acc = mongoose_acc:new(?ACC_PARAMS#{element => sample_stanza()}),
?PRT("Acc", Acc),
?assertEqual(<<"iq">>, mongoose_acc:stanza_name(Acc)),
?assertEqual(<<"set">>, mongoose_acc:stanza_type(Acc)),
ok.


produce_iq_meta_automatically(_C) ->
Acc = mongoose_acc:new(#{ location => ?LOCATION,
lserver => <<"localhost">>,
element => sample_stanza() }),
Acc = mongoose_acc:new(?ACC_PARAMS#{element => sample_stanza()}),
{Command, Acc1} = mongoose_iq:command(Acc),
?assertEqual(<<"block">>, Command),
% We check for exactly the same Acc, as there is no need to update the cache
Expand All @@ -133,38 +126,92 @@ produce_iq_meta_automatically(_C) ->
ok.

parse_with_cdata(_C) ->
Acc = mongoose_acc:new(#{ location => ?LOCATION,
lserver => <<"localhost">>,
element => stanza_with_cdata() }),
Acc = mongoose_acc:new(?ACC_PARAMS#{element => stanza_with_cdata()}),
{XMLNS, _} = mongoose_iq:xmlns(Acc),
?assertEqual(<<"jabber:iq:roster">>, XMLNS).

strip(_C) ->
Acc = mongoose_acc:new(#{ location => ?LOCATION,
lserver => <<"localhost">>,
element => iq_stanza(),
from_jid => jid:make(<<"jajid">>, <<"localhost">>, <<>>),
to_jid => jid:make(<<"tyjid">>, <<"localhost">>, <<>>) }),
El = iq_stanza(),
FromJID = jid:make(<<"jajid">>, <<"localhost">>, <<>>),
ToJID = jid:make(<<"tyjid">>, <<"localhost">>, <<>>),
Server = maps:get(lserver, ?ACC_PARAMS),
HostType = maps:get(host_type, ?ACC_PARAMS),
Acc = mongoose_acc:new(?ACC_PARAMS#{element => El,
from_jid => FromJID,
to_jid => ToJID}),
{XMLNS1, Acc1} = mongoose_iq:xmlns(Acc),
?assertEqual(<<"urn:ietf:params:xml:ns:xmpp-session">>, XMLNS1),
?assertEqual(<<"set">>, mongoose_acc:stanza_type(Acc1)),
?assertEqual(undefined, mongoose_acc:get(ns, ppp, undefined, Acc1)),
Acc2 = mongoose_acc:set_permanent(ns, ppp, 997, Acc1),
Acc3 = mongoose_acc:set_permanent(ns2, [{a, 1}, {b, 2}], Acc2),
Acc3 = mongoose_acc:set(ns2, [{a, 1}, {b, 2}], Acc2),
?assertMatch([_, _], mongoose_acc:get(ns2, Acc3)),
Acc4 = mongoose_acc:delete(ns2, Acc3),
?assertEqual(997, mongoose_acc:get(ns, ppp, Acc4)),
?assertEqual([], mongoose_acc:get(ns2, Acc4)),
Ref = mongoose_acc:ref(Acc4),
NAcc1 = mongoose_acc:strip(#{ lserver => <<"localhost">>,
element => sample_stanza() }, Acc4),
?assertEqual(Server, mongoose_acc:lserver(Acc3)),
?assertEqual(HostType, mongoose_acc:host_type(Acc3)),
?assertEqual({FromJID, ToJID, El}, mongoose_acc:packet(Acc3)),
Ref = mongoose_acc:ref(Acc3),
NewServer = <<"test.", Server/binary>>,
NewHostType = <<"new ",HostType/binary>>,
NAcc1 = mongoose_acc:strip(#{lserver => NewServer,
host_type => NewHostType,
element => sample_stanza() }, Acc3),
{XMLNS2, NAcc2} = mongoose_iq:xmlns(NAcc1),
?assertEqual(<<"urn:xmpp:blocking">>, XMLNS2),
?assertEqual(jid:from_binary(<<"a@localhost">>), mongoose_acc:to_jid(NAcc2)),
?assertEqual(Ref, mongoose_acc:ref(NAcc2)),
?assertEqual(997, mongoose_acc:get(ns, ppp, NAcc2)),
?assertEqual([], mongoose_acc:get(ns2, NAcc2)).

?assertEqual([], mongoose_acc:get(ns2, NAcc2)),
?assertEqual(sample_stanza(), mongoose_acc:element(NAcc2)),
?assertEqual(NewServer, mongoose_acc:lserver(NAcc2)),
?assertEqual(NewHostType, mongoose_acc:host_type(NAcc2)),
NAcc3 = mongoose_acc:strip(Acc3),
{XMLNS3, NAcc4} = mongoose_iq:xmlns(NAcc3),
?assertEqual(<<"urn:ietf:params:xml:ns:xmpp-session">>, XMLNS3),
?assertEqual(<<"set">>, mongoose_acc:stanza_type(NAcc3)),
?assertEqual(Server, mongoose_acc:lserver(NAcc4)),
?assertEqual(HostType, mongoose_acc:host_type(NAcc4)),
?assertEqual({FromJID, ToJID, El}, mongoose_acc:packet(NAcc4)),
?assertEqual(Ref, mongoose_acc:ref(NAcc4)),
?assertEqual(997, mongoose_acc:get(ns, ppp, NAcc4)),
?assertEqual([], mongoose_acc:get(ns2, NAcc4)).

tries_to_get_host_type(_Config) ->
meck:new(mongoose_domain_api),
meck:expect(mongoose_domain_api, get_host_type,
fun
(<<"host1">>) -> {ok, <<"host 1">>};
(<<"host2">>) -> {ok, <<"host 2">>};
(_) -> {error, not_found}
end),
AccParams=#{location => ?LOCATION},
StripParams = #{element => sample_stanza()},
%% new with <<"host 1">> host type
Acc1 = mongoose_acc:new(AccParams#{lserver => <<"host1">>}),
?assertEqual(<<"host 1">>, mongoose_acc:host_type(Acc1)),
?assertEqual(1, meck:num_calls(mongoose_domain_api, get_host_type, [<<"host1">>])),
%% new with 'undefined' host type
Acc2 = mongoose_acc:new(AccParams#{lserver => <<"unknown.host1">>}),
?assertEqual(undefined, mongoose_acc:host_type(Acc2)),
?assertEqual(1, meck:num_calls(mongoose_domain_api, get_host_type, [<<"unknown.host1">>])),
%% host type changes from <<"host 1">> to 'undefined'
Acc3 = mongoose_acc:strip(StripParams#{lserver => <<"unknown.host1">>},Acc1),
?assertEqual(undefined, mongoose_acc:host_type(Acc3)),
?assertEqual(2, meck:num_calls(mongoose_domain_api, get_host_type, [<<"unknown.host1">>])),
%% host type changes from <<"host 1">> to <<host 2>>
Acc6 = mongoose_acc:strip(StripParams#{lserver => <<"host2">>},Acc1),
?assertEqual(<<"host 2">>, mongoose_acc:host_type(Acc6)),
?assertEqual(1, meck:num_calls(mongoose_domain_api, get_host_type, [<<"host2">>])),
%% host type changes from 'undefined' to <<"host 1">>
Acc4 = mongoose_acc:strip(StripParams#{lserver => <<"host1">>},Acc2),
?assertEqual(<<"host 1">>, mongoose_acc:host_type(Acc4)),
?assertEqual(2, meck:num_calls(mongoose_domain_api, get_host_type, [<<"host1">>])),
%% host type changes from 'undefined' to `undefined`
Acc5 = mongoose_acc:strip(StripParams#{lserver => <<"unknown.host2">>},Acc2),
?assertEqual(undefined, mongoose_acc:host_type(Acc5)),
?assertEqual(1, meck:num_calls(mongoose_domain_api, get_host_type, [<<"unknown.host2">>])),
%% check overall calls to mongoose_domain_api:get_host_type/1
?assertEqual(6, meck:num_calls(mongoose_domain_api, get_host_type, 1)),
meck:unload(mongoose_domain_api).

sample_stanza() ->
{xmlel, <<"iq">>,
Expand Down
4 changes: 3 additions & 1 deletion test/ejabberd_sm_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,9 @@ set_test_case_meck(MaxUserSessions) ->
meck:new(acl, []),
meck:expect(acl, match_rule, fun(_, _, _) -> MaxUserSessions end),
meck:new(ejabberd_hooks, []),
meck:expect(ejabberd_hooks, run_for_host_type, fun(_, _, Acc, _) -> Acc end).
meck:expect(ejabberd_hooks, run_for_host_type, fun(_, _, Acc, _) -> Acc end),
meck:new(mongoose_domain_api, []),
meck:expect(mongoose_domain_api, get_host_type, fun(H) -> {ok, H} end).

set_test_case_meck_unique_count_crash(Backend) ->
F = get_fun_for_unique_count(Backend),
Expand Down
1 change: 1 addition & 0 deletions test/jlib_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ error_reply_check(_) ->
BaseIQReply = jlib:make_result_iq_reply(base_iq()),
Acc = mongoose_acc:new(#{ location => ?LOCATION,
lserver => <<"localhost">>,
host_type => <<"localhost">>,
element => BaseIQReply,
from_jid => jid:make_noprep(<<"a">>, <<"localhost">>, <<>>),
to_jid => jid:make_noprep(<<>>, <<"localhost">>, <<>>) }),
Expand Down
16 changes: 7 additions & 9 deletions test/mod_aws_sns_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@
{muc_messages_topic, "user_messagegroup_sent-dev-1"}
]).

-define(ACC_PARAMS, #{location => ?LOCATION,
host_type => undefined,
lserver => <<"localhost">>,
element => undefined}).
all() ->
[
handles_unicode_messages,
Expand Down Expand Up @@ -147,25 +151,19 @@ send_packet_callback(Config, Type, Body) ->
Packet = message(Config, Type, Body),
Sender = #jid{lserver = Host} = ?config(sender, Config),
Recipient = ?config(recipient, Config),
mod_event_pusher_sns:push_event(mongoose_acc:new(#{ location => ?LOCATION,
lserver => Host,
element => undefined }), Host,
mod_event_pusher_sns:push_event(mongoose_acc:new(?ACC_PARAMS), Host,
#chat_event{type = chat, direction = in,
from = Sender, to = Recipient,
packet = Packet}).

user_present_callback(Config) ->
Jid = #jid{lserver = Host} = ?config(sender, Config),
mod_event_pusher_sns:push_event(mongoose_acc:new(#{ location => ?LOCATION,
lserver => Host,
element => undefined }), Host,
mod_event_pusher_sns:push_event(mongoose_acc:new(?ACC_PARAMS), Host,
#user_status_event{jid = Jid, status = online}).

user_not_present_callback(Config) ->
Jid = #jid{lserver = Host} = ?config(sender, Config),
mod_event_pusher_sns:push_event(mongoose_acc:new(#{ location => ?LOCATION,
lserver => Host,
element => undefined }), Host,
mod_event_pusher_sns:push_event(mongoose_acc:new(?ACC_PARAMS), Host,
#user_status_event{jid = Jid, status = offline}).

%% Helpers
Expand Down
1 change: 1 addition & 0 deletions test/mod_global_distrib_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ fake_acc_to_component(From) ->
},
{mongoose_acc:new(#{ location => ?LOCATION,
lserver => From#jid.lserver,
host_type => From#jid.lserver,
element => Packet }), To}.

%%--------------------------------------------------------------------
Expand Down
1 change: 1 addition & 0 deletions test/mongoose_cleanup_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ stream_management(_Config) ->
Acc = mongoose_acc:new(
#{location => ?LOCATION,
lserver => S,
host_type => S,
element => undefined}),
mongoose_hooks:session_cleanup(S, Acc, U, R, SID),
{error, smid_not_found} = mod_stream_management:get_sid(SMID).
Expand Down
47 changes: 12 additions & 35 deletions test/privacy_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@
-define(ALICE, jid:from_binary(<<"alice@localhost">>)).
-define(BOB, jid:from_binary(<<"bob@localhost">>)).
-define(JEFF, jid:from_binary(<<"jeff@localhost">>)).
-define(ACC_PARAMS, #{location => ?LOCATION,
lserver => (?ALICE)#jid.lserver,
host_type => (?ALICE)#jid.lserver,
from_jid => ?ALICE,
to_jid => ?BOB}).

all() ->
[ check_with_allowed,
Expand Down Expand Up @@ -47,11 +52,7 @@ end_per_suite(_C) ->
ok.

check_with_allowed(_C) ->
Acc = mongoose_acc:new(#{ location => ?LOCATION,
lserver => (?ALICE)#jid.lserver,
from_jid => ?ALICE,
to_jid => ?BOB,
element => message() }),
Acc = mongoose_acc:new(?ACC_PARAMS#{element => message()}),
Acc1 = make_check(Acc, userlist(none), ?BOB, out, allow),
Acc2 = make_check(Acc1, userlist(none), ?BOB, in, allow),
Acc3 = make_check(Acc2, userlist(none), ?JEFF, out, allow),
Expand All @@ -60,55 +61,35 @@ check_with_allowed(_C) ->

check_with_denied(_C) ->
% message
Acc = mongoose_acc:new(#{ location => ?LOCATION,
lserver => (?ALICE)#jid.lserver,
from_jid => ?ALICE,
to_jid => ?BOB,
element => message() }),
Acc = mongoose_acc:new(?ACC_PARAMS#{element => message()}),
Acc1 = make_check(Acc, userlist(deny_all_message), ?BOB, out, allow),
Acc2 = make_check(Acc1, userlist(deny_all_message), ?BOB, in, deny),
Acc3 = make_check(Acc2, userlist(deny_all_message), ?JEFF, out, allow),
_Acc4 = make_check(Acc3, userlist(deny_all_message), ?JEFF, in, deny),
% presence
NAcc = mongoose_acc:new(#{ location => ?LOCATION,
lserver => (?ALICE)#jid.lserver,
from_jid => ?ALICE,
to_jid => ?BOB,
element => presence() }),
NAcc = mongoose_acc:new(?ACC_PARAMS#{element => presence()}),
NAcc1 = make_check(NAcc, userlist(deny_all_message), ?BOB, out, allow),
NAcc2 = make_check(NAcc1, userlist(deny_all_message), ?BOB, in, allow),
NAcc3 = make_check(NAcc2, userlist(deny_all_message), ?JEFF, out, allow),
_NAcc4 = make_check(NAcc3, userlist(deny_all_message), ?JEFF, in, allow),
ok.

check_with_denied_bob(_C) ->
Acc = mongoose_acc:new(#{ location => ?LOCATION,
lserver => (?ALICE)#jid.lserver,
from_jid => ?ALICE,
to_jid => ?BOB,
element => message() }),
Acc = mongoose_acc:new(?ACC_PARAMS#{element => message()}),
Acc1 = make_check(Acc, userlist(deny_all_message), ?BOB, out, allow),
Acc2 = make_check(Acc1, userlist(deny_all_message), ?BOB, in, deny),
Acc3 = make_check(Acc2, userlist(none), ?JEFF, out, allow),
_Acc4 = make_check(Acc3, userlist(none), ?JEFF, in, allow),
ok.

check_with_bob_blocked(_C) ->
Acc = mongoose_acc:new(#{ location => ?LOCATION,
lserver => (?ALICE)#jid.lserver,
from_jid => ?ALICE,
to_jid => ?BOB,
element => message() }),
Acc = mongoose_acc:new(?ACC_PARAMS#{element => message()}),
Acc1 = make_check(Acc, userlist(block_bob), ?BOB, out, block),
Acc2 = make_check(Acc1, userlist(block_bob), ?BOB, in, allow),
Acc3 = make_check(Acc2, userlist(none), ?JEFF, out, allow),
_Acc4 = make_check(Acc3, userlist(none), ?JEFF, in, allow),
% presence
NAcc = mongoose_acc:new(#{ location => ?LOCATION,
lserver => (?ALICE)#jid.lserver,
from_jid => ?ALICE,
to_jid => ?BOB,
element => presence() }),
NAcc = mongoose_acc:new(?ACC_PARAMS#{element => presence()}),
NAcc1 = make_check(NAcc, userlist(block_bob), ?BOB, out, block),
NAcc2 = make_check(NAcc1, userlist(block_bob), ?BOB, in, allow),
NAcc3 = make_check(NAcc2, userlist(block_bob), ?JEFF, out, allow),
Expand All @@ -120,11 +101,7 @@ check_with_changing_stanza(_C) ->
M = message(),
P = presence(),
Ul = userlist(deny_all_message),
Acc = mongoose_acc:new(#{ location => ?LOCATION,
lserver => (?ALICE)#jid.lserver,
from_jid => ?ALICE,
to_jid => ?BOB,
element => M }),
Acc = mongoose_acc:new(?ACC_PARAMS#{element => M}),
Acc1 = make_check({Acc, M}, Ul, ?BOB, out, allow),
Acc2 = make_check({Acc1, M}, Ul, ?BOB, in, deny),
Acc3 = make_check({Acc2, M}, Ul, ?JEFF, out, allow),
Expand Down
Loading

0 comments on commit a22f2e4

Please sign in to comment.