fix: updated e2e tests
This commit is contained in:
@@ -0,0 +1 @@
|
||||
test
|
||||
@@ -0,0 +1,183 @@
|
||||
import { afterAll, afterEach, beforeEach, describe, expect, it } from "@jest/globals";
|
||||
import { deserialize } from "class-transformer";
|
||||
import { AccessInformation } from "../../../src/services/documents/entities/drive-file";
|
||||
import { init, TestPlatform } from "../setup";
|
||||
import { TestDbService } from "../utils.prepare.db";
|
||||
import { e2e_createDocument, e2e_getDocument } from "./utils";
|
||||
|
||||
const url = "/internal/services/documents/v1";
|
||||
|
||||
describe("the Drive Twake tabs feature", () => {
|
||||
let platform: TestPlatform;
|
||||
|
||||
class DriveFileMockClass {
|
||||
id: string;
|
||||
name: string;
|
||||
size: number;
|
||||
added: string;
|
||||
parent_id: string;
|
||||
access_info: AccessInformation;
|
||||
}
|
||||
|
||||
class DriveItemDetailsMockClass {
|
||||
path: string[];
|
||||
item: DriveFileMockClass;
|
||||
children: DriveFileMockClass[];
|
||||
versions: Record<string, unknown>[];
|
||||
}
|
||||
|
||||
beforeEach(async () => {
|
||||
platform = await init({
|
||||
services: [
|
||||
"webserver",
|
||||
"database",
|
||||
"applications",
|
||||
"search",
|
||||
"storage",
|
||||
"message-queue",
|
||||
"user",
|
||||
"search",
|
||||
"files",
|
||||
"websocket",
|
||||
"messages",
|
||||
"auth",
|
||||
"realtime",
|
||||
"channels",
|
||||
"counter",
|
||||
"statistics",
|
||||
"platform-services",
|
||||
"documents",
|
||||
],
|
||||
});
|
||||
});
|
||||
|
||||
afterEach(async () => {
|
||||
await platform.tearDown();
|
||||
});
|
||||
|
||||
afterAll(async () => {
|
||||
await platform.app.close();
|
||||
});
|
||||
|
||||
it("did create a tab configuration on Drive side", async done => {
|
||||
await TestDbService.getInstance(platform, true);
|
||||
|
||||
const item = {
|
||||
name: "new tab test file",
|
||||
parent_id: "root",
|
||||
company_id: platform.workspace.company_id,
|
||||
};
|
||||
|
||||
const version = {};
|
||||
|
||||
const response = await e2e_createDocument(platform, item, version);
|
||||
const doc = deserialize<DriveFileMockClass>(DriveFileMockClass, response.body);
|
||||
|
||||
const tab = {
|
||||
company_id: platform.workspace.company_id,
|
||||
tab_id: "1234567890",
|
||||
channel_id: "abcdefghij",
|
||||
item_id: doc.id,
|
||||
level: "write",
|
||||
};
|
||||
|
||||
const token = await platform.auth.getJWTToken();
|
||||
|
||||
const createdTab = await platform.app.inject({
|
||||
method: "POST",
|
||||
url: `${url}/companies/${platform.workspace.company_id}/tabs/${tab.tab_id}`,
|
||||
headers: {
|
||||
authorization: `Bearer ${token}`,
|
||||
},
|
||||
payload: tab,
|
||||
});
|
||||
|
||||
expect(createdTab.statusCode).toBe(200);
|
||||
expect(createdTab.body).toBeDefined();
|
||||
expect(createdTab.json().company_id).toBe(tab.company_id);
|
||||
expect(createdTab.json().tab_id).toBe(tab.tab_id);
|
||||
expect(createdTab.json().item_id).toBe(tab.item_id);
|
||||
|
||||
const getTabResponse = await platform.app.inject({
|
||||
method: "GET",
|
||||
url: `${url}/companies/${platform.workspace.company_id}/tabs/${tab.tab_id}`,
|
||||
headers: {
|
||||
authorization: `Bearer ${token}`,
|
||||
},
|
||||
});
|
||||
|
||||
expect(getTabResponse.statusCode).toBe(200);
|
||||
expect(getTabResponse.body).toBeDefined();
|
||||
expect(getTabResponse.json().company_id).toBe(tab.company_id);
|
||||
expect(getTabResponse.json().tab_id).toBe(tab.tab_id);
|
||||
expect(getTabResponse.json().item_id).toBe(tab.item_id);
|
||||
|
||||
const documentResponse = await e2e_getDocument(platform, doc.id);
|
||||
const documentResult = deserialize<DriveItemDetailsMockClass>(
|
||||
DriveItemDetailsMockClass,
|
||||
documentResponse.body,
|
||||
);
|
||||
|
||||
console.log(documentResult?.item);
|
||||
|
||||
expect(
|
||||
documentResult?.item?.access_info?.entities?.find(
|
||||
a => a?.type === "channel" && a.id === "abcdefghij" && a.level === "write",
|
||||
),
|
||||
).toBeDefined();
|
||||
|
||||
done?.();
|
||||
});
|
||||
|
||||
it("did refuse to create a tab configuration for an item I can't manage", async done => {
|
||||
const dbService = await TestDbService.getInstance(platform, true);
|
||||
const ws0pk = {
|
||||
id: platform.workspace.workspace_id,
|
||||
company_id: platform.workspace.company_id,
|
||||
};
|
||||
const otherUser = await dbService.createUser([ws0pk]);
|
||||
|
||||
const item = {
|
||||
name: "new tab test file",
|
||||
parent_id: "root",
|
||||
company_id: platform.workspace.company_id,
|
||||
access_info: {
|
||||
entities: [
|
||||
{
|
||||
type: "folder",
|
||||
id: "parent",
|
||||
level: "none",
|
||||
} as any,
|
||||
],
|
||||
},
|
||||
};
|
||||
|
||||
const version = {};
|
||||
|
||||
const response = await e2e_createDocument(platform, item, version);
|
||||
const doc = deserialize<DriveFileMockClass>(DriveFileMockClass, response.body);
|
||||
|
||||
const tab = {
|
||||
company_id: platform.workspace.company_id,
|
||||
tab_id: "1234567890",
|
||||
channel_id: "abcdefghij",
|
||||
item_id: doc.id,
|
||||
level: "read",
|
||||
};
|
||||
|
||||
const token = await platform.auth.getJWTToken({ sub: otherUser.id });
|
||||
|
||||
const createdTab = await platform.app.inject({
|
||||
method: "POST",
|
||||
url: `${url}/companies/${platform.workspace.company_id}/tabs/${tab.tab_id}`,
|
||||
headers: {
|
||||
authorization: `Bearer ${token}`,
|
||||
},
|
||||
payload: tab,
|
||||
});
|
||||
|
||||
expect(createdTab.statusCode).toBe(403);
|
||||
|
||||
done?.();
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,238 @@
|
||||
import { describe, beforeEach, afterEach, it, expect, afterAll } from "@jest/globals";
|
||||
import { deserialize } from "class-transformer";
|
||||
import { File } from "../../../src/services/files/entities/file";
|
||||
import { ResourceUpdateResponse } from "../../../src/utils/types";
|
||||
import { init, TestPlatform } from "../setup";
|
||||
import { TestDbService } from "../utils.prepare.db";
|
||||
import {
|
||||
e2e_createDocument,
|
||||
e2e_createDocumentFile,
|
||||
e2e_createVersion,
|
||||
e2e_deleteDocument,
|
||||
e2e_getDocument,
|
||||
e2e_searchDocument,
|
||||
e2e_updateDocument,
|
||||
} from "./utils";
|
||||
|
||||
describe("the Drive feature", () => {
|
||||
let platform: TestPlatform;
|
||||
|
||||
class DriveFileMockClass {
|
||||
id: string;
|
||||
name: string;
|
||||
size: number;
|
||||
added: string;
|
||||
parent_id: string;
|
||||
}
|
||||
|
||||
class DriveItemDetailsMockClass {
|
||||
path: string[];
|
||||
item: DriveFileMockClass;
|
||||
children: DriveFileMockClass[];
|
||||
versions: Record<string, unknown>[];
|
||||
}
|
||||
|
||||
class SearchResultMockClass {
|
||||
entities: DriveFileMockClass[];
|
||||
}
|
||||
|
||||
beforeEach(async () => {
|
||||
platform = await init({
|
||||
services: [
|
||||
"webserver",
|
||||
"database",
|
||||
"applications",
|
||||
"search",
|
||||
"storage",
|
||||
"message-queue",
|
||||
"user",
|
||||
"search",
|
||||
"files",
|
||||
"websocket",
|
||||
"messages",
|
||||
"auth",
|
||||
"realtime",
|
||||
"channels",
|
||||
"counter",
|
||||
"statistics",
|
||||
"platform-services",
|
||||
"documents",
|
||||
],
|
||||
});
|
||||
});
|
||||
|
||||
afterEach(async () => {
|
||||
await platform.tearDown();
|
||||
});
|
||||
|
||||
afterAll(async () => {
|
||||
await platform.app.close();
|
||||
});
|
||||
|
||||
const createItem = async (): Promise<DriveFileMockClass> => {
|
||||
await TestDbService.getInstance(platform, true);
|
||||
|
||||
const item = {
|
||||
name: "new test file",
|
||||
parent_id: "root",
|
||||
company_id: platform.workspace.company_id,
|
||||
};
|
||||
|
||||
const version = {};
|
||||
|
||||
const response = await e2e_createDocument(platform, item, version);
|
||||
return deserialize<DriveFileMockClass>(DriveFileMockClass, response.body);
|
||||
};
|
||||
|
||||
it("did create the drive item", async done => {
|
||||
const result = await createItem();
|
||||
|
||||
expect(result).toBeDefined();
|
||||
expect(result.name).toEqual("new test file");
|
||||
expect(result.added).toBeDefined();
|
||||
|
||||
done?.();
|
||||
});
|
||||
|
||||
it("did fetch the drive item", async done => {
|
||||
await TestDbService.getInstance(platform, true);
|
||||
|
||||
const response = await e2e_getDocument(platform, "");
|
||||
const result = deserialize<DriveItemDetailsMockClass>(DriveItemDetailsMockClass, response.body);
|
||||
|
||||
expect(result.item.name).toEqual("root");
|
||||
|
||||
done?.();
|
||||
});
|
||||
|
||||
it("did fetch the trash", async done => {
|
||||
await TestDbService.getInstance(platform, true);
|
||||
|
||||
const response = await e2e_getDocument(platform, "trash");
|
||||
const result = deserialize<DriveItemDetailsMockClass>(DriveItemDetailsMockClass, response.body);
|
||||
|
||||
expect(result.item.name).toEqual("trash");
|
||||
|
||||
done?.();
|
||||
});
|
||||
|
||||
it("did delete an item", async done => {
|
||||
const createItemResult = await createItem();
|
||||
|
||||
expect(createItemResult.id).toBeDefined();
|
||||
|
||||
const deleteResponse = await e2e_deleteDocument(platform, createItemResult.id);
|
||||
expect(deleteResponse.statusCode).toEqual(200);
|
||||
|
||||
done?.();
|
||||
});
|
||||
|
||||
it("did update an item", async done => {
|
||||
const createItemResult = await createItem();
|
||||
|
||||
expect(createItemResult.id).toBeDefined();
|
||||
|
||||
const update = {
|
||||
name: "somethingelse",
|
||||
};
|
||||
|
||||
const updateItemResponse = await e2e_updateDocument(platform, createItemResult.id, update);
|
||||
const updateItemResult = deserialize<DriveFileMockClass>(
|
||||
DriveFileMockClass,
|
||||
updateItemResponse.body,
|
||||
);
|
||||
|
||||
expect(createItemResult.id).toEqual(updateItemResult.id);
|
||||
expect(updateItemResult.name).toEqual("somethingelse");
|
||||
|
||||
done?.();
|
||||
});
|
||||
|
||||
it("did move an item to trash", async done => {
|
||||
const createItemResult = await createItem();
|
||||
|
||||
expect(createItemResult.id).toBeDefined();
|
||||
|
||||
const moveToTrashResponse = await e2e_deleteDocument(platform, createItemResult.id);
|
||||
expect(moveToTrashResponse.statusCode).toEqual(200);
|
||||
|
||||
const listTrashResponse = await e2e_getDocument(platform, "trash");
|
||||
const listTrashResult = deserialize<DriveItemDetailsMockClass>(
|
||||
DriveItemDetailsMockClass,
|
||||
listTrashResponse.body,
|
||||
);
|
||||
|
||||
expect(listTrashResult.item.name).toEqual("trash");
|
||||
expect(listTrashResult.children.some(({ id }) => id === createItemResult.id)).toBeTruthy();
|
||||
|
||||
done?.();
|
||||
});
|
||||
|
||||
// TODO: wait for elastic search index
|
||||
it("did search for an item", async done => {
|
||||
const createItemResult = await createItem();
|
||||
|
||||
expect(createItemResult.id).toBeDefined();
|
||||
|
||||
await e2e_getDocument(platform, "root");
|
||||
await e2e_getDocument(platform, createItemResult.id);
|
||||
|
||||
await new Promise(resolve => setTimeout(resolve, 3000));
|
||||
|
||||
const searchPayload = {
|
||||
search: "test",
|
||||
};
|
||||
|
||||
const searchResponse = await e2e_searchDocument(platform, searchPayload);
|
||||
const searchResult = deserialize<SearchResultMockClass>(
|
||||
SearchResultMockClass,
|
||||
searchResponse.body,
|
||||
);
|
||||
|
||||
expect(searchResult.entities.length).toBeGreaterThanOrEqual(1);
|
||||
|
||||
done?.();
|
||||
});
|
||||
|
||||
it("did search for an item that doesn't exist", async done => {
|
||||
await createItem();
|
||||
|
||||
const unexistingSeachPayload = {
|
||||
search: "somethingthatdoesn'tandshouldn'texist",
|
||||
};
|
||||
const failSearchResponse = await e2e_searchDocument(platform, unexistingSeachPayload);
|
||||
const failSearchResult = deserialize<SearchResultMockClass>(
|
||||
SearchResultMockClass,
|
||||
failSearchResponse.body,
|
||||
);
|
||||
|
||||
expect(failSearchResult.entities).toHaveLength(0);
|
||||
|
||||
done?.();
|
||||
});
|
||||
|
||||
it("did create a version for a drive item", async done => {
|
||||
const item = await createItem();
|
||||
const fileUploadResponse = await e2e_createDocumentFile(platform);
|
||||
const fileUploadResult = deserialize<ResourceUpdateResponse<File>>(
|
||||
ResourceUpdateResponse,
|
||||
fileUploadResponse.body,
|
||||
);
|
||||
|
||||
const file_metadata = { external_id: fileUploadResult.resource.id };
|
||||
|
||||
await e2e_createVersion(platform, item.id, { filename: "file2", file_metadata });
|
||||
await e2e_createVersion(platform, item.id, { filename: "file3", file_metadata });
|
||||
await e2e_createVersion(platform, item.id, { filename: "file4", file_metadata });
|
||||
|
||||
const fetchItemResponse = await e2e_getDocument(platform, item.id);
|
||||
const fetchItemResult = deserialize<DriveItemDetailsMockClass>(
|
||||
DriveItemDetailsMockClass,
|
||||
fetchItemResponse.body,
|
||||
);
|
||||
|
||||
expect(fetchItemResult.versions).toHaveLength(4);
|
||||
|
||||
done?.();
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,116 @@
|
||||
import { DriveFile } from "../../../src/services/documents/entities/drive-file";
|
||||
import { FileVersion } from "../../../src/services/documents/entities/file-version";
|
||||
import { TestPlatform } from "../setup";
|
||||
import formAutoContent from "form-auto-content";
|
||||
import fs from "fs";
|
||||
|
||||
const url = "/internal/services/documents/v1";
|
||||
|
||||
export const e2e_createDocument = async (
|
||||
platform: TestPlatform,
|
||||
item: Partial<DriveFile>,
|
||||
version: Partial<FileVersion>,
|
||||
) => {
|
||||
const token = await platform.auth.getJWTToken();
|
||||
|
||||
return await platform.app.inject({
|
||||
method: "POST",
|
||||
url: `${url}/companies/${platform.workspace.company_id}/item`,
|
||||
headers: {
|
||||
authorization: `Bearer ${token}`,
|
||||
},
|
||||
payload: {
|
||||
item,
|
||||
version,
|
||||
},
|
||||
});
|
||||
};
|
||||
|
||||
export const e2e_getDocument = async (platform: TestPlatform, id: string | "root" | "trash") => {
|
||||
const token = await platform.auth.getJWTToken();
|
||||
|
||||
return await platform.app.inject({
|
||||
method: "GET",
|
||||
url: `${url}/companies/${platform.workspace.company_id}/item/${id}`,
|
||||
headers: {
|
||||
authorization: `Bearer ${token}`,
|
||||
},
|
||||
});
|
||||
};
|
||||
|
||||
export const e2e_deleteDocument = async (platform: TestPlatform, id: string | "root" | "trash") => {
|
||||
const token = await platform.auth.getJWTToken();
|
||||
|
||||
return await platform.app.inject({
|
||||
method: "DELETE",
|
||||
url: `${url}/companies/${platform.workspace.company_id}/item/${id}`,
|
||||
headers: {
|
||||
authorization: `Bearer ${token}`,
|
||||
},
|
||||
});
|
||||
};
|
||||
|
||||
export const e2e_updateDocument = async (
|
||||
platform: TestPlatform,
|
||||
id: string | "root" | "trash",
|
||||
item: Partial<DriveFile>,
|
||||
) => {
|
||||
const token = await platform.auth.getJWTToken();
|
||||
|
||||
return await platform.app.inject({
|
||||
method: "POST",
|
||||
url: `${url}/companies/${platform.workspace.company_id}/item/${id}`,
|
||||
headers: {
|
||||
authorization: `Bearer ${token}`,
|
||||
},
|
||||
payload: item,
|
||||
});
|
||||
};
|
||||
|
||||
export const e2e_searchDocument = async (
|
||||
platform: TestPlatform,
|
||||
payload: Record<string, string>,
|
||||
) => {
|
||||
const token = await platform.auth.getJWTToken();
|
||||
|
||||
const response = await platform.app.inject({
|
||||
method: "POST",
|
||||
url: `${url}/companies/${platform.workspace.company_id}/search`,
|
||||
headers: {
|
||||
authorization: `Bearer ${token}`,
|
||||
},
|
||||
payload,
|
||||
});
|
||||
|
||||
return response;
|
||||
};
|
||||
|
||||
export const e2e_createVersion = async (
|
||||
platform: TestPlatform,
|
||||
id: string,
|
||||
payload: Partial<FileVersion>,
|
||||
) => {
|
||||
const token = await platform.auth.getJWTToken();
|
||||
|
||||
return await platform.app.inject({
|
||||
method: "POST",
|
||||
url: `${url}/companies/${platform.workspace.company_id}/item/${id}/version`,
|
||||
headers: {
|
||||
authorization: `Bearer ${token}`,
|
||||
},
|
||||
payload,
|
||||
});
|
||||
};
|
||||
|
||||
export const e2e_createDocumentFile = async (platform: TestPlatform) => {
|
||||
const filePath = `${__dirname}/assets/test.txt`;
|
||||
const token = await platform.auth.getJWTToken();
|
||||
const form = formAutoContent({ file: fs.createReadStream(filePath) });
|
||||
form.headers["authorization"] = `Bearer ${token}`;
|
||||
|
||||
return await platform.app.inject({
|
||||
method: "POST",
|
||||
url: `/internal/services/files/v1/companies/${platform.workspace.company_id}/files`,
|
||||
...form,
|
||||
});
|
||||
};
|
||||
@@ -0,0 +1,469 @@
|
||||
import { afterAll, beforeAll, describe, expect, it } from "@jest/globals";
|
||||
import { init, TestPlatform } from "../setup";
|
||||
import { TestDbService, uuid } from "../utils.prepare.db";
|
||||
import { v1 as uuidv1 } from "uuid";
|
||||
|
||||
describe("The /workspace users API", () => {
|
||||
const url = "/internal/services/workspaces/v1";
|
||||
let platform: TestPlatform;
|
||||
|
||||
let testDbService: TestDbService;
|
||||
|
||||
const nonExistentId = uuidv1();
|
||||
let companyId = "";
|
||||
|
||||
const checkUserObject = (resource: any) => {
|
||||
expect(resource).toMatchObject({
|
||||
id: expect.any(String),
|
||||
company_id: expect.any(String),
|
||||
workspace_id: expect.any(String),
|
||||
user_id: expect.any(String),
|
||||
created_at: expect.any(Number),
|
||||
role: expect.stringMatching(/moderator|member/),
|
||||
user: {
|
||||
id: expect.any(String),
|
||||
provider: expect.any(String),
|
||||
provider_id: expect.any(String),
|
||||
email: expect.any(String),
|
||||
is_verified: expect.any(Boolean),
|
||||
picture: expect.any(String),
|
||||
first_name: expect.any(String),
|
||||
last_name: expect.any(String),
|
||||
created_at: expect.any(Number),
|
||||
deleted: expect.any(Boolean),
|
||||
status: expect.any(String),
|
||||
last_activity: expect.any(Number),
|
||||
},
|
||||
});
|
||||
};
|
||||
|
||||
beforeAll(async ends => {
|
||||
platform = await init({
|
||||
services: [
|
||||
"database",
|
||||
"message-queue",
|
||||
"search",
|
||||
"webserver",
|
||||
"user",
|
||||
"workspaces",
|
||||
"applications",
|
||||
"auth",
|
||||
"console",
|
||||
"counter",
|
||||
"storage",
|
||||
"statistics",
|
||||
"platform-services",
|
||||
],
|
||||
});
|
||||
|
||||
companyId = platform.workspace.company_id;
|
||||
|
||||
await platform.database.getConnector().init();
|
||||
testDbService = new TestDbService(platform);
|
||||
await testDbService.createCompany(companyId);
|
||||
const ws0pk = { id: uuidv1(), company_id: companyId };
|
||||
const ws1pk = { id: uuidv1(), company_id: companyId };
|
||||
const ws2pk = { id: uuidv1(), company_id: companyId };
|
||||
const ws3pk = { id: uuidv1(), company_id: companyId };
|
||||
await testDbService.createWorkspace(ws0pk);
|
||||
await testDbService.createWorkspace(ws1pk);
|
||||
await testDbService.createWorkspace(ws2pk);
|
||||
await testDbService.createWorkspace(ws3pk);
|
||||
await testDbService.createUser([ws0pk, ws1pk]);
|
||||
await testDbService.createUser([ws2pk], { companyRole: "admin" });
|
||||
await testDbService.createUser([ws2pk], { workspaceRole: "moderator" });
|
||||
await testDbService.createUser([ws2pk], { workspaceRole: "member" });
|
||||
await testDbService.createUser([ws2pk], { workspaceRole: "member" });
|
||||
await testDbService.createUser([], { companyRole: "member" });
|
||||
await testDbService.createUser([ws3pk], { companyRole: "guest", workspaceRole: "member" });
|
||||
ends();
|
||||
});
|
||||
|
||||
afterAll(async ends => {
|
||||
await platform.tearDown();
|
||||
ends();
|
||||
});
|
||||
|
||||
describe("The GET /workspaces/users route", () => {
|
||||
it("should 401 when not authenticated", async done => {
|
||||
const companyId = testDbService.company.id;
|
||||
|
||||
const response = await platform.app.inject({
|
||||
method: "GET",
|
||||
url: `${url}/companies/${companyId}/workspaces/${nonExistentId}/users`,
|
||||
});
|
||||
expect(response.statusCode).toBe(401);
|
||||
done();
|
||||
});
|
||||
|
||||
it("should 404 when workspace not found", async done => {
|
||||
const userId = testDbService.users[0].id;
|
||||
|
||||
const jwtToken = await platform.auth.getJWTToken({ sub: userId });
|
||||
|
||||
const response = await platform.app.inject({
|
||||
method: "GET",
|
||||
url: `${url}/companies/${companyId}/workspaces/${nonExistentId}/users`,
|
||||
headers: { authorization: `Bearer ${jwtToken}` },
|
||||
});
|
||||
expect(response.statusCode).toBe(404);
|
||||
done();
|
||||
});
|
||||
|
||||
it("should 200 when ok", async done => {
|
||||
const companyId = testDbService.company.id;
|
||||
const workspaceId = testDbService.workspaces[2].workspace.id;
|
||||
const userId = testDbService.workspaces[2].users[0].id;
|
||||
|
||||
const jwtToken = await platform.auth.getJWTToken({ sub: userId });
|
||||
const response = await platform.app.inject({
|
||||
method: "GET",
|
||||
url: `${url}/companies/${companyId}/workspaces/${workspaceId}/users`,
|
||||
headers: { authorization: `Bearer ${jwtToken}` },
|
||||
});
|
||||
expect(response.statusCode).toBe(200);
|
||||
|
||||
const resources = response.json()["resources"];
|
||||
|
||||
expect(resources.length).toBeGreaterThan(0);
|
||||
|
||||
for (const resource of resources) {
|
||||
checkUserObject(resource);
|
||||
}
|
||||
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
describe("The GET /workspaces/users/:user_id route", () => {
|
||||
it("should 401 when not authenticated", async done => {
|
||||
const companyId = testDbService.company.id;
|
||||
const userId = testDbService.users[0].id;
|
||||
|
||||
const response = await platform.app.inject({
|
||||
method: "GET",
|
||||
url: `${url}/companies/${companyId}/workspaces/${nonExistentId}/users/${userId}`,
|
||||
});
|
||||
expect(response.statusCode).toBe(401);
|
||||
done();
|
||||
});
|
||||
|
||||
it("should 404 when workspace not found", async done => {
|
||||
const userId = testDbService.users[0].id;
|
||||
|
||||
const jwtToken = await platform.auth.getJWTToken({ sub: userId });
|
||||
|
||||
const response = await platform.app.inject({
|
||||
method: "GET",
|
||||
url: `${url}/companies/${companyId}/workspaces/${nonExistentId}/users/${userId}`,
|
||||
headers: { authorization: `Bearer ${jwtToken}` },
|
||||
});
|
||||
expect(response.statusCode).toBe(404);
|
||||
done();
|
||||
});
|
||||
|
||||
it("should 200 when ok", async done => {
|
||||
const companyId = testDbService.company.id;
|
||||
const workspaceId = testDbService.workspaces[0].workspace.id;
|
||||
const userId = testDbService.workspaces[0].users[0].id;
|
||||
|
||||
const jwtToken = await platform.auth.getJWTToken({ sub: userId });
|
||||
const response = await platform.app.inject({
|
||||
method: "GET",
|
||||
url: `${url}/companies/${companyId}/workspaces/${workspaceId}/users/${userId}`,
|
||||
headers: { authorization: `Bearer ${jwtToken}` },
|
||||
});
|
||||
expect(response.statusCode).toBe(200);
|
||||
|
||||
const resource = response.json()["resource"];
|
||||
checkUserObject(resource);
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
describe("The POST /workspaces/users route (add)", () => {
|
||||
it("should 401 when not authenticated", async done => {
|
||||
const companyId = testDbService.company.id;
|
||||
|
||||
const response = await platform.app.inject({
|
||||
method: "POST",
|
||||
url: `${url}/companies/${companyId}/workspaces/${nonExistentId}/users`,
|
||||
});
|
||||
expect(response.statusCode).toBe(401);
|
||||
done();
|
||||
});
|
||||
|
||||
it("should 403 user is not workspace moderator", async done => {
|
||||
const userId = testDbService.users[0].id;
|
||||
const anotherUserId = testDbService.users[1].id;
|
||||
const workspaceId = testDbService.workspaces[0].workspace.id;
|
||||
|
||||
const jwtToken = await platform.auth.getJWTToken({ sub: userId });
|
||||
|
||||
const response = await platform.app.inject({
|
||||
method: "POST",
|
||||
url: `${url}/companies/${companyId}/workspaces/${workspaceId}/users`,
|
||||
headers: { authorization: `Bearer ${jwtToken}` },
|
||||
payload: {
|
||||
resource: {
|
||||
user_id: anotherUserId,
|
||||
role: "moderator",
|
||||
},
|
||||
},
|
||||
});
|
||||
expect(response.statusCode).toBe(403);
|
||||
done();
|
||||
});
|
||||
|
||||
it("should 400 when requested user not in company", async done => {
|
||||
const companyId = testDbService.company.id;
|
||||
const workspaceId = testDbService.workspaces[2].workspace.id;
|
||||
const userId = testDbService.workspaces[2].users[1].id;
|
||||
|
||||
const jwtToken = await platform.auth.getJWTToken({ sub: userId });
|
||||
const response = await platform.app.inject({
|
||||
method: "POST",
|
||||
url: `${url}/companies/${companyId}/workspaces/${workspaceId}/users`,
|
||||
headers: { authorization: `Bearer ${jwtToken}` },
|
||||
payload: {
|
||||
resource: {
|
||||
user_id: nonExistentId,
|
||||
role: "moderator",
|
||||
},
|
||||
},
|
||||
});
|
||||
expect(response.statusCode).toBe(400);
|
||||
done();
|
||||
});
|
||||
|
||||
it("should 201 when requested already in workspace (ignore)", async done => {
|
||||
const companyId = testDbService.company.id;
|
||||
const workspaceId = testDbService.workspaces[2].workspace.id;
|
||||
const userId = testDbService.workspaces[2].users[1].id;
|
||||
|
||||
const jwtToken = await platform.auth.getJWTToken({ sub: userId });
|
||||
const response = await platform.app.inject({
|
||||
method: "POST",
|
||||
url: `${url}/companies/${companyId}/workspaces/${workspaceId}/users`,
|
||||
headers: { authorization: `Bearer ${jwtToken}` },
|
||||
payload: {
|
||||
resource: {
|
||||
user_id: userId,
|
||||
role: "moderator",
|
||||
},
|
||||
},
|
||||
});
|
||||
expect(response.statusCode).toBe(201);
|
||||
done();
|
||||
});
|
||||
|
||||
it("should 200 when ok", async done => {
|
||||
const companyId = testDbService.company.id;
|
||||
const workspaceId = testDbService.workspaces[2].workspace.id;
|
||||
const userId = testDbService.workspaces[2].users[1].id;
|
||||
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);
|
||||
|
||||
expect(workspaceUsersCount).toBe(4);
|
||||
// expect(companyUsersCount).toBe(6);
|
||||
|
||||
const jwtToken = await platform.auth.getJWTToken({ sub: userId });
|
||||
const response = await platform.app.inject({
|
||||
method: "POST",
|
||||
url: `${url}/companies/${companyId}/workspaces/${workspaceId}/users`,
|
||||
headers: { authorization: `Bearer ${jwtToken}` },
|
||||
payload: {
|
||||
resource: {
|
||||
user_id: anotherUserId,
|
||||
role: "moderator",
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
expect(response.statusCode).toBe(201);
|
||||
const resource = response.json()["resource"];
|
||||
checkUserObject(resource);
|
||||
|
||||
workspaceUsersCount = await testDbService.getWorkspaceUsersCountFromDb(workspaceId);
|
||||
companyUsersCount = await testDbService.getCompanyUsersCountFromDb(companyId);
|
||||
expect(workspaceUsersCount).toBe(5);
|
||||
// expect(companyUsersCount).toBe(6);
|
||||
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
describe("The POST /workspaces/users/:user_id route (update)", () => {
|
||||
it("should 401 when not authenticated", async done => {
|
||||
const companyId = testDbService.company.id;
|
||||
const userId = testDbService.users[0].id;
|
||||
|
||||
const response = await platform.app.inject({
|
||||
method: "POST",
|
||||
url: `${url}/companies/${companyId}/workspaces/${nonExistentId}/users/${userId}`,
|
||||
});
|
||||
expect(response.statusCode).toBe(401);
|
||||
done();
|
||||
});
|
||||
|
||||
it("should 403 user is not workspace moderator", async done => {
|
||||
const userId = testDbService.users[0].id;
|
||||
const anotherUserId = testDbService.users[1].id;
|
||||
const workspaceId = testDbService.workspaces[0].workspace.id;
|
||||
|
||||
const jwtToken = await platform.auth.getJWTToken({ sub: userId });
|
||||
|
||||
const response = await platform.app.inject({
|
||||
method: "POST",
|
||||
url: `${url}/companies/${companyId}/workspaces/${workspaceId}/users/${userId}`,
|
||||
headers: { authorization: `Bearer ${jwtToken}` },
|
||||
payload: {
|
||||
resource: {
|
||||
user_id: anotherUserId,
|
||||
role: "moderator",
|
||||
},
|
||||
},
|
||||
});
|
||||
expect(response.statusCode).toBe(403);
|
||||
done();
|
||||
});
|
||||
|
||||
it("should 404 when user not found in workspace", async done => {
|
||||
const workspaceId = testDbService.workspaces[2].workspace.id;
|
||||
const userId = testDbService.workspaces[2].users[1].id;
|
||||
const anotherWorkspaceUserId = testDbService.workspaces[3].users[0].id;
|
||||
const jwtToken = await platform.auth.getJWTToken({ sub: userId });
|
||||
|
||||
const response = await platform.app.inject({
|
||||
method: "POST",
|
||||
url: `${url}/companies/${companyId}/workspaces/${workspaceId}/users/${anotherWorkspaceUserId}`,
|
||||
headers: { authorization: `Bearer ${jwtToken}` },
|
||||
payload: {
|
||||
resource: {
|
||||
role: "moderator",
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
expect(response.statusCode).toBe(404);
|
||||
done();
|
||||
});
|
||||
|
||||
it("should 200 when ok", async done => {
|
||||
const companyId = testDbService.company.id;
|
||||
const workspaceId = testDbService.workspaces[2].workspace.id;
|
||||
const userId = testDbService.workspaces[2].users[1].id;
|
||||
const anotherUserId = testDbService.workspaces[2].users[2].id;
|
||||
|
||||
const jwtToken = await platform.auth.getJWTToken({ sub: userId });
|
||||
const response = await platform.app.inject({
|
||||
method: "POST",
|
||||
url: `${url}/companies/${companyId}/workspaces/${workspaceId}/users/${anotherUserId}`,
|
||||
headers: { authorization: `Bearer ${jwtToken}` },
|
||||
payload: {
|
||||
resource: {
|
||||
role: "moderator",
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
expect(response.statusCode).toBe(201);
|
||||
const resource = response.json()["resource"];
|
||||
checkUserObject(resource);
|
||||
|
||||
expect(resource["role"]).toBe("moderator");
|
||||
|
||||
const usersCount = await testDbService.getWorkspaceUsersCountFromDb(workspaceId);
|
||||
expect(usersCount).toBe(5);
|
||||
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
describe("The DELETE /workspaces/users/:user_id route", () => {
|
||||
it("should 401 when not authenticated", async done => {
|
||||
const companyId = testDbService.company.id;
|
||||
const anotherUserId = testDbService.users[1].id;
|
||||
|
||||
const response = await platform.app.inject({
|
||||
method: "DELETE",
|
||||
url: `${url}/companies/${companyId}/workspaces/${nonExistentId}/users/${anotherUserId}`,
|
||||
});
|
||||
expect(response.statusCode).toBe(401);
|
||||
done();
|
||||
});
|
||||
|
||||
it("should 403 user is not workspace moderator", async done => {
|
||||
const companyId = testDbService.company.id;
|
||||
const workspaceId = testDbService.workspaces[2].workspace.id;
|
||||
const userId = testDbService.workspaces[2].users[3].id;
|
||||
const anotherUserId = testDbService.workspaces[2].users[1].id;
|
||||
|
||||
const jwtToken = await platform.auth.getJWTToken({ sub: userId });
|
||||
|
||||
const response = await platform.app.inject({
|
||||
method: "DELETE",
|
||||
url: `${url}/companies/${companyId}/workspaces/${workspaceId}/users/${anotherUserId}`,
|
||||
headers: { authorization: `Bearer ${jwtToken}` },
|
||||
});
|
||||
|
||||
console.log(response.body);
|
||||
|
||||
expect(response.statusCode).toBe(403);
|
||||
done();
|
||||
});
|
||||
|
||||
it("should 404 when user not found in workspace", async done => {
|
||||
const companyId = testDbService.company.id;
|
||||
const workspaceId = testDbService.workspaces[2].workspace.id;
|
||||
const userId = testDbService.workspaces[2].users[1].id;
|
||||
|
||||
const jwtToken = await platform.auth.getJWTToken({ sub: userId });
|
||||
const response = await platform.app.inject({
|
||||
method: "DELETE",
|
||||
url: `${url}/companies/${companyId}/workspaces/${workspaceId}/users/${nonExistentId}`,
|
||||
headers: { authorization: `Bearer ${jwtToken}` },
|
||||
});
|
||||
|
||||
expect(response.statusCode).toBe(404);
|
||||
|
||||
done();
|
||||
});
|
||||
|
||||
it("should 200 when ok", async done => {
|
||||
const companyId = testDbService.company.id;
|
||||
const workspaceId = testDbService.workspaces[2].workspace.id;
|
||||
const userId = testDbService.workspaces[2].users[1].id;
|
||||
const anotherUserId = testDbService.workspaces[2].users[2].id;
|
||||
|
||||
const jwtToken = await platform.auth.getJWTToken({ sub: userId });
|
||||
let response = await platform.app.inject({
|
||||
method: "DELETE",
|
||||
url: `${url}/companies/${companyId}/workspaces/${workspaceId}/users/${anotherUserId}`,
|
||||
headers: { authorization: `Bearer ${jwtToken}` },
|
||||
});
|
||||
|
||||
expect(response.statusCode).toBe(204);
|
||||
|
||||
response = await platform.app.inject({
|
||||
method: "GET",
|
||||
url: `${url}/companies/${companyId}/workspaces/${workspaceId}/users`,
|
||||
headers: { authorization: `Bearer ${jwtToken}` },
|
||||
});
|
||||
|
||||
expect(response.statusCode).toBe(200);
|
||||
const resources = response.json()["resources"];
|
||||
expect(resources.find((a: { user_id: uuid }) => a.user_id === anotherUserId)).toBeUndefined();
|
||||
|
||||
const usersCount = await testDbService.getWorkspaceUsersCountFromDb(workspaceId);
|
||||
expect(usersCount).toBe(4);
|
||||
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,513 @@
|
||||
import { afterAll, beforeAll, describe, expect, it } from "@jest/globals";
|
||||
import { init, TestPlatform } from "../setup";
|
||||
import { TestDbService } from "../utils.prepare.db";
|
||||
import { v1 as uuidv1 } from "uuid";
|
||||
|
||||
describe("The /workspaces API", () => {
|
||||
const url = "/internal/services/workspaces/v1";
|
||||
let platform: TestPlatform;
|
||||
|
||||
let testDbService: TestDbService;
|
||||
|
||||
const nonExistentId = uuidv1();
|
||||
let companyId = "";
|
||||
|
||||
beforeAll(async ends => {
|
||||
platform = await init({
|
||||
services: [
|
||||
"database",
|
||||
"message-queue",
|
||||
"webserver",
|
||||
"user",
|
||||
"search",
|
||||
"workspaces",
|
||||
"auth",
|
||||
"console",
|
||||
"counter",
|
||||
"storage",
|
||||
"applications",
|
||||
"statistics",
|
||||
"platform-services",
|
||||
],
|
||||
});
|
||||
|
||||
companyId = platform.workspace.company_id;
|
||||
|
||||
await platform.database.getConnector().init();
|
||||
testDbService = new TestDbService(platform);
|
||||
await testDbService.createCompany(companyId);
|
||||
const ws0pk = { id: uuidv1(), company_id: companyId };
|
||||
const ws1pk = { id: uuidv1(), company_id: companyId };
|
||||
const ws2pk = { id: uuidv1(), company_id: companyId };
|
||||
await testDbService.createWorkspace(ws0pk);
|
||||
await testDbService.createWorkspace(ws1pk);
|
||||
await testDbService.createWorkspace(ws2pk);
|
||||
await testDbService.createUser([ws0pk, ws1pk]);
|
||||
await testDbService.createUser([ws2pk], { companyRole: "admin" });
|
||||
await testDbService.createUser([ws2pk], { companyRole: undefined, workspaceRole: "moderator" });
|
||||
await testDbService.createUser([], { companyRole: "guest" });
|
||||
ends();
|
||||
});
|
||||
|
||||
afterAll(async ends => {
|
||||
await platform.tearDown();
|
||||
ends();
|
||||
});
|
||||
|
||||
describe("The GET /workspaces/ route", () => {
|
||||
it("should 401 when not authenticated", async done => {
|
||||
const response = await platform.app.inject({
|
||||
method: "GET",
|
||||
url: `${url}/companies/${nonExistentId}/workspaces`,
|
||||
});
|
||||
expect(response.statusCode).toBe(401);
|
||||
done();
|
||||
});
|
||||
|
||||
it("should 404 when company not found", async done => {
|
||||
const userId = testDbService.workspaces[0].users[0].id;
|
||||
|
||||
const jwtToken = await platform.auth.getJWTToken({ sub: userId });
|
||||
|
||||
const response = await platform.app.inject({
|
||||
method: "GET",
|
||||
url: `${url}/companies/${nonExistentId}/workspaces`,
|
||||
headers: { authorization: `Bearer ${jwtToken}` },
|
||||
});
|
||||
expect(response.statusCode).toBe(404);
|
||||
done();
|
||||
});
|
||||
|
||||
it("should 200 when company belongs to user", async done => {
|
||||
const userId = testDbService.workspaces[0].users[0].id;
|
||||
|
||||
const jwtToken = await platform.auth.getJWTToken({ sub: userId });
|
||||
const companyId = testDbService.company.id;
|
||||
const response = await platform.app.inject({
|
||||
method: "GET",
|
||||
url: `${url}/companies/${companyId}/workspaces`,
|
||||
headers: { authorization: `Bearer ${jwtToken}` },
|
||||
});
|
||||
expect(response.statusCode).toBe(200);
|
||||
|
||||
const resources = response.json()["resources"];
|
||||
|
||||
expect(resources.length).toBe(2);
|
||||
|
||||
for (const resource of resources) {
|
||||
expect(resource).toMatchObject({
|
||||
id: expect.any(String),
|
||||
company_id: expect.any(String),
|
||||
name: expect.any(String),
|
||||
logo: expect.any(String),
|
||||
default: expect.any(Boolean),
|
||||
archived: expect.any(Boolean),
|
||||
role: expect.stringMatching(/moderator|member/),
|
||||
});
|
||||
|
||||
if (resource.stats) {
|
||||
expect(resource.stats).toMatchObject({
|
||||
created_at: expect.any(Number),
|
||||
total_members: 1,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
describe("The GET /workspaces/:workspace_id route", () => {
|
||||
it("should 401 when not authenticated", async done => {
|
||||
const workspaceId = testDbService.workspaces[0].workspace.id;
|
||||
|
||||
const response = await platform.app.inject({
|
||||
method: "GET",
|
||||
url: `${url}/companies/${nonExistentId}/workspaces/${workspaceId}`,
|
||||
});
|
||||
expect(response.statusCode).toBe(401);
|
||||
done();
|
||||
});
|
||||
|
||||
it("should 404 when workspace not found", async done => {
|
||||
const userId = testDbService.workspaces[0].users[0].id;
|
||||
const jwtToken = await platform.auth.getJWTToken({ sub: userId });
|
||||
|
||||
const response = await platform.app.inject({
|
||||
method: "GET",
|
||||
url: `${url}/companies/${companyId}/workspaces/${uuidv1()}`,
|
||||
headers: { authorization: `Bearer ${jwtToken}` },
|
||||
});
|
||||
expect(response.statusCode).toBe(404);
|
||||
done();
|
||||
});
|
||||
|
||||
it("should 403 when user not belong to workspace and not company_admin", async done => {
|
||||
const workspaceId = testDbService.workspaces[2].workspace.id;
|
||||
const userIdFromAnotherWorkspace = testDbService.workspaces[0].users[0].id;
|
||||
|
||||
const jwtToken = await platform.auth.getJWTToken({ sub: userIdFromAnotherWorkspace });
|
||||
|
||||
const response = await platform.app.inject({
|
||||
method: "GET",
|
||||
url: `${url}/companies/${companyId}/workspaces/${workspaceId}`,
|
||||
headers: { authorization: `Bearer ${jwtToken}` },
|
||||
});
|
||||
expect(response.statusCode).toBe(403);
|
||||
|
||||
expect(response.json()).toEqual({
|
||||
error: "Forbidden",
|
||||
message: `You are not belong to workspace ${workspaceId}`,
|
||||
statusCode: 403,
|
||||
});
|
||||
|
||||
done();
|
||||
});
|
||||
|
||||
it("should 200 when user is company_admin", async done => {
|
||||
const workspaceId = testDbService.workspaces[0].workspace.id;
|
||||
const userIdFromAnotherWorkspace = testDbService.workspaces[2].users[0].id;
|
||||
|
||||
const jwtToken = await platform.auth.getJWTToken({ sub: userIdFromAnotherWorkspace });
|
||||
|
||||
const response = await platform.app.inject({
|
||||
method: "GET",
|
||||
url: `${url}/companies/${companyId}/workspaces/${workspaceId}`,
|
||||
headers: { authorization: `Bearer ${jwtToken}` },
|
||||
});
|
||||
expect(response.statusCode).toBe(200);
|
||||
|
||||
const resource = response.json()["resource"];
|
||||
|
||||
expect(resource).toMatchObject({
|
||||
id: expect.any(String),
|
||||
company_id: expect.any(String),
|
||||
name: expect.any(String),
|
||||
logo: expect.any(String),
|
||||
default: expect.any(Boolean),
|
||||
archived: expect.any(Boolean),
|
||||
});
|
||||
|
||||
if (resource.stats) {
|
||||
expect(resource.stats).toMatchObject({
|
||||
created_at: expect.any(Number),
|
||||
total_members: 1,
|
||||
});
|
||||
}
|
||||
|
||||
done();
|
||||
});
|
||||
|
||||
it("should 200 when user is belong to workspace", async done => {
|
||||
const workspaceId = testDbService.workspaces[0].workspace.id;
|
||||
const userIdFromThisWorkspace = testDbService.workspaces[0].users[0].id;
|
||||
|
||||
const jwtToken = await platform.auth.getJWTToken({ sub: userIdFromThisWorkspace });
|
||||
|
||||
const response = await platform.app.inject({
|
||||
method: "GET",
|
||||
url: `${url}/companies/${companyId}/workspaces/${workspaceId}`,
|
||||
headers: { authorization: `Bearer ${jwtToken}` },
|
||||
});
|
||||
expect(response.statusCode).toBe(200);
|
||||
|
||||
const resource = response.json()["resource"];
|
||||
|
||||
expect(resource).toMatchObject({
|
||||
id: expect.any(String),
|
||||
company_id: expect.any(String),
|
||||
name: expect.any(String),
|
||||
logo: expect.any(String),
|
||||
default: expect.any(Boolean),
|
||||
archived: expect.any(Boolean),
|
||||
role: expect.stringMatching(/moderator|member/),
|
||||
});
|
||||
|
||||
if (resource.stats) {
|
||||
expect(resource.stats).toMatchObject({
|
||||
created_at: expect.any(Number),
|
||||
total_members: 1,
|
||||
});
|
||||
}
|
||||
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
// create
|
||||
|
||||
describe("The POST /workspaces route (creating workspace)", () => {
|
||||
it("should 401 when not authenticated", async done => {
|
||||
const companyId = testDbService.company.id;
|
||||
|
||||
const response = await platform.app.inject({
|
||||
method: "POST",
|
||||
url: `${url}/companies/${companyId}/workspaces/${nonExistentId}`,
|
||||
});
|
||||
expect(response.statusCode).toBe(401);
|
||||
done();
|
||||
});
|
||||
|
||||
it("should 403 when user is not (company member or company admin) ", async done => {
|
||||
const companyId = testDbService.company.id;
|
||||
const userId = testDbService.users[3].id;
|
||||
const jwtToken = await platform.auth.getJWTToken({ sub: userId });
|
||||
|
||||
const response = await platform.app.inject({
|
||||
method: "POST",
|
||||
url: `${url}/companies/${companyId}/workspaces`,
|
||||
headers: { authorization: `Bearer ${jwtToken}` },
|
||||
payload: {
|
||||
resource: {
|
||||
name: "Some channel name",
|
||||
logo: "",
|
||||
default: false,
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
expect(response.statusCode).toBe(403);
|
||||
done();
|
||||
});
|
||||
|
||||
it("should 201 when workspace created well", async done => {
|
||||
const companyId = testDbService.company.id;
|
||||
const userId = testDbService.users[0].id;
|
||||
const jwtToken = await platform.auth.getJWTToken({ sub: userId });
|
||||
|
||||
const response = await platform.app.inject({
|
||||
method: "POST",
|
||||
url: `${url}/companies/${companyId}/workspaces`,
|
||||
headers: { authorization: `Bearer ${jwtToken}` },
|
||||
payload: {
|
||||
resource: {
|
||||
name: "Random channel name",
|
||||
logo: "logo",
|
||||
default: false,
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
expect(response.statusCode).toBe(201);
|
||||
|
||||
const resource = response.json()["resource"];
|
||||
|
||||
expect(resource).toMatchObject({
|
||||
id: expect.any(String),
|
||||
company_id: expect.any(String),
|
||||
name: expect.any(String),
|
||||
logo: expect.any(String),
|
||||
default: expect.any(Boolean),
|
||||
archived: expect.any(Boolean),
|
||||
role: expect.stringMatching(/moderator/),
|
||||
});
|
||||
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
// update
|
||||
|
||||
describe("The POST /workspaces/:workspace_id route (updating workspace)", () => {
|
||||
it("should 401 when not authenticated", async done => {
|
||||
const companyId = testDbService.company.id;
|
||||
|
||||
const response = await platform.app.inject({
|
||||
method: "POST",
|
||||
url: `${url}/companies/${companyId}/workspaces/${nonExistentId}`,
|
||||
payload: { resource: {} },
|
||||
});
|
||||
expect(response.statusCode).toBe(401);
|
||||
done();
|
||||
});
|
||||
|
||||
it("should 403 when not workspace not found", async done => {
|
||||
const companyId = testDbService.company.id;
|
||||
const userId = testDbService.workspaces[0].users[0].id;
|
||||
|
||||
const jwtToken = await platform.auth.getJWTToken({ sub: userId });
|
||||
|
||||
const response = await platform.app.inject({
|
||||
method: "POST",
|
||||
url: `${url}/companies/${companyId}/workspaces/${nonExistentId}`,
|
||||
headers: { authorization: `Bearer ${jwtToken}` },
|
||||
payload: { resource: {} },
|
||||
});
|
||||
|
||||
expect(response.statusCode).toBe(403);
|
||||
|
||||
done();
|
||||
});
|
||||
|
||||
it("should 403 when not belong to workspace", async done => {
|
||||
const companyId = testDbService.company.id;
|
||||
const workspaceId = testDbService.workspaces[1].workspace.id;
|
||||
const userId = testDbService.workspaces[0].users[0].id;
|
||||
|
||||
const jwtToken = await platform.auth.getJWTToken({ sub: userId });
|
||||
|
||||
const response = await platform.app.inject({
|
||||
method: "POST",
|
||||
url: `${url}/companies/${companyId}/workspaces/${workspaceId}`,
|
||||
headers: { authorization: `Bearer ${jwtToken}` },
|
||||
payload: { resource: {} },
|
||||
});
|
||||
|
||||
expect(response.statusCode).toBe(403);
|
||||
|
||||
done();
|
||||
});
|
||||
|
||||
it("should 403 when not workspace moderator", async done => {
|
||||
const companyId = testDbService.company.id;
|
||||
const workspaceId = testDbService.workspaces[1].workspace.id;
|
||||
const userId = testDbService.workspaces[0].users[0].id;
|
||||
|
||||
const jwtToken = await platform.auth.getJWTToken({ sub: userId });
|
||||
|
||||
const response = await platform.app.inject({
|
||||
method: "POST",
|
||||
url: `${url}/companies/${companyId}/workspaces/${workspaceId}`,
|
||||
headers: { authorization: `Bearer ${jwtToken}` },
|
||||
payload: { resource: {} },
|
||||
});
|
||||
|
||||
expect(response.statusCode).toBe(403);
|
||||
|
||||
done();
|
||||
});
|
||||
|
||||
it("should 200 when admin of company (full update)", async done => {
|
||||
const companyId = testDbService.company.id;
|
||||
const workspaceId = testDbService.workspaces[2].workspace.id;
|
||||
const userId = testDbService.workspaces[2].users[0].id; // company owner
|
||||
|
||||
const jwtToken = await platform.auth.getJWTToken({ sub: userId });
|
||||
|
||||
const response = await platform.app.inject({
|
||||
method: "POST",
|
||||
url: `${url}/companies/${companyId}/workspaces/${workspaceId}`,
|
||||
headers: { authorization: `Bearer ${jwtToken}` },
|
||||
payload: {
|
||||
resource: {
|
||||
name: "Another workspace name",
|
||||
logo: "logo",
|
||||
default: false,
|
||||
archived: false,
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
expect(response.statusCode).toBe(200);
|
||||
|
||||
const resource = response.json()["resource"];
|
||||
|
||||
expect(resource).toMatchObject({
|
||||
id: workspaceId,
|
||||
company_id: companyId,
|
||||
name: "Another workspace name",
|
||||
logo: expect.any(String),
|
||||
default: false,
|
||||
archived: false,
|
||||
role: "moderator", //Company admin is a moderator
|
||||
});
|
||||
|
||||
done();
|
||||
});
|
||||
|
||||
it("should 200 when moderator of workspace (partial update)", async done => {
|
||||
const companyId = testDbService.company.id;
|
||||
const workspaceId = testDbService.workspaces[2].workspace.id;
|
||||
const userId = testDbService.workspaces[2].users[1].id; // workspace admin
|
||||
|
||||
const jwtToken = await platform.auth.getJWTToken({ sub: userId });
|
||||
|
||||
const response = await platform.app.inject({
|
||||
method: "POST",
|
||||
url: `${url}/companies/${companyId}/workspaces/${workspaceId}`,
|
||||
headers: { authorization: `Bearer ${jwtToken}` },
|
||||
payload: {
|
||||
resource: {
|
||||
name: "My awesome workspace",
|
||||
default: true,
|
||||
logo: "",
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
expect(response.statusCode).toBe(200);
|
||||
|
||||
const resource = response.json()["resource"];
|
||||
|
||||
expect(resource).toMatchObject({
|
||||
id: workspaceId,
|
||||
company_id: companyId,
|
||||
name: "My awesome workspace",
|
||||
logo: expect.any(String),
|
||||
default: true,
|
||||
archived: false,
|
||||
role: "moderator",
|
||||
});
|
||||
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
// delete
|
||||
|
||||
describe("The DELETE /workspaces route", () => {
|
||||
it("should 401 when not authenticated", async done => {
|
||||
const companyId = testDbService.company.id;
|
||||
|
||||
const response = await platform.app.inject({
|
||||
method: "DELETE",
|
||||
url: `${url}/companies/${companyId}/workspaces/${nonExistentId}`,
|
||||
});
|
||||
expect(response.statusCode).toBe(401);
|
||||
done();
|
||||
});
|
||||
|
||||
it("should 403 when user is not (company member or company admin) ", async done => {
|
||||
const companyId = testDbService.company.id;
|
||||
const userId = testDbService.users[3].id;
|
||||
const jwtToken = await platform.auth.getJWTToken({ sub: userId });
|
||||
const workspaceId = testDbService.workspaces[0].workspace.id;
|
||||
|
||||
const response = await platform.app.inject({
|
||||
method: "DELETE",
|
||||
url: `${url}/companies/${companyId}/workspaces/${workspaceId}`,
|
||||
headers: { authorization: `Bearer ${jwtToken}` },
|
||||
});
|
||||
|
||||
expect(response.statusCode).toBe(403);
|
||||
done();
|
||||
});
|
||||
|
||||
it("should 204 when workspace deleted", async done => {
|
||||
const companyId = testDbService.company.id;
|
||||
const workspaceId = testDbService.workspaces[2].workspace.id;
|
||||
const userId = testDbService.workspaces[2].users[0].id;
|
||||
const jwtToken = await platform.auth.getJWTToken({ sub: userId });
|
||||
|
||||
const response = await platform.app.inject({
|
||||
method: "DELETE",
|
||||
url: `${url}/companies/${companyId}/workspaces/${workspaceId}`,
|
||||
headers: { authorization: `Bearer ${jwtToken}` },
|
||||
});
|
||||
|
||||
expect(response.statusCode).toBe(204);
|
||||
|
||||
const checkResponse = await platform.app.inject({
|
||||
method: "GET",
|
||||
url: `${url}/companies/${companyId}/workspaces`,
|
||||
headers: { authorization: `Bearer ${jwtToken}` },
|
||||
});
|
||||
|
||||
const checkResponseJson = checkResponse.json();
|
||||
|
||||
expect(checkResponseJson.resources.find((a: any) => a.id === workspaceId)).toBe(undefined);
|
||||
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user