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