import assert from "node:assert"; import { test } from "node:test"; import { validate } from "uuid"; import { apiCookie, apiToken, 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 responseLogin1Array = await apiCookie(`auth/login`, { username: state.username1, password: state.password1, }); const responseLogin1 = responseLogin1Array[0]; state.cookie1 = responseLogin1Array[1]; state.sessionId1 = responseLogin1.id; const responseLogin2Array = await apiCookie(`auth/login`, { username: state.username2, password: state.password2, }); const responseLogin2 = responseLogin2Array[0]; state.cookie2 = responseLogin2Array[1]; state.sessionId2 = responseLogin2.id; const responseRefresh1 = await apiToken(`auth/refresh`, state.cookie1); state.token1 = responseRefresh1.token; const responseRefresh2 = await apiToken(`auth/refresh`, state.cookie2); state.token2 = responseRefresh2.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); assert.notEqual(responseCreate.creationDate, undefined); 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); assert.notEqual(responseGet.creationDate, undefined); }); 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); }); 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); 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"; state.channelDescription = "Test Channel Desc"; const response = await apiPost( `channel`, { name: state.channelName, description: state.channelDescription, communityId: state.communityId, }, state.token1, ); assert.equal(validate(response.id), true); assert.equal(response.name, state.channelName); assert.equal(response.description, state.channelDescription); assert.equal(response.communityId, state.communityId); assert.notEqual(response.creationDate, undefined); state.channelId = response.id; }); test("can create role", async () => { state.roleName = "Test Role"; state.roleDescription = "Test Role Desc"; const response = await apiPost( `role`, { name: state.roleName, description: state.roleDescription, 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.description, state.roleDescription); assert.equal(response.communityId, state.communityId); assert.equal(response.permissions.length, 3); assert.notEqual(response.creationDate, undefined); 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); }); 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); }); 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); }); test("can get permissions", async () => { const response = await apiGet(`role/${state.roleId}`, state.token2); assert.equal(response.id, state.roleId); assert.equal(response.name, state.roleName); assert.equal(response.description, state.roleDescription); assert.equal(response.communityId, state.communityId); assert.equal(response.permissions.length, 3); assert.equal(response.permissions[1], "CHANNELS_READ"); assert.notEqual(response.creationDate, undefined); }); 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"); });