Skip to content

Commit

Permalink
fix: lint
Browse files Browse the repository at this point in the history
  • Loading branch information
DevNono committed Aug 24, 2023
1 parent 1d61a37 commit c9ad1b9
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 60 deletions.
3 changes: 2 additions & 1 deletion src/controllers/partners/getPartners.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { NextFunction, Request, Response } from 'express';
import { pick } from 'lodash';
import { fetchPartners } from '../../operations/partner';
import { success } from '../../utils/responses';

Expand All @@ -13,7 +14,7 @@ export default [
result = result.filter((partner) => partner.display);

// Don't pick the display field
const partners = result.map(({ display, ...rest }) => rest);
const partners = pick(result, ['id', 'name', 'link']);

return success(response, partners);
} catch (error) {
Expand Down
3 changes: 3 additions & 0 deletions src/utils/filters.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { pick } from 'lodash';
import { Partner } from '@prisma/client';
import {
CartItem,
CartWithCartItems,
Expand Down Expand Up @@ -148,3 +149,5 @@ export const filterTournamentRestricted = (tournament: Tournament) => {
teams: tournament.teams.map(filterTeamRestricted),
};
};

export const filterPartnerRestricted = (partner: Partner) => pick(partner, 'id', 'name', 'link');
14 changes: 3 additions & 11 deletions tests/admin/auth/login.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,15 @@ import { setLoginAllowed } from '../../../src/operations/settings';
import database from '../../../src/services/database';
import { sandbox } from '../../setup';
import { createFakeUser } from '../../utils';
import { Partner } from '@prisma/client';
import { serializePermissions } from '../../../src/utils/helpers';

describe('POST /admin/auth/login', () => {
const password = 'bonjour123456';
let user: User;
let nonAdminUser: User;
let admin: User;
let adminToken: string;

before(async () => {
user = await createFakeUser({ password });
await setLoginAllowed(false);

admin = await createFakeUser({ type: UserType.orga, permissions: [Permission.admin] });
nonAdminUser = await createFakeUser();
adminToken = userUtils.generateToken(admin);
});

after(async () => {
Expand All @@ -45,9 +37,9 @@ describe('POST /admin/auth/login', () => {
id: user.id,
},
data: {
permissions: serializePermissions([ Permission.anim ]),
}
})
permissions: serializePermissions([Permission.anim]),
},
});
});

it('should return an error as incorrect body', async () => {
Expand Down
21 changes: 9 additions & 12 deletions tests/admin/partners/getPartners.test.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import { expect } from 'chai';
import request from 'supertest';
import { faker } from '@faker-js/faker';
import { Partner } from '@prisma/client';
import app from '../../../src/app';
import { sandbox } from '../../setup';
import * as partnerOperations from '../../../src/operations/partner';
import database from '../../../src/services/database';
import { Error, Permission, User, UserType } from '../../../src/types';
import { faker } from '@faker-js/faker';
import nanoid from '../../../src/utils/nanoid';
import { generateToken } from '../../../src/utils/users';
import { createFakeUser } from '../../utils';
import { Partner } from '@prisma/client';

describe('GET /admin/partners', () => {
let nonAdminUser: User;
Expand All @@ -24,7 +24,7 @@ describe('GET /admin/partners', () => {
before(async () => {
const partnersList = [] as Partner[];

for (let i = 0; i < 10; i++) {
for (let index = 0; index < 10; index++) {
partnersList.push({
id: nanoid(),
name: faker.company.name(),
Expand All @@ -43,7 +43,7 @@ describe('GET /admin/partners', () => {
});

it('should error as the user is not authenticated', () =>
request(app).get(`/admin/partners`).expect(401, { error: Error.Unauthenticated }));
request(app).get(`/admin/partners`).expect(401, { error: Error.Unauthenticated }));

it('should error as the user is not an administrator', () => {
const userToken = generateToken(nonAdminUser);
Expand All @@ -56,10 +56,12 @@ describe('GET /admin/partners', () => {
it('should fail with an internal server error', async () => {
sandbox.stub(partnerOperations, 'fetchPartners').throws('Unexpected error');

await request(app).get('/admin/partners').set('Authorization', `Bearer ${adminToken}`).expect(500, { error: Error.InternalServerError });
await request(app)
.get('/admin/partners')
.set('Authorization', `Bearer ${adminToken}`)
.expect(500, { error: Error.InternalServerError });
});


it('should return 200 with an array of partners', async () => {
const partners = await database.partner.findMany();

Expand All @@ -78,12 +80,7 @@ describe('GET /admin/partners', () => {
expect(response.body).to.have.lengthOf(partners.length);
// Not to have tournaments[0] because it has display false
expect(response.body).not.to.have.deep.members([partners[0]]);
expect(response.body[0]).to.have.all.keys([
'id',
'name',
'link',
'display',
]);
expect(response.body[0]).to.have.all.keys(['id', 'name', 'link', 'display']);
expect(response.body[0].name).to.be.a('string');
expect(response.body[0].link).to.be.a('string');
});
Expand Down
35 changes: 24 additions & 11 deletions tests/admin/settings/updateSetting.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import { sandbox } from '../../setup';
import * as settingsOperations from '../../../src/operations/settings';
import database from '../../../src/services/database';
import { Error, Permission, User, UserType } from '../../../src/types';
import { faker } from '@faker-js/faker';
import nanoid from '../../../src/utils/nanoid';
import { setLoginAllowed, setShopAllowed, setTrombiAllowed } from '../../../src/operations/settings';
import { createFakeUser } from '../../utils';
import { generateToken } from '../../../src/utils/users';
Expand All @@ -31,9 +29,9 @@ describe('PATCH /admin/settings', () => {
nonAdminUser = await createFakeUser();
adminToken = generateToken(admin);
});

it('should error as the user is not authenticated', () =>
request(app).patch(`/admin/settings/login`).send({ value: true }).expect(401, { error: Error.Unauthenticated }));
request(app).patch(`/admin/settings/login`).send({ value: true }).expect(401, { error: Error.Unauthenticated }));

it('should error as the user is not an administrator', () => {
const userToken = generateToken(nonAdminUser);
Expand All @@ -47,20 +45,35 @@ describe('PATCH /admin/settings', () => {
it('should fail with an internal server error', async () => {
sandbox.stub(settingsOperations, 'setLoginAllowed').throws('Unexpected error');

await request(app).patch('/admin/settings/login').send({ value: true }).set('Authorization', `Bearer ${adminToken}`).expect(500, { error: Error.InternalServerError });
await request(app)
.patch('/admin/settings/login')
.send({ value: true })
.set('Authorization', `Bearer ${adminToken}`)
.expect(500, { error: Error.InternalServerError });
});


it('should throw an error as setting\'s id does not exist', async () => {
await request(app).patch('/admin/settings/aaaaaa').send({ value: true }).set('Authorization', `Bearer ${adminToken}`).expect(404, { error: Error.NotFound });
it("should throw an error as setting's id does not exist", async () => {
await request(app)
.patch('/admin/settings/aaaaaa')
.send({ value: true })
.set('Authorization', `Bearer ${adminToken}`)
.expect(404, { error: Error.NotFound });
});

it('should throw an error as setting\'s value is not a boolean', async () => {
await request(app).patch('/admin/settings/login').send({ value: 'hello' }).set('Authorization', `Bearer ${adminToken}`).expect(400, { error: Error.InvalidPermission });
it("should throw an error as setting's value is not a boolean", async () => {
await request(app)
.patch('/admin/settings/login')
.send({ value: 'hello' })
.set('Authorization', `Bearer ${adminToken}`)
.expect(400, { error: Error.InvalidPermission });
});

it('should successfully update the settings', async () => {
await request(app).patch('/admin/settings/login').send({ value: true }).expect(200, { id: "login", value: true });
await request(app)
.patch('/admin/settings/login')
.send({ value: true })
.set('Authorization', `Bearer ${adminToken}`)
.expect(200, { id: 'login', value: true });

const login = await settingsOperations.fetchSetting('login');

Expand Down
39 changes: 22 additions & 17 deletions tests/admin/tournaments/getTournaments.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ describe('GET /admin/tournaments', () => {
let nonAdminUser: User;
let admin: User;
let adminToken: string;

after(async () => {
await database.team.deleteMany();
await database.user.deleteMany();
Expand All @@ -32,7 +32,7 @@ describe('GET /admin/tournaments', () => {
});

it('should error as the user is not authenticated', () =>
request(app).get(`/admin/tournaments`).expect(401, { error: Error.Unauthenticated }));
request(app).get(`/admin/tournaments`).expect(401, { error: Error.Unauthenticated }));

it('should error as the user is not an administrator', () => {
const userToken = generateToken(nonAdminUser);
Expand All @@ -45,7 +45,10 @@ describe('GET /admin/tournaments', () => {
it('should fail with an internal server error', async () => {
sandbox.stub(tournamentOperations, 'fetchTournaments').throws('Unexpected error');

await request(app).get('/admin/tournaments').set('Authorization', `Bearer ${adminToken}`).expect(500, { error: Error.InternalServerError });
await request(app)
.get('/admin/tournaments')
.set('Authorization', `Bearer ${adminToken}`)
.expect(500, { error: Error.InternalServerError });
});

it('should return 200 with an array of tournaments', async () => {
Expand All @@ -63,7 +66,10 @@ describe('GET /admin/tournaments', () => {

await createFakeTeam({ members: tournaments[0].playersPerTeam, tournament: tournaments[0].id });

const response = await request(app).get('/admin/tournaments').set('Authorization', `Bearer ${adminToken}`).expect(200);
const response = await request(app)
.get('/admin/tournaments')
.set('Authorization', `Bearer ${adminToken}`)
.expect(200);

expect(response.body).to.have.lengthOf(tournaments.length);
// Not to have tournaments[0] because it has display false
Expand All @@ -84,16 +90,14 @@ describe('GET /admin/tournaments', () => {
'display',
'displayCashprize',
'displayCasters',
"discordRespoRoleId",
"discordRoleId",
"discordTextCategoryId",
"discordVocalCategoryId",
'discordRespoRoleId',
'discordRoleId',
'discordTextCategoryId',
'discordVocalCategoryId',
]);
expect(response.body[0].lockedTeamsCount).to.be.a('number');
expect(response.body[0].cashprize).to.be.a('number');
expect(response.body[0].casters).to.be.a('array');
expect(response.body[0].teams[0].players[0].id).to.be.a('string');
expect(response.body[0].teams[0].players[0].firstname).to.be.undefined;
});

it('should return 200 with an array of tournaments with the right fields', async () => {
Expand All @@ -110,7 +114,10 @@ describe('GET /admin/tournaments', () => {
},
});

const response = await request(app).get('/admin/tournaments').set('Authorization', `Bearer ${adminToken}`).expect(200);
const response = await request(app)
.get('/admin/tournaments')
.set('Authorization', `Bearer ${adminToken}`)
.expect(200);

expect(response.body).to.have.lengthOf(tournaments.length);
expect(response.body[0]).to.have.all.keys([
Expand All @@ -129,15 +136,13 @@ describe('GET /admin/tournaments', () => {
'display',
'displayCashprize',
'displayCasters',
"discordRespoRoleId",
"discordRoleId",
"discordTextCategoryId",
"discordVocalCategoryId",
'discordRespoRoleId',
'discordRoleId',
'discordTextCategoryId',
'discordVocalCategoryId',
]);
expect(response.body[1].lockedTeamsCount).to.be.a('number');
expect(response.body[0].cashprize).to.be.a('number');
expect(response.body[0].casters).to.be.a('array');
expect(response.body[1].teams[0].players[0].id).to.be.a('string');
expect(response.body[1].teams[0].players[0].firstname).to.be.undefined;
});
});
12 changes: 4 additions & 8 deletions tests/partners/getPartners.test.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { expect } from 'chai';
import request from 'supertest';
import { faker } from '@faker-js/faker';
import { Partner } from '@prisma/client';
import app from '../../src/app';
import { sandbox } from '../setup';
import * as partnerOperations from '../../src/operations/partner';
import database from '../../src/services/database';
import { Error } from '../../src/types';
import { faker } from '@faker-js/faker';
import nanoid from '../../src/utils/nanoid';
import { Partner } from '@prisma/client';

describe('GET /partners', () => {
after(async () => {
Expand All @@ -17,7 +17,7 @@ describe('GET /partners', () => {
before(async () => {
const partnersList = [] as Partner[];

for (let i = 0; i < 10; i++) {
for (let index = 0; index < 10; index++) {
partnersList.push({
id: nanoid(),
name: faker.company.name(),
Expand Down Expand Up @@ -55,11 +55,7 @@ describe('GET /partners', () => {
expect(response.body).to.have.lengthOf(partners.length - 1);
// Not to have tournaments[0] because it has display false
expect(response.body).not.to.have.deep.members([partners[0]]);
expect(response.body[0]).to.have.all.keys([
'id',
'name',
'link'
]);
expect(response.body[0]).to.have.all.keys(['id', 'name', 'link']);
expect(response.body[0].name).to.be.a('string');
expect(response.body[0].link).to.be.a('string');
});
Expand Down

0 comments on commit c9ad1b9

Please sign in to comment.