✨ Split e2e testing file upload for raw requests (#418)
This commit is contained in:
committed by
Anton Shepilov
parent
987da01692
commit
ce77e26cb3
@@ -1,5 +1,6 @@
|
|||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
import fs from "fs";
|
import fs from "fs";
|
||||||
|
import { Readable } from 'stream';
|
||||||
import { ResourceUpdateResponse, Workspace } from "../../../src/utils/types";
|
import { ResourceUpdateResponse, Workspace } from "../../../src/utils/types";
|
||||||
import { File } from "../../../src/services/files/entities/file";
|
import { File } from "../../../src/services/files/entities/file";
|
||||||
import { deserialize } from "class-transformer";
|
import { deserialize } from "class-transformer";
|
||||||
@@ -79,18 +80,31 @@ 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 uploadFile(filename: string) {
|
async injectUploadRequest(readable: Readable | string) {
|
||||||
logger.info(`Upload ${filename} for the user: ${this.user.id}`);
|
function makeReadableFromString(str: string): Readable {
|
||||||
const fullPath = `${__dirname}/assets/${filename}`;
|
const result = new Readable();
|
||||||
|
result._read = () => {};
|
||||||
|
result.push(str);
|
||||||
|
result.push(null);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
if (typeof readable === "string")
|
||||||
|
readable = makeReadableFromString(readable);
|
||||||
const url = "/internal/services/files/v1";
|
const url = "/internal/services/files/v1";
|
||||||
const form = formAutoContent({ file: fs.createReadStream(fullPath) });
|
const form = formAutoContent({ file: readable });
|
||||||
form.headers["authorization"] = `Bearer ${this.jwt}`;
|
form.headers["authorization"] = `Bearer ${this.jwt}`;
|
||||||
|
|
||||||
const filesUploadRaw = await this.platform.app.inject({
|
return await this.platform.app.inject({
|
||||||
method: "POST",
|
method: "POST",
|
||||||
url: `${url}/companies/${this.platform.workspace.company_id}/files?thumbnail_sync=0`,
|
url: `${url}/companies/${this.platform.workspace.company_id}/files?thumbnail_sync=0`,
|
||||||
...form
|
...form
|
||||||
});
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
async uploadFile(filename: string) {
|
||||||
|
logger.info(`Upload ${filename} for the user: ${this.user.id}`);
|
||||||
|
const fullPath = `${__dirname}/assets/${filename}`;
|
||||||
|
const filesUploadRaw = await this.injectUploadRequest(fs.createReadStream(fullPath));
|
||||||
|
|
||||||
const filesUpload: ResourceUpdateResponse<File> = deserialize<ResourceUpdateResponse<File>>(
|
const filesUpload: ResourceUpdateResponse<File> = deserialize<ResourceUpdateResponse<File>>(
|
||||||
ResourceUpdateResponse,
|
ResourceUpdateResponse,
|
||||||
|
|||||||
Reference in New Issue
Block a user