PageRenderTime 36ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/ucengine/src/tests/fixtures.erl

http://github.com/AF83/ucengine
Erlang | 192 lines | 145 code | 30 blank | 17 comment | 0 complexity | 4c4f13fbbbd91b42923585191f5942d2 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(fixtures).
  19. -include("uce.hrl").
  20. -export([setup/0, teardown/1, get_default_domain/0, get_base_url/0]).
  21. get_default_domain() ->
  22. Hosts = config:get(hosts),
  23. {Domain, _Config} = hd(Hosts),
  24. Domain.
  25. get_base_url() ->
  26. Domain = get_default_domain(),
  27. Port = config:get(port),
  28. "http://" ++ Domain ++ ":" ++ integer_to_list(Port) ++ "/api/" ++ ?VERSION ++ "/".
  29. setup() ->
  30. Domain = get_default_domain(),
  31. (list_to_atom(lists:concat([config:get(Domain, db), "_db"]))):drop(),
  32. drop_model(Domain, [uce_role, uce_user, uce_meeting, uce_file, uce_event]),
  33. setup_meetings(Domain),
  34. UsersUid = setup_users(Domain),
  35. [Domain, get_base_url(), setup_testers(Domain, UsersUid)].
  36. drop_model(_Domain, []) ->
  37. ok;
  38. drop_model(Domain, [Model|Models]) ->
  39. {exports, Funs} = proplists:lookup(exports, Model:module_info()),
  40. case proplists:lookup(drop, Funs) of
  41. {drop, 1} ->
  42. Model:drop(Domain);
  43. _ ->
  44. ok
  45. end,
  46. drop_model(Domain, Models).
  47. teardown([Domain, _, _Testers]) ->
  48. teardown_solr(Domain),
  49. ok.
  50. add_meeting(Domain, Meeting) ->
  51. case catch uce_meeting:add(Domain, Meeting) of
  52. {ok, _} -> ok;
  53. {error, conflict} -> ok;
  54. {error, Reason} -> throw({error, Reason})
  55. end.
  56. setup_meetings(Domain) ->
  57. ok = add_meeting(Domain, #uce_meeting{id="testmeeting",
  58. metadata={struct, [{"description", "Meeting 1"}]}}),
  59. ok = add_meeting(Domain, #uce_meeting{id="meeting2",
  60. metadata={struct, [{"description", "Meeting 2"}]}}),
  61. ok = add_meeting(Domain, #uce_meeting{id="meeting3",
  62. metadata={struct, [{"description", "Meeting 3"}]}}),
  63. ok.
  64. add_role(Domain, Role) ->
  65. case catch uce_role:add(Domain, Role) of
  66. {ok, _} -> ok;
  67. {error, conflict} -> ok;
  68. {error, Reason} -> throw({error, Reason})
  69. end.
  70. add_user(Domain, User) ->
  71. case catch uce_user:add(Domain, User) of
  72. {ok, _} -> ok;
  73. {error, conflict} -> ok;
  74. {error, Reason} -> throw({error, Reason})
  75. end.
  76. add_role_to_user(Domain, Uid, Params) ->
  77. case catch uce_user:add_role(Domain, Uid, Params) of
  78. {ok, _} -> ok;
  79. {error, conflict} -> ok;
  80. {error, Reason} -> throw({error, Reason})
  81. end.
  82. setup_users(Domain) ->
  83. ok = add_role(Domain, #uce_role{id="default",
  84. acl=[#uce_access{action="add", object="presence"},
  85. #uce_access{action="delete", object="presence"}]}),
  86. ok = add_role(Domain, #uce_role{id="root",
  87. acl=[#uce_access{action="all", object="all"}]}),
  88. ok = add_role(Domain, #uce_role{id="participant",
  89. acl=[#uce_access{action="add", object="presence"},
  90. #uce_access{action="delete", object="presence"},
  91. #uce_access{action="add", object="event"},
  92. #uce_access{action="list", object="event"},
  93. #uce_access{action="get", object="infos"},
  94. #uce_access{action="add", object="roster"},
  95. #uce_access{action="list", object="roster"},
  96. #uce_access{action="get", object="meeting"},
  97. #uce_access{action="list", object="meeting"},
  98. #uce_access{action="find", object="user"}]}),
  99. ok = add_role(Domain, #uce_role{id="testrole_location",
  100. acl=[#uce_access{action="testaction", object="testobject", conditions=[{"a", "b"}]}]}),
  101. ok = add_role(Domain, #uce_role{id="testrole_without_location",
  102. acl=[#uce_access{action="testaction_global", object="testobject_global", conditions=[{"c", "d"}]}]}),
  103. ok = add_role(Domain, #uce_role{id="anonymous",
  104. acl=[#uce_access{action="add", object="presence"},
  105. #uce_access{action="delete", object="presence"}]}),
  106. ParticipantUid = "participant.user@af83.com",
  107. ok = add_user(Domain, #uce_user{id=ParticipantUid,
  108. name=ParticipantUid,
  109. auth="password",
  110. credential="pwd"}),
  111. timer:sleep(10),
  112. ok = add_role_to_user(Domain, ParticipantUid, {"participant", ""}),
  113. ok = add_role_to_user(Domain, ParticipantUid, {"testrole_location", "testmeeting"}),
  114. ok = add_role_to_user(Domain, ParticipantUid, {"testrole_without_location", ""}),
  115. ok = add_role_to_user(Domain, ParticipantUid, {"participant", ""}),
  116. AnonymousUid = "anonymous.user@af83.com",
  117. ok = add_user(Domain, #uce_user{id=AnonymousUid,
  118. name=AnonymousUid,
  119. auth="none"}),
  120. timer:sleep(10),
  121. ok = add_role_to_user(Domain, AnonymousUid, {"anonymous", ""}),
  122. ok = add_user(Domain, #uce_user{id="token.user@af83.com",
  123. name="token.user@af83.com",
  124. auth="password",
  125. credential="4444"}),
  126. ok = add_user(Domain, #uce_user{id="user_2",
  127. name="user_2",
  128. auth="password",
  129. credential="pwd"}),
  130. ok = add_user(Domain, #uce_user{id="user_3",
  131. name="user_3",
  132. auth="password",
  133. credential="pwd"}),
  134. {ok, RootUid} = uce_user:add(Domain,
  135. #uce_user{name="root.user@af83.com",
  136. auth="password",
  137. credential="pwd"}),
  138. uce_role:add_access(Domain, RootUid, #uce_access{action="all", object="all"}),
  139. {ok, UglyUid} = uce_user:add(Domain,
  140. #uce_user{name="ugly.user@af83.com",
  141. auth="password",
  142. credential="pwd",
  143. roles=[]}),
  144. uce_user:delete_role(Domain, UglyUid, {"default", ""}),
  145. {RootUid, ParticipantUid, UglyUid, AnonymousUid}.
  146. setup_testers(Domain, {RootUid, ParticipantUid, UglyUid, AnonymousUid}) ->
  147. {ok, RootSid} = uce_presence:add(Domain,
  148. #uce_presence{id=none,
  149. user=RootUid,
  150. auth="password"}),
  151. {ok, ParticipantSid} = uce_presence:add(Domain,
  152. #uce_presence{id=none,
  153. user=ParticipantUid,
  154. auth="password"}),
  155. {ok, UglySid} = uce_presence:add(Domain,
  156. #uce_presence{id=none,
  157. user=UglyUid,
  158. auth="password"}),
  159. [{RootUid, RootSid}, {ParticipantUid, ParticipantSid}, {UglyUid, UglySid}, {AnonymousUid, ""}].
  160. teardown_solr(Domain) ->
  161. uce_event_solr_search:delete(Domain, "*:*"),
  162. ok.