PageRenderTime 285ms CodeModel.GetById 70ms app.highlight 128ms RepoModel.GetById 84ms app.codeStats 0ms

/ucengine/src/tests/ctl_tests.erl

http://github.com/AF83/ucengine
Erlang | 246 lines | 182 code | 36 blank | 28 comment | 0 complexity | dfe17e52102991b26593d5b48f4a3a31 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(ctl_tests).
 19
 20-include("uce.hrl").
 21-include_lib("eunit/include/eunit.hrl").
 22
 23ctl_meeting_test_() ->
 24    { setup
 25      , fun fixtures:setup/0
 26      , fun fixtures:teardown/1
 27      , fun([Domain, _BaseUrl, _Testers]) ->
 28        [ ?_test(test_meeting_add(Domain))
 29        % TODO: Test the conflict case
 30        , ?_test(test_meeting_get(Domain))
 31        , ?_test(test_meeting_get_not_found(Domain))
 32        , ?_test(test_meeting_update(Domain))
 33        , ?_test(test_meeting_update_not_found(Domain))
 34        , ?_test(test_meeting_delete(Domain))
 35        , ?_test(test_meeting_delete_not_found(Domain))
 36        , ?_test(test_meeting_list(Domain))
 37        ]
 38      end
 39    }.
 40
 41ctl_user_test_() ->
 42    { setup
 43      , fun fixtures:setup/0
 44      , fun fixtures:teardown/1
 45      , fun([Domain, _BaseUrl, _Testers]) ->
 46        [ ?_test(test_user_add(Domain))
 47        % TODO: Test the conflict case
 48        , ?_test(test_user_get(Domain))
 49        , ?_test(test_user_get_not_found(Domain))
 50        , ?_test(test_user_update(Domain))
 51        , ?_test(test_user_update_not_found(Domain))
 52        , ?_test(test_user_add_role(Domain))
 53        , ?_test(test_user_delete_role(Domain))
 54        , ?_test(test_user_delete(Domain))
 55        , ?_test(test_user_delete_not_found(Domain))
 56        , ?_test(test_user_list(Domain))
 57        ]
 58      end
 59    }.
 60
 61ctl_roles_test_() ->
 62    { setup
 63      , fun fixtures:setup/0
 64      , fun fixtures:teardown/1
 65      , fun([Domain, _BaseUrl, _Testers]) ->
 66                [ ?_test(test_role_add(Domain))
 67                ,  ?_test(test_role_add_conflict(Domain))
 68                ,  ?_test(test_role_delete(Domain))
 69                ,  ?_test(test_role_delete_not_found(Domain))
 70                ,  ?_test(test_role_add_access(Domain))
 71                ,  ?_test(test_role_check_access(Domain))
 72                ,  ?_test(test_role_delete_access(Domain))
 73                ]
 74        end
 75    }.
 76
 77%%
 78%% Meeting
 79%%
 80
 81test_meeting_add(Domain) ->
 82    false = uce_meeting:exists(Domain, "newmeeting"),
 83    Params = [{"description", ""}],
 84    ok = uce_ctl:cmd({dummy, [Domain, "meeting", "add", "newmeeting"]}, Params),
 85    Expected = {ok, #uce_meeting{id="newmeeting",
 86                                 metadata=json_helpers:to_struct([{"description", ""}])}},
 87    ?assertEqual(Expected, uce_meeting:get(Domain, "newmeeting")).
 88
 89test_meeting_get(Domain) ->
 90    ?assertMatch({ok, _}, uce_ctl:cmd({dummy, [Domain, "meeting", "get", "testmeeting"]}, [])).
 91
 92test_meeting_get_not_found(Domain) ->
 93    {error, not_found} = (catch uce_ctl:cmd({dummy, [Domain, "meeting", "get", "meeting that doesn't exists"]}, [])).
 94
 95test_meeting_update(Domain) ->
 96    {ok, #uce_meeting{ id="testmeeting"
 97                     , metadata={struct, [{"description", _Description}]}
 98                     }} = uce_meeting:get(Domain, "testmeeting"),
 99    Params = [{"description", "A new description"}],
100    ok = uce_ctl:cmd({dummy, [Domain, "meeting", "update", "testmeeting"]}, Params),
101    Expected = {ok, #uce_meeting{ id="testmeeting"
102                                , metadata=json_helpers:to_struct([{"description", "A new description"}])
103                                }},
104    ?assertMatch(Expected, uce_meeting:get(Domain, "testmeeting")).
105
106test_meeting_update_not_found(Domain) ->
107    {error, not_found} = (catch uce_ctl:cmd({dummy, [Domain, "meeting", "update",
108                                                     "meeting that doesnt exists"]}, [])).
109
110test_meeting_delete(Domain) ->
111    {ok, #uce_meeting{ id="testmeeting"
112                     , metadata={struct, [{"description", _Description}]}
113                     }} = uce_meeting:get(Domain, "testmeeting"),
114    ok = uce_ctl:cmd({dummy, [Domain, "meeting", "delete", "testmeeting"]}, []),
115    false = uce_meeting:exists(Domain, "testmeeting").
116
117test_meeting_delete_not_found(Domain) ->
118    {error, not_found} = (catch uce_ctl:cmd({dummy, [Domain, "meeting", "delete", "meeting that doesn't exists"]}, [])).
119
120test_meeting_list(Domain) ->
121    {ok, _} = uce_ctl:cmd({dummy, [Domain, "meeting", "list"]}, []).
122
123%%
124%% User
125%%
126
127test_user_add(Domain) ->
128    ?assertMatch(false, uce_user:exists(Domain, "test.user@af83.com")),
129    ?assertMatch(ok, uce_ctl:cmd({dummy, [Domain, "user", "add", "test.user@af83.com", "password", "pwd"]}, [])),
130    ?assertMatch({ok, #uce_user{id=_,
131                                name="test.user@af83.com",
132                                auth="password",
133                                credential="pwd",
134                                metadata={struct, []}}}, uce_user:get_by_name(Domain, "test.user@af83.com")).
135
136test_user_get(Domain) ->
137    {ok, _} = uce_ctl:cmd({dummy, [Domain, "user", "get", "participant.user@af83.com"]}, []).
138
139test_user_get_not_found(Domain) ->
140    {error, not_found} = (catch uce_ctl:cmd({dummy, [Domain, "user", "get", "nobody@af83.com"]}, [])).
141
142test_user_update(Domain) ->
143    {ok, #uce_user{id=Uid,
144                   name="anonymous.user@af83.com",
145                   auth="none"}} =
146        uce_user:get_by_name(Domain, "anonymous.user@af83.com"),
147    ok = uce_ctl:cmd({dummy, [Domain, "user", "update", "anonymous.user@af83.com", "password", "pwd"]}, []),
148    {ok, #uce_user{id=Uid,
149                   name="anonymous.user@af83.com",
150                   auth="password",
151                   credential="pwd"}} =
152        uce_user:get_by_name(Domain, "anonymous.user@af83.com").
153
154test_user_update_not_found(Domain) ->
155    {error, not_found} = (catch uce_ctl:cmd({dummy, [Domain, "user", "update", "nobody@af83.com", "password", "passwd"]}, [])).
156
157test_user_add_role(Domain) ->
158    Params = [{"location", "testmeeting"}],
159    ok = uce_ctl:cmd({dummy, [Domain, "user", "role", "add", "anonymous.user@af83.com", "root"]}, Params),
160    {ok, #uce_user{id=_Uid,
161                   auth="password",
162                   credential="pwd",
163                   roles=[{"root","testmeeting"}]}} =
164        uce_user:get_by_name(Domain, "anonymous.user@af83.com").
165
166test_user_delete_role(Domain) ->
167    {ok, #uce_user{id=_Uid,
168                   name="anonymous.user@af83.com",
169                   auth="password",
170                   credential="pwd",
171                   roles=[{"root","testmeeting"}]}} =
172        uce_user:get_by_name(Domain, "anonymous.user@af83.com"),
173    Params = [{"location", "testmeeting"}],
174    ok = uce_ctl:cmd({dummy, [Domain, "user", "role", "delete", "anonymous.user@af83.com", "root"]}, Params),
175    {ok, #uce_user{id=_Uid,
176                   auth="password",
177                   credential="pwd",
178                   roles=[]}} =
179        uce_user:get_by_name(Domain, "anonymous.user@af83.com").
180
181test_user_delete(Domain) ->
182    true = uce_user:exists(Domain, "participant.user@af83.com"),
183    ok = uce_ctl:cmd({dummy, [Domain, "user", "delete", "participant.user@af83.com"]}, []),
184    false = uce_user:exists(Domain, "participant.user@af83.com").
185
186test_user_delete_not_found(Domain) ->
187    {error, not_found} = (catch uce_ctl:cmd({dummy, [Domain, "user", "delete", "nobody@af83.com"]}, [])).
188
189test_user_list(Domain) ->
190    {ok, _} = uce_ctl:cmd({dummy, [Domain, "user", "list"]}, []).
191
192%%
193%% Roles
194%%
195test_role_add(Domain) ->
196    {error, not_found} = (catch uce_role:get(Domain, "test_role")),
197    ok = uce_ctl:cmd({dummy, [Domain, "role", "add", "test_role"]}, []),
198    {ok, #uce_role{id="test_role", acl=[]}} = uce_role:get(Domain, "test_role").
199
200test_role_add_conflict(Domain) ->
201    {error, conflict} = (catch uce_ctl:cmd({dummy, [Domain, "role", "add", "test_role"]}, [])),
202    {ok, #uce_role{id="test_role", acl=[]}} = uce_role:get(Domain, "test_role").
203
204test_role_delete(Domain) ->
205    {ok, #uce_role{id="test_role", acl=[]}} = uce_role:get(Domain, "test_role"),
206    ok = uce_ctl:cmd({dummy, [Domain, "role", "delete", "test_role"]}, []),
207    {error, not_found} = (catch uce_role:get(Domain, "test_role")).
208
209test_role_delete_not_found(Domain) ->
210    {error, not_found} = (catch uce_ctl:cmd({dummy, [Domain, "role", "delete", "test_role"]}, [])).
211
212test_role_add_access(Domain) ->
213    uce_role:add(Domain, #uce_role{id="test_role_2"}),
214    Params = [ {"a", "b"}
215             , {"c", "d"}],
216
217    ok = uce_ctl:cmd({dummy, [Domain, "role", "access", "add", "test_role_2", "testaction", "testobject"]}, Params),
218
219    {ok, #uce_role{id="test_role_2",
220                   acl=[#uce_access{object="testobject",
221                                    action="testaction",
222                                    conditions=[{"a", "b"}, {"c", "d"}]}]}} =
223                 uce_role:get(Domain, "test_role_2").
224
225test_role_check_access(Domain) ->
226    {ok, Anonymous} = uce_user:get_by_name(Domain, "anonymous.user@af83.com"),
227    AnonymousUid = Anonymous#uce_user.id,
228
229    uce_user:add_role(Domain, AnonymousUid, {"test_role_2", ""}),
230    Params = [ {"a", "b"}
231             , {"c", "d"}],
232
233    ok = uce_ctl:cmd({dummy, [Domain, "role", "access", "check", "anonymous.user@af83.com", "testobject", "testaction"]}, Params).
234
235test_role_delete_access(Domain) ->
236    {ok, #uce_role{id="test_role_2",
237                   acl=[#uce_access{object="testobject",
238                                    action="testaction",
239                                    conditions=[{"a", "b"}, {"c", "d"}]}]}} =
240        uce_role:get(Domain, "test_role_2"),
241    Params = [ {"c", "d"}
242             , {"a", "b"}],
243
244    ok = uce_ctl:cmd({dummy, [Domain, "role", "access", "delete", "test_role_2", "testaction", "testobject"]}, Params),
245    ?assertMatch({ok, #uce_role{id="test_role_2",
246                   acl=[]}}, uce_role:get(Domain, "test_role_2")).