Skip to content

Commit 58c2bf8

Browse files
authored
Merge pull request #202 from esl/amoc-scenarios-common-parts
Common building blocks for scenario writing
2 parents f20bee4 + 3b762df commit 58c2bf8

File tree

3 files changed

+58
-14
lines changed

3 files changed

+58
-14
lines changed

src/escalus_session.erl

+13
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@
3030
bind/2,
3131
session/2]).
3232

33+
-export([send_presence_available/1,
34+
send_presence_unavailable/1]).
35+
3336
%% Public Types
3437
-export_type([feature/0,
3538
features/0,
@@ -125,6 +128,16 @@ session(Client) ->
125128
escalus:assert(is_iq_result, SessionReply),
126129
Client.
127130

131+
-spec send_presence_available(escalus:client()) -> ok.
132+
send_presence_available(Client) ->
133+
Pres = escalus_stanza:presence(<<"available">>),
134+
escalus_connection:send(Client, Pres).
135+
136+
-spec send_presence_unavailable(escalus:client()) -> ok.
137+
send_presence_unavailable(Client) ->
138+
Pres = escalus_stanza:presence(<<"unavailable">>),
139+
escalus_connection:send(Client, Pres).
140+
128141
-spec use_ssl(user_spec(), features()) -> boolean().
129142
use_ssl(Props, Features) ->
130143
UserNeedsSSL = proplists:get_value(starttls, Props, false),

src/escalus_stanza.erl

+43-14
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,13 @@
1818

1919
%% old ones
2020
-export([id/0,
21-
message/4,
21+
message/2, message/4,
2222
chat_to/2,
2323
chat/3,
2424
chat_to_short_jid/2,
2525
chat_without_carbon_to/2,
26+
chat_to_with_id_and_timestamp/2,
27+
chat_to_with_id/2,
2628
groupchat_to/2,
2729
iq_result/1,
2830
iq_result/2,
@@ -304,24 +306,51 @@ message(From, Recipient, Type, Msg) ->
304306
message1(From, Recipient, Type, Msg).
305307

306308
message1(From, Recipient, Type, Msg) ->
307-
FromAttr = case From of
308-
undefined -> [];
309-
_ -> [{<<"from">>, From}]
310-
end,
311-
#xmlel{name = <<"message">>,
312-
attrs = FromAttr ++ [{<<"type">>, Type},
313-
{<<"to">>, escalus_utils:get_jid(Recipient)}],
314-
children = [#xmlel{name = <<"body">>,
315-
children = [#xmlcdata{content = Msg}]}]}.
309+
AttrsIn = #{to => Recipient,
310+
type => Type},
311+
Attrs = case From of
312+
undefined -> AttrsIn;
313+
_ -> AttrsIn#{from => From}
314+
end,
315+
message(Msg, Attrs).
316+
317+
-spec message(binary(), Attrs) -> exml:element() when
318+
Attrs :: #{to => escalus_utils:jid_spec(),
319+
atom() | binary() => binary()}.
320+
message(Text, Attrs) ->
321+
maps:fold(fun (to, V, Stanza) -> setattr(Stanza, <<"to">>, escalus_utils:get_jid(V));
322+
(K, V, Stanza) when is_atom(K) -> setattr(Stanza, atom_to_binary(K, utf8), V);
323+
(K, V, Stanza) when is_binary(K) -> setattr(Stanza, K, V) end,
324+
#xmlel{name = <<"message">>,
325+
children = [#xmlel{name = <<"body">>,
326+
children = [#xmlcdata{content = Text}]}]},
327+
Attrs).
316328

317329
chat_to(Recipient, Msg) ->
318-
message(undefined, Recipient, <<"chat">>, Msg).
330+
message(Msg, #{type => <<"chat">>,
331+
to => Recipient}).
319332

320333
chat(Sender, Recipient, Msg) ->
321-
message(Sender, Recipient, <<"chat">>, Msg).
334+
message(Msg, #{type => <<"chat">>,
335+
from => Sender,
336+
to => Recipient}).
322337

323338
chat_to_short_jid(Recipient, Msg) ->
324-
chat_to(escalus_utils:get_short_jid(Recipient), Msg).
339+
message(Msg, #{type => <<"chat">>,
340+
to => escalus_utils:get_short_jid(Recipient)}).
341+
342+
-spec chat_to_with_id(escalus_utils:jid_spec(), binary()) -> exml:element().
343+
chat_to_with_id(Recipient, Msg) ->
344+
message(Msg, #{type => <<"chat">>,
345+
to => Recipient,
346+
id => uuid_v4()}).
347+
348+
-spec chat_to_with_id_and_timestamp(escalus_utils:jid_spec(), binary()) -> exml:element().
349+
chat_to_with_id_and_timestamp(Recipient, Msg) ->
350+
message(Msg, #{type => <<"chat">>,
351+
to => Recipient,
352+
id => uuid_v4(),
353+
timestamp => integer_to_binary(os:system_time(microsecond))}).
325354

326355
chat_without_carbon_to(Recipient, Msg) ->
327356
Stanza = #xmlel{children = Children} = chat_to(Recipient, Msg),
@@ -821,7 +850,7 @@ id() ->
821850

822851
-spec uuid_v4() -> binary().
823852
uuid_v4() ->
824-
iolist_to_binary(uuid:uuid_to_string(uuid:get_v4())).
853+
uuid:uuid_to_string(uuid:get_v4(), binary_standard).
825854

826855
%%--------------------------------------------------------------------
827856
%% Stanzas from inline XML

src/escalus_users.erl

+2
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,8 @@ is_mod_register_enabled(Config) ->
336336
| 'connection_steps' %% [escalus_session:step()]
337337
| 'parser_opts' %% a list of exml parser opts,
338338
%% e.g. infinite_stream
339+
| received_stanza_handlers %% list of escalus_connection:stanza_handler()
340+
| sent_stanza_handlers %% similar as above but for sent stanzas
339341
.
340342

341343
-type ejabberd_option() :: 'ejabberd_node'

0 commit comments

Comments
 (0)