PageRenderTime 124ms CodeModel.GetById 41ms app.highlight 25ms RepoModel.GetById 55ms app.codeStats 0ms

/ucengine/src/tests/presence_tests.erl

http://github.com/AF83/ucengine
Erlang | 156 lines | 111 code | 21 blank | 24 comment | 0 complexity | 3b69bde5fbf66d43b0be0426fe638ae8 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(presence_tests).
 19
 20-include("uce.hrl").
 21-include_lib("eunit/include/eunit.hrl").
 22
 23presence_test_() ->
 24    { setup
 25    , fun fixtures:setup/0
 26    , fun fixtures:teardown/1
 27    , fun([_, BaseUrl, [_Root, _Participant, Ugly|_]]) ->
 28              [ ?_test(test_presence_create_password(BaseUrl)),
 29                ?_test(test_presence_create_with_no_password(BaseUrl)),
 30                ?_test(test_presence_create_missing_credential(BaseUrl)),
 31                ?_test(test_presence_create_bad_password(BaseUrl)),
 32                ?_test(test_presence_create_not_found_user(BaseUrl)),
 33                ?_test(test_presence_create_unauthorized(BaseUrl)),
 34
 35                ?_test(test_presence_get(BaseUrl)),
 36                ?_test(test_presence_get_not_found(BaseUrl)),
 37
 38                ?_test(test_presence_close(BaseUrl)),
 39                ?_test(test_presence_close_unauthorized(BaseUrl, Ugly)),
 40                ?_test(test_presence_close_not_foundsid(BaseUrl)),
 41                {timeout, 120, ?_test(test_presence_timeout(BaseUrl))},
 42                {timeout, 120, ?_test(test_multiple_presence_timeout(BaseUrl)) }]
 43      end
 44    }.
 45
 46create_presence(BaseUrl, Name) ->
 47    Params = [{"name", Name}],
 48    tests_utils:post(BaseUrl, "/presence/", Params).
 49create_presence(BaseUrl, Name, Credential) ->
 50    create_presence(BaseUrl, Name, Credential, []).
 51create_presence(BaseUrl, Name, Credential, Metadata) ->
 52    Params = [{"name", Name},
 53              {"credential", Credential}] ++ Metadata,
 54    tests_utils:post(BaseUrl, "/presence/", Params).
 55
 56test_presence_create_password(BaseUrl) ->
 57    {struct,[{"result", _}]} = create_presence(BaseUrl, "participant.user@af83.com", "pwd").
 58
 59test_presence_create_with_no_password(BaseUrl) ->
 60    {struct,[{"result", _}]} = create_presence(BaseUrl, "anonymous.user@af83.com").
 61
 62test_presence_create_missing_credential(BaseUrl) ->
 63    {struct,[{"error", "bad_credentials"}]} = create_presence(BaseUrl, "participant.user@af83.com").
 64
 65test_presence_create_bad_password(BaseUrl) ->
 66    {struct,[{"error", "bad_credentials"}]} = create_presence(BaseUrl, "participant.user@af83.com", "badpwd").
 67
 68test_presence_create_not_found_user(BaseUrl) ->
 69    {struct,[{"error", "not_found"}]} = create_presence(BaseUrl, "unexistent.user@af83.com", "pwd").
 70
 71test_presence_create_unauthorized(BaseUrl) ->
 72    {struct,[{"error", "unauthorized"}]} = create_presence(BaseUrl, "ugly.user@af83.com", "pwd").
 73
 74test_presence_get(BaseUrl) ->
 75    Uid = "participant.user@af83.com",
 76    {struct,[{"result", {struct, [{"uid", _}, {"sid", Sid}]}}]} =
 77        create_presence(BaseUrl, Uid, "pwd"),
 78
 79    {struct,[{"result",
 80              {struct,[{"id",Sid},
 81                       {"domain",_},
 82                       {"user",_}, %%"participant.user@af83.com"},
 83                       {"auth","password"}]}}]} =
 84        tests_utils:get(BaseUrl, "/presence/" ++ Sid).
 85
 86test_presence_get_not_found(BaseUrl) ->
 87    {struct,[{"error", "not_found"}]} =
 88        tests_utils:get(BaseUrl, "/presence/unexistent_sid").
 89
 90test_presence_close(BaseUrl) ->
 91    Uid = "participant.user@af83.com",
 92    {struct,[{"result", {struct, [{"uid", Id}, {"sid", Sid}]}}]} = create_presence(BaseUrl, Uid, "pwd"),
 93
 94    ParamsDelete = [{"uid", Id},
 95                    {"sid", Sid}],
 96    {struct, [{"result", "ok"}]} =
 97        tests_utils:delete(BaseUrl, "/presence/" ++ Sid, ParamsDelete).
 98
 99test_presence_close_unauthorized(BaseUrl, {UglyUid, UglySid}) ->
100    ParamsDelete = [{"uid", UglyUid},
101                    {"sid", UglySid}],
102    {struct, [{"error", "unauthorized"}]} =
103        tests_utils:delete(BaseUrl, "/presence/" ++ UglySid, ParamsDelete).
104
105test_presence_close_not_foundsid(BaseUrl) ->
106    {struct,[{"result", {struct, [{"uid", Id},
107                                  {"sid", Sid}]}}]} = create_presence(BaseUrl, "participant.user@af83.com", "pwd"),
108
109    ParamsDelete = [{"uid", Id},
110                    {"sid", Sid}],
111    {struct, [{"error", "not_found"}]} =
112        tests_utils:delete(BaseUrl, "/presence/unexistentsid", ParamsDelete).
113
114test_presence_timeout(BaseUrl) ->
115    DefaultTimeout = config:get(timeout_refresh),
116    {struct,[{"result", {struct, [{"uid", Uid}, {"sid", Sid}]}}]} =
117        create_presence(BaseUrl, "participant.user@af83.com", "pwd", [{"timeout", integer_to_list(DefaultTimeout)}]),
118    timer:sleep(DefaultTimeout * 2000),
119    ParamsGet = [{"uid", Uid},
120                 {"sid", Sid}],
121    {struct, [{"error", "not_found"}]} =
122        tests_utils:get(BaseUrl, "/presence/" ++ Sid, ParamsGet).
123
124%%
125%% Test the case when a user have multiple session on the same meeting
126%% We should not cleanup meeting in this case
127%%
128test_multiple_presence_timeout(BaseUrl) ->
129    DefaultTimeout = config:get(timeout_refresh),
130    % Create first presence and join a meeting
131    {struct,[{"result", {struct, [{"uid", Uid}, {"sid", Sid}]}}]} =
132        create_presence(BaseUrl, "participant.user@af83.com", "pwd", [{"timeout", integer_to_list(DefaultTimeout)}]),
133    Params = [ {"uid", Uid}
134             , {"sid", Sid}],
135    ?assertMatch({struct, [{"result", "ok"}]}, tests_utils:post(BaseUrl, "/meeting/testmeeting/roster/", Params)),
136    ?assertMatch({struct, [{"result", "ok"}]}, tests_utils:post(BaseUrl, "/meeting/meeting2/roster/", Params)),
137    timer:sleep(1000),
138    % Create second presence and join the same meeting
139    {struct,[{"result", {struct, [{"uid", Uid}, {"sid", Sid2}]}}]} =
140        create_presence(BaseUrl, "participant.user@af83.com", "pwd", [{"timeout", integer_to_list(DefaultTimeout * 15000)}]),
141    Params2 = [ {"uid", Uid}
142              , {"sid", Sid2}],
143    ?assertMatch({struct, [{"result", "ok"}]}, tests_utils:post(BaseUrl, "/meeting/testmeeting/roster/", Params2)),
144    timer:sleep(DefaultTimeout * 1000),
145    ?assertMatch({struct, [{"error", "not_found"}]}, tests_utils:get(BaseUrl, "/presence/" ++ Sid, Params)),
146    ?assertMatch({struct, [{"result", _}]}, tests_utils:get(BaseUrl, "/presence/" ++ Sid2, Params2)),
147    {struct, [{"result", {array, Array}}]} =
148        tests_utils:get(BaseUrl, "/meeting/testmeeting/roster/", Params2),
149    %% We should be here
150    [{struct,[{"uid", Uid},
151              {"name",_},
152              {"domain",_},
153              {"auth","password"},
154              {"metadata",{struct,[]}}]}] = Array,
155    ?assertMatch({struct, [{"result", {array, []}}]},
156                 tests_utils:get(BaseUrl, "/meeting/meeting2/roster/", Params2)).