378 lines
11 KiB
JavaScript
378 lines
11 KiB
JavaScript
import assert from "node:assert";
|
|
import { test } from "node:test";
|
|
import { validate } from "uuid";
|
|
import { apiGet, apiPost, apiPatch, apiDelete } from "./api.js";
|
|
|
|
const state = {};
|
|
|
|
test("can create community", async () => {
|
|
state.communityName = "testCommunity";
|
|
state.communityDescription = "this is a test community";
|
|
|
|
state.username1 = "testuser1";
|
|
state.password1 = "8556";
|
|
state.email1 = "testuser1@test.test";
|
|
state.username2 = "testuser2";
|
|
state.password2 = "8556";
|
|
state.email2 = "testuser2@test.test";
|
|
|
|
const responseRegister1 = await apiPost(`auth/register`, {
|
|
username: state.username1,
|
|
password: state.password1,
|
|
email: state.email1,
|
|
});
|
|
state.userId1 = responseRegister1.id;
|
|
|
|
const responseRegister2 = await apiPost(`auth/register`, {
|
|
username: state.username2,
|
|
password: state.password2,
|
|
email: state.email2,
|
|
});
|
|
state.userId2 = responseRegister2.id;
|
|
|
|
const responseLogin1 = await apiPost(`auth/login`, {
|
|
username: state.username1,
|
|
password: state.password1,
|
|
});
|
|
state.sessionId1 = responseLogin1.id;
|
|
state.token1 = responseLogin1.token;
|
|
|
|
const responseLogin2 = await apiPost(`auth/login`, {
|
|
username: state.username2,
|
|
password: state.password2,
|
|
});
|
|
state.sessionId2 = responseLogin2.id;
|
|
state.token2 = responseLogin2.token;
|
|
|
|
const responseCreate = await apiPost(
|
|
`community`,
|
|
{
|
|
name: state.communityName,
|
|
description: state.communityDescription,
|
|
},
|
|
state.token1,
|
|
);
|
|
state.communityId = responseCreate.id;
|
|
assert.equal(responseCreate.name, state.communityName);
|
|
assert.equal(responseCreate.description, state.communityDescription);
|
|
assert.equal(responseCreate.ownerId, state.userId1);
|
|
|
|
const responseGet = await apiGet(`community/${state.communityId}`);
|
|
assert.equal(responseGet.name, state.communityName);
|
|
assert.equal(responseGet.description, state.communityDescription);
|
|
assert.equal(responseGet.ownerId, state.userId1);
|
|
});
|
|
|
|
test("shouldn't be able to create invite", async () => {
|
|
const response = await apiPost(
|
|
`community/${state.communityId}/invite`,
|
|
{
|
|
totalInvites: 1,
|
|
},
|
|
state.token2,
|
|
);
|
|
|
|
assert.equal(response.error, "ACCESS_DENIED");
|
|
});
|
|
|
|
test("can create invite", async () => {
|
|
const response = await apiPost(
|
|
`community/${state.communityId}/invite`,
|
|
{
|
|
totalInvites: 1,
|
|
},
|
|
state.token1,
|
|
);
|
|
|
|
assert.equal(response.id, state.communityId);
|
|
assert.equal(validate(response.inviteId), true);
|
|
|
|
state.inviteId = response.inviteId;
|
|
});
|
|
|
|
test("shouldn't be able to accept invite", async () => {
|
|
const response = await apiGet(
|
|
`invite/${state.inviteId}/accept`,
|
|
state.token1,
|
|
);
|
|
|
|
assert.equal(response.error, "ACCESS_DENIED");
|
|
});
|
|
|
|
test("can accept invite", async () => {
|
|
const response = await apiGet(
|
|
`invite/${state.inviteId}/accept`,
|
|
state.token2,
|
|
);
|
|
|
|
assert.equal(response.id, state.inviteId);
|
|
assert.equal(response.userId, state.userId2);
|
|
assert.equal(response.userName, state.username2);
|
|
assert.equal(response.communityId, state.communityId);
|
|
assert.equal(response.communityName, state.communityName);
|
|
|
|
const responseGet = await apiGet(
|
|
`community/${state.communityId}/members`,
|
|
state.token1,
|
|
);
|
|
|
|
assert.equal(responseGet.id, state.communityId);
|
|
assert.equal(responseGet.name, state.communityName);
|
|
assert.equal(responseGet.members.length === 2, true);
|
|
});
|
|
|
|
test("can get user communities", async () => {
|
|
const response = await apiGet(
|
|
`user/${state.userId2}/communities`,
|
|
state.token2,
|
|
);
|
|
|
|
assert.equal(response.id, state.userId2);
|
|
assert.equal(response.communities.length === 1, true);
|
|
assert.equal(response.communities[0].id, state.communityId);
|
|
assert.equal(response.communities[0].name, state.communityName);
|
|
assert.equal(
|
|
response.communities[0].description,
|
|
state.communityDescription,
|
|
);
|
|
});
|
|
|
|
test("can get invite", async () => {
|
|
const response = await apiGet(`invite/${state.inviteId}`);
|
|
|
|
assert.equal(response.id, state.inviteId);
|
|
assert.equal(response.communityId, state.communityId);
|
|
assert.equal(response.valid, false);
|
|
assert.equal(response.unlimitedInvites, false);
|
|
assert.equal(response.hasExpiration, false);
|
|
assert.equal(response.totalInvites, 1);
|
|
assert.equal(response.remainingInvites, 0);
|
|
});
|
|
|
|
test("shouldn't be able to get members", async () => {
|
|
const response = await apiGet(
|
|
`community/${state.communityId}/members`,
|
|
state.token2,
|
|
);
|
|
|
|
assert.equal(response.error, "ACCESS_DENIED");
|
|
});
|
|
|
|
test("shouldn't be able to get channels", async () => {
|
|
const response = await apiGet(
|
|
`community/${state.communityId}/channels`,
|
|
state.token2,
|
|
);
|
|
|
|
assert.equal(response.error, "ACCESS_DENIED");
|
|
});
|
|
|
|
test("shouldn't be able to get roles", async () => {
|
|
const response = await apiGet(
|
|
`community/${state.communityId}/roles`,
|
|
state.token2,
|
|
);
|
|
|
|
assert.equal(response.error, "ACCESS_DENIED");
|
|
});
|
|
|
|
test("can create channel", async () => {
|
|
state.channelName = "Test Channel";
|
|
|
|
const response = await apiPost(
|
|
`channel`,
|
|
{
|
|
name: state.channelName,
|
|
communityId: state.communityId,
|
|
},
|
|
state.token1,
|
|
);
|
|
|
|
assert.equal(validate(response.id), true);
|
|
assert.equal(response.name, state.channelName);
|
|
assert.equal(response.communityId, state.communityId);
|
|
|
|
state.channelId = response.id;
|
|
});
|
|
|
|
test("can create role", async () => {
|
|
state.roleName = "Test Role";
|
|
|
|
const response = await apiPost(
|
|
`role`,
|
|
{
|
|
name: state.roleName,
|
|
communityId: state.communityId,
|
|
permissions: ["MEMBERS_READ", "CHANNELS_READ", "ROLES_READ"],
|
|
},
|
|
state.token1,
|
|
);
|
|
|
|
assert.equal(validate(response.id), true);
|
|
assert.equal(response.name, state.roleName);
|
|
assert.equal(response.communityId, state.communityId);
|
|
|
|
state.roleId = response.id;
|
|
});
|
|
|
|
test("can assign role to user", async () => {
|
|
const response = await apiPost(
|
|
`role/${state.roleId}/assign`,
|
|
{
|
|
userId: state.userId2,
|
|
},
|
|
state.token1,
|
|
);
|
|
|
|
assert.equal(response.id, state.roleId);
|
|
assert.equal(response.name, state.roleName);
|
|
assert.equal(response.communityId, state.communityId);
|
|
assert.equal(response.userId, state.userId2);
|
|
});
|
|
|
|
test("can get members", async () => {
|
|
const response = await apiGet(
|
|
`community/${state.communityId}/members`,
|
|
state.token2,
|
|
);
|
|
|
|
assert.equal(response.id, state.communityId);
|
|
assert.equal(response.name, state.communityName);
|
|
assert.equal(response.members.length === 2, true);
|
|
});
|
|
|
|
test("can get channels", async () => {
|
|
const response = await apiGet(
|
|
`community/${state.communityId}/channels`,
|
|
state.token2,
|
|
);
|
|
|
|
assert.equal(response.id, state.communityId);
|
|
assert.equal(response.name, state.communityName);
|
|
assert.equal(response.channels.length === 1, true);
|
|
});
|
|
|
|
test("can get roles", async () => {
|
|
const response = await apiGet(
|
|
`community/${state.communityId}/roles`,
|
|
state.token2,
|
|
);
|
|
|
|
assert.equal(response.id, state.communityId);
|
|
assert.equal(response.name, state.communityName);
|
|
assert.equal(response.roles.length === 1, true);
|
|
});
|
|
|
|
test("can get permissions", async () => {
|
|
const response = await apiGet(
|
|
`role/${state.roleId}/permissions`,
|
|
state.token2,
|
|
);
|
|
|
|
assert.equal(response.id, state.roleId);
|
|
assert.equal(response.name, state.roleName);
|
|
assert.equal(response.communityId, state.communityId);
|
|
assert.equal(response.permissions.length === 3, true);
|
|
});
|
|
|
|
test("can unassign role from user", async () => {
|
|
const response = await apiPost(
|
|
`role/${state.roleId}/unassign`,
|
|
{
|
|
userId: state.userId2,
|
|
},
|
|
state.token1,
|
|
);
|
|
|
|
assert.equal(response.id, state.roleId);
|
|
assert.equal(response.name, state.roleName);
|
|
assert.equal(response.communityId, state.communityId);
|
|
assert.equal(response.userId, state.userId2);
|
|
});
|
|
|
|
test("shouldn't be able to get channels 2", async () => {
|
|
const response = await apiGet(
|
|
`community/${state.communityId}/channels`,
|
|
state.token2,
|
|
);
|
|
|
|
assert.equal(response.error, "ACCESS_DENIED");
|
|
});
|
|
|
|
test("can update channel", async () => {
|
|
state.channelName = "Test Channel Mod";
|
|
|
|
const responsePatch = await apiPatch(
|
|
`channel/${state.channelId}`,
|
|
{
|
|
name: state.channelName,
|
|
},
|
|
state.token1,
|
|
);
|
|
assert.equal(responsePatch.id, state.channelId);
|
|
assert.equal(responsePatch.name, state.channelName);
|
|
assert.equal(responsePatch.communityId, state.communityId);
|
|
|
|
const responseGet = await apiGet(
|
|
`channel/${state.channelId}`,
|
|
state.token1,
|
|
);
|
|
assert.equal(responseGet.name, state.channelName);
|
|
});
|
|
|
|
test("can delete channel", async () => {
|
|
const responseDelete = await apiDelete(
|
|
`channel/${state.channelId}`,
|
|
{},
|
|
state.token1,
|
|
);
|
|
assert.equal(responseDelete.id, state.channelId);
|
|
|
|
const responseGet = await apiGet(
|
|
`channel/${state.channelId}`,
|
|
state.token1,
|
|
);
|
|
assert.equal(responseGet.error, "ACCESS_DENIED");
|
|
});
|
|
|
|
test("can update role", async () => {
|
|
state.roleName = "Test Role Mod";
|
|
|
|
const responsePatch = await apiPatch(
|
|
`role/${state.roleId}`,
|
|
{
|
|
name: state.roleName,
|
|
},
|
|
state.token1,
|
|
);
|
|
assert.equal(responsePatch.id, state.roleId);
|
|
assert.equal(responsePatch.name, state.roleName);
|
|
assert.equal(responsePatch.communityId, state.communityId);
|
|
|
|
const responseGet = await apiGet(`role/${state.roleId}`, state.token1);
|
|
assert.equal(responseGet.name, state.roleName);
|
|
});
|
|
|
|
test("can delete role", async () => {
|
|
const responseDelete = await apiDelete(
|
|
`role/${state.roleId}`,
|
|
{},
|
|
state.token1,
|
|
);
|
|
assert.equal(responseDelete.id, state.roleId);
|
|
|
|
const responseGet = await apiGet(`role/${state.roleId}`, state.token1);
|
|
assert.equal(responseGet.error, "ACCESS_DENIED");
|
|
});
|
|
|
|
test("can delete community", async () => {
|
|
const responseDelete = await apiDelete(
|
|
`community/${state.communityId}`,
|
|
{},
|
|
state.token1,
|
|
);
|
|
assert.equal(responseDelete.id, state.communityId);
|
|
|
|
const responseGet = await apiGet(`community/${state.communityId}`);
|
|
assert.equal(responseGet.error, "NOT_FOUND");
|
|
});
|