Skip to content

Commit

Permalink
Test group roster_advanced available only for mnesia backend for mod_…
Browse files Browse the repository at this point in the history
…roster

Command process_rosteritems is implemented only for mod_roster mnesia backend.
  • Loading branch information
ludwikbukowski committed Dec 2, 2015
1 parent 7a7a406 commit 3060355
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 69 deletions.
8 changes: 4 additions & 4 deletions apps/ejabberd/src/mod_admin_extra_private.erl
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@ private_get(Username, Host, Element, Ns) ->
end.

do_private_get(Username, Host, Element, Ns) ->
From = jlib:make_jid(Username, Host, <<"">>),
To = jlib:make_jid(Username, Host, <<"">>),
From = jid:make(Username, Host, <<"">>),
To = jid:make(Username, Host, <<"">>),
IQ = {iq, <<"">>, get, ?NS_PRIVATE, <<"">>,
#xmlel{ name = <<"query">>,
attrs = [{<<"xmlns">>,?NS_PRIVATE}],
Expand Down Expand Up @@ -114,8 +114,8 @@ private_set2(Username, Host, Xml) ->
do_private_set2(Username, Host, Xml) ->
case is_private_module_loaded(Host) of
true ->
From = jlib:make_jid(Username, Host, <<"">>),
To = jlib:make_jid(Username, Host, <<"">>),
From = jid:make(Username, Host, <<"">>),
To = jid:make(Username, Host, <<"">>),
IQ = {iq, <<"">>, set, ?NS_PRIVATE, <<"">>,
#xmlel{ name = <<"query">>,
attrs = [{<<"xmlns">>,?NS_PRIVATE}],
Expand Down
109 changes: 49 additions & 60 deletions apps/ejabberd/src/mod_admin_extra_vcard.erl
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
get_vcard_multi/4,
set_vcard/4,
set_vcard/5
]).
]).

-include("ejabberd.hrl").
-include("ejabberd_commands.hrl").
Expand Down Expand Up @@ -106,13 +106,13 @@ commands() ->
module = ?MODULE, function = set_vcard,
args = [{user, binary}, {host, binary}, {name, binary}, {subname, binary}, {contents, {list, binary}}],
result = {res, restuple}}
].
].

%%%
%%% Vcard
%%%
-spec get_vcard(ejabberd:user(), ejabberd:server(), any())
-> {error, string()} | [binary()].
-> {error, string()} | [binary()].
get_vcard(User, Host, Name) ->
case ejabberd_auth:is_user_exists(User, Host) of
true ->
Expand All @@ -132,7 +132,7 @@ get_vcard(User, Host, Name, Subname) ->
end.

-spec get_vcard_multi(ejabberd:user(), ejabberd:server(), any(), any())
-> {error, string()} | list(binary()).
-> {error, string()} | list(binary()).
get_vcard_multi(User, Host, Name, Subname) ->
case ejabberd_auth:is_user_exists(User, Host) of
true ->
Expand Down Expand Up @@ -174,21 +174,16 @@ get_module_resource(Server) ->


-spec get_vcard_content(ejabberd:user(), ejabberd:server(), any())
-> {error, string()} | list(binary()).
-> {error, string()} | list(binary()).
get_vcard_content(User, Server, Data) ->
case ejabberd_auth:is_user_exists(User, Server) of
true ->
[{_, Module, Function, _Opts}] = ets:lookup(sm_iqtable, {?NS_VCARD, Server}),
JID = jid:make(User, Server, list_to_binary(get_module_resource(Server))),
IQ = #iq{type = get, xmlns = ?NS_VCARD},
%% TODO: This may benefit from better type control
IQr = Module:Function(JID, JID, IQ),
case IQr#iq.sub_el of
[A1] ->
case get_vcard(Data, A1) of
[] -> error_no_value_found_in_vcard;
ElemList -> [exml_query:cdata(Elem) || Elem <- ElemList]
end;
[{_, Module, Function, _Opts}] = ets:lookup(sm_iqtable, {?NS_VCARD, Server}),
JID = jid:make(User, Server, list_to_binary(get_module_resource(Server))),
IQ = #iq{type = get, xmlns = ?NS_VCARD},
%% TODO: This may benefit from better type control
IQr = Module:Function(JID, JID, IQ),
case IQr#iq.sub_el of
[A1] ->
case get_vcard(Data, A1) of
[] ->
{error, "Value not found in vcard"};
ElemList ->
Expand All @@ -209,57 +204,51 @@ get_vcard([Data], A1) ->
exml_query:subelements(A1, Data).

-spec set_vcard_content(ejabberd:user(), ejabberd:server(), Data :: [binary()],
ContentList :: binary() | [binary()]) -> {ok, string()}.
ContentList :: binary() | [binary()]) -> {ok, string()}.
set_vcard_content(U, S, D, SomeContent) when is_binary(SomeContent) ->
set_vcard_content(U, S, D, [SomeContent]);
set_vcard_content(User, Server, Data, ContentList) ->
case ejabberd_auth:is_user_exists(User, Server) of
true ->
[{_, Module, Function, _Opts}] = ets:lookup(sm_iqtable, {?NS_VCARD, Server}),
JID = jid:make(User, Server, <<>>),
IQ = #iq{type = get, xmlns = ?NS_VCARD},
IQr = Module:Function(JID, JID, IQ),

%% Get old vcard
A4 = case IQr#iq.sub_el of
[A1] ->
{_, _, _, A2} = A1,
update_vcard_els(Data, ContentList, A2);
_ ->
update_vcard_els(Data, ContentList, [])
end,

%% Build new vcard
SubEl = #xmlel{ name = <<"vCard">>, attrs = [{<<"xmlns">>,<<"vcard-temp">>}], children = A4},
IQ2 = #iq{type=set, sub_el = SubEl},

Module:Function(JID, JID, IQ2),
ok;
false ->
error_user_not_exists
end.
[{_, Module, Function, _Opts}] = ets:lookup(sm_iqtable, {?NS_VCARD, Server}),
JID = jid:make(User, Server, <<>>),
IQ = #iq{type = get, xmlns = ?NS_VCARD},
IQr = Module:Function(JID, JID, IQ),

%% Get old vcard
A4 = case IQr#iq.sub_el of
[A1] ->
{_, _, _, A2} = A1,
update_vcard_els(Data, ContentList, A2);
_ ->
update_vcard_els(Data, ContentList, [])
end,

%% Build new vcard
SubEl = #xmlel{name = <<"vCard">>, attrs = [{<<"xmlns">>, <<"vcard-temp">>}], children = A4},
IQ2 = #iq{type = set, sub_el = SubEl},
Module:Function(JID, JID, IQ2),
{ok, ""}.

-spec update_vcard_els(Data :: [binary(),...],
ContentList :: [binary() | string()],
Els :: [jlib:xmlcdata() | jlib:xmlel()]
) -> [jlib:xmlcdata() | jlib:xmlel()].
) -> [jlib:xmlcdata() | jlib:xmlel()].
update_vcard_els(Data, ContentList, Els1) ->
Els2 = lists:keysort(2, Els1),
[Data1 | Data2] = Data,
NewEls = case Data2 of
[] ->
[#xmlel{ name = Data1, children = [#xmlcdata{content = Content}] } || Content <- ContentList];
[D2] ->
OldEl = case lists:keysearch(Data1, 2, Els2) of
{value, A} -> A;
false -> #xmlel{ name = Data1 }
end,
ContentOld1 = OldEl#xmlel.children,
Content2 = [#xmlel{ name = D2, children = [#xmlcdata{content=Content}]} || Content <- ContentList],
ContentOld2 = [A || {_, X, _, _} = A <- ContentOld1, X/=D2],
ContentOld3 = lists:keysort(2, ContentOld2),
ContentNew = lists:keymerge(2, Content2, ContentOld3),
[#xmlel{ name = Data1, children = ContentNew}]
end,
[] ->
[#xmlel{ name = Data1, children = [#xmlcdata{content = Content}] } || Content <- ContentList];
[D2] ->
OldEl = case lists:keysearch(Data1, 2, Els2) of
{value, A} -> A;
false -> #xmlel{ name = Data1 }
end,
ContentOld1 = OldEl#xmlel.children,
Content2 = [#xmlel{ name = D2, children = [#xmlcdata{content=Content}]} || Content <- ContentList],
ContentOld2 = [A || {_, X, _, _} = A <- ContentOld1, X/=D2],
ContentOld3 = lists:keysort(2, ContentOld2),
ContentNew = lists:keymerge(2, Content2, ContentOld3),
[#xmlel{ name = Data1, children = ContentNew}]
end,
Els3 = lists:keydelete(Data1, 2, Els2),
lists:keymerge(2, NewEls, Els3).
lists:keymerge(2, NewEls, Els3).
2 changes: 0 additions & 2 deletions test/ejabberd_tests/t.spec
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,3 @@
{config, ["test.config"]}.
{logdir, "ct_report"}.
{ct_hooks, [ct_tty_hook, ct_mongoose_hook]}.
%%To enable printing group and case enters on server side
%%{ct_hooks, [{ct_tty_hook, [print_group, print_case]}]}.
14 changes: 11 additions & 3 deletions test/ejabberd_tests/tests/ejabberdctl_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,7 @@ all() ->
{group, sessions},
{group, vcard},
{group, roster},
%% Use this group only for Mnesia tests.
%% {group, roster_advanced},
{group, roster_advanced},
{group, last},
{group, private},
{group, stanza},
Expand All @@ -60,7 +59,7 @@ groups() ->
{last, [sequence], last()},
{private, [sequence], private()},
{stanza, [sequence], stanza()},
%% {roster_advanced, [sequence], roster_advanced()},
{roster_advanced, [sequence], roster_advanced()},
{basic, [sequence], basic()},
{stats, [sequence], stats()}].

Expand Down Expand Up @@ -141,6 +140,15 @@ init_per_group(vcard, Config) ->
_ ->
Config
end;

init_per_group(roster_advanced, Config) ->
case escalus_ejabberd:rpc(gen_mod,get_module_opt,[ct:get_config(ejabberd_domain), mod_roster, backend, mnesia]) of
mnesia ->
Config;
_ ->
{skip, command_process_rosteritems_supports_only_mnesia}
end;

init_per_group(_GroupName, Config) ->
Config.

Expand Down

0 comments on commit 3060355

Please sign in to comment.