Skip to content

Commit

Permalink
test: added some tests
Browse files Browse the repository at this point in the history
* added tests to verify a team in the queue would be moved to the locked teams
if necessary with routes :
	- DELETE /teams/current/users/:userId
	- DELETE /teams/current/users/current
	- POST /admin/carts/:cartId/refund
  • Loading branch information
Teddy Roncin committed Jun 29, 2023
1 parent b5ad922 commit 06cf183
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 12 deletions.
31 changes: 27 additions & 4 deletions tests/admin/carts/refund.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,43 @@ import { expect } from 'chai';
import app from '../../../src/app';
import { createFakeTeam, createFakeUser } from '../../utils';
import database from '../../../src/services/database';
import { Cart, Error, Permission, User, TransactionState } from '../../../src/types';
import { Cart, Error, Permission, User, TransactionState, Team } from '../../../src/types';
import * as cartOperations from '../../../src/operations/carts';
import * as teamOperations from '../../../src/operations/team';
import * as tournamentOperations from '../../../src/operations/tournament';
import * as userOperations from '../../../src/operations/user';
import { sandbox } from '../../setup';
import { generateToken } from '../../../src/utils/users';

describe('POST /admin/carts/:cartId/refund', () => {
// eslint-disable-next-line func-names
describe('POST /admin/carts/:cartId/refund', function () {
// Setup is slow
this.timeout(30000);

let user: User;
let admin: User;
let adminToken: string;
let playerCart: Cart;
let coachCart: Cart;
let waitingTeam: Team;

before(async () => {
const tournament = await tournamentOperations.fetchTournament('lol');
user = await createFakeUser();
const coach = await createFakeUser();
admin = await createFakeUser({ permissions: [Permission.admin] });
adminToken = generateToken(admin);
const team = await createFakeTeam({ members: tournament.playersPerTeam - 2, paid: true, locked: true });
const team = await createFakeTeam({ members: tournament.playersPerTeam - 1, paid: true, locked: true, tournament: 'lol' });
await teamOperations.joinTeam(team.id, user, 'player');
await teamOperations.joinTeam(team.id, coach, 'coach');

// Fill the tournament
for (let index = 0; index < tournament.placesLeft - 1; index++) {
await createFakeTeam({ members: tournament.playersPerTeam, paid: true, locked: true, tournament: 'lol' });
}
waitingTeam = await createFakeTeam({ members: tournament.playersPerTeam, paid: true, tournament: 'lol' });
await teamOperations.lockTeam(waitingTeam.id);

// Refresh the user
user = await userOperations.fetchUser(user.id);

Expand Down Expand Up @@ -104,9 +117,14 @@ describe('POST /admin/carts/:cartId/refund', () => {
team = await teamOperations.fetchTeam(user.teamId);
expect(team.lockedAt).to.be.not.null;
expect(team.enteredQueueAt).to.be.null;

// Verify the waiting team has not been unlocked
waitingTeam = await teamOperations.fetchTeam(waitingTeam.id);
expect(waitingTeam.lockedAt).to.be.null;
expect(waitingTeam.enteredQueueAt).to.be.not.null;
});

it('should refund the cart, and unlock the team', async () => {
it('should refund the cart, unlock the team, and make the waiting team locked', async () => {
await database.cart.update({
data: { transactionState: TransactionState.paid },
where: { id: playerCart.id },
Expand All @@ -121,5 +139,10 @@ describe('POST /admin/carts/:cartId/refund', () => {
const team = await teamOperations.fetchTeam(user.teamId);
expect(team.lockedAt).to.be.null;
expect(team.enteredQueueAt).to.be.null;

// Verify the waiting team has been locked
waitingTeam = await teamOperations.fetchTeam(waitingTeam.id);
expect(waitingTeam.lockedAt).to.be.not.null;
expect(waitingTeam.enteredQueueAt).to.be.null;
});
});
29 changes: 28 additions & 1 deletion tests/teams/kickUser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,11 @@ describe('DELETE /teams/current/users/:userId', () => {

let captainToken: string;
let team: Team;
let waitingTeam: Team;

before(async () => {
const tournament = await tournamentOperations.fetchTournament('rl');
team = await createFakeTeam({ members: tournament.playersPerTeam, paid: true, locked: true });
team = await createFakeTeam({ members: tournament.playersPerTeam, paid: true, locked: true, tournament: 'rl' });

// Find a user that is not a captain
userToKick = team.players.find((player) => player.id !== team.captainId);
Expand All @@ -29,6 +30,22 @@ describe('DELETE /teams/current/users/:userId', () => {

const captain = getCaptain(team);
captainToken = generateToken(captain);

// Fill the tournament
const promises = [];
for (let index = 0; index < tournament.placesLeft - 1; index++) {
promises.push(createFakeTeam({ members: tournament.playersPerTeam, locked: true, paid: true, tournament: 'rl' }));
}
await Promise.all(promises);

// Create a team that is in the waiting list
waitingTeam = await createFakeTeam({
members: tournament.playersPerTeam,
locked: false,
paid: true,
tournament: 'rl',
});
await teamOperations.lockTeam(waitingTeam.id);
});

after(async () => {
Expand Down Expand Up @@ -116,6 +133,11 @@ describe('DELETE /teams/current/users/:userId', () => {

// Rejoin the team for next tests
await teamOperations.joinTeam(team.id, kickedUser, UserType.player);

// Verify the waiting team is still in the queue
waitingTeam = await teamOperations.fetchTeam(waitingTeam.id);
expect(waitingTeam.lockedAt).to.be.null;
expect(waitingTeam.enteredQueueAt).to.be.not.null;
});

it('should successfully kick the user (as the captain of the team and as a coach), and unlock the team', async () => {
Expand All @@ -136,6 +158,11 @@ describe('DELETE /teams/current/users/:userId', () => {
const databaseTeam = await teamOperations.fetchTeam(team.id);
expect(databaseTeam.lockedAt).to.be.null;
expect(databaseTeam.enteredQueueAt).to.be.null;

// Verify the waiting team has been locked
waitingTeam = await teamOperations.fetchTeam(waitingTeam.id);
expect(waitingTeam.lockedAt).to.be.not.null;
expect(waitingTeam.enteredQueueAt).to.be.null;
});

it('should fail as the user has already been kicked', async () => {
Expand Down
45 changes: 38 additions & 7 deletions tests/teams/leaveTeam.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import { createFakeTeam, createFakeUser } from '../utils';
import { generateToken } from '../../src/utils/users';
import { fetchUser } from '../../src/operations/user';
import { getCaptain } from '../../src/utils/teams';
import { fetchTeam } from '../../src/operations/team';

describe('DELETE /teams/current/users/current', () => {
let user: User;
Expand All @@ -20,10 +19,11 @@ describe('DELETE /teams/current/users/current', () => {
let coachToken: string;

let team: Team;
let waitingTeam: Team;

before(async () => {
const tournament = await tournamentOperations.fetchTournament('rl');
team = await createFakeTeam({ members: tournament.playersPerTeam, locked: true, paid: true });
team = await createFakeTeam({ members: tournament.playersPerTeam, locked: true, paid: true, tournament: 'rl' });

// Find a user that is not a captain
user = team.players.find((player) => player.id !== team.captainId);
Expand All @@ -32,7 +32,23 @@ describe('DELETE /teams/current/users/current', () => {
// Create a coach
coach = await createFakeUser({ type: UserType.coach });
coachToken = generateToken(coach);
teamOperations.joinTeam(team.id, coach, UserType.coach);
await teamOperations.joinTeam(team.id, coach, UserType.coach);

// Fill the tournament
const promises = [];
for (let index = 0; index < tournament.placesLeft - 1; index++) {
promises.push(createFakeTeam({ members: tournament.playersPerTeam, locked: true, paid: true, tournament: 'rl' }));
}
await Promise.all(promises);

// Create a team that is in the waiting list
waitingTeam = await createFakeTeam({
members: tournament.playersPerTeam,
locked: false,
paid: true,
tournament: 'rl',
});
await teamOperations.lockTeam(waitingTeam.id);
});

after(async () => {
Expand Down Expand Up @@ -66,20 +82,30 @@ describe('DELETE /teams/current/users/current', () => {

it('should successfully quit the team, but not unlock the team as the user is a coach', async () => {
// Verify the team is locked
let databaseTeam = await fetchTeam(team.id);
let databaseTeam = await teamOperations.fetchTeam(team.id);
expect(databaseTeam.lockedAt).to.be.not.null;
expect(databaseTeam.enteredQueueAt).to.be.null;

// Make the request
await request(app).delete('/teams/current/users/current').set('Authorization', `Bearer ${coachToken}`).expect(204);

// Verify the user has been removed from the team
const removedUser = await fetchUser(coach.id);
expect(removedUser.teamId).to.be.null;
expect(removedUser.type).to.be.null;

// Verify the team is still locked
databaseTeam = await fetchTeam(team.id);
databaseTeam = await teamOperations.fetchTeam(team.id);
expect(databaseTeam.lockedAt).to.be.not.null;
expect(databaseTeam.enteredQueueAt).to.be.null;

// Verify the waiting team is still in the queue
waitingTeam = await teamOperations.fetchTeam(waitingTeam.id);
expect(waitingTeam.lockedAt).to.be.null;
expect(waitingTeam.enteredQueueAt).to.be.not.null;
});

it('should successfully quit the team and unlock the team', async () => {
it('should successfully quit the team and unlock the team, and lock the waiting team', async () => {
await request(app).delete('/teams/current/users/current').set('Authorization', `Bearer ${token}`).expect(204);

const removedUser = await fetchUser(user.id);
Expand All @@ -88,10 +114,15 @@ describe('DELETE /teams/current/users/current', () => {
expect(removedUser.type).to.be.null;

// Verify the team has been unlocked
const databaseTeam = await fetchTeam(team.id);
const databaseTeam = await teamOperations.fetchTeam(team.id);
expect(databaseTeam.lockedAt).to.be.null;
expect(databaseTeam.enteredQueueAt).to.be.null;

// Verify the waiting team has been locked
waitingTeam = await teamOperations.fetchTeam(waitingTeam.id);
expect(waitingTeam.lockedAt).to.be.not.null;
expect(waitingTeam.enteredQueueAt).to.be.null;

// Rejoin the team for next tests
await teamOperations.joinTeam(team.id, removedUser, UserType.player);
});
Expand Down

0 comments on commit 06cf183

Please sign in to comment.