Skip to content

Commit

Permalink
Add test for leaving waitlist (#678)
Browse files Browse the repository at this point in the history
* Remove `removeFromWaitlist` helper

The "standard" `waitlist.removeUser()` method can be used instead.

* comment

* Add test for leaving waitlist
  • Loading branch information
goto-bus-stop authored Nov 29, 2024
1 parent ff66f07 commit 47d7cde
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 26 deletions.
13 changes: 9 additions & 4 deletions src/controllers/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import {
HTTPError,
PermissionError,
UserNotFoundError,
UserNotInWaitlistError,
} from '../errors/index.js';
import skipIfCurrentDJ from '../utils/skipIfCurrentDJ.js';
import removeFromWaitlist from '../utils/removeFromWaitlist.js';
import getOffsetPagination from '../utils/getOffsetPagination.js';
import toItemResponse from '../utils/toItemResponse.js';
import toListResponse from '../utils/toListResponse.js';
Expand Down Expand Up @@ -183,16 +183,21 @@ async function changeAvatar() {
}

/**
* Remove the user ID from the online users list.
*
* @param {import('../Uwave.js').default} uw
* @param {UserID} userID
*/
async function disconnectUser(uw, userID) {
await skipIfCurrentDJ(uw, userID);

try {
await removeFromWaitlist(uw, userID);
} catch {
// Ignore
await uw.waitlist.removeUser(userID);
} catch (err) {
// It's expected that the user would not be in the waitlist
if (!(err instanceof UserNotInWaitlistError)) {
throw err;
}
}

await uw.redis.lrem('users', 0, userID);
Expand Down
10 changes: 4 additions & 6 deletions src/plugins/waitlist.js
Original file line number Diff line number Diff line change
Expand Up @@ -172,9 +172,7 @@ class Waitlist {
}

/**
* used both for joining the waitlist, and for
* adding someone else to the waitlist.
* TODO maybe split this up and let http-api handle the difference
* Add a user to the waitlist.
*
* @param {UserID} userID
* @param {{moderator?: User}} [options]
Expand Down Expand Up @@ -272,17 +270,17 @@ class Waitlist {

/**
* @param {UserID} userID
* @param {{moderator: User}} options
* @param {{moderator?: User}} [options]
* @returns {Promise<void>}
*/
async removeUser(userID, { moderator }) {
async removeUser(userID, { moderator } = {}) {
const { acl, users } = this.#uw;
const user = await users.getUser(userID);
if (!user) {
throw new UserNotFoundError({ id: userID });
}

const isRemoving = moderator && user.id !== moderator.id;
const isRemoving = moderator != null && user.id !== moderator.id;
if (isRemoving && !(await acl.isAllowed(moderator, Permissions.WaitlistRemove))) {
throw new PermissionError({
requiredRole: 'waitlist.remove',
Expand Down
16 changes: 0 additions & 16 deletions src/utils/removeFromWaitlist.js

This file was deleted.

55 changes: 55 additions & 0 deletions test/waitlist.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -353,4 +353,59 @@ describe('Waitlist', () => {
.expect(200);
});
});

describe('DELETE /waitlist/:id', () => {
it('requires authentication', async () => {
await supertest(uw.server)
.delete(`/api/waitlist/${user.id}`)
.expect(401);
});

it('returns a useful error when user is not in waitlist', async () => {
const token = await uw.test.createTestSessionToken(user);
await uw.acl.allow(user, ['user']);

const res = await supertest(uw.server)
.delete(`/api/waitlist/${user.id}`)
.set('Cookie', `uwsession=${token}`)
.expect(404);
sinon.assert.match(res.body, {
errors: sinon.match.some(sinon.match.has('code', 'not-in-waitlist')),
});
});

it('can remove self', async () => {
// This is a bit janky, but we need a user to take up the DJ spot,
// so the user we remove is still in the waitlist.
const dj = await uw.test.createUser();
await uw.test.connectToWebSocketAs(user);
await uw.test.connectToWebSocketAs(dj);

await uw.acl.allow(dj, ['user']);
await uw.acl.allow(user, ['user']);

await createTestPlaylistItem(dj);
await createTestPlaylistItem(user);

await uw.waitlist.addUser(dj.id);
await uw.waitlist.addUser(user.id);

const prevWaitlist = await supertest(uw.server)
.get('/api/waitlist')
.expect(200);
sinon.assert.match(prevWaitlist.body.data, [user.id]);

const token = await uw.test.createTestSessionToken(user);

await supertest(uw.server)
.delete(`/api/waitlist/${user.id}`)
.set('Cookie', `uwsession=${token}`)
.expect(200);

const nextWaitlist = await supertest(uw.server)
.get('/api/waitlist')
.expect(200);
sinon.assert.match(nextWaitlist.body.data, []);
});
});
});

0 comments on commit 47d7cde

Please sign in to comment.