♻️Refactor files uploading test to use public API
This commit is contained in:
committed by
Anton Shepilov
parent
9ec4e2a079
commit
389d42fe4f
@@ -80,16 +80,9 @@ export default class UserApi {
|
|||||||
return await this.uploadFile(UserApi.ALL_FILES[Math.floor((Math.random() * UserApi.ALL_FILES.length))]);
|
return await this.uploadFile(UserApi.ALL_FILES[Math.floor((Math.random() * UserApi.ALL_FILES.length))]);
|
||||||
}
|
}
|
||||||
|
|
||||||
async injectUploadRequest(readable: Readable | string) {
|
private async injectUploadRequest(readable: Readable | string) {
|
||||||
function makeReadableFromString(str: string): Readable {
|
|
||||||
const result = new Readable();
|
|
||||||
result._read = () => {};
|
|
||||||
result.push(str);
|
|
||||||
result.push(null);
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
if (typeof readable === "string")
|
if (typeof readable === "string")
|
||||||
readable = makeReadableFromString(readable);
|
readable = Readable.from(readable);
|
||||||
const url = "/internal/services/files/v1";
|
const url = "/internal/services/files/v1";
|
||||||
const form = formAutoContent({ file: readable });
|
const form = formAutoContent({ file: readable });
|
||||||
form.headers["authorization"] = `Bearer ${this.jwt}`;
|
form.headers["authorization"] = `Bearer ${this.jwt}`;
|
||||||
@@ -106,11 +99,17 @@ export default class UserApi {
|
|||||||
const fullPath = `${__dirname}/assets/${filename}`;
|
const fullPath = `${__dirname}/assets/${filename}`;
|
||||||
const filesUploadRaw = await this.injectUploadRequest(fs.createReadStream(fullPath));
|
const filesUploadRaw = await this.injectUploadRequest(fs.createReadStream(fullPath));
|
||||||
|
|
||||||
const filesUpload: ResourceUpdateResponse<File> = deserialize<ResourceUpdateResponse<File>>(
|
if (filesUploadRaw.statusCode == 200) {
|
||||||
ResourceUpdateResponse,
|
const filesUpload: ResourceUpdateResponse<File> = deserialize<ResourceUpdateResponse<File>>(
|
||||||
filesUploadRaw.body
|
ResourceUpdateResponse,
|
||||||
);
|
filesUploadRaw.body
|
||||||
return filesUpload.resource;
|
);
|
||||||
|
return filesUpload.resource;
|
||||||
|
} else this.throwServerError(filesUploadRaw.statusCode);
|
||||||
|
}
|
||||||
|
|
||||||
|
private throwServerError(code: number) {
|
||||||
|
throw new Error("Error code: " + code)
|
||||||
}
|
}
|
||||||
|
|
||||||
public getJWTTokenForUser(userId: string): string {
|
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"
|
//then file should be not found with 404 error and "File not found message"
|
||||||
expect(fileDownloadResponse).toBeTruthy();
|
expect(fileDownloadResponse).toBeTruthy();
|
||||||
expect(fileDownloadResponse.statusCode).toBe(500);
|
expect(fileDownloadResponse.statusCode).toBe(500);
|
||||||
}, 120000);
|
});
|
||||||
|
|
||||||
it("should fail an upload POST when the backend storage fails", async () => {
|
it("should fail an upload POST when the backend storage fails", async () => {
|
||||||
const thrower = () => {
|
const thrower = () => {
|
||||||
@@ -58,12 +58,13 @@ describe("The Files feature", () => {
|
|||||||
};
|
};
|
||||||
const writeS3Mock = jest.spyOn(MinioClient.prototype, "putObject").mockImplementation(thrower);
|
const writeS3Mock = jest.spyOn(MinioClient.prototype, "putObject").mockImplementation(thrower);
|
||||||
const writeLocalMock = jest.spyOn(LocalConnectorService.prototype, "write").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.statusCode).toBe(500);
|
||||||
expect(response).toBeTruthy();
|
await expect(helpers.uploadRandomFile()).rejects.toThrow("Error code: 500");
|
||||||
expect(response.statusCode).toBe(500);
|
|
||||||
|
// expect(response.statusCode).toBe(500);
|
||||||
expect(writeS3Mock.mock.calls.length + writeLocalMock.mock.calls.length).toEqual(1);
|
expect(writeS3Mock.mock.calls.length + writeLocalMock.mock.calls.length).toEqual(1);
|
||||||
}, 120000);
|
});
|
||||||
|
|
||||||
it("Download file should return 200 if file exists", async () => {
|
it("Download file should return 200 if file exists", async () => {
|
||||||
//given file
|
//given file
|
||||||
@@ -80,7 +81,7 @@ describe("The Files feature", () => {
|
|||||||
//then file should be not found with 404 error and "File not found message"
|
//then file should be not found with 404 error and "File not found message"
|
||||||
expect(fileDownloadResponse).toBeTruthy();
|
expect(fileDownloadResponse).toBeTruthy();
|
||||||
expect(fileDownloadResponse.statusCode).toBe(200);
|
expect(fileDownloadResponse.statusCode).toBe(200);
|
||||||
}, 120000);
|
});
|
||||||
|
|
||||||
it.skip("should save file and generate previews", async () => {
|
it.skip("should save file and generate previews", async () => {
|
||||||
for (const i in UserApi.ALL_FILES) {
|
for (const i in UserApi.ALL_FILES) {
|
||||||
@@ -101,6 +102,6 @@ describe("The Files feature", () => {
|
|||||||
expect(thumbnails.statusCode).toBe(200);
|
expect(thumbnails.statusCode).toBe(200);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}, 1200000);
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user