/ucengine/src/tests/file_tests.erl

http://github.com/AF83/ucengine · Erlang · 198 lines · 154 code · 27 blank · 17 comment · 0 complexity · f8057f3c897e1c7b37ecd6c062987461 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(file_tests).
  19. -include("uce.hrl").
  20. -include_lib("eunit/include/eunit.hrl").
  21. file_test_() ->
  22. { setup
  23. , fun fixtures:setup/0
  24. , fun fixtures:teardown/1
  25. , fun([_, BaseUrl, [Root|_]]) ->
  26. [?_test(test_upload_small(BaseUrl, Root)),
  27. ?_test(test_upload_with_force_content_type(BaseUrl, Root)),
  28. ?_test(test_upload_big(BaseUrl, Root)),
  29. ?_test(test_upload_big_param(BaseUrl, Root)),
  30. ?_test(test_upload_not_found_meeting(BaseUrl, Root)),
  31. ?_test(test_list(BaseUrl, Root)),
  32. ?_test(test_list_reverse(BaseUrl, Root)),
  33. ?_test(test_list_not_found_meeting(BaseUrl, Root)),
  34. ?_test(test_get(BaseUrl, Root)),
  35. ?_test(test_get_not_found(BaseUrl, Root)),
  36. ?_test(test_delete(BaseUrl, Root))]
  37. end
  38. }.
  39. gen_file(Size, FileName) ->
  40. Body = string:copies("content", Size),
  41. "------WebKitFormBoundaryLwCN5mZmxIA54Aif\r\n" ++
  42. "Content-Disposition: form-data; name=\"content\"; filename=\"" ++ FileName ++ "\"\r\n" ++
  43. "Content-Type: application/octet-stream\r\n\r\n" ++
  44. Body ++ "\r\n".
  45. gen_param(Name, Value) ->
  46. "------WebKitFormBoundaryLwCN5mZmxIA54Aif\r\n" ++
  47. "Content-Disposition: form-data; name=\""++ Name ++"\"\r\n\r\n" ++
  48. Value ++ "\r\n".
  49. gen_params(Body, []) ->
  50. Body ++ "------WebKitFormBoundaryLwCN5mZmxIA54Aif--\r\n";
  51. gen_params(Body, [{Name, Value} | Rest]) ->
  52. gen_params(Body ++ gen_param(Name, Value), Rest).
  53. gen_params(Params) when is_list(Params) ->
  54. gen_params("", Params).
  55. upload(BaseUrl, Params, Body) ->
  56. upload(BaseUrl, "testmeeting", Params, Body).
  57. upload(BaseUrl, Meeting, URIParams, Body) ->
  58. tests_utils:post(BaseUrl,
  59. "/file/" ++ Meeting ++"/",
  60. URIParams,
  61. "multipart/form-data; boundary=----WebKitFormBoundaryLwCN5mZmxIA54Aif",
  62. Body).
  63. upload_raw(BaseUrl, Params, Body) ->
  64. upload_raw(BaseUrl, "testmeeting", Params, Body).
  65. upload_raw(BaseUrl, Meeting, URIParams, Body) ->
  66. tests_utils:post_raw(BaseUrl,
  67. "/file/" ++ Meeting ++"/",
  68. URIParams,
  69. "multipart/form-data; boundary=----WebKitFormBoundaryLwCN5mZmxIA54Aif",
  70. Body).
  71. test_upload_small(BaseUrl, {RootUid, RootSid}) ->
  72. Params = [{"uid", RootUid},
  73. {"sid", RootSid},
  74. {"metadata[description]", "test_file"}],
  75. {struct,[{"result", _}]} = upload(BaseUrl, [], gen_file(4, "small.pdf") ++ gen_params(Params)),
  76. ParamsGet = [{"uid", RootUid},
  77. {"sid", RootSid},
  78. {"type", "internal.file.add"},
  79. {"count", "1"},
  80. {"order", "desc"}],
  81. {struct,[{"result", {array, [{struct, [{"type", "internal.file.add"},
  82. {"domain", _},
  83. {"datetime", _},
  84. {"id", _},
  85. {"location", "testmeeting"},
  86. {"from", RootUid},
  87. {"metadata", Metadata}]}]}}]} =
  88. tests_utils:get(BaseUrl, "/event/testmeeting", ParamsGet),
  89. ?assertMatch({struct, [{"id", _},
  90. {"domain", _},
  91. {"name", "small.pdf"},
  92. {"size", "28"},
  93. {"mime", "application/pdf"},
  94. {"description", "test_file"}]}, Metadata).
  95. test_upload_with_force_content_type(BaseUrl, {RootUid, RootSid}) ->
  96. Params = [{"uid", RootUid},
  97. {"sid", RootSid},
  98. {"metadata[description]", "test_file"},
  99. {"forceContentType", "text/html"}],
  100. {ok, "201", Headers, _} = upload_raw(BaseUrl, [], gen_file(4, "small.pdf") ++ gen_params(Params)),
  101. ?assertEqual(lists:keyfind("Content-Type", 1, Headers), {"Content-Type", "text/html"}).
  102. test_upload_big(BaseUrl, {RootUid, RootSid}) ->
  103. Params = [{"uid", RootUid},
  104. {"sid", RootSid},
  105. {"metadata[description]", "test_file"}],
  106. ?assertMatch({struct,[{"result", _}]}, upload(BaseUrl, [], gen_file(4000, "big") ++ gen_params(Params))).
  107. test_upload_big_param(BaseUrl, {RootUid, RootSid}) ->
  108. Params = [{"uid", RootUid},
  109. {"sid", RootSid},
  110. {"metadata[description]", string:copies("test_file", 4000)}],
  111. ?assertMatch({struct,[{"result", _}]}, upload(BaseUrl, [], gen_file(4, "small") ++ gen_params(Params))).
  112. test_upload_not_found_meeting(BaseUrl, {RootUid, RootSid}) ->
  113. Params = [{"uid", RootUid},
  114. {"sid", RootSid},
  115. {"metadata[description]", "test_file"}],
  116. ?assertEqual({struct,[{"error", "not_found"}]}, upload(BaseUrl, "nonexistentmeeting", [], gen_file(4, "small") ++ gen_params(Params))).
  117. test_list(BaseUrl, {RootUid, RootSid}) ->
  118. Params = [{"uid", RootUid},
  119. {"sid", RootSid}],
  120. Result = tests_utils:get(BaseUrl, "/file/testmeeting/", Params),
  121. ?assertMatch({struct,
  122. [{"result",
  123. {array,
  124. [{struct,
  125. [{"id", _},
  126. {"domain", _},
  127. {"name",_},
  128. {"uri", _},
  129. {"location", "testmeeting"},
  130. {"metadata",{struct,[{"description", "test_file"}]}}]}|_]}}]}, Result).
  131. test_list_reverse(BaseUrl, {RootUid, RootSid}) ->
  132. Params = [{"uid", RootUid},
  133. {"sid", RootSid},
  134. {"order", "desc"}],
  135. Result = tests_utils:get(BaseUrl, "/file/testmeeting/", Params),
  136. String = string:copies("test_file", 4000),
  137. ?assertMatch({struct,
  138. [{"result",
  139. {array,
  140. [{struct,
  141. [{"id", _},
  142. {"domain", _},
  143. {"name",_},
  144. {"uri", _},
  145. {"location", "testmeeting"},
  146. {"metadata",{struct,[{"description", String}]}}]}|_]}}]}, Result).
  147. test_list_not_found_meeting(BaseUrl, {RootUid, RootSid}) ->
  148. Params = [{"uid", RootUid},
  149. {"sid", RootSid}],
  150. ?assertMatch({struct,[{"error", "not_found"}]}, tests_utils:get(BaseUrl, "/file/unexistentmeeting/", Params)).
  151. test_get(BaseUrl, {RootUid, RootSid}) ->
  152. Params = [{"uid", RootUid},
  153. {"sid", RootSid},
  154. {"metadata[description]", "test_file"}],
  155. {struct,[{"result", Id}]} = upload(BaseUrl, [], gen_file(4, "small") ++ gen_params(Params)),
  156. ParamsGet = [{"uid", RootUid},
  157. {"sid", RootSid}],
  158. tests_utils:get_raw(BaseUrl, "/file/testmeeting/" ++ Id, ParamsGet).
  159. test_get_not_found(BaseUrl, {RootUid, RootSid}) ->
  160. Params = [{"uid", RootUid},
  161. {"sid", RootSid}],
  162. {struct,[{"error", "not_found"}]} =
  163. tests_utils:get(BaseUrl, "/file/testmeeting/unexistentfile", Params).
  164. test_delete(BaseUrl, {RootUid, RootSid}) ->
  165. Params = [{"uid", RootUid},
  166. {"sid", RootSid}],
  167. {struct,[{"result", Id}]} = upload(BaseUrl, [], gen_file(4, "small") ++ gen_params(Params)),
  168. ?assertMatch({struct,[{"result", "ok"}]},
  169. tests_utils:delete(BaseUrl, "/file/testmeeting/" ++ Id, Params)),
  170. ?assertMatch({struct,[{"error", "not_found"}]},
  171. tests_utils:get(BaseUrl, "/file/testmeeting/" ++ Id, Params)).