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