From 389d42fe4f3b36ee11b1cb2138cc360d5ee8d4d2 Mon Sep 17 00:00:00 2001 From: Anton SHEPILOV Date: Fri, 22 Mar 2024 16:57:23 +0100 Subject: [PATCH] =?UTF-8?q?=E2=99=BB=EF=B8=8FRefactor=20files=20uploading?= =?UTF-8?q?=20test=20to=20use=20public=20API?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../backend/node/test/e2e/common/user-api.ts | 27 +++++++++---------- .../backend/node/test/e2e/files/files.spec.ts | 17 ++++++------ 2 files changed, 22 insertions(+), 22 deletions(-) diff --git a/tdrive/backend/node/test/e2e/common/user-api.ts b/tdrive/backend/node/test/e2e/common/user-api.ts index 61ca55b6..ade72f20 100644 --- a/tdrive/backend/node/test/e2e/common/user-api.ts +++ b/tdrive/backend/node/test/e2e/common/user-api.ts @@ -80,16 +80,9 @@ export default class UserApi { return await this.uploadFile(UserApi.ALL_FILES[Math.floor((Math.random() * UserApi.ALL_FILES.length))]); } - async injectUploadRequest(readable: Readable | string) { - function makeReadableFromString(str: string): Readable { - const result = new Readable(); - result._read = () => {}; - result.push(str); - result.push(null); - return result; - } + private async injectUploadRequest(readable: Readable | string) { if (typeof readable === "string") - readable = makeReadableFromString(readable); + readable = Readable.from(readable); const url = "/internal/services/files/v1"; const form = formAutoContent({ file: readable }); form.headers["authorization"] = `Bearer ${this.jwt}`; @@ -106,11 +99,17 @@ export default class UserApi { const fullPath = `${__dirname}/assets/${filename}`; const filesUploadRaw = await this.injectUploadRequest(fs.createReadStream(fullPath)); - const filesUpload: ResourceUpdateResponse = deserialize>( - ResourceUpdateResponse, - filesUploadRaw.body - ); - return filesUpload.resource; + if (filesUploadRaw.statusCode == 200) { + const filesUpload: ResourceUpdateResponse = deserialize>( + ResourceUpdateResponse, + filesUploadRaw.body + ); + return filesUpload.resource; + } else this.throwServerError(filesUploadRaw.statusCode); + } + + private throwServerError(code: number) { + throw new Error("Error code: " + code) } public getJWTTokenForUser(userId: string): string { diff --git a/tdrive/backend/node/test/e2e/files/files.spec.ts b/tdrive/backend/node/test/e2e/files/files.spec.ts index 948433cb..07b4a34a 100644 --- a/tdrive/backend/node/test/e2e/files/files.spec.ts +++ b/tdrive/backend/node/test/e2e/files/files.spec.ts @@ -50,7 +50,7 @@ describe("The Files feature", () => { //then file should be not found with 404 error and "File not found message" expect(fileDownloadResponse).toBeTruthy(); expect(fileDownloadResponse.statusCode).toBe(500); - }, 120000); + }); it("should fail an upload POST when the backend storage fails", async () => { const thrower = () => { @@ -58,12 +58,13 @@ describe("The Files feature", () => { }; const writeS3Mock = jest.spyOn(MinioClient.prototype, "putObject").mockImplementation(thrower); const writeLocalMock = jest.spyOn(LocalConnectorService.prototype, "write").mockImplementation(thrower); - // const writeMock = jest.spyOn(platform.storage, "write").mockImplementation(thrower) - const response = await helpers.injectUploadRequest("dummy"); - expect(response).toBeTruthy(); - expect(response.statusCode).toBe(500); + + // expect(response.statusCode).toBe(500); + await expect(helpers.uploadRandomFile()).rejects.toThrow("Error code: 500"); + + // expect(response.statusCode).toBe(500); expect(writeS3Mock.mock.calls.length + writeLocalMock.mock.calls.length).toEqual(1); - }, 120000); + }); it("Download file should return 200 if file exists", async () => { //given file @@ -80,7 +81,7 @@ describe("The Files feature", () => { //then file should be not found with 404 error and "File not found message" expect(fileDownloadResponse).toBeTruthy(); expect(fileDownloadResponse.statusCode).toBe(200); - }, 120000); + }); it.skip("should save file and generate previews", async () => { for (const i in UserApi.ALL_FILES) { @@ -101,6 +102,6 @@ describe("The Files feature", () => { expect(thumbnails.statusCode).toBe(200); } } - }, 1200000); + }); }); });