PageRenderTime 348ms CodeModel.GetById 100ms app.highlight 167ms RepoModel.GetById 75ms app.codeStats 0ms

/ucengine/src/tests/user_tests.erl

http://github.com/AF83/ucengine
Erlang | 400 lines | 330 code | 53 blank | 17 comment | 0 complexity | 64717e9fe2ba96847b75d59536462c99 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(user_tests).
 19
 20-include("uce.hrl").
 21-include_lib("eunit/include/eunit.hrl").
 22
 23user_test_() ->
 24    { setup
 25    , fun fixtures:setup/0
 26    , fun fixtures:teardown/1
 27    , fun([_, BaseUrl, [Root, Participant, Ugly, Anonymous|_]]) ->
 28              [?_test(test_register(BaseUrl)),
 29               ?_test(test_register_missing_auth(BaseUrl)),
 30               ?_test(test_register_missing_credential(BaseUrl)),
 31               ?_test(test_register_missing_name(BaseUrl)),
 32               ?_test(test_register_conflict(BaseUrl)),
 33               ?_test(test_register_with_restricted_access(BaseUrl)),
 34               ?_test(test_register_with_restricted_access_and_root(BaseUrl, Root)),
 35
 36               ?_test(test_get(BaseUrl, Root)),
 37               ?_test(test_get_not_found(BaseUrl, Root)),
 38               ?_test(test_get_unauthorized(BaseUrl, Ugly)),
 39
 40               ?_test(test_find_by_name(BaseUrl, Root)),
 41
 42               ?_test(test_list(BaseUrl, Root)),
 43               ?_test(test_list_unauthorized(BaseUrl, Ugly)),
 44
 45               ?_test(test_update(BaseUrl, Root)),
 46               ?_test(test_update_missing_auth(BaseUrl, Root)),
 47               ?_test(test_update_missing_credential(BaseUrl, Root)),
 48               ?_test(test_update_not_found(BaseUrl, Root)),
 49               ?_test(test_update_unauthorized(BaseUrl, Ugly)),
 50
 51               ?_test(test_set_role(BaseUrl, Root, Anonymous)),
 52               ?_test(test_set_role_with_location(BaseUrl, Root, Anonymous)),
 53               ?_test(test_set_role_missing_role(BaseUrl, Root, Anonymous)),
 54               ?_test(test_set_role_not_found_user(BaseUrl, Root)),
 55               ?_test(test_set_role_not_found_role(BaseUrl, Root, Anonymous)),
 56               ?_test(test_set_role_not_found_location(BaseUrl, Root, Anonymous)),
 57               ?_test(test_set_role_unauthorized(BaseUrl, Ugly, Anonymous)),
 58
 59               ?_test(test_delete_role(BaseUrl, Root, Anonymous)),
 60               ?_test(test_delete_role_with_location(BaseUrl, Root, Anonymous)),
 61               ?_test(test_delete_role_not_found_user(BaseUrl, Root)),
 62               ?_test(test_delete_role_not_found_role(BaseUrl, Root)),
 63               ?_test(test_delete_role_not_found_location(BaseUrl, Root)),
 64               ?_test(test_delete_role_unauthorized(BaseUrl, Ugly)),
 65
 66               ?_test(test_check_false_location(BaseUrl, Root, Participant)),
 67               ?_test(test_check_false_location_without_meeting(BaseUrl, Root, Participant)),
 68               ?_test(test_check_false_conditions(BaseUrl, Root, Participant)),
 69
 70               ?_test(test_check_unauthorized(BaseUrl, Ugly, Participant)),
 71
 72               ?_test(test_check_true(BaseUrl, Root, Participant)),
 73               ?_test(test_check_true_without_meeting(BaseUrl, Root, Participant)),
 74
 75               ?_test(test_delete_unauthorized(BaseUrl, Ugly)),
 76               ?_test(test_delete(BaseUrl, Root)),
 77               ?_test(test_delete_not_found(BaseUrl, Root))]
 78      end
 79    }.
 80
 81test_register(BaseUrl) ->
 82    Params = [{"auth", "test"},
 83              {"credential", "test"},
 84              {"name", "test.user@af83.com"},
 85              {"metadata[nickname]", "test_nickname"}],
 86    {struct, [{"result", _}]} =
 87        tests_utils:post(BaseUrl, "/user/", Params).
 88
 89test_register_missing_auth(BaseUrl) ->
 90    Params = [{"credential", "test"},
 91              {"name", "test.user@af83.com"},
 92              {"metadata[nickname]", "test_nickname"}],
 93    {struct, [{"error", "missing_parameters"}, {"infos", _}]} =
 94        tests_utils:post(BaseUrl, "/user/", Params).
 95
 96test_register_missing_credential(BaseUrl) ->
 97    Params = [{"auth", "test"},
 98              {"name", "test.user@af83.com"},
 99              {"metadata[nickname]", "test_nickname"}],
100    {struct, [{"error", "missing_parameters"}, {"infos", _}]} =
101        tests_utils:post(BaseUrl, "/user/", Params).
102
103test_register_missing_name(BaseUrl) ->
104    Params = [{"auth", "test"},
105              {"metadata[nickname]", "test_nickname"}],
106    {struct, [{"error", "missing_parameters"}, {"infos", _}]} =
107        tests_utils:post(BaseUrl, "/user/", Params).
108
109test_register_conflict(BaseUrl) ->
110    Params = [{"auth", "test"},
111              {"name", "test.user@af83.com"},
112              {"credential", "test"}],
113    {struct, [{"error", "conflict"}]} =
114        tests_utils:post(BaseUrl, "/user/", Params).
115
116test_register_with_restricted_access(BaseUrl) ->
117    config:set("localhost", register, restricted),
118    Params = [{"auth", "test"},
119              {"name", "test.user2@af83.com"},
120              {"credential", "test"}],
121    ?assertMatch({struct, [{"error", "unauthorized"}]},
122                 tests_utils:post(BaseUrl, "/user/", Params)),
123    config:set("localhost", register, open).
124
125test_register_with_restricted_access_and_root(BaseUrl, {RootUid, RootSid}) ->
126    config:set("localhost", register, restricted),
127    Params = [{"auth", "test"},
128              {"name", "test.user2@af83.com"},
129              {"credential", "test"},
130              {"uid", RootUid},
131              {"sid", RootSid}],
132    ?assertMatch({struct, [{"result", _}]},
133                 tests_utils:post(BaseUrl, "/user/", Params)),
134    config:set("localhost", register, open).
135
136test_get(BaseUrl, {RootUid, RootSid}) ->
137    Params = [{"uid", RootUid},
138              {"sid", RootSid}],
139    {struct,[{"result",
140              {struct,[{"uid",RootUid},
141                       {"name","root.user@af83.com"},
142                       {"domain",_},
143                       {"auth","password"},
144                       {"metadata",_}
145                      ]}
146             }]} = tests_utils:get(BaseUrl, "/user/"++RootUid, Params).
147
148test_get_not_found(BaseUrl, {RootUid, RootSid}) ->
149    Params = [{"uid", RootUid},
150              {"sid", RootSid}],
151    {struct, [{"error", "not_found"}]} =
152        tests_utils:get(BaseUrl, "/user/unexistent.user@af83.com", Params).
153
154test_get_unauthorized(BaseUrl, {UglyUid, UglySid}) ->
155    Params = [{"uid", UglyUid},
156              {"sid", UglySid}],
157    {struct, [{"error", "unauthorized"}]} =
158        tests_utils:get(BaseUrl, "/user/unexistent.user@af83.com", Params).
159
160test_find_by_name(BaseUrl, {RootUid, RootSid}) ->
161    Params = [{"uid", RootUid},
162              {"sid", RootSid},
163              {"by_name", "test.user@af83.com"}],
164    {struct,[{"result",
165              {struct,[{"uid",_},
166                       {"name","test.user@af83.com"},
167                       {"domain",_},
168                       {"auth",_},
169                       {"metadata",_}
170                      ]}
171             }]} = tests_utils:get(BaseUrl, "/find/user", Params).
172
173test_list(BaseUrl, {RootUid, RootSid}) ->
174    Params = [{"uid", RootUid},
175              {"sid", RootSid}],
176    {struct,[{"result",
177              {array,
178               [{struct,[{"uid",_},
179                         {"name",_},
180                         {"domain", _},
181                         {"auth",_},
182                         {"metadata",{struct,_}}
183                        ]}|_]
184              }}]} = tests_utils:get(BaseUrl, "/user/", Params).
185
186test_list_unauthorized(BaseUrl, {UglyUid, UglySid}) ->
187    Params = [{"uid", UglyUid},
188              {"sid", UglySid}],
189    {struct, [{"error", "unauthorized"}]} = tests_utils:get(BaseUrl, "/user/", Params).
190
191test_update(BaseUrl, {RootUid, RootSid}) ->
192    Params = [{"uid", RootUid},
193              {"sid", RootSid},
194              {"name", "root.user@af83.com"},
195              {"auth", "password"},
196              {"credential", "pwd"},
197              {"metadata[nickname]", "root_nickname"}],
198    {struct, [{"result", "ok"}]} =
199        tests_utils:put(BaseUrl, "/user/"++RootUid, Params).
200
201
202test_update_missing_auth(BaseUrl, {RootUid, RootSid}) ->
203    Params = [{"uid", RootUid},
204              {"sid", RootSid},
205              {"name", "test.user@af83.com"},
206              {"credential", "test_modified"},
207              {"metadata[nickname]", "test_modified_nickname"}],
208    {struct, [{"error", "missing_parameters"}, {"infos", _}]} =
209        tests_utils:put(BaseUrl, "/user/"++RootUid, Params).
210
211test_update_missing_credential(BaseUrl, {RootUid, RootSid}) ->
212    Params = [{"uid", RootUid},
213              {"sid", RootSid},
214              {"name", "test.user@af83.com"},
215              {"auth", "test_modified"},
216              {"metadata[nickname]", "test_modified_nickname"}],
217    {struct, [{"error", "missing_parameters"}, {"infos", _}]} =
218        tests_utils:put(BaseUrl, "/user/"++RootUid, Params).
219
220test_update_not_found(BaseUrl, {RootUid, RootSid}) ->
221    Params = [{"uid", RootUid},
222              {"sid", RootSid},
223              {"name","unexistent.user@af83.com"},
224              {"auth", "test_modified"},
225              {"credential", "test_modified"},
226              {"metadata[nickname]", "test_modified_nickname"}],
227    {struct, [{"error", "not_found"}]} =
228        tests_utils:put(BaseUrl, "/user/unexistent.user@af83.com", Params).
229
230test_update_unauthorized(BaseUrl, {UglyUid, UglySid}) ->
231    Params = [{"uid", UglyUid},
232              {"sid", UglySid},
233              {"name","test.user@af83.com"},
234              {"auth", "test_modified"},
235              {"credential", "test_modified"},
236              {"metadata[nickname]", "test_modified_nickname"}],
237    {struct, [{"error", "unauthorized"}]} =
238        tests_utils:put(BaseUrl, "/user/test.user@af83.com", Params).
239
240test_set_role(BaseUrl, {RootUid, RootSid}, {AnonymousUid, _}) ->
241    Params = [{"uid", RootUid},
242              {"sid", RootSid},
243              {"role", "anonymous"}],
244    {struct, [{"result", "ok"}]} =
245        tests_utils:post(BaseUrl, "/user/"++AnonymousUid++"/roles/", Params).
246
247test_set_role_with_location(BaseUrl, {RootUid, RootSid}, {AnonymousUid, _}) ->
248    Params = [{"uid", RootUid},
249              {"sid", RootSid},
250              {"role", "anonymous"},
251              {"location", "testmeeting"}],
252    {struct, [{"result", "ok"}]} =
253        tests_utils:post(BaseUrl, "/user/"++AnonymousUid++"/roles/", Params).
254
255test_set_role_missing_role(BaseUrl, {RootUid, RootSid}, {AnonymousUid, _}) ->
256    Params = [{"uid", RootUid},
257              {"sid", RootSid},
258              {"location", "testmeeting"}],
259    {struct, [{"error", "missing_parameters"}, {"infos", _}]} =
260        tests_utils:post(BaseUrl, "/user/"++AnonymousUid++"/roles/", Params).
261
262test_set_role_not_found_user(BaseUrl, {RootUid, RootSid}) ->
263    Params = [{"uid", RootUid},
264              {"sid", RootSid},
265              {"role", "anonymous"},
266              {"location", "testmeeting"}],
267    {struct, [{"error", "not_found"}]} =
268        tests_utils:post(BaseUrl, "/user/unexistent_user/roles/", Params).
269
270test_set_role_not_found_role(BaseUrl, {RootUid, RootSid}, {AnonymousUid, _}) ->
271    Params = [{"uid", RootUid},
272              {"sid", RootSid},
273              {"role", "unexistent_role"},
274              {"location", "testmeeting"}],
275    {struct, [{"error", "not_found"}]} =
276        tests_utils:post(BaseUrl, "/user/"++AnonymousUid++"/roles/", Params).
277
278test_set_role_not_found_location(BaseUrl, {RootUid, RootSid}, {AnonymousUid, _}) ->
279    Params = [{"uid", RootUid},
280              {"sid", RootSid},
281              {"role", "anonymous"},
282              {"location", "unexistent_meeting"}],
283    {struct, [{"error", "not_found"}]} =
284        tests_utils:post(BaseUrl, "/user/"++AnonymousUid++"/roles/", Params).
285
286test_set_role_unauthorized(BaseUrl, {UglyUid, UglySid}, {AnonymousUid, _}) ->
287    Params = [{"uid", UglyUid},
288              {"sid", UglySid},
289              {"role", "anonymous"},
290              {"location", "testmeeting"}],
291    {struct, [{"error", "unauthorized"}]} =
292        tests_utils:post(BaseUrl, "/user/"++AnonymousUid++"/roles/", Params).
293
294test_delete_role(BaseUrl, {RootUid, RootSid}, {AnonymousUid, _}) ->
295    Params = [{"uid", RootUid},
296              {"sid", RootSid}],
297    {struct, [{"result", "ok"}]} =
298        tests_utils:delete(BaseUrl, "/user/"++AnonymousUid++"/roles/anonymous", Params),
299    {struct, [{"error", "not_found"}]} =
300        tests_utils:delete(BaseUrl, "/user/"++AnonymousUid++"/roles/anonymous", Params).
301
302test_delete_role_with_location(BaseUrl, {RootUid, RootSid}, {AnonymousUid, _}) ->
303    Params = [{"uid", RootUid},
304              {"sid", RootSid}],
305    {struct, [{"result", "ok"}]} =
306        tests_utils:delete(BaseUrl, "/user/"++AnonymousUid++"/roles/anonymous/testmeeting", Params),
307    {struct, [{"error", "not_found"}]} =
308        tests_utils:delete(BaseUrl, "/user/"++AnonymousUid++"/roles/anonymous", Params).
309
310test_delete_role_not_found_user(BaseUrl, {RootUid, RootSid}) ->
311    Params = [{"uid", RootUid},
312              {"sid", RootSid}],
313    {struct, [{"error", "not_found"}]} =
314        tests_utils:delete(BaseUrl, "/user/unexistent_user/roles/anonymous", Params).
315
316test_delete_role_not_found_role(BaseUrl, {RootUid, RootSid}) ->
317    Params = [{"uid", RootUid},
318              {"sid", RootSid}],
319    {struct, [{"error", "not_found"}]} =
320        tests_utils:delete(BaseUrl, "/user/test.user@af83.com/roles/unexistent_role", Params).
321
322test_delete_role_not_found_location(BaseUrl, {RootUid, RootSid}) ->
323    Params = [{"uid", RootUid},
324              {"sid", RootSid}],
325    {struct, [{"error", "not_found"}]} =
326        tests_utils:delete(BaseUrl, "/user/test.user@af83.com/roles/anonymous/unexistent_location", Params).
327
328test_delete_role_unauthorized(BaseUrl, {UglyUid, UglySid}) ->
329    Params = [{"uid", UglyUid},
330              {"sid", UglySid}],
331    {struct, [{"error", "unauthorized"}]} =
332        tests_utils:delete(BaseUrl, "/user/test.user@af83.com/roles/anonymous", Params).
333
334test_check_true(BaseUrl, {RootUid, RootSid}, {ParticipantUid, _}) ->
335    Params = [{"uid", RootUid},
336              {"sid", RootSid},
337              {"conditions[a]", "b"}],
338    {struct, [{"result", "true"}]} =
339        tests_utils:get(BaseUrl, "/user/"++ParticipantUid++"/can/testaction/testobject/testmeeting", Params).
340
341test_check_unauthorized(BaseUrl, {UglyUid, UglySid}, {ParticipantUid, _}) ->
342    Params = [{"uid", UglyUid},
343              {"sid", UglySid},
344              {"conditions[a]", "b"}],
345    {struct, [{"error", "unauthorized"}]} =
346        tests_utils:get(BaseUrl, "/user/"++ParticipantUid++"/can/testaction/testobject/testmeeting", Params).
347
348test_check_true_without_meeting(BaseUrl, {RootUid, RootSid}, {ParticipantUid, _}) ->
349    Params = [{"uid", RootUid},
350              {"sid", RootSid},
351              {"conditions[c]", "d"}],
352    {struct, [{"result", "true"}]} =
353        tests_utils:get(BaseUrl, "/user/"++ParticipantUid++"/can/testaction_global/testobject_global/", Params).
354
355test_check_false_location(BaseUrl, {RootUid, RootSid}, {ParticipantUid, _}) ->
356    Params = [{"uid", RootUid},
357              {"sid", RootSid},
358              {"conditions[a]", "b"}],
359    {struct, [{"result", "false"}]} =
360        tests_utils:get(BaseUrl, "/user/"++ParticipantUid++"/can/testaction/testobject/othermeeting", Params).
361
362test_check_false_location_without_meeting(BaseUrl, {RootUid, RootSid}, {ParticipantUid, _}) ->
363    Params = [{"uid", RootUid},
364              {"sid", RootSid},
365              {"conditions[a]", "b"}],
366    {struct, [{"result", "false"}]} =
367        tests_utils:get(BaseUrl, "/user/"++ParticipantUid++"/can/testaction/testobject/", Params).
368
369test_check_false_conditions(BaseUrl, {RootUid, RootSid}, {ParticipantUid, _}) ->
370    Params = [{"uid", RootUid},
371              {"sid", RootSid},
372              {"conditions[a]", "c"}],
373    {struct, [{"result", "false"}]} =
374        tests_utils:get(BaseUrl, "/user/"++ParticipantUid++"/can/testaction/testobject/testmeeting", Params).
375
376test_delete_unauthorized(BaseUrl, {UglyUid, UglySid}) ->
377    Params = [{"uid", UglyUid},
378              {"sid", UglySid}],
379    {struct, [{"error", "unauthorized"}]} =
380        tests_utils:delete(BaseUrl, "/user/"++UglyUid, Params).
381
382test_delete(BaseUrl, {RootUid, RootSid}) ->
383    Params = [{"uid", RootUid},
384              {"sid", RootSid}],
385    Params2 = [{"auth", "test"},
386               {"credential", "test"},
387               {"name", "testdeletion.user@af83.com"},
388               {"metadata[nickname]", "test_nickname"}],
389    {struct, [{"result", Id}]} =
390        tests_utils:post(BaseUrl, "/user/", Params2),
391    {struct, [{"result", "ok"}]} =
392        tests_utils:delete(BaseUrl, "/user/"++Id, Params),
393    {struct, [{"error", "not_found"}]} =
394        tests_utils:get(BaseUrl, "/user/"++Id, Params).
395
396test_delete_not_found(BaseUrl, {RootUid, RootSid}) ->
397    Params = [{"uid", RootUid},
398              {"sid", RootSid}],
399    {struct, [{"error", "not_found"}]} =
400        tests_utils:delete(BaseUrl, "/user/unexistent.user@af83.com", Params).