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 createResponse = await apiPost( `community`, { name: state.communityName, description: state.communityDescription, }, state.token1, ); state.communityId = createResponse.id; assert.equal(createResponse.name, state.communityName); assert.equal(createResponse.description, state.communityDescription); assert.equal(createResponse.ownerId, state.userId1); const getResponse = await apiGet(`community/${state.communityId}`); assert.equal(getResponse.name, state.communityName); assert.equal(getResponse.description, state.communityDescription); assert.equal(getResponse.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 getResponse = await apiGet( `community/${state.communityId}/members`, state.token1, ); assert.equal(getResponse.id, state.communityId); assert.equal(getResponse.name, state.communityName); assert.equal(getResponse.members.length === 2, true); }); 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 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 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); });