diff --git a/package.json b/package.json index 70742b66c4..1c86409bad 100644 --- a/package.json +++ b/package.json @@ -75,6 +75,7 @@ "@sentry/tracing": "^7.51.2", "@tippyjs/react": "^4.2.6", "@tommoor/remove-markdown": "^0.3.2", + "@types/form-data": "^2.5.0", "@types/sanitize-filename": "^1.6.3", "@vitejs/plugin-react": "^3.1.0", "addressparser": "^1.0.1", @@ -104,6 +105,7 @@ "fetch-retry": "^5.0.5", "fetch-with-proxy": "^3.0.1", "focus-visible": "^5.2.0", + "form-data": "^4.0.0", "fractional-index": "^1.0.0", "framer-motion": "^4.1.17", "fs-extra": "^11.1.1", diff --git a/plugins/storage/server/api/files.test.ts b/plugins/storage/server/api/files.test.ts index 0721c1ca69..c39c187d5d 100644 --- a/plugins/storage/server/api/files.test.ts +++ b/plugins/storage/server/api/files.test.ts @@ -1,4 +1,10 @@ -import { buildUser } from "@server/test/factories"; +import { existsSync } from "fs"; +import { readFile } from "fs/promises"; +import path from "path"; +import FormData from "form-data"; +import env from "@server/env"; +import "@server/test/env"; +import { buildAttachment, buildUser } from "@server/test/factories"; import { getTestServer } from "@server/test/support"; const server = getTestServer(); @@ -18,6 +24,35 @@ describe("#files.create", () => { "key: Must be of the form uploads/// or public///" ); }); + + it("should succeed with status 200 ok and create a file", async () => { + const user = await buildUser(); + const attachment = await buildAttachment({ + teamId: user.teamId, + userId: user.id, + }); + + const fileName = "images.docx"; + const content = await readFile( + path.resolve(__dirname, "..", "test", "fixtures", fileName) + ); + const form = new FormData(); + form.append("key", attachment.key); + form.append("file", content, fileName); + form.append("token", user.getJwtToken()); + + const res = await server.post(`/api/files.create`, { + headers: form.getHeaders(), + body: form, + }); + + const body = await res.json(); + expect(res.status).toEqual(200); + expect(body.success).toEqual(true); + expect( + existsSync(path.join(env.FILE_STORAGE_LOCAL_ROOT_DIR, attachment.key)) + ).toBe(true); + }); }); describe("#files.get", () => { diff --git a/plugins/storage/server/test/fixtures/images.docx b/plugins/storage/server/test/fixtures/images.docx new file mode 100644 index 0000000000..25a9156a8d Binary files /dev/null and b/plugins/storage/server/test/fixtures/images.docx differ diff --git a/server/test/env.ts b/server/test/env.ts index 9a300a2046..a85682ea77 100644 --- a/server/test/env.ts +++ b/server/test/env.ts @@ -18,6 +18,9 @@ env.OIDC_USERINFO_URI = "http://localhost/userinfo"; env.RATE_LIMITER_ENABLED = false; +env.FILE_STORAGE = "local"; +env.FILE_STORAGE_LOCAL_ROOT_DIR = "/tmp"; + if (process.env.DATABASE_URL_TEST) { env.DATABASE_URL = process.env.DATABASE_URL_TEST; } diff --git a/server/test/factories.ts b/server/test/factories.ts index f4596960aa..7f9f8ea62a 100644 --- a/server/test/factories.ts +++ b/server/test/factories.ts @@ -435,7 +435,7 @@ export async function buildAttachment(overrides: Partial = {}) { overrides.documentId = document.id; } - return Attachment.create({ + const attachment = await Attachment.create({ key: `uploads/key/to/${faker.system.fileName}.png`, contentType: "image/png", size: 100, @@ -444,6 +444,9 @@ export async function buildAttachment(overrides: Partial = {}) { updatedAt: new Date("2018-01-02T00:00:00.000Z"), ...overrides, }); + + attachment.key = `public/${overrides.userId}/${attachment.id}/images.docx`; + return attachment.save(); } export async function buildWebhookSubscription( diff --git a/server/test/support.ts b/server/test/support.ts index f5ad901e69..28d0f9daad 100644 --- a/server/test/support.ts +++ b/server/test/support.ts @@ -1,4 +1,6 @@ -import TestServer from "fetch-test-server"; +import http from "http"; +// eslint-disable-next-line no-restricted-imports +import nodeFetch from "node-fetch"; import { WhereOptions } from "sequelize"; import sharedEnv from "@shared/env"; import env from "@server/env"; @@ -7,10 +9,80 @@ import onerror from "@server/onerror"; import webService from "@server/services/web"; import { sequelize } from "@server/storage/database"; +function TestServer(app: any) { + // allow custom promise + if (!TestServer.Promise) { + throw new Error( + "native promise missing, set TestServer.Promise to your favorite alternative" + ); + } + + this.Promise = TestServer.Promise; + this.server = http.createServer(app); + + ["delete", "get", "head", "options", "patch", "post", "put"].forEach( + (method) => { + this[method] = (path: any, options: any) => + this.fetch( + path, + Object.assign({}, options, { method: method.toUpperCase() }) + ); + } + ); + + Object.defineProperty(this, "address", { + get: function address() { + const port = this.server.address().port; + return `http://localhost:${port}`; + }, + }); +} + +TestServer.prototype.listen = function listen() { + if (!this.listener) { + this.listener = new this.Promise((resolve: any, reject: any) => { + this.server + .listen(0, () => resolve()) + .on("error", (err: any) => { + reject(err); + }); + }); + } + + return this.listener; +}; + +TestServer.prototype.close = function close() { + this.listener = null; + + return new this.Promise((resolve: any, reject: any) => { + this.server.close((err: any) => (err ? reject(err) : resolve())); + }); +}; + +TestServer.prototype.fetch = function fetch(path: any, opts: any) { + return this.listen().then(() => { + const url = `${this.address}${path}`; + const options = Object.assign({ headers: {} }, opts); + const contentType = + options.headers["Content-Type"] ?? options.headers["content-type"]; + // automatic JSON encoding + if (!contentType && typeof options.body === "object") { + options.headers["Content-Type"] = "application/json"; + options.body = JSON.stringify(options.body); + } + + return nodeFetch(url, options); + }); +}; + +// expose Promise +TestServer.Promise = global.Promise; + export function getTestServer() { const app = webService(); onerror(app); - const server = new TestServer(app.callback()); + const server = new (TestServer as any)(app.callback()); server.disconnect = async () => { await sequelize.close(); diff --git a/yarn.lock b/yarn.lock index 907e53baa4..942abee21d 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2908,6 +2908,13 @@ "@types/qs" "*" "@types/serve-static" "*" +"@types/form-data@^2.5.0": + version "2.5.0" + resolved "https://registry.yarnpkg.com/@types/form-data/-/form-data-2.5.0.tgz#5025f7433016f923348434c40006d9a797c1b0e8" + integrity sha512-23/wYiuckYYtFpL+4RPWiWmRQH2BjFuqCUi2+N3amB1a1Drv+i/byTrGvlLwRVLFNAZbwpbQ7JvTK+VCAPMbcg== + dependencies: + form-data "*" + "@types/formidable@^2.0.5", "@types/formidable@^2.0.6": version "2.0.6" resolved "https://registry.yarnpkg.com/@types/formidable/-/formidable-2.0.6.tgz#811ed3cd8a8a7675e02420b3f861c317e055376a" @@ -3221,14 +3228,19 @@ integrity sha512-WYMWhAQLuBym+6qQ2Ojptm6qIACnkkYYs08sj+PVgRCrB6b7k1QpTRk0yMmxhlpPn5MbXcSfd6sHOYlzaokU3w== "@types/node-fetch@^2.6.2": - version "2.6.2" - resolved "https://registry.yarnpkg.com/@types/node-fetch/-/node-fetch-2.6.2.tgz#d1a9c5fd049d9415dce61571557104dec3ec81da" - integrity sha512-DHqhlq5jeESLy19TYhLakJ07kNumXWjcDdxXsLUMJZ6ue8VZJj4kLPQVE/2mdHh3xZziNF1xppu5lwmS53HR+A== + version "2.6.4" + resolved "https://registry.yarnpkg.com/@types/node-fetch/-/node-fetch-2.6.4.tgz#1bc3a26de814f6bf466b25aeb1473fa1afe6a660" + integrity sha512-1ZX9fcN4Rvkvgv4E6PAY5WXUFWFcRWxZa3EW83UjycOB9ljJCedb2CupIP4RZMEwF/M3eTcCihbBRgwtGbg5Rg== dependencies: "@types/node" "*" form-data "^3.0.0" -"@types/node@*", "@types/node@18.0.6", "@types/node@>=10.0.0", "@types/node@>=13.7.0": +"@types/node@*", "@types/node@>=10.0.0", "@types/node@>=13.7.0": + version "20.5.9" + resolved "https://registry.yarnpkg.com/@types/node/-/node-20.5.9.tgz#a70ec9d8fa0180a314c3ede0e20ea56ff71aed9a" + integrity sha512-PcGNd//40kHAS3sTlzKB9C9XL4K0sTup8nbG5lC14kzEteTNuAFh9u5nA0o5TWnSG2r/JNPRXFVcHJIIeRlmqQ== + +"@types/node@18.0.6": version "18.0.6" resolved "https://registry.yarnpkg.com/@types/node/-/node-18.0.6.tgz#0ba49ac517ad69abe7a1508bc9b3a5483df9d5d7" integrity sha512-/xUq6H2aQm261exT6iZTMifUySEt4GR5KX8eYyY+C4MSNPqSh9oNIP7tz2GLKTlFaiBbgZNxffoR3CVRG+cljw== @@ -3987,7 +3999,7 @@ async@^3.2.3: asynckit@^0.4.0: version "0.4.0" resolved "https://registry.yarnpkg.com/asynckit/-/asynckit-0.4.0.tgz#c79ed97f7f34cb8f2ba1bc9790bcc366474b4b79" - integrity sha1-x57Zf380y48robyXkLzDZkdLS3k= + integrity sha512-Oei9OH4tRh0YqU3GxhX79dM/mwVgvbZJaSNaRk+bshkj0S5cfHcgYakreBjrHwatXKbz+IoIdYLxrKim2MjW0Q== at-least-node@^1.0.0: version "1.0.0" @@ -5692,7 +5704,7 @@ delay@^5.0.0: delayed-stream@~1.0.0: version "1.0.0" resolved "https://registry.yarnpkg.com/delayed-stream/-/delayed-stream-1.0.0.tgz#df3ae199acadfb7d440aaae0b29e2272b24ec619" - integrity sha1-3zrhmayt+31ECqrgsp4icrJOxhk= + integrity sha512-ZySD7Nf91aLB0RxL4KGrKHBXl7Eds1DAmEdcoVawXnLD7SDhpNgtuII2aAkg7a7QS41jxPSZ17p4VdGnMHk3MQ== delegates@^1.0.0: version "1.0.0" @@ -6932,19 +6944,19 @@ for-each@^0.3.3: dependencies: is-callable "^1.1.3" -form-data@^3.0.0: - version "3.0.0" - resolved "https://registry.yarnpkg.com/form-data/-/form-data-3.0.0.tgz#31b7e39c85f1355b7139ee0c647cf0de7f83c682" - integrity sha512-CKMFDglpbMi6PyN+brwB9Q/GOw0eAnsrEZDgcsH5Krhz5Od/haKHAX0NmQfha2zPPz0JpWzA7GJHGSnvCRLWsg== +form-data@*, form-data@^4.0.0: + version "4.0.0" + resolved "https://registry.yarnpkg.com/form-data/-/form-data-4.0.0.tgz#93919daeaf361ee529584b9b31664dc12c9fa452" + integrity sha512-ETEklSGi5t0QMZuiXoA/Q6vcnxcLQP5vdugSpuAyi6SVGi2clPPp+xgEhuMaHC+zGgn31Kd235W35f7Hykkaww== dependencies: asynckit "^0.4.0" combined-stream "^1.0.8" mime-types "^2.1.12" -form-data@^4.0.0: - version "4.0.0" - resolved "https://registry.yarnpkg.com/form-data/-/form-data-4.0.0.tgz#93919daeaf361ee529584b9b31664dc12c9fa452" - integrity sha512-ETEklSGi5t0QMZuiXoA/Q6vcnxcLQP5vdugSpuAyi6SVGi2clPPp+xgEhuMaHC+zGgn31Kd235W35f7Hykkaww== +form-data@^3.0.0: + version "3.0.1" + resolved "https://registry.yarnpkg.com/form-data/-/form-data-3.0.1.tgz#ebd53791b78356a99af9a300d4282c4d5eb9755f" + integrity sha512-RHkBKtLWUVwd7SqRIvCZMEvAMoGUp0XU+seQiZejj0COz3RI3hWP4sCv3gZWWLjJTd7rGwcsF5eKZGii0r/hbg== dependencies: asynckit "^0.4.0" combined-stream "^1.0.8" @@ -12840,7 +12852,7 @@ tr46@^4.1.1: tr46@~0.0.3: version "0.0.3" resolved "https://registry.yarnpkg.com/tr46/-/tr46-0.0.3.tgz#8184fd347dac9cdc185992f3a6622e14b9d9ab6a" - integrity sha1-gYT9NH2snNwYWZLzpmIuFLnZq2o= + integrity sha512-N3WMsuqV66lT30CrXNbEjx4GEwlow3v6rr4mCcv6prnfwhS01rkgyFdjPNBYd9br7LpXV1+Emh01fHnq2Gdgrw== tree-kill@^1.2.2: version "1.2.2" @@ -13434,7 +13446,7 @@ walker@^1.0.8: webidl-conversions@^3.0.0: version "3.0.1" resolved "https://registry.yarnpkg.com/webidl-conversions/-/webidl-conversions-3.0.1.tgz#24534275e2a7bc6be7bc86611cc16ae0a5654871" - integrity sha1-JFNCdeKnvGvnvIZhHMFq4KVlSHE= + integrity sha512-2JAn3z8AR6rjK8Sm8orRC0h/bcl/DqL7tRPdGZ4I1CjdF+EaMLmYxBHyXuKL849eucPFhvBoxMsflfOb8kxaeQ== webidl-conversions@^4.0.2: version "4.0.2" @@ -13490,7 +13502,7 @@ whatwg-url@^12.0.0, whatwg-url@^12.0.1: whatwg-url@^5.0.0: version "5.0.0" resolved "https://registry.yarnpkg.com/whatwg-url/-/whatwg-url-5.0.0.tgz#966454e8765462e37644d3626f6742ce8b70965d" - integrity sha1-lmRU6HZUYuN2RNNib2dCzotwll0= + integrity sha512-saE57nupxk6v3HY35+jzBwYa0rKSy0XR8JSxZPwgLr7ys0IBzhGviA1/TUGJLmSVqs8pb9AnvICXEuOHLprYTw== dependencies: tr46 "~0.0.3" webidl-conversions "^3.0.0"