🐛 #23 Fix throw error when file doesn't exist
* #23 Fix throw error when file doesn't exist Run all the tests in once without special runner Add positive scenario Move coverage to the backend build workflow
This commit is contained in:
@@ -8,15 +8,18 @@ import fs from "fs";
|
||||
import { File } from "../../../src/services/files/entities/file";
|
||||
import { deserialize } from "class-transformer";
|
||||
import formAutoContent from "form-auto-content";
|
||||
import LocalConnectorService from "../../../src/core/platform/services/storage/connectors/local/service";
|
||||
|
||||
describe.skip("The Files feature", () => {
|
||||
|
||||
describe("The Files feature", () => {
|
||||
const url = "/internal/services/files/v1";
|
||||
let platform: TestPlatform;
|
||||
|
||||
beforeAll(async () => {
|
||||
platform = await init({
|
||||
services: ["webserver", "database", "storage", "message-queue", "files", "previews"],
|
||||
services: ["webserver", "database", "storage", "files", "previews"],
|
||||
});
|
||||
await platform.database.getConnector().init();
|
||||
});
|
||||
|
||||
afterAll(async done => {
|
||||
@@ -25,6 +28,22 @@ describe.skip("The Files feature", () => {
|
||||
done();
|
||||
});
|
||||
|
||||
async function uploadFile(file: string) {
|
||||
const form = formAutoContent({file: fs.createReadStream(file)});
|
||||
form.headers["authorization"] = `Bearer ${await platform.auth.getJWTToken()}`;
|
||||
|
||||
const filesUploadRaw = await platform.app.inject({
|
||||
method: "POST",
|
||||
url: `${url}/companies/${platform.workspace.company_id}/files?thumbnail_sync=1`,
|
||||
...form,
|
||||
});
|
||||
const filesUpload: ResourceUpdateResponse<File> = deserialize(
|
||||
ResourceUpdateResponse,
|
||||
filesUploadRaw.body,
|
||||
);
|
||||
return filesUpload;
|
||||
}
|
||||
|
||||
describe("On user send files", () => {
|
||||
const files = [
|
||||
"assets/sample.png",
|
||||
@@ -36,22 +55,49 @@ describe.skip("The Files feature", () => {
|
||||
].map(p => `${__dirname}/${p}`);
|
||||
const thumbnails = [1, 1, 2, 5, 0, 1];
|
||||
|
||||
it("should save file and generate previews", async done => {
|
||||
it("Download file should return 500 if file doesn't exists", async () => {
|
||||
//given file
|
||||
const filesUpload = await uploadFile(files[0]);
|
||||
expect(filesUpload.resource.id).toBeTruthy();
|
||||
//clean files directory
|
||||
expect(platform.storage.getConnector()).toBeInstanceOf(LocalConnectorService)
|
||||
const path = (<LocalConnectorService>platform.storage.getConnector()).configuration.path;
|
||||
fs.readdirSync(path).forEach(f => fs.rmSync(`${path}/${f}`, {recursive: true, force: true}));
|
||||
//when try to download the file
|
||||
const fileDownloadResponse = await platform.app.inject({
|
||||
method: "GET",
|
||||
url: `${url}/companies/${platform.workspace.company_id}/files/${filesUpload.resource.id}/download`,
|
||||
});
|
||||
//then file should be not found with 404 error and "File not found message"
|
||||
expect(fileDownloadResponse).toBeTruthy();
|
||||
expect(fileDownloadResponse.statusCode).toBe(500);
|
||||
|
||||
}, 120000);
|
||||
|
||||
it("Download file should return 200 if file exists", async () => {
|
||||
//given file
|
||||
const filesUpload = await uploadFile(files[0]);
|
||||
expect(filesUpload.resource.id).toBeTruthy();
|
||||
//clean files directory
|
||||
expect(platform.storage.getConnector()).toBeInstanceOf(LocalConnectorService)
|
||||
|
||||
//when try to download the file
|
||||
const fileDownloadResponse = await platform.app.inject({
|
||||
method: "GET",
|
||||
url: `${url}/companies/${platform.workspace.company_id}/files/${filesUpload.resource.id}/download`,
|
||||
});
|
||||
//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 done => {
|
||||
for (const i in files) {
|
||||
const file = files[i];
|
||||
|
||||
const form = formAutoContent({ file: fs.createReadStream(file) });
|
||||
form.headers["authorization"] = `Bearer ${await platform.auth.getJWTToken()}`;
|
||||
|
||||
const filesUploadRaw = await platform.app.inject({
|
||||
method: "POST",
|
||||
url: `${url}/companies/${platform.workspace.company_id}/files?thumbnail_sync=1`,
|
||||
...form,
|
||||
});
|
||||
const filesUpload: ResourceUpdateResponse<File> = deserialize(
|
||||
ResourceUpdateResponse,
|
||||
filesUploadRaw.body,
|
||||
);
|
||||
const filesUpload = await uploadFile(file);
|
||||
|
||||
expect(filesUpload.resource.id).not.toBeFalsy();
|
||||
expect(filesUpload.resource.encryption_key).toBeFalsy(); //This must not be disclosed
|
||||
@@ -59,6 +105,7 @@ describe.skip("The Files feature", () => {
|
||||
|
||||
for (const thumb of filesUpload.resource.thumbnails) {
|
||||
const thumbnails = await platform.app.inject({
|
||||
headers: {"authorization": `Bearer ${await platform.auth.getJWTToken()}`},
|
||||
method: "GET",
|
||||
url: `${url}/companies/${platform.workspace.company_id}/files/${filesUpload.resource.id}/thumbnails/${thumb.index}`,
|
||||
});
|
||||
@@ -67,6 +114,7 @@ describe.skip("The Files feature", () => {
|
||||
}
|
||||
|
||||
done();
|
||||
}, 120000);
|
||||
}, 1200000);
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
@@ -11,6 +11,8 @@ import { MessageQueueServiceAPI } from "../../../src/core/platform/services/mess
|
||||
// @ts-ignore
|
||||
import config from "config";
|
||||
import globalResolver from "../../../src/services/global-resolver";
|
||||
import {FileServiceImpl} from "../../../src/services/files/services";
|
||||
import StorageAPI from "../../../src/core/platform/services/storage/provider";
|
||||
|
||||
type TokenPayload = {
|
||||
sub: string;
|
||||
@@ -32,8 +34,10 @@ export interface TestPlatform {
|
||||
workspace: Workspace;
|
||||
app: FastifyInstance;
|
||||
database: DatabaseServiceAPI;
|
||||
storage: StorageAPI;
|
||||
messageQueue: MessageQueueServiceAPI;
|
||||
authService: AuthServiceAPI;
|
||||
filesService: FileServiceImpl;
|
||||
auth: {
|
||||
getJWTToken(payload?: TokenPayload): Promise<string>;
|
||||
};
|
||||
@@ -68,17 +72,21 @@ export async function init(
|
||||
await platform.start();
|
||||
|
||||
const database = platform.getProvider<DatabaseServiceAPI>("database");
|
||||
await database.getConnector().drop();
|
||||
const messageQueue = platform.getProvider<MessageQueueServiceAPI>("message-queue");
|
||||
const auth = platform.getProvider<AuthServiceAPI>("auth");
|
||||
const storage: StorageAPI = platform.getProvider<StorageAPI>("storage");
|
||||
|
||||
testPlatform = {
|
||||
platform,
|
||||
app,
|
||||
messageQueue,
|
||||
database,
|
||||
storage,
|
||||
workspace: { company_id: "", workspace_id: "" },
|
||||
currentUser: { id: "" },
|
||||
authService: auth,
|
||||
filesService: globalResolver.services.files,
|
||||
auth: {
|
||||
getJWTToken,
|
||||
},
|
||||
@@ -104,7 +112,7 @@ export async function init(
|
||||
payload.sub = testPlatform.currentUser.id;
|
||||
}
|
||||
|
||||
if (testPlatform.currentUser.isWorkspaceModerator) {
|
||||
if (testPlatform .currentUser.isWorkspaceModerator) {
|
||||
payload.org = {};
|
||||
payload.org[testPlatform.workspace.company_id] = {
|
||||
role: "",
|
||||
|
||||
@@ -123,7 +123,7 @@ describe("The /users API", () => {
|
||||
expect(resources.length).toBe(0);
|
||||
|
||||
done();
|
||||
});
|
||||
}, 1200000);
|
||||
});
|
||||
|
||||
async function search(search: string, companyId?: string): Promise<any[]> {
|
||||
|
||||
@@ -156,11 +156,13 @@ export class TestDbService {
|
||||
}
|
||||
|
||||
this.users.push(createdUser);
|
||||
await gr.services.companies.setUserRole(
|
||||
this.company ? this.company.id : workspacesPk[0].company_id,
|
||||
createdUser.id,
|
||||
options.companyRole ? options.companyRole : "member",
|
||||
);
|
||||
if (workspacesPk && workspacesPk.length) {
|
||||
await gr.services.companies.setUserRole(
|
||||
this.company ? this.company.id : workspacesPk[0].company_id,
|
||||
createdUser.id,
|
||||
options.companyRole ? options.companyRole : "member",
|
||||
);
|
||||
}
|
||||
|
||||
if (workspacesPk && workspacesPk.length) {
|
||||
for (const workspacePk of workspacesPk) {
|
||||
|
||||
@@ -264,7 +264,6 @@ describe("The /workspace users API", () => {
|
||||
const anotherUserId = testDbService.workspaces[0].users[0].id;
|
||||
|
||||
let workspaceUsersCount = await testDbService.getWorkspaceUsersCountFromDb(workspaceId);
|
||||
let companyUsersCount = await testDbService.getCompanyUsersCountFromDb(companyId);
|
||||
|
||||
console.log(testDbService.workspaces[2].users);
|
||||
console.log(workspaceUsersCount);
|
||||
@@ -290,10 +289,7 @@ describe("The /workspace users API", () => {
|
||||
checkUserObject(resource);
|
||||
|
||||
workspaceUsersCount = await testDbService.getWorkspaceUsersCountFromDb(workspaceId);
|
||||
companyUsersCount = await testDbService.getCompanyUsersCountFromDb(companyId);
|
||||
expect(workspaceUsersCount).toBe(5);
|
||||
// expect(companyUsersCount).toBe(6);
|
||||
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
@@ -35,7 +35,7 @@ describe("The /workspaces API", () => {
|
||||
|
||||
await platform.database.getConnector().init();
|
||||
testDbService = new TestDbService(platform);
|
||||
await testDbService.createCompany(companyId);
|
||||
await testDbService.createCompany(companyId, "Company name");
|
||||
const ws0pk = { id: uuidv1(), company_id: companyId };
|
||||
const ws1pk = { id: uuidv1(), company_id: companyId };
|
||||
const ws2pk = { id: uuidv1(), company_id: companyId };
|
||||
|
||||
Reference in New Issue
Block a user