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