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