♻️Refactor files uploading test to use public API

This commit is contained in:
Anton SHEPILOV
2024-03-22 16:57:23 +01:00
committed by Anton Shepilov
parent 9ec4e2a079
commit 389d42fe4f
2 changed files with 22 additions and 22 deletions
+13 -14
View File
@@ -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<File> = deserialize<ResourceUpdateResponse<File>>(
ResourceUpdateResponse,
filesUploadRaw.body
);
return filesUpload.resource;
if (filesUploadRaw.statusCode == 200) {
const filesUpload: ResourceUpdateResponse<File> = deserialize<ResourceUpdateResponse<File>>(
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 {
@@ -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);
});
});
});