Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multi tenancy hooks #3089

Merged
merged 12 commits into from
Apr 22, 2021
103 changes: 70 additions & 33 deletions test/acc_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,9 @@ groups() ->
init_from_element,
produce_iq_meta_automatically,
strip,
strip_with_params,
parse_with_cdata,
tries_to_get_host_type
tries_to_get_host_type_if_it_is_not_provided
]
}
].
Expand Down Expand Up @@ -150,11 +151,45 @@ strip(_C) ->
?assertEqual(HostType, mongoose_acc:host_type(Acc3)),
?assertEqual({FromJID, ToJID, El}, mongoose_acc:packet(Acc3)),
Ref = mongoose_acc:ref(Acc3),
%% strip stanza and check that only non-permanent fields are missing
NAcc1 = mongoose_acc:strip(Acc3),
{XMLNS3, NAcc2} = mongoose_iq:xmlns(NAcc1),
?assertEqual(<<"urn:ietf:params:xml:ns:xmpp-session">>, XMLNS3),
?assertEqual(<<"set">>, mongoose_acc:stanza_type(NAcc2)),
?assertEqual(Server, mongoose_acc:lserver(NAcc2)),
?assertEqual(HostType, mongoose_acc:host_type(NAcc2)),
?assertEqual({FromJID, ToJID, El}, mongoose_acc:packet(NAcc2)),
?assertEqual(Ref, mongoose_acc:ref(NAcc2)),
?assertEqual(997, mongoose_acc:get(ns, ppp, NAcc2)),
?assertEqual([], mongoose_acc:get(ns2, NAcc2)).

strip_with_params(_Config) ->
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 => iq_stanza(),
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(ns2, [{a, 1}, {b, 2}], Acc2),
?assertMatch([_, _], mongoose_acc:get(ns2, Acc3)),
?assertEqual(Server, mongoose_acc:lserver(Acc3)),
?assertEqual(HostType, mongoose_acc:host_type(Acc3)),
Ref = mongoose_acc:ref(Acc3),
%% strip stanza with params and check that new params are applied
%% and non-permanent fields are missing
NewServer = <<"test.", Server/binary>>,
NewHostType = <<"new ",HostType/binary>>,
NAcc1 = mongoose_acc:strip(#{lserver => NewServer,
host_type => NewHostType,
element => sample_stanza() }, Acc3),
NewStanza = sample_stanza(),
StripParams = #{lserver => NewServer,
host_type => NewHostType,
element => NewStanza},
NAcc1 = mongoose_acc:strip(StripParams, Acc3),
{XMLNS2, NAcc2} = mongoose_iq:xmlns(NAcc1),
?assertEqual(<<"urn:xmpp:blocking">>, XMLNS2),
?assertEqual(jid:from_binary(<<"a@localhost">>), mongoose_acc:to_jid(NAcc2)),
Expand All @@ -163,55 +198,57 @@ strip(_C) ->
?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) ->
?assertEqual(NewHostType, mongoose_acc:host_type(NAcc2)).

tries_to_get_host_type_if_it_is_not_provided(_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),
allocate_with_unknown_host_type_and_change_it_on_stripping(),
meck:reset(mongoose_domain_api),
allocate_with_known_host_type_and_change_it_on_stripping(),
meck:unload(mongoose_domain_api).

allocate_with_known_host_type_and_change_it_on_stripping() ->
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">>}),
%% host type changes from <<"host 1">> to 'undefined'
Acc2 = mongoose_acc:strip(StripParams#{lserver => <<"unknown.host1">>},Acc1),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor: missing commas before Acc1, this happens a few times below.

?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)),
Acc3 = mongoose_acc:strip(StripParams#{lserver => <<"host2">>},Acc1),
?assertEqual(<<"host 2">>, mongoose_acc:host_type(Acc3)),
?assertEqual(1, meck:num_calls(mongoose_domain_api, get_host_type, [<<"host2">>])),
%% check overall number of calls to mongoose_domain_api:get_host_type/1
?assertEqual(3, meck:num_calls(mongoose_domain_api, get_host_type, 1)).

allocate_with_unknown_host_type_and_change_it_on_stripping() ->
AccParams=#{location => ?LOCATION},
StripParams = #{element => sample_stanza()},
%% new acc with 'undefined' host type
Acc1 = mongoose_acc:new(AccParams#{lserver => <<"unknown.host1">>}),
?assertEqual(undefined, mongoose_acc:host_type(Acc1)),
?assertEqual(1, meck:num_calls(mongoose_domain_api, get_host_type, [<<"unknown.host1">>])),
%% 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">>])),
Acc2 = mongoose_acc:strip(StripParams#{lserver => <<"host1">>},Acc1),
?assertEqual(<<"host 1">>, mongoose_acc:host_type(Acc2)),
?assertEqual(1, 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)),
Acc3 = mongoose_acc:strip(StripParams#{lserver => <<"unknown.host2">>},Acc1),
?assertEqual(undefined, mongoose_acc:host_type(Acc3)),
?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).
%% check overall number of calls to mongoose_domain_api:get_host_type/1
?assertEqual(3, meck:num_calls(mongoose_domain_api, get_host_type, 1)).


sample_stanza() ->
{xmlel, <<"iq">>,
Expand Down