PageRenderTime 1148ms CodeModel.GetById 171ms app.highlight 813ms RepoModel.GetById 154ms app.codeStats 0ms

/ucengine/src/tests/event_tests.erl

http://github.com/AF83/ucengine
Erlang | 886 lines | 795 code | 72 blank | 19 comment | 0 complexity | 01cb0b31a4b83d15ca34ae43c3858697 MD5 | raw file
  1%%
  2%%  U.C.Engine - Unified Collaboration Engine
  3%%  Copyright (C) 2011 af83
  4%%
  5%%  This program is free software: you can redistribute it and/or modify
  6%%  it under the terms of the GNU Affero General Public License as published by
  7%%  the Free Software Foundation, either version 3 of the License, or
  8%%  (at your option) any later version.
  9%%
 10%%  This program is distributed in the hope that it will be useful,
 11%%  but WITHOUT ANY WARRANTY; without even the implied warranty of
 12%%  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 13%%  GNU Affero General Public License for more details.
 14%%
 15%%  You should have received a copy of the GNU Affero General Public License
 16%%  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 17%%
 18-module(event_tests).
 19
 20-include("uce.hrl").
 21-include_lib("eunit/include/eunit.hrl").
 22
 23-export([send_long_polling_event/2, send_long_polling_event/3]).
 24
 25setup_events(Domain) ->
 26    {ok, Participant} = uce_user:get_by_name(Domain, "participant.user@af83.com"),
 27    {ok, User2} = uce_user:get_by_name(Domain, "user_2"),
 28    {ok, User3} = uce_user:get_by_name(Domain, "user_3"),
 29
 30    uce_event:add(Domain,
 31                  #uce_event{ id=none,
 32                              type="test_event_1",
 33                              location="testmeeting",
 34                              from=Participant#uce_user.id}),
 35    timer:sleep(10),
 36    uce_event:add(Domain,
 37                  #uce_event{ id=none,
 38                              type="test_event_2",
 39                              location="testmeeting",
 40                              from=User2#uce_user.id}),
 41    timer:sleep(10),
 42    uce_event:add(Domain,
 43                  #uce_event{ id=none,
 44                              type="test_event_3",
 45                              location="testmeeting",
 46                              from=User3#uce_user.id,
 47                              metadata=json_helpers:to_struct([{"description", "test"}])}),
 48    ok.
 49
 50event_test_() ->
 51    { setup
 52      , fun() ->
 53                [Domain, BaseUrl, Testers] = fixtures:setup(),
 54                setup_events(Domain),
 55                [Domain, BaseUrl, Testers]
 56        end
 57      , fun fixtures:teardown/1
 58      , fun([_, BaseUrl, [Root, Participant, Ugly|_]]) ->
 59                [?_test(test_push(BaseUrl, Root)),
 60                 ?_test(test_push_big(BaseUrl, Root)),
 61                 ?_test(test_push_internal_event(BaseUrl, Root)),
 62                 ?_test(test_push_without_meeting(BaseUrl, Root)),
 63                 ?_test(test_push_with_parent(BaseUrl, Root)),
 64                 ?_test(test_push_to_me(BaseUrl, Root)),
 65                 ?_test(test_push_to_unauthorized(BaseUrl, Root, Participant, Ugly)),
 66                 ?_test(test_push_to_other(BaseUrl, Root, Participant)),
 67                 ?_test(test_push_json(BaseUrl, Root)),
 68
 69                 ?_test(test_push_missing_type(BaseUrl, Root)),
 70                 ?_test(test_push_not_found_meeting(BaseUrl, Root)),
 71                 ?_test(test_push_not_found_parent(BaseUrl, Root)),
 72                 ?_test(test_push_not_found_to(BaseUrl, Root)),
 73
 74                 ?_test(test_get(BaseUrl, Root)),
 75                 ?_test(test_get_without_meeting(BaseUrl, Root)),
 76                 ?_test(test_get_with_keywords(BaseUrl, Root)),
 77                 ?_test(test_get_with_keywords_without_meeting(BaseUrl, Root)),
 78                 ?_test(test_get_with_keywords_with_from(BaseUrl, Root, Participant)),
 79                 ?_test(test_get_with_keywords_in_metadata(BaseUrl, Root)),
 80                 ?_test(test_get_with_keywords_and_timestart_and_timeend(BaseUrl, Root)),
 81                 ?_test(test_get_with_type(BaseUrl, Root)),
 82                 ?_test(test_get_with_types(BaseUrl, Root)),
 83                 ?_test(test_get_with_type_and_timestart(BaseUrl, Root)),
 84                 ?_test(test_get_with_type_and_timestart_and_timeend(BaseUrl, Root)),
 85                 ?_test(test_get_with_type_and_timeend(BaseUrl, Root)),
 86                 ?_test(test_last(BaseUrl, Root)),
 87                 ?_test(test_long_polling(BaseUrl, Root)),
 88                 ?_test(test_long_polling_with_types(BaseUrl, Root)),
 89                 ?_test(test_long_polling_with_to(BaseUrl, Root, Participant)),
 90                 ?_test(test_long_polling_with_to_and_participant(BaseUrl, Root, Participant))
 91                ]
 92        end}.
 93
 94event_timeout_test_() ->
 95    { setup
 96      , fun() ->
 97                [Domain, BaseUrl, Testers] = fixtures:setup(),
 98                setup_events(Domain),
 99                % Set the timeout to a low value to avoid waiting too long.
100                PreviousTimeout = config:get(connection_timeout),
101                config:set(connection_timeout, 10),
102                [Domain, BaseUrl, Testers, PreviousTimeout]
103        end
104      , fun ([Domain, BaseUrl, Testers, PreviousTimeout]) ->
105                %% Restore the previous long polling timeout
106                config:set(connection_timeout, PreviousTimeout),
107                fixtures:teardown([Domain, BaseUrl, Testers])
108        end
109      , fun([Domain, BaseUrl, [Root, Participant, Ugly|_], _]) ->
110                [
111                 {timeout, 40, ?_test(test_long_polling_with_from(BaseUrl, Root))},
112                 {timeout, 40, ?_test(test_long_polling_with_other_from(BaseUrl, Root))},
113                 {timeout, 40, ?_test(test_long_polling_with_parent(Domain, BaseUrl, Root))},
114                 {timeout, 40, ?_test(test_long_polling_with_other_parent(BaseUrl, Root))},
115                 {timeout, 40, ?_test(test_long_polling_with_search(BaseUrl, Root))},
116                 {timeout, 40, ?_test(test_long_polling_with_other_search(BaseUrl, Root))},
117                 {timeout, 40, ?_test(test_long_polling_with_to_and_other_participant(BaseUrl, Root, Participant, Ugly))}
118                ]
119        end}.
120
121auth_params({Uid, Sid}) ->
122    [{"uid", Uid},
123     {"sid", Sid}].
124
125post_event(BaseUrl, Meeting, AuthParams, Params) ->
126    {struct, [{"result", Id}]} = tests_utils:post(BaseUrl, "/event/"++ Meeting, Params ++ auth_params(AuthParams)),
127    Id.
128
129test_push(BaseUrl, {RootUid, _RootSid} = AuthParams) ->
130    Params = [{"type", "test_push_1"},
131              {"metadata[description]", "pushed_event"}],
132    Id = post_event(BaseUrl, "testmeeting", AuthParams, Params),
133    ?assertMatch({struct, [{"result",
134                            {struct, [{"type", "test_push_1"},
135                                      {"domain", _},
136                                      {"datetime", _},
137                                      {"id", Id},
138                                      {"location", "testmeeting"},
139                                      {"from", RootUid},
140                                      {"metadata", {struct, [{"description", "pushed_event"}]}}]}}]},
141                 tests_utils:get(BaseUrl, "/event/testmeeting/" ++ Id, auth_params(AuthParams))).
142
143test_push_big(BaseUrl, {RootUid, _RootSid} = AuthParams) ->
144    Text = string:copies("pushed_event", 5000),
145    Params = [{"type", "test_push_1"},
146              {"metadata[description]", Text}],
147    Id = post_event(BaseUrl, "testmeeting", AuthParams, Params),
148    ?assertMatch({struct, [{"result",
149                            {struct, [{"type", "test_push_1"},
150                                      {"domain", _},
151                                      {"datetime", _},
152                                      {"id", Id},
153                                      {"location", "testmeeting"},
154                                      {"from", RootUid},
155                                      {"metadata", {struct, [{"description", Text}]}}]}}]},
156                 tests_utils:get(BaseUrl, "/event/testmeeting/" ++ Id, auth_params(AuthParams))).
157
158test_push_internal_event(BaseUrl, AuthParams) ->
159    Params = [{"type", "internal.roster.add"},
160              {"metadata[description]", "pushed_event"}] ++ auth_params(AuthParams),
161    {struct, [{"error", "unauthorized"}]} = tests_utils:post(BaseUrl, "/event/testmeeting", Params).
162
163test_push_without_meeting(BaseUrl, {RootUid, _RootSid} = AuthParams) ->
164    Params = [{"type", "test_push_1"},
165              {"metadata[description]", "pushed_event"}],
166    Id = post_event(BaseUrl, "", AuthParams, Params),
167    {struct, [{"result",
168               {struct, [{"type", "test_push_1"},
169                         {"domain", _},
170                         {"datetime", _},
171                         {"id", Id},
172                         {"from", RootUid},
173                         {"metadata", {struct, [{"description", "pushed_event"}]}}]}}]} =
174                   tests_utils:get(BaseUrl, "/event/all/" ++ Id, auth_params(AuthParams)).
175
176test_push_with_parent(BaseUrl, {RootUid, _RootSid} = AuthParams) ->
177    Params = [{"type", "test_push_1"},
178              {"metadata[description]", "pushed_event"}],
179    ParentId = post_event(BaseUrl, "testmeeting", AuthParams, Params),
180
181    ParamsChild = [{"type", "test_push_1"},
182                   {"parent", ParentId},
183                   {"metadata[description]", "pushed_event"}],
184    ChildId = post_event(BaseUrl,"testmeeting", AuthParams, ParamsChild),
185
186    {struct, [{"result",
187               {struct, [{"type", "test_push_1"},
188                         {"domain", _},
189                         {"datetime", _},
190                         {"id", ChildId},
191                         {"location", "testmeeting"},
192                         {"from", RootUid},
193                         {"parent", ParentId},
194                         {"metadata", {struct, [{"description", "pushed_event"}]}}]}}]} =
195                   tests_utils:get(BaseUrl, "/event/testmeeting/" ++ ChildId, auth_params(AuthParams)).
196
197test_push_to_me(BaseUrl, {RootUid, _RootSid} = AuthParams) ->
198    Params = [{"type", "test_push_1"},
199              {"to", RootUid},
200              {"metadata[description]", "pushed_event"}],
201    Id = post_event(BaseUrl, "testmeeting", AuthParams, Params),
202    {struct, [{"result",
203               {struct, [{"type", "test_push_1"},
204                         {"domain", _},
205                         {"datetime", _},
206                         {"id", Id},
207                         {"location", "testmeeting"},
208                         {"to", RootUid},
209                         {"from", RootUid},
210                         {"metadata", {struct, [{"description", "pushed_event"}]}}]}}]} =
211                   tests_utils:get(BaseUrl, "/event/testmeeting/" ++ Id, auth_params(AuthParams)).
212
213test_push_to_unauthorized(BaseUrl,
214                          RootAuthParams,
215                          ParticipantParams,
216                          {UglyUid, _UglySid}) ->
217    Params = auth_params(ParticipantParams) ++ [{"type", "test_push_1"},
218                                                {"to", UglyUid},
219                                                {"metadata[description]", "pushed_event"}],
220    {struct, [{"result", _Id}]} =
221        tests_utils:post(BaseUrl, "/event/testmeeting", Params),
222
223    ParamsRoot = auth_params(RootAuthParams) ++ [{"type", "test_push_to_other"}],
224     {struct, [{"result", {array, []}}]} =
225         tests_utils:get(BaseUrl, "/event/testmeeting/", ParamsRoot).
226
227test_push_to_other(BaseUrl, {RootUid, RootSid}, {ParticipantUid, ParticipantSid}) ->
228    Params = [{"uid", RootUid},
229              {"sid", RootSid},
230              {"type", "test_push_to_other"},
231              {"to", ParticipantUid},
232              {"metadata[description]", "pushed_event"}],
233
234    {struct, [{"result", Id}]} =
235        tests_utils:post(BaseUrl, "/event/testmeeting", Params),
236
237    {struct, [{"error", "unauthorized"}]} =
238        tests_utils:get(BaseUrl, "/event/testmeeting/" ++ Id, Params),
239
240    ParamsParticipant = [{"uid", ParticipantUid},
241                         {"sid", ParticipantSid},
242                         {"type", "test_push_to_other"}],
243    {struct, [{"result", {array, [
244               {struct, [{"type", "test_push_to_other"},
245                         {"domain", _},
246                         {"datetime", _},
247                         {"id", Id},
248                         {"location", "testmeeting"},
249                         {"to", "participant.user@af83.com"},
250                         {"from", RootUid},
251                         {"metadata", {struct, [{"description", "pushed_event"}]}}]}]}}]} =
252        tests_utils:get(BaseUrl, "/event/testmeeting/", ParamsParticipant),
253
254    ParamsRootGet = [{"uid", RootUid},
255                     {"sid", RootSid},
256                     {"type", "test_push_to_other"}],
257    {struct, [{"result", {array, [
258               {struct, [{"type", "test_push_to_other"},
259                         {"domain", _},
260                         {"datetime", _},
261                         {"id", Id},
262                         {"location", "testmeeting"},
263                         {"to", "participant.user@af83.com"},
264                         {"from", RootUid},
265                         {"metadata", {struct, [{"description", "pushed_event"}]}}]}]}}]} =
266        tests_utils:get(BaseUrl, "/event/testmeeting/", ParamsRootGet).
267
268test_push_json(BaseUrl, AuthParams) ->
269    RequestBody = {struct, [{"type", "test_json"},
270                            {"metadata", {struct, [{"complex", {array, [10, {struct, [{"name", "plip"},
271                                                                                      {"hop", null},
272                                                                                      {"hop2", true},
273                                                                                      {"hop3", false}]}]}}]}}]
274                                ++ auth_params(AuthParams)},
275    {ok, "201", _, Body} =
276        tests_utils:post_raw(BaseUrl, "/event/testmeeting", [], "application/json", mochijson:encode(RequestBody)),
277    {struct, [{"result", Id}]} = mochijson:decode(Body),
278    {struct, [{"result",
279               {struct, [{"type", "test_json"},
280                         {"domain", _},
281                         {"datetime", _},
282                         {"id", Id},
283                         {"location", "testmeeting"},
284                         {"from", _},
285                         {"metadata", {struct, [{"complex", {array, [10, {struct, [{"name", "plip"},
286                                                                                   {"hop", null},
287                                                                                   {"hop2", true},
288                                                                                   {"hop3", false}
289                                                                                  ]}]}}]}}]}}]} =
290                   tests_utils:get(BaseUrl, "/event/testmeeting/" ++ Id, auth_params(AuthParams)).
291
292test_push_missing_type(BaseUrl, AuthParams) ->
293    Params = auth_params(AuthParams) ++ [{"metadata[description]", "pushed_event"}],
294    {struct, [{"error", "missing_parameters"}, {"infos", _}]} =
295        tests_utils:post(BaseUrl, "/event/testmeeting", Params).
296
297test_push_not_found_meeting(BaseUrl, AuthParams) ->
298    Params = auth_params(AuthParams) ++ [{"type", "test_push_1"},
299                                         {"metadata[description]", "pushed_event"}],
300    {struct, [{"error", "not_found"}]} =
301        tests_utils:post(BaseUrl, "/event/unexistentmeeting", Params).
302
303test_push_not_found_parent(BaseUrl, AuthParams) ->
304    ParamsChild = auth_params(AuthParams) ++ [{"type", "test_push_1"},
305                                              {"parent", "unexistent_id"},
306                                              {"metadata[description]", "pushed_event"}],
307    {struct, [{"error", "not_found"}]} =
308        tests_utils:post(BaseUrl, "/event/testmeeting", ParamsChild).
309
310test_push_not_found_to(BaseUrl, AuthParams) ->
311    Params = auth_params(AuthParams) ++ [{"type", "test_push_1"},
312                                         {"to", "unexistent_user"},
313                                         {"metadata[description]", "pushed_event"}],
314    {struct, [{"error", "not_found"}]} =
315        tests_utils:post(BaseUrl, "/event/testmeeting", Params).
316
317test_get(BaseUrl, AuthParams) ->
318    ?assertMatch({struct, [{"result", {array,
319                          [ {struct, [{"type", "test_event_1"}
320                                      , {"domain", _}
321                                      , {"datetime", _}
322                                      , {"id", _}
323                                      , {"location", "testmeeting"}
324                                      , {"from", _}
325                                      , {"metadata", {struct, []}}
326                                     ]},
327                            {struct, [{"type", "test_event_2"}
328                                      , {"domain", _}
329                                      , {"datetime", _}
330                                      , {"id", _}
331                                      , {"location", "testmeeting"}
332                                      , {"from", _}
333                                      , {"metadata", {struct, []}}
334                                     ]},
335                            {struct, [{"type", "test_event_3"}
336                                      , {"domain", _}
337                                      , {"datetime", _}
338                                      , {"id", _}
339                                      , {"location", "testmeeting"}
340                                      , {"from", _}
341                                      , {"metadata", {struct, [{"description", "test"}]}}
342                                     ]}|_]
343                         }}]}, tests_utils:get(BaseUrl, "/event/testmeeting", auth_params(AuthParams))).
344
345test_get_without_meeting(BaseUrl, AuthParams) ->
346    Params = [{"type", "non_global_event"},
347              {"metadata[description]", "plop2"}],
348    post_event(BaseUrl, "testmeeting", AuthParams, Params),
349
350    timer:sleep(1000),
351
352    ParamsGet = auth_params(AuthParams) ++ [{"order", "desc"},
353                                            {"count", "1"}],
354    Result = tests_utils:get(BaseUrl, "/event/", ParamsGet),
355    ?assertMatch({struct, [{"result", {array,
356                          [{struct, [{"type", "non_global_event"}
357                                     , {"domain", _}
358                                     , {"datetime", _}
359                                     , {"id", _}
360                                     , {"location", "testmeeting"}
361                                     , {"from", _}
362                                     , {"metadata", {struct, [{"description", "plop2"}]}}
363                                    ]}]}}]},
364                 Result).
365
366test_get_with_keywords(BaseUrl, AuthParams) ->
367    Params = [{"type", "search_event"},
368              {"metadata[description]", "lonely event"}],
369    post_event(BaseUrl, "testmeeting", AuthParams, Params),
370
371    timer:sleep(1000),
372
373    ParamsGet = auth_params(AuthParams) ++ [{"search", "lonely"},
374                                            {"count", "1"}],
375    ?assertMatch({struct, [{"result", {array,
376                          [{struct, [{"type", "search_event"}
377                                     , {"domain", _}
378                                     , {"datetime", _}
379                                     , {"id", _}
380                                     , {"location", "testmeeting"}
381                                     , {"from", _}
382                                     , {"metadata", {struct, [{"description", "lonely event"}]}}
383                                    ]}]}}]},
384                 tests_utils:get(BaseUrl, "/event/testmeeting", ParamsGet)).
385
386test_get_with_keywords_without_meeting(BaseUrl, {RootUid, RootSid}) ->
387    Params = [{"uid", RootUid},
388              {"sid", RootSid},
389              {"type", "search_event"},
390              {"metadata[description]", "lonely hungry event"}],
391    {struct, [{"result", _}]} = tests_utils:post(BaseUrl, "/event/testmeeting", Params),
392
393    ParamsGet = [{"uid", RootUid},
394                 {"sid", RootSid},
395                 {"search", "hungry"},
396                 {"count", "1"}],
397    ?assertMatch({struct, [{"result", {array,
398                          [{struct, [{"type", "search_event"}
399                                     , {"domain", _}
400                                     , {"datetime", _}
401                                     , {"id", _}
402                                     , {"location", "testmeeting"}
403                                     , {"from", RootUid}
404                                     , {"metadata", {struct, [{"description", "lonely hungry event"}]}}
405                                    ]}]}}]}, tests_utils:get(BaseUrl, "/event/", ParamsGet)).
406
407test_get_with_keywords_with_from(BaseUrl, {RootUid, RootSid}, {ParticipantUid, ParticipantSid}) ->
408    Params = [{"uid", RootUid},
409              {"sid", RootSid},
410              {"type", "search_event_test_from"},
411              {"metadata[description]", "lonely event"}],
412    {struct, [{"result", _}]} = tests_utils:post(BaseUrl, "/event/testmeeting", Params),
413
414    Params2 = [{"uid", ParticipantUid},
415              {"sid", ParticipantSid},
416              {"type", "search_event_test_from"},
417              {"metadata[description]", "lonely event"}],
418    {struct, [{"result", _}]} = tests_utils:post(BaseUrl, "/event/testmeeting", Params2),
419
420    timer:sleep(1000),
421
422    ParamsGet = [{"uid", RootUid},
423                 {"sid", RootSid},
424                 {"from", RootUid},
425                 {"search", "lonely"},
426                 {"order", "desc"},
427                 {"count", "1"}],
428    ?assertMatch({struct, [{"result", {array,
429                          [{struct, [{"type", "search_event_test_from"}
430                                     , {"domain", _}
431                                     , {"datetime", _}
432                                     , {"id", _}
433                                     , {"location", "testmeeting"}
434                                     , {"from", RootUid}
435                                     , {"metadata", {struct, [{"description", "lonely event"}]}}
436                                    ]}]}}]},
437                 tests_utils:get(BaseUrl, "/event/testmeeting", ParamsGet)).
438
439test_get_with_keywords_in_metadata(BaseUrl, {RootUid, RootSid}) ->
440    Params = [{"uid", RootUid},
441              {"sid", RootSid},
442              {"type", "search_event"},
443              {"metadata[description]", "lonely happy event"}],
444    {struct, [{"result", _}]} = tests_utils:post(BaseUrl, "/event/testmeeting", Params),
445
446    timer:sleep(1000),
447
448    ParamsGet = [{"uid", RootUid},
449                 {"sid", RootSid},
450                 {"search", "happy"},
451                 {"count", "1"}],
452    ?assertMatch({struct, [{"result", {array,
453                                       [{struct, [{"type", "search_event"}
454                                                  , {"domain", _}
455                                                  , {"datetime", _}
456                                                  , {"id", _}
457                                                  , {"location", "testmeeting"}
458                                                  , {"from", RootUid}
459                                                  , {"metadata", {struct, [{"description", "lonely happy event"}]}}
460                                                 ]}]}}]},
461                 tests_utils:get(BaseUrl, "/event/testmeeting", ParamsGet)).
462
463test_get_with_keywords_and_timestart_and_timeend(BaseUrl, {RootUid, RootSid}) ->
464    Params = [{"uid", RootUid},
465              {"sid", RootSid}],
466    {struct, [{"result", {array,
467                          [ {struct, [{"type", "test_event_1"}
468                                      , {"domain", _}
469                                      , {"datetime", _}
470                                      , {"id", _}
471                                      , {"location", "testmeeting"}
472                                      , {"from", _}
473                                      , {"metadata", {struct, []}}
474                                     ]},
475                            {struct, [{"type", "test_event_2"}
476                                      , {"domain", _}
477                                      , {"datetime", _}
478                                      , {"id", _}
479                                      , {"location", "testmeeting"}
480                                      , {"from", _}
481                                      , {"metadata", {struct, []}}
482                                     ]},
483                            {struct, [{"type", "test_event_3"}
484                                      , {"domain", _}
485                                      , {"datetime", Third}
486                                      , {"id", _}
487                                      , {"location", "testmeeting"}
488                                      , {"from", _}
489                                      , {"metadata", {struct, [{"description", "test"}]}}
490                                     ]}|_]
491                         }}]} = tests_utils:get(BaseUrl, "/event/testmeeting", Params),
492
493    ParamsGetStart = [{"uid", RootUid},
494                      {"sid", RootSid},
495                      {"type", "test_event_3"},
496                      {"search", "test"},
497                      {"start", integer_to_list(Third)},
498                      {"end", integer_to_list(Third + 1)}],
499    {struct, [{"result", {array,
500                          [ {struct, [{"type", "test_event_3"}
501                                      , {"domain", _}
502                                      , {"datetime", Third}
503                                      , {"id", _}
504                                      , {"location", "testmeeting"}
505                                      , {"from", _}
506                                      , {"metadata", {struct, [{"description", "test"}]}}
507                                     ]}|_]
508                         }}]} = tests_utils:get(BaseUrl, "/event/testmeeting/", ParamsGetStart),
509    ParamsGetNothing = [{"uid", RootUid},
510                        {"sid", RootSid},
511                        {"type", "test_event_3"},
512                        {"search", "test"},
513                        {"start", integer_to_list(Third - 2)},
514                        {"end", integer_to_list(Third - 1)}],
515    {struct, [{"result", {array,[]}}]} =
516        tests_utils:get(BaseUrl, "/event/testmeeting/", ParamsGetNothing).
517
518test_get_with_type(BaseUrl, {RootUid, RootSid}) ->
519    Params = [{"uid", RootUid},
520              {"sid", RootSid}],
521    {struct, [{"result", {array,
522                          [ {struct, [{"type", "test_event_1"}
523                                      , {"domain", _}
524                                      , {"datetime", _}
525                                      , {"id", _}
526                                      , {"location", "testmeeting"}
527                                      , {"from", _}
528                                      , {"metadata", {struct, []}}
529                                     ]},
530                            {struct, [{"type", "test_event_2"}
531                                      , {"domain", _}
532                                      , {"datetime", _}
533                                      , {"id", _}
534                                      , {"location", "testmeeting"}
535                                      , {"from", _}
536                                      , {"metadata", {struct, []}}
537                                     ]},
538                            {struct, [{"type", "test_event_3"}
539                                      , {"domain", _}
540                                      , {"datetime", Third}
541                                      , {"id", _}
542                                      , {"location", "testmeeting"}
543                                      , {"from", _}
544                                      , {"metadata", {struct, [{"description", "test"}]}}
545                                     ]}|_]
546                         }}]} = tests_utils:get(BaseUrl, "/event/testmeeting", Params),
547    ParamsGetStart = [{"uid", RootUid},
548                      {"sid", RootSid},
549                      {"type", "test_event_3"}],
550    ?assertMatch({struct, [{"result", {array,
551                                       [ {struct, [{"type", "test_event_3"}
552                                                   , {"domain", _}
553                                                   , {"datetime", Third}
554                                                   , {"id", _}
555                                                   , {"location", "testmeeting"}
556                                                   , {"from", _}
557                                                   , {"metadata", {struct, [{"description", "test"}]}}
558                                                  ]}|_]
559                                      }}]},
560                 tests_utils:get(BaseUrl, "/event/testmeeting/", ParamsGetStart)).
561
562test_get_with_types(BaseUrl, {RootUid, RootSid}) ->
563    Params = [{"uid", RootUid},
564              {"sid", RootSid}],
565    {struct, [{"result", {array,
566                          [ {struct, [{"type", "test_event_1"}
567                                      , {"domain", _}
568                                      , {"datetime", _}
569                                      , {"id", _}
570                                      , {"location", "testmeeting"}
571                                      , {"from", _}
572                                      , {"metadata", {struct, []}}
573                                     ]},
574                            {struct, [{"type", "test_event_2"}
575                                      , {"domain", _}
576                                      , {"datetime", _}
577                                      , {"id", _}
578                                      , {"location", "testmeeting"}
579                                      , {"from", _}
580                                      , {"metadata", {struct, []}}
581                                     ]},
582                            {struct, [{"type", "test_event_3"}
583                                      , {"domain", _}
584                                      , {"datetime", Third}
585                                      , {"id", _}
586                                      , {"location", "testmeeting"}
587                                      , {"from", _}
588                                      , {"metadata", {struct, [{"description", "test"}]}}
589                                     ]}|_]
590                         }}]} = tests_utils:get(BaseUrl, "/event/testmeeting", Params),
591    ParamsGetStart = [{"uid", RootUid},
592                      {"sid", RootSid},
593                      {"type", "test_event_3,test_event_1"}],
594    ?assertMatch({struct, [{"result", {array,
595                                       [ {struct, [{"type", "test_event_1"}
596                                                   , {"domain", _}
597                                                   , {"datetime", _}
598                                                   , {"id", _}
599                                                   , {"location", "testmeeting"}
600                                                   , {"from", _}
601                                                   , {"metadata", {struct, []}}
602                                                  ]},
603                                         {struct, [{"type", "test_event_3"}
604                                                   , {"domain", _}
605                                                   , {"datetime", Third}
606                                                   , {"id", _}
607                                                   , {"location", "testmeeting"}
608                                                   , {"from", _}
609                                                   , {"metadata", {struct, [{"description", "test"}]}}
610                                                  ]}|_]
611                                      }}]},
612                 tests_utils:get(BaseUrl, "/event/testmeeting/", ParamsGetStart)).
613
614test_get_with_type_and_timestart(BaseUrl, {RootUid, RootSid}) ->
615    Params = [{"uid", RootUid},
616              {"sid", RootSid}],
617    {struct, [{"result", {array,
618                          [ {struct, [{"type", "test_event_1"}
619                                      , {"domain", _}
620                                      , {"datetime", _}
621                                      , {"id", _}
622                                      , {"location", "testmeeting"}
623                                      , {"from", _}
624                                      , {"metadata", {struct, []}}
625                                     ]},
626                            {struct, [{"type", "test_event_2"}
627                                      , {"domain", _}
628                                      , {"datetime", _}
629                                      , {"id", _}
630                                      , {"location", "testmeeting"}
631                                      , {"from", _}
632                                      , {"metadata", {struct, []}}
633                                     ]},
634                            {struct, [{"type", "test_event_3"}
635                                      , {"domain", _}
636                                      , {"datetime", Third}
637                                      , {"id", _}
638                                      , {"location", "testmeeting"}
639                                      , {"from", _}
640                                      , {"metadata", {struct, [{"description", "test"}]}}
641                                     ]}|_]
642                         }}]} = tests_utils:get(BaseUrl, "/event/testmeeting", Params),
643    ParamsGetStart = [{"uid", RootUid},
644                      {"sid", RootSid},
645                      {"type", "test_event_3"},
646                      {"start", integer_to_list(Third)}],
647    ?assertMatch({struct, [{"result", {array,
648                                       [ {struct, [{"type", "test_event_3"}
649                                                   , {"domain", _}
650                                                   , {"datetime", Third}
651                                                   , {"id", _}
652                                                   , {"location", "testmeeting"}
653                                                   , {"from", _}
654                                                   , {"metadata", {struct, [{"description", "test"}]}}
655                                                  ]}|_]
656                                      }}]},
657                 tests_utils:get(BaseUrl, "/event/testmeeting/", ParamsGetStart)).
658
659test_get_with_type_and_timestart_and_timeend(BaseUrl, {RootUid, RootSid}) ->
660    Params = [{"uid", RootUid},
661              {"sid", RootSid}],
662    {struct, [{"result", {array,
663                          [ {struct, [{"type", "test_event_1"}
664                                      , {"domain", _}
665                                      , {"datetime", _}
666                                      , {"id", _}
667                                      , {"location", "testmeeting"}
668                                      , {"from", _}
669                                      , {"metadata", {struct, []}}
670                                     ]},
671                            {struct, [{"type", "test_event_2"}
672                                      , {"domain", _}
673                                      , {"datetime", _}
674                                      , {"id", _}
675                                      , {"location", "testmeeting"}
676                                      , {"from", _}
677                                      , {"metadata", {struct, []}}
678                                     ]},
679                            {struct, [{"type", "test_event_3"}
680                                      , {"domain", _}
681                                      , {"datetime", Third}
682                                      , {"id", _}
683                                      , {"location", "testmeeting"}
684                                      , {"from", _}
685                                      , {"metadata", {struct, [{"description", "test"}]}}
686                                     ]}|_]
687                         }}]} = tests_utils:get(BaseUrl, "/event/testmeeting", Params),
688    ParamsGetStart = [{"uid", RootUid},
689                      {"sid", RootSid},
690                      {"type", "test_event_3"},
691                      {"start", integer_to_list(Third)},
692                      {"end", integer_to_list(Third + 1)}],
693    {struct, [{"result", {array,
694                          [ {struct, [{"type", "test_event_3"}
695                                      , {"domain", _}
696                                      , {"datetime", Third}
697                                      , {"id", _}
698                                      , {"location", "testmeeting"}
699                                      , {"from", _}
700                                      , {"metadata", {struct, [{"description", "test"}]}}
701                                     ]}|_]
702                         }}]} = tests_utils:get(BaseUrl, "/event/testmeeting/", ParamsGetStart),
703    ParamsGetNothing = [{"uid", RootUid},
704                      {"sid", RootSid},
705                      {"type", "test_event_3"},
706                      {"start", integer_to_list(Third - 2)},
707                      {"end", integer_to_list(Third - 1)}],
708    {struct, [{"result", {array,[]}}]} =
709        tests_utils:get(BaseUrl, "/event/testmeeting/", ParamsGetNothing).
710
711test_get_with_type_and_timeend(BaseUrl, {RootUid, RootSid}) ->
712    Params = [{"uid", RootUid},
713              {"sid", RootSid}],
714    {struct, [{"result", {array,
715                          [ {struct, [{"type", "test_event_1"}
716                                      , {"domain", _}
717                                      , {"datetime", _}
718                                      , {"id", _}
719                                      , {"location", "testmeeting"}
720                                      , {"from", _}
721                                      , {"metadata", {struct, []}}
722                                     ]},
723                            {struct, [{"type", "test_event_2"}
724                                      , {"domain", _}
725                                      , {"datetime", Second}
726                                      , {"id", _}
727                                      , {"location", "testmeeting"}
728                                      , {"from", _}
729                                      , {"metadata", {struct, []}}
730                                     ]},
731                            {struct, [{"type", "test_event_3"}
732                                      , {"domain", _}
733                                      , {"datetime", Third}
734                                      , {"id", _}
735                                      , {"location", "testmeeting"}
736                                      , {"from", _}
737                                      , {"metadata", {struct, [{"description", "test"}]}}
738                                     ]}|_]
739                         }}]} = tests_utils:get(BaseUrl, "/event/testmeeting", Params),
740    ParamsGetStart = [{"uid", RootUid},
741                      {"sid", RootSid},
742                      {"type", "test_event_2"},
743                      {"end", integer_to_list(Third - 1)}],
744    {struct, [{"result", {array,
745                          [ {struct, [{"type", "test_event_2"}
746                                      , {"domain", _}
747                                      , {"datetime", Second}
748                                      , {"id", _}
749                                      , {"location", "testmeeting"}
750                                      , {"from", _}
751                                      , {"metadata", {struct, []}}
752                                     ]}|_]
753                         }}]} = tests_utils:get(BaseUrl, "/event/testmeeting/", ParamsGetStart).
754
755test_last(BaseUrl, {RootUid, RootSid}) ->
756    Params = [{"uid", RootUid},
757              {"sid", RootSid},
758              {"type", "last_event"},
759              {"metadata[description]", "pushed_event"}],
760    {struct, [{"result", _}]} = tests_utils:post(BaseUrl, "/event/testmeeting", Params),
761
762    ParamsGetLast = [{"uid", RootUid},
763                     {"sid", RootSid},
764                     {"count", "1"},
765                     {"order", "desc"}],
766    ?assertMatch({struct, [{"result",
767                            {array, [{struct, [{"type", "last_event"}
768                                  , {"domain", _}
769                                  , {"datetime", _}
770                                  , {"id", _}
771                                  , {"location", "testmeeting"}
772                                  , {"from", _}
773                                  , {"metadata", {struct, [{"description", "pushed_event"}]}}
774                                 ]}]}}]}, tests_utils:get(BaseUrl, "/event/testmeeting/", ParamsGetLast)).
775
776send_long_polling_event(BaseUrl, {RootUid, RootSid}) ->
777    send_long_polling_event(BaseUrl, {RootUid, RootSid}, []).
778send_long_polling_event(BaseUrl, {RootUid, RootSid}, ParamsEvent) ->
779    timer:sleep(4000),
780    Params = ParamsEvent ++[{"uid", RootUid},
781                            {"sid", RootSid},
782                            {"type", "long_polling_event"},
783                            {"metadata[description]", "relax, don't do it"}],
784    {struct, [{"result", _}]} = tests_utils:post(BaseUrl, "/event/testmeeting", Params).
785
786do_long_polling(BaseUrl, {RootUid, RootSid}, Now, Params) ->
787    ParamsGet = Params ++ [{"uid", RootUid},
788                           {"sid", RootSid},
789                           {"start", integer_to_list(Now)},
790                           {"mode", "longpolling"}],
791    tests_utils:get(BaseUrl, "/live/testmeeting", ParamsGet).
792
793assert_long_polling(BaseUrl, AuthParams = {RootUid, _RootSid}, Params) ->
794    Now = utils:now(),
795    Result = do_long_polling(BaseUrl, AuthParams, Now, Params),
796    {struct, [{"result", {array,
797                          [{struct, [{"type", "long_polling_event"}
798                                     , {"domain", _}
799                                     , {"datetime", _Datetime}
800                                     , {"id", _}
801                                     , {"location", "testmeeting"}
802                                     , {"from", RootUid}
803                                     , {"metadata", {struct, [{"description", "relax, don't do it"}]}}
804                                    ]}]}}]} = Result.
805
806assert_long_polling_to(BaseUrl, AuthParams, Params, From, To) ->
807    Now = utils:now(),
808    Result = do_long_polling(BaseUrl, AuthParams, Now, Params),
809    {struct, [{"result", {array,
810                          [{struct, [{"type", "long_polling_event"}
811                                     , {"domain", _}
812                                     , {"datetime", _Datetime}
813                                     , {"id", _}
814                                     , {"location", "testmeeting"}
815                                     , {"to", To}
816                                     , {"from", From}
817                                     , {"metadata", {struct, [{"description", "relax, don't do it"}]}}
818                                    ]}]}}]} = Result.
819
820assert_long_polling_parent(BaseUrl, AuthParams = {RootUid, _RootSid}, Params, Parent) ->
821    Now = utils:now(),
822    Result = do_long_polling(BaseUrl, AuthParams, Now, Params),
823    {struct, [{"result", {array,
824                          [{struct, [{"type", "long_polling_event"}
825                                     , {"domain", _}
826                                     , {"datetime", _Datetime}
827                                     , {"id", _}
828                                     , {"location", "testmeeting"}
829                                     , {"from", RootUid}
830                                     , {"parent", Parent}
831                                     , {"metadata", {struct, [{"description", "relax, don't do it"}]}}
832                                    ]}]}}]} = Result.
833
834assert_no_result_long_polling(BaseUrl, AuthParams, Params) ->
835    Now = utils:now(),
836    Result = do_long_polling(BaseUrl, AuthParams, Now, Params),
837    {struct, [{"result", {array, []}}]} = Result.
838
839test_long_polling(BaseUrl, AuthParams) ->
840    spawn(?MODULE, send_long_polling_event, [BaseUrl, AuthParams]),
841    assert_long_polling(BaseUrl, AuthParams, [{"type", "long_polling_event"}]).
842
843test_long_polling_with_types(BaseUrl, AuthParams) ->
844    spawn(?MODULE, send_long_polling_event, [BaseUrl, AuthParams]),
845    assert_long_polling(BaseUrl, AuthParams, [{"type", "long_polling_event,long_polling_event2"}]).
846
847test_long_polling_with_from(BaseUrl, AuthParams = {RootUid, _}) ->
848    spawn(?MODULE, send_long_polling_event, [BaseUrl, AuthParams]),
849    assert_long_polling(BaseUrl, AuthParams, [{"from", RootUid}]).
850
851test_long_polling_with_other_from(BaseUrl, AuthParams) ->
852    spawn(?MODULE, send_long_polling_event, [BaseUrl, AuthParams]),
853    assert_no_result_long_polling(BaseUrl, AuthParams, [{"from", "other_from"}]).
854
855test_long_polling_with_parent(Domain, BaseUrl, AuthParams = {RootUid, _}) ->
856    {ok, EventId} = uce_event:add(Domain,
857                                  #uce_event{ id=none,
858                                              type="test_long_polling_for_parent",
859                                              location="testmeeting",
860                                              from=RootUid}),
861    spawn(?MODULE, send_long_polling_event, [BaseUrl, AuthParams, [{"parent", EventId}]]),
862    assert_long_polling_parent(BaseUrl, AuthParams, [{"parent", EventId}], EventId).
863
864test_long_polling_with_other_parent(BaseUrl, AuthParams) ->
865    spawn(?MODULE, send_long_polling_event, [BaseUrl, AuthParams]),
866    assert_no_result_long_polling(BaseUrl, AuthParams, [{"parent", "other_parent"}]).
867
868test_long_polling_with_search(BaseUrl, AuthParams) ->
869    spawn(?MODULE, send_long_polling_event, [BaseUrl, AuthParams]),
870    assert_long_polling(BaseUrl, AuthParams, [{"search", "relax"}]).
871
872test_long_polling_with_other_search(BaseUrl, AuthParams) ->
873    spawn(?MODULE, send_long_polling_event, [BaseUrl, AuthParams]),
874    assert_no_result_long_polling(BaseUrl, AuthParams, [{"search", "plop"}]).
875
876test_long_polling_with_to(BaseUrl, {RootUid, _} = RootAuthParams, {ParticipantUid, _ParticipantSid} = _ParticipantAuthParams) ->
877    spawn(?MODULE, send_long_polling_event, [BaseUrl, RootAuthParams, [{"to", ParticipantUid}]]),
878    assert_long_polling_to(BaseUrl, RootAuthParams, [{"type", "long_polling_event"}], RootUid, ParticipantUid).
879
880test_long_polling_with_to_and_participant(BaseUrl, {RootUid, _} = RootAuthParams, {ParticipantUid, _} = ParticipantAuthParams) ->
881    spawn(?MODULE, send_long_polling_event, [BaseUrl, RootAuthParams, [{"to", ParticipantUid}]]),
882    assert_long_polling_to(BaseUrl, ParticipantAuthParams, [{"type", "long_polling_event"}], RootUid, ParticipantUid).
883
884test_long_polling_with_to_and_other_participant(BaseUrl, RootAuthParams, ParticipantAuthParams, {OtherUid, _}) ->
885    spawn(?MODULE, send_long_polling_event, [BaseUrl, RootAuthParams, [{"to", OtherUid}]]),
886    assert_no_result_long_polling(BaseUrl, ParticipantAuthParams, [{"type", "long_polling_event"}]).