feat: init

This commit is contained in:
montaghanmy
2023-03-23 11:03:16 +01:00
commit 10fe6f78d1
11518 changed files with 509786 additions and 0 deletions
@@ -0,0 +1,176 @@
import "reflect-metadata";
import { afterEach, beforeEach, describe, expect, it } from "@jest/globals";
import { ObjectId } from "mongodb";
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
import io from "socket.io-client";
import { Channel } from "../../../src/services/channels/entities/channel";
import {
getChannelPath,
getPublicRoomName,
} from "../../../src/services/channels/services/channel/realtime";
import { WorkspaceExecutionContext } from "../../../src/services/channels/types";
import { init, TestPlatform } from "../setup";
import { ChannelUtils, get as getChannelUtils } from "./utils";
import gr from "../../../src/services/global-resolver";
describe("The Channels Realtime feature", () => {
const url = "/internal/services/channels/v1";
let platform: TestPlatform;
let socket: SocketIOClient.Socket;
let channelUtils: ChannelUtils;
beforeEach(async () => {
platform = await init({
services: [
"webserver",
"database",
"search",
"storage",
"message-queue",
"user",
"search",
"websocket",
"applications",
"webserver",
"channels",
"auth",
"search",
"realtime",
"counter",
"statistics",
"platform-services",
],
});
channelUtils = getChannelUtils(platform);
});
afterEach(async () => {
await platform.tearDown();
platform = null;
socket && socket.close();
socket = null;
});
function connect() {
socket = io.connect("http://localhost:3000", { path: "/socket" });
socket.connect();
}
describe("On channel creation", () => {
it("should notify the client", async done => {
const jwtToken = await platform.auth.getJWTToken();
const roomToken = "twake";
const channelName = new ObjectId().toString();
connect();
socket.on("connect", () => {
socket
.emit("authenticate", { token: jwtToken })
.on("authenticated", () => {
socket.emit("realtime:join", {
name: getPublicRoomName(platform.workspace),
token: roomToken,
});
socket.on("realtime:join:error", () => done(new Error("Should not occur")));
socket.on("realtime:join:success", async () => {
const response = await platform.app.inject({
method: "POST",
url: `${url}/companies/${platform.workspace.company_id}/workspaces/${platform.workspace.workspace_id}/channels`,
headers: {
authorization: `Bearer ${jwtToken}`,
},
payload: {
resource: {
name: channelName,
},
},
});
expect(response.statusCode).toEqual(201);
});
socket.on(
"realtime:resource",
(event: { type: any; action: any; resource: { name: any } }) => {
// we can also receive other types of events (channel_activity etc)
if (event.type === "channel") {
expect(event.type).toEqual("channel");
expect(event.action).toEqual("saved");
expect(event.resource.name).toEqual(channelName);
done();
}
},
);
})
.on("unauthorized", () => {
done(new Error("Should not occur"));
});
});
});
});
describe("On channel removal", () => {
it("should notify the client", async done => {
const jwtToken = await platform.auth.getJWTToken();
const roomToken = "twake";
const channelName = new ObjectId().toString();
const channel = channelUtils.getChannel(platform.currentUser.id);
channel.name = channelName;
const creationResult = await gr.services.channels.channels.save(
channel,
{},
channelUtils.getContext({ id: channel.owner }),
);
connect();
socket.on("connect", () => {
socket
.emit("authenticate", { token: jwtToken })
.on("authenticated", () => {
socket.on(
"realtime:resource",
(event: { action: string; type: any; path: any; resource: { id: any } }) => {
if (event.action !== "deleted" || event.type !== "channel") {
// we can receive event when resource is created...
// we can also receive other types of events (channel_activity etc)
return;
}
expect(event.type).toEqual("channel");
expect(event.action).toEqual("deleted");
expect(event.path).toEqual(
getChannelPath(
{ id: creationResult.entity.id } as Channel,
{
workspace: platform.workspace,
} as WorkspaceExecutionContext,
),
);
expect(event.resource.id).toEqual(creationResult.entity.id);
done();
},
);
socket.emit("realtime:join", {
name: getPublicRoomName(platform.workspace),
token: roomToken,
});
socket.on("realtime:join:error", () => done(new Error("Should not occur")));
socket.on("realtime:join:success", async () => {
await platform.app.inject({
method: "DELETE",
url: `${url}/companies/${creationResult.entity.company_id}/workspaces/${creationResult.entity.workspace_id}/channels/${creationResult.entity.id}`,
headers: {
authorization: `Bearer ${jwtToken}`,
},
});
});
})
.on("unauthorized", () => {
done(new Error("Should not occur"));
});
});
});
});
});
File diff suppressed because it is too large Load Diff
@@ -0,0 +1,136 @@
import { afterEach, beforeEach, describe, expect, it } from "@jest/globals";
import { v1 as uuidv1 } from "uuid";
import { deserialize } from "class-transformer";
import { init, TestPlatform } from "../setup";
import { ResourceGetResponse, ResourceListResponse, User } from "../../../src/utils/types";
import { Channel } from "../../../src/services/channels/entities";
import { ChannelVisibility, WorkspaceExecutionContext } from "../../../src/services/channels/types";
import { ChannelMember } from "../../../src/services/channels/entities";
import { ChannelUtils, get as getChannelUtils } from "./utils";
import { TestDbService } from "../utils.prepare.db";
import { ChannelObject } from "../../../src/services/channels/services/channel/types";
import { Api } from "../utils.api";
import gr from "../../../src/services/global-resolver";
describe("The /internal/services/channels/v1 API", () => {
const url = "/internal/services/channels/v1";
let platform: TestPlatform;
let channelUtils: ChannelUtils;
let testDbService: TestDbService;
let api: Api;
beforeEach(async () => {
platform = await init();
testDbService = new TestDbService(platform);
api = new Api(platform);
channelUtils = getChannelUtils(platform);
});
afterEach(async () => {
await platform?.tearDown();
platform = null;
});
async function testAccess(url, method, done) {
const jwtToken = await platform.auth.getJWTToken();
const response = await platform.app.inject({
method,
url,
headers: {
authorization: `Bearer ${jwtToken}`,
},
});
expect(response.statusCode).toBe(400);
done();
}
function getContext(user?: User): WorkspaceExecutionContext {
return {
workspace: platform.workspace,
user: user || platform.currentUser,
};
}
/**
* Get a new channel instance
*
* @param owner will be a random uuidv4 if not defined
*/
function getChannel(owner: string = uuidv1()): Channel {
const channel = new Channel();
channel.name = "Test Channel";
channel.company_id = platform.workspace.company_id;
channel.workspace_id = platform.workspace.workspace_id;
channel.is_default = false;
channel.visibility = ChannelVisibility.PRIVATE;
channel.archived = false;
channel.owner = owner;
return channel;
}
async function getChannelREST(channelId: string): Promise<ChannelObject> {
const response = await api.get(
`${url}/companies/${platform.workspace.company_id}/workspaces/${platform.workspace.workspace_id}/channels/${channelId}`,
);
expect(response.statusCode).toEqual(200);
const channelGetResult: ResourceGetResponse<ChannelObject> = deserialize(
ResourceGetResponse,
response.body,
);
return channelGetResult.resource;
}
describe("Channels search", () => {
it("Should find channels by name", async done => {
const ws0pk = { id: uuidv1(), company_id: platform.workspace.company_id };
await testDbService.createWorkspace(ws0pk);
const newUser = await testDbService.createUser([ws0pk]);
for (let i = 0; i < 10; i++) {
const channel = getChannel();
channel.name = `test channel ${i}`;
await gr.services.channels.channels.save(channel, {}, getContext());
if (i == 0) continue;
await gr.services.channels.members.save(
{
channel_id: channel.id,
workspace_id: channel.workspace_id,
company_id: channel.company_id,
user_id: newUser.id,
} as ChannelMember,
channelUtils.getChannelContext(channel, platform.currentUser),
);
}
await new Promise(r => setTimeout(() => r(true), 1000));
const jwtToken = await platform.auth.getJWTToken({ sub: newUser.id });
const response = await platform.app.inject({
method: "GET",
url: `${url}/companies/${platform.workspace.company_id}/search`,
headers: {
authorization: `Bearer ${jwtToken}`,
},
query: {
q: "test",
},
});
const result: ResourceListResponse<Channel> = deserialize(
ResourceListResponse,
response.body,
);
expect(response.statusCode).toBe(200);
expect(result.resources.length).toEqual(9);
done();
});
});
});
@@ -0,0 +1,420 @@
import { afterEach, beforeEach, describe, expect, it } from "@jest/globals";
import { v1 as uuidv1 } from "uuid";
import { deserialize } from "class-transformer";
import { init, TestPlatform } from "../setup";
import {
ResourceCreateResponse,
ResourceListResponse,
User,
Workspace,
} from "../../../src/utils/types";
import { Channel } from "../../../src/services/channels/entities";
import { ChannelVisibility, WorkspaceExecutionContext } from "../../../src/services/channels/types";
import { ChannelUtils, get as getChannelUtils } from "./utils";
import { DirectChannel } from "../../../src/services/channels/entities/direct-channel";
import gr from "../../../src/services/global-resolver";
describe("The direct channels API", () => {
const url = "/internal/services/channels/v1";
let platform: TestPlatform;
let channelUtils: ChannelUtils;
beforeEach(async () => {
platform = await init({
services: [
"webserver",
"database",
"search",
"storage",
"message-queue",
"user",
"applications",
"websocket",
"channels",
"auth",
"storage",
"counter",
"statistics",
],
});
channelUtils = getChannelUtils(platform);
});
afterEach(async () => {
await platform.tearDown();
});
function getContext(user?: User): WorkspaceExecutionContext {
return {
workspace: platform.workspace,
user: user || platform.currentUser,
};
}
describe("Channel List - GET /channels", () => {
it("should return empty list of direct channels", async done => {
const jwtToken = await platform.auth.getJWTToken();
const response = await platform.app.inject({
method: "GET",
url: `${url}/companies/${platform.workspace.company_id}/workspaces/direct/channels`,
headers: {
authorization: `Bearer ${jwtToken}`,
},
});
const result: ResourceListResponse<Channel> = deserialize(
ResourceListResponse,
response.body,
);
expect(response.statusCode).toBe(200);
expect(result.resources.length).toEqual(0);
done();
});
it("should return list of direct channels the user is member of", async done => {
const channel = channelUtils.getChannel();
const directChannelIn = channelUtils.getDirectChannel();
const directChannelNotIn = channelUtils.getDirectChannel();
const members = [platform.currentUser.id, uuidv1()];
const directWorkspace: Workspace = {
company_id: platform.workspace.company_id,
workspace_id: ChannelVisibility.DIRECT,
};
const creationResult = await Promise.all([
gr.services.channels.channels.save(channel, {}, getContext()),
gr.services.channels.channels.save(
directChannelIn,
{
members,
},
{ ...getContext(), ...{ workspace: directWorkspace } },
),
gr.services.channels.channels.save(
directChannelNotIn,
{
members: [uuidv1(), uuidv1()],
},
{ ...getContext({ id: uuidv1() }), ...{ workspace: directWorkspace } },
),
]);
const jwtToken = await platform.auth.getJWTToken();
const directResponse = await platform.app.inject({
method: "GET",
url: `${url}/companies/${platform.workspace.company_id}/workspaces/direct/channels`,
headers: {
authorization: `Bearer ${jwtToken}`,
},
});
const directResult: ResourceListResponse<Channel> = deserialize(
ResourceListResponse,
directResponse.body,
);
expect(directResponse.statusCode).toBe(200);
expect(directResult.resources.length).toEqual(1);
expect(directResult.resources[0]).toMatchObject({
id: creationResult[1].entity.id,
workspace_id: ChannelVisibility.DIRECT,
user_member: {
user_id: platform.currentUser.id,
},
});
expect(directResult.resources[0].members).toContain(members[0]);
expect(directResult.resources[0].members).toContain(members[1]);
const response = await platform.app.inject({
method: "GET",
url: `${url}/companies/${platform.workspace.company_id}/workspaces/${platform.workspace.workspace_id}/channels`,
headers: {
authorization: `Bearer ${jwtToken}`,
},
});
const result: ResourceListResponse<Channel> = deserialize(
ResourceListResponse,
response.body,
);
expect(response.statusCode).toBe(200);
expect(result.resources.length).toEqual(1);
expect(result.resources[0]).toMatchObject({
id: creationResult[0].entity.id,
});
done();
});
it("should not return direct channels in workspace list", async done => {
const channel = channelUtils.getChannel();
const directChannelIn = channelUtils.getDirectChannel();
const directChannelIn2 = channelUtils.getDirectChannel();
const directChannelNotIn = channelUtils.getDirectChannel();
const members = [platform.currentUser.id, uuidv1()];
const directWorkspace: Workspace = {
company_id: platform.workspace.company_id,
workspace_id: ChannelVisibility.DIRECT,
};
const creationResult = await Promise.all([
//This channel will automatically contains the requester because it is added automatically in it
gr.services.channels.channels.save(channel, {}, getContext()),
//It will contain the currentUser
gr.services.channels.channels.save(
directChannelIn,
{
members,
},
{ ...getContext({ id: uuidv1() }), ...{ workspace: directWorkspace } },
),
//This channel will automatically contains the requester because it is added automatically in it
gr.services.channels.channels.save(
directChannelIn2,
{
members: [uuidv1(), uuidv1()],
},
{ ...getContext(), ...{ workspace: directWorkspace } },
),
//This channel will not contain the currentUser
gr.services.channels.channels.save(
directChannelNotIn,
{
members: [uuidv1(), uuidv1()],
},
{ ...getContext({ id: uuidv1() }), ...{ workspace: directWorkspace } },
),
]);
const jwtToken = await platform.auth.getJWTToken();
const response = await platform.app.inject({
method: "GET",
url: `${url}/companies/${platform.workspace.company_id}/workspaces/${platform.workspace.workspace_id}/channels`,
headers: {
authorization: `Bearer ${jwtToken}`,
},
});
const result: ResourceListResponse<Channel> = deserialize(
ResourceListResponse,
response.body,
);
expect(response.statusCode).toBe(200);
expect(result.resources.length).toEqual(1);
expect(result.resources[0]).toMatchObject({
id: creationResult[0].entity.id,
});
expect(result.resources[0].visibility).not.toEqual(ChannelVisibility.DIRECT);
done();
});
it("should not return direct channels in workspace list with mine parameter", async done => {
const channel = channelUtils.getChannel();
const channel2 = channelUtils.getChannel();
const directChannelIn = channelUtils.getDirectChannel();
const directChannelNotIn = channelUtils.getDirectChannel();
const members = [platform.currentUser.id, uuidv1()];
const directWorkspace: Workspace = {
company_id: platform.workspace.company_id,
workspace_id: ChannelVisibility.DIRECT,
};
await Promise.all([
//This channel will automatically contains the requester because it is added automatically in it
gr.services.channels.channels.save(channel, {}, getContext()),
//This channel will not contain currentUser
gr.services.channels.channels.save(channel2, {}, getContext({ id: uuidv1() })),
//This channel will automatically contains the requester because it is added automatically in it
gr.services.channels.channels.save(
directChannelIn,
{
members,
},
{ ...getContext(), ...{ workspace: directWorkspace } },
),
gr.services.channels.channels.save(
directChannelNotIn,
{
members: [uuidv1(), uuidv1(), uuidv1()],
},
{ ...getContext(), ...{ workspace: directWorkspace } },
),
]);
const jwtToken = await platform.auth.getJWTToken();
const response = await platform.app.inject({
method: "GET",
url: `${url}/companies/${platform.workspace.company_id}/workspaces/${platform.workspace.workspace_id}/channels`,
headers: {
authorization: `Bearer ${jwtToken}`,
},
query: {
mine: "true",
},
});
const result: ResourceListResponse<Channel> = deserialize(
ResourceListResponse,
response.body,
);
expect(response.statusCode).toBe(200);
expect(result.resources.length).toEqual(1);
done();
});
});
describe("Create direct channel - POST /channels", () => {
it("should be able to create a direct channel with members", async done => {
const jwtToken = await platform.auth.getJWTToken();
const members = [uuidv1(), platform.currentUser.id];
const response = await platform.app.inject({
method: "POST",
url: `${url}/companies/${platform.workspace.company_id}/workspaces/direct/channels`,
headers: {
authorization: `Bearer ${jwtToken}`,
},
payload: {
options: {
members,
},
resource: {
description: "A direct channel description",
visibility: "direct",
},
},
});
expect(response.statusCode).toEqual(201);
const channelCreateResult: ResourceCreateResponse<Channel> = deserialize(
ResourceCreateResponse,
response.body,
);
expect(channelCreateResult.resource).toBeDefined();
const createdChannel = await gr.services.channels.channels.get({
id: channelCreateResult.resource.id,
company_id: channelCreateResult.resource.company_id,
workspace_id: ChannelVisibility.DIRECT,
});
expect(createdChannel).toBeDefined();
const directChannelEntity = await gr.services.channels.channels.getDirectChannel({
channel_id: createdChannel.id,
company_id: createdChannel.company_id,
users: DirectChannel.getUsersAsString(members),
});
expect(directChannelEntity).toBeDefined();
const directChannelsInCompany = await gr.services.channels.channels.getDirectChannelInCompany(
createdChannel.company_id,
members,
);
expect(directChannelsInCompany).toBeDefined();
done();
});
it("should not be able to create the same direct channel twice (with same users)", async done => {
function createChannel(members: string[]) {
return platform.app.inject({
method: "POST",
url: `${url}/companies/${platform.workspace.company_id}/workspaces/direct/channels`,
headers: {
authorization: `Bearer ${jwtToken}`,
},
payload: {
options: {
members,
},
resource: {
name: "Hello",
visibility: "direct",
},
},
});
}
const jwtToken = await platform.auth.getJWTToken();
const members = [uuidv1(), platform.currentUser.id];
const ids = new Set<string>();
let response = await createChannel(members);
expect(response.statusCode).toEqual(201);
let channelCreateResult: ResourceCreateResponse<Channel> = deserialize(
ResourceCreateResponse,
response.body,
);
ids.add(channelCreateResult.resource.id);
response = await createChannel(members);
expect(response.statusCode).toEqual(201);
channelCreateResult = deserialize(ResourceCreateResponse, response.body);
ids.add(channelCreateResult.resource.id);
expect(ids.size).toEqual(1);
done();
});
it("should not be able to create the same direct channel twice (with same users not in the same order)", async done => {
function createChannel(members: string[]) {
return platform.app.inject({
method: "POST",
url: `${url}/companies/${platform.workspace.company_id}/workspaces/direct/channels`,
headers: {
authorization: `Bearer ${jwtToken}`,
},
payload: {
options: {
members,
},
resource: {
name: "Hello",
visibility: "direct",
},
},
});
}
const jwtToken = await platform.auth.getJWTToken();
const members = [uuidv1(), platform.currentUser.id];
const ids = new Set<string>();
let response = await createChannel(members);
expect(response.statusCode).toEqual(201);
let channelCreateResult: ResourceCreateResponse<Channel> = deserialize(
ResourceCreateResponse,
response.body,
);
ids.add(channelCreateResult.resource.id);
response = await createChannel(members.reverse());
expect(response.statusCode).toEqual(201);
channelCreateResult = deserialize(ResourceCreateResponse, response.body);
ids.add(channelCreateResult.resource.id);
expect(ids.size).toEqual(1);
done();
});
});
});
@@ -0,0 +1,177 @@
import "reflect-metadata";
import { afterEach, beforeEach, describe, expect, it } from "@jest/globals";
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
import io from "socket.io-client";
import { Channel, ChannelMember } from "../../../src/services/channels/entities";
import { init, TestPlatform } from "../setup";
import { ChannelUtils, get as getChannelUtils } from "./utils";
import { getPublicRoomName } from "../../../src/services/channels/services/member/realtime";
import { SaveResult } from "../../../src/core/platform/framework/api/crud-service";
import gr from "../../../src/services/global-resolver";
describe.skip("The Channels Members Realtime feature", () => {
const url = "/internal/services/channels/v1";
let platform: TestPlatform;
let socket: SocketIOClient.Socket;
let channelUtils: ChannelUtils;
beforeEach(async () => {
platform = await init({
services: [
"message-queue",
"user",
"search",
"websocket",
"webserver",
"channels",
"auth",
"database",
"search",
"realtime",
],
});
channelUtils = getChannelUtils(platform);
});
afterEach(async () => {
await platform.tearDown();
platform = null;
socket && socket.close();
socket = null;
});
function connect() {
socket = io.connect("http://localhost:3000", { path: "/socket.io" });
socket.connect();
}
describe("On channel member creation", () => {
let channel;
let createdChannel: SaveResult<Channel>;
beforeEach(async () => {
channel = channelUtils.getChannel();
createdChannel = await gr.services.channels.channels.save(
channel,
{},
channelUtils.getContext({ id: channel.owner }),
);
});
it("should notify the client", async done => {
const jwtToken = await platform.auth.getJWTToken();
const roomToken = "twake";
connect();
socket.on("connect", () => {
socket
.emit("authenticate", { token: jwtToken })
.on("authenticated", () => {
socket.emit("realtime:join", {
name: getPublicRoomName(createdChannel.entity),
token: roomToken,
});
socket.on("realtime:join:error", () => done(new Error("Should not occur")));
socket.on("realtime:join:success", async () => {
const response = await platform.app.inject({
method: "POST",
url: `${url}/companies/${platform.workspace.company_id}/workspaces/${platform.workspace.workspace_id}/channels/${createdChannel.entity.id}/members`,
headers: {
authorization: `Bearer ${jwtToken}`,
},
payload: {
resource: {
user_id: platform.currentUser.id,
},
},
});
expect(response.statusCode).toEqual(201);
});
socket.on("realtime:resource", event => {
expect(event.type).toEqual("channel_member");
expect(event.action).toEqual("saved");
expect(event.resource).toMatchObject({
company_id: platform.workspace.company_id,
workspace_id: platform.workspace.workspace_id,
user_id: platform.currentUser.id,
channel_id: createdChannel.entity.id,
});
done();
});
})
.on("unauthorized", () => {
done(new Error("Should not occur"));
});
});
});
});
describe("On channel member removal", () => {
it("should notify the client", async done => {
const jwtToken = await platform.auth.getJWTToken();
const roomToken = "twake";
const channel = channelUtils.getChannel(platform.currentUser.id);
const creationResult = await gr.services.channels.channels.save(
channel,
{},
channelUtils.getContext({ id: channel.owner }),
);
const member = {
channel_id: creationResult.entity.id,
workspace_id: platform.workspace.workspace_id,
company_id: platform.workspace.company_id,
user_id: platform.currentUser.id,
} as ChannelMember;
await gr.services.channels.members.save(member, {
channel: creationResult.entity,
user: platform.currentUser,
});
connect();
socket.on("connect", () => {
socket
.emit("authenticate", { token: jwtToken })
.on("authenticated", () => {
socket.on("realtime:resource", event => {
if (event.action !== "deleted") {
// we can receive event when resource is created...
return;
}
expect(event.type).toEqual("channel_member");
expect(event.action).toEqual("deleted");
expect(event.resource).toMatchObject({
company_id: platform.workspace.company_id,
workspace_id: platform.workspace.workspace_id,
user_id: platform.currentUser.id,
channel_id: creationResult.entity.id,
});
done();
});
socket.emit("realtime:join", {
name: getPublicRoomName(creationResult.entity),
token: roomToken,
});
socket.on("realtime:join:error", () => done(new Error("Should not occur")));
socket.on("realtime:join:success", async () => {
const response = await platform.app.inject({
method: "DELETE",
url: `${url}/companies/${creationResult.entity.company_id}/workspaces/${creationResult.entity.workspace_id}/channels/${creationResult.entity.id}/members/${platform.currentUser.id}`,
headers: {
authorization: `Bearer ${jwtToken}`,
},
});
});
})
.on("unauthorized", () => {
done(new Error("Should not occur"));
});
});
});
});
});
@@ -0,0 +1,401 @@
import { afterEach, beforeEach, describe, expect, it } from "@jest/globals";
import { v1 as uuidv1, v4 as uuidv4 } from "uuid";
import { deserialize } from "class-transformer";
import { init, TestPlatform } from "../setup";
import { Channel } from "../../../src/services/channels/entities/channel";
import { ChannelMember } from "../../../src/services/channels/entities/channel-member";
import {
ChannelExecutionContext,
ChannelVisibility,
WorkspaceExecutionContext,
} from "../../../src/services/channels/types";
import { ResourceGetResponse, ResourceListResponse, User } from "../../../src/utils/types";
import gr from "../../../src/services/global-resolver";
describe.skip("The ChannelMembers REST API", () => {
const url = "/internal/services/channels/v1";
let platform: TestPlatform;
beforeEach(async () => {
platform = await init({
services: ["websocket", "webserver", "channels", "auth", "database"],
});
});
afterEach(async () => {
await platform.tearDown();
platform = null;
});
function getWorkspaceContext(user?: User): WorkspaceExecutionContext {
return {
workspace: platform.workspace,
user: user || platform.currentUser,
};
}
function getContext(channel: Channel, user?: User): ChannelExecutionContext {
return {
channel,
user,
};
}
/**
* Get a new channel instance
*
* @param owner will be a random uuidv4 if not defined
*/
function getChannel(owner: string = uuidv1()): Channel {
const channel = new Channel();
channel.name = "Test Channel";
channel.company_id = platform.workspace.company_id;
channel.workspace_id = platform.workspace.workspace_id;
channel.is_default = false;
channel.visibility = ChannelVisibility.PRIVATE;
channel.archived = false;
channel.owner = owner;
return channel;
}
function getMember(channel: Channel, user: User): ChannelMember {
const member = new ChannelMember();
member.company_id = platform.workspace.company_id;
member.workspace_id = platform.workspace.workspace_id;
member.channel_id = channel?.id;
member.user_id = user?.id;
return member;
}
describe("The GET / - Get members list", () => {
let channel;
let createdChannel;
beforeEach(async () => {
channel = getChannel();
createdChannel = await gr.services.channels.channels.save(channel, {}, getWorkspaceContext());
});
it("should 404 when channel does not exists", done => {
done();
});
it("should return empty list of members", async done => {
const jwtToken = await platform.auth.getJWTToken();
const response = await platform.app.inject({
method: "GET",
url: `${url}/companies/${platform.workspace.company_id}/workspaces/${platform.workspace.workspace_id}/channels/${createdChannel.entity.id}/members`,
headers: {
authorization: `Bearer ${jwtToken}`,
},
});
const result: ResourceListResponse<ChannelMember> = deserialize(
ResourceListResponse,
response.body,
);
expect(response.statusCode).toBe(200);
expect(result.resources.length).toEqual(0);
done();
});
it("should return list of members the user has access to", async done => {
const member = getMember(createdChannel.entity, platform.currentUser);
const memberCreationResult = await gr.services.channels.members.save(
member,
getContext(channel),
);
const jwtToken = await platform.auth.getJWTToken();
const response = await platform.app.inject({
method: "GET",
url: `${url}/companies/${platform.workspace.company_id}/workspaces/${platform.workspace.workspace_id}/channels/${createdChannel.entity.id}/members`,
headers: {
authorization: `Bearer ${jwtToken}`,
},
});
const result: ResourceListResponse<ChannelMember> = deserialize(
ResourceListResponse,
response.body,
);
expect(response.statusCode).toBe(200);
expect(result.resources.length).toEqual(1);
expect(result.resources[0]).toMatchObject({
channel_id: memberCreationResult.entity.channel_id,
workspace_id: memberCreationResult.entity.workspace_id,
company_id: memberCreationResult.entity.company_id,
user_id: memberCreationResult.entity.user_id,
});
done();
});
});
describe("The POST / - Add member", () => {
let channel;
let createdChannel;
beforeEach(async () => {
channel = getChannel();
createdChannel = await gr.services.channels.channels.save(channel, {}, getWorkspaceContext());
});
it("should fail when user_id is not defined", async done => {
const jwtToken = await platform.auth.getJWTToken();
const response = await platform.app.inject({
method: "POST",
url: `${url}/companies/${platform.workspace.company_id}/workspaces/${platform.workspace.workspace_id}/channels/${createdChannel.entity.id}/members`,
headers: {
authorization: `Bearer ${jwtToken}`,
},
payload: {
resource: {},
},
});
expect(response.statusCode).toEqual(400);
done();
});
it("should be able to add current member", async done => {
const jwtToken = await platform.auth.getJWTToken();
const response = await platform.app.inject({
method: "POST",
url: `${url}/companies/${platform.workspace.company_id}/workspaces/${platform.workspace.workspace_id}/channels/${createdChannel.entity.id}/members`,
headers: {
authorization: `Bearer ${jwtToken}`,
},
payload: {
resource: {
user_id: platform.currentUser.id,
},
},
});
expect(response.statusCode).toEqual(201);
done();
});
it("should be able to add another member", async done => {
const jwtToken = await platform.auth.getJWTToken();
const response = await platform.app.inject({
method: "POST",
url: `${url}/companies/${platform.workspace.company_id}/workspaces/${platform.workspace.workspace_id}/channels/${createdChannel.entity.id}/members`,
headers: {
authorization: `Bearer ${jwtToken}`,
},
payload: {
resource: {
user_id: uuidv1(),
},
},
});
expect(response.statusCode).toEqual(201);
done();
});
});
describe("The GET /:member_id - Get a member", () => {
let channel;
let createdChannel;
beforeEach(async () => {
channel = getChannel();
createdChannel = await gr.services.channels.channels.save(channel, {}, getWorkspaceContext());
});
it("should 404 when member does not exist", async done => {
const jwtToken = await platform.auth.getJWTToken();
const response = await platform.app.inject({
method: "GET",
url: `${url}/companies/${platform.workspace.company_id}/workspaces/${
platform.workspace.workspace_id
}/channels/${createdChannel.entity.id}/members/${uuidv1()}`,
headers: {
authorization: `Bearer ${jwtToken}`,
},
});
expect(response.statusCode).toEqual(404);
done();
});
it("should send back member", async done => {
const member = getMember(createdChannel.entity, platform.currentUser);
const memberCreationResult = await gr.services.channels.members.save(
member,
getContext(channel),
);
const jwtToken = await platform.auth.getJWTToken();
const response = await platform.app.inject({
method: "GET",
url: `${url}/companies/${platform.workspace.company_id}/workspaces/${platform.workspace.workspace_id}/channels/${createdChannel.entity.id}/members/${memberCreationResult.entity.user_id}`,
headers: {
authorization: `Bearer ${jwtToken}`,
},
});
expect(response.statusCode).toEqual(200);
const result: ResourceGetResponse<ChannelMember> = deserialize(
ResourceGetResponse,
response.body,
);
expect(response.statusCode).toBe(200);
expect(result.resource).toMatchObject({
channel_id: memberCreationResult.entity.channel_id,
workspace_id: memberCreationResult.entity.workspace_id,
company_id: memberCreationResult.entity.company_id,
user_id: memberCreationResult.entity.user_id,
});
done();
});
});
describe("The POST /:member_id - Update a member", () => {
let channel;
let createdChannel;
beforeEach(async () => {
channel = getChannel();
createdChannel = await gr.services.channels.channels.save(channel, {}, getWorkspaceContext());
});
it("should not be able to update a member when current user is not the member", async done => {
const member = getMember(createdChannel.entity, { id: uuidv4() });
const memberCreationResult = await gr.services.channels.members.save(
member,
getContext(channel),
);
const jwtToken = await platform.auth.getJWTToken();
const response = await platform.app.inject({
method: "POST",
url: `${url}/companies/${platform.workspace.company_id}/workspaces/${platform.workspace.workspace_id}/channels/${createdChannel.entity.id}/members/${memberCreationResult.entity.user_id}`,
headers: {
authorization: `Bearer ${jwtToken}`,
},
payload: {
resource: {
favorite: true,
},
},
});
expect(response.statusCode).toEqual(400);
done();
});
it("should be able to update member when current user is the member", async done => {
const member = getMember(createdChannel.entity, platform.currentUser);
const memberCreationResult = await gr.services.channels.members.save(
member,
getContext(channel),
);
const jwtToken = await platform.auth.getJWTToken();
const response = await platform.app.inject({
method: "POST",
url: `${url}/companies/${platform.workspace.company_id}/workspaces/${platform.workspace.workspace_id}/channels/${createdChannel.entity.id}/members/${memberCreationResult.entity.user_id}`,
headers: {
authorization: `Bearer ${jwtToken}`,
},
payload: {
resource: {
favorite: true,
},
},
});
expect(response.statusCode).toEqual(200);
const channelMember: ChannelMember = await gr.services.channels.members.get(member);
expect(channelMember).toMatchObject({
channel_id: memberCreationResult.entity.channel_id,
workspace_id: memberCreationResult.entity.workspace_id,
company_id: memberCreationResult.entity.company_id,
user_id: memberCreationResult.entity.user_id,
favorite: true,
});
done();
});
});
describe("The DELETE /:member_id - Remove a member", () => {
let channel;
let createdChannel;
beforeEach(async () => {
channel = getChannel();
createdChannel = await gr.services.channels.channels.save(channel, {}, getWorkspaceContext());
});
it("should 404 when member does not exist", async done => {
const jwtToken = await platform.auth.getJWTToken();
const response = await platform.app.inject({
method: "DELETE",
url: `${url}/companies/${platform.workspace.company_id}/workspaces/${
platform.workspace.workspace_id
}/channels/${createdChannel.entity.id}/members/${uuidv1()}`,
headers: {
authorization: `Bearer ${jwtToken}`,
},
payload: {
resource: {
favorite: true,
},
},
});
expect(response.statusCode).toEqual(404);
done();
});
it("should not be able to remove the member when current user does not have enough rights", async done => {
// const member = getMember(createdChannel.entity, { id: uuidv4() });
// const memberCreationResult = await gr.services.channels.members.save(member);
// const jwtToken = await platform.auth.getJWTToken();
// const response = await platform.app.inject({
// method: "DELETE",
// url: `${url}/companies/${platform.workspace.company_id}/workspaces/${platform.workspace.workspace_id}/channels/${createdChannel.entity.id}/members/${memberCreationResult.entity.user_id}`,
// headers: {
// authorization: `Bearer ${jwtToken}`,
// },
// });
//
// expect(response.statusCode).toEqual(400);
// done();
});
it("should be able to remove the member when current user is the member", async done => {
const member = getMember(createdChannel.entity, platform.currentUser);
const memberCreationResult = await gr.services.channels.members.save(
member,
getContext(channel),
);
const jwtToken = await platform.auth.getJWTToken();
const response = await platform.app.inject({
method: "DELETE",
url: `${url}/companies/${platform.workspace.company_id}/workspaces/${platform.workspace.workspace_id}/channels/${createdChannel.entity.id}/members/${memberCreationResult.entity.user_id}`,
headers: {
authorization: `Bearer ${jwtToken}`,
},
});
expect(response.statusCode).toEqual(204);
done();
});
});
});
@@ -0,0 +1,90 @@
import { v1 as uuidv1 } from "uuid";
import { ChannelMember } from "../../../src/services/channels/entities";
import { Channel } from "../../../src/services/channels/entities/channel";
import {
ChannelExecutionContext,
ChannelVisibility,
WorkspaceExecutionContext,
} from "../../../src/services/channels/types";
import { User } from "../../../src/utils/types";
import { TestPlatform } from "../setup";
export interface ChannelUtils {
getContext(user?: User): WorkspaceExecutionContext;
getChannel(owner?: string): Channel;
getDirectChannel(owner?: string): Channel;
getChannelContext(channel: Channel, user: User): ChannelExecutionContext;
}
export interface ChannelMemberUtils {
getMember(channel: Channel, user: User): ChannelMember;
}
export function getMemberUtils(platform: TestPlatform): ChannelMemberUtils {
return {
getMember,
};
function getMember(channel: Channel, user: User): ChannelMember {
const member = new ChannelMember();
member.company_id = platform.workspace.company_id;
member.workspace_id = platform.workspace.workspace_id;
member.channel_id = channel?.id;
member.user_id = user?.id;
return member;
}
}
export function get(platform: TestPlatform): ChannelUtils {
return {
getContext,
getChannel,
getDirectChannel,
getChannelContext,
};
function getContext(user?: User): WorkspaceExecutionContext {
return {
workspace: platform.workspace,
user: user || platform.currentUser,
};
}
function getChannelContext(channel: Channel, user?: User): ChannelExecutionContext {
return {
channel,
user,
};
}
/**
* Get a new channel instance
*
* @param owner will be a random uuidv4 if not defined
*/
function getChannel(owner: string = uuidv1()): Channel {
const channel = new Channel();
channel.name = "Test Channel";
channel.company_id = platform.workspace.company_id;
channel.workspace_id = platform.workspace.workspace_id;
channel.is_default = false;
channel.visibility = ChannelVisibility.PUBLIC;
channel.archived = false;
channel.owner = owner;
return channel;
}
function getDirectChannel(owner: string = uuidv1()): Channel {
const channel = getChannel(owner);
channel.visibility = ChannelVisibility.DIRECT;
channel.workspace_id = ChannelVisibility.DIRECT;
channel.name = "A direct channel";
return channel;
}
}