Skip to content

Commit 27dee6e

Browse files
authored
Merge pull request #609 from TaloDev/load-org-games-in-verify2fa
Load organisation.games in verify2fa, add more testing
2 parents 6c627b7 + d628045 commit 27dee6e

File tree

8 files changed

+37
-17
lines changed

8 files changed

+37
-17
lines changed

src/services/public/user-public.service.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ export default class UserPublicService extends Service {
288288
const { code, userId } = req.body
289289
const em: EntityManager = req.ctx.em
290290

291-
const user = await em.getRepository(User).findOneOrFail(userId)
291+
const user = await em.repo(User).findOneOrFail(userId, { populate: ['organisation.games'] })
292292

293293
const redis: Redis = req.ctx.redis
294294
const hasSession = (await redis.get(`2fa:${user.id}`)) === 'true'

tests/services/_public/user-public/login.test.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
import request from 'supertest'
22
import UserFactory from '../../../fixtures/UserFactory'
33
import { differenceInMinutes, sub } from 'date-fns'
4+
import createOrganisationAndGame from '../../../utils/createOrganisationAndGame'
45

56
describe('User public service - login', () => {
67
it('should let a user login', async () => {
7-
const user = await new UserFactory().loginable().one()
8-
user.lastSeenAt = new Date(2020, 1, 1)
8+
const [organisation] = await createOrganisationAndGame()
9+
const user = await new UserFactory().loginable().state(() => ({
10+
organisation,
11+
lastSeenAt: new Date(2020, 1, 1)
12+
})).one()
913
await em.persistAndFlush(user)
1014

1115
const res = await request(app)
@@ -16,7 +20,7 @@ describe('User public service - login', () => {
1620
expect(res.body.accessToken).toBeTruthy()
1721
expect(res.body.user).toBeTruthy()
1822
expect(res.body.user.organisation).toBeTruthy()
19-
expect(res.body.user.organisation.games).toEqual([])
23+
expect(res.body.user.organisation.games).toHaveLength(1)
2024
expect(new Date(res.body.user.lastSeenAt).getDay()).toEqual(new Date().getDay())
2125
})
2226

tests/services/_public/user-public/refresh.test.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
import request from 'supertest'
22
import UserSession from '../../../../src/entities/user-session'
33
import UserFactory from '../../../fixtures/UserFactory'
4+
import createOrganisationAndGame from '../../../utils/createOrganisationAndGame'
45

56
describe('User public service - refresh', () => {
67
it('should let a user refresh their session if they have one', async () => {
7-
const user = await new UserFactory().one()
8-
user.lastSeenAt = new Date(2020, 1, 1)
8+
const [organisation] = await createOrganisationAndGame()
9+
const user = await new UserFactory().state(() => ({
10+
organisation,
11+
lastSeenAt: new Date(2020, 1, 1)
12+
})).one()
913
const session = new UserSession(user)
1014
await em.persistAndFlush(session)
1115

@@ -17,7 +21,7 @@ describe('User public service - refresh', () => {
1721
expect(res.body.accessToken).toBeTruthy()
1822
expect(res.body.user).toBeTruthy()
1923
expect(res.body.user.organisation).toBeTruthy()
20-
expect(res.body.user.organisation.games).toEqual([])
24+
expect(res.body.user.organisation.games).toHaveLength(1)
2125

2226
expect(new Date(res.body.user.lastSeenAt).getDay()).toEqual(new Date().getDay())
2327
})

tests/services/_public/user-public/use-recovery-code.test.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import createUserAndToken from '../../../utils/createUserAndToken'
55
import UserTwoFactorAuth from '../../../../src/entities/user-two-factor-auth'
66
import User from '../../../../src/entities/user'
77
import generateRecoveryCodes from '../../../../src/lib/auth/generateRecoveryCodes'
8+
import createOrganisationAndGame from '../../../utils/createOrganisationAndGame'
89

910
async function setTwoFactorAuthSession(user: User) {
1011
await redis.set(`2fa:${user.id}`, 'true')
@@ -15,9 +16,10 @@ async function removeTwoFactorAuthSession(user: User) {
1516
}
1617

1718
async function createUserWithTwoFactorAuth(em: EntityManager): Promise<[string, User]> {
19+
const [organisation] = await createOrganisationAndGame()
1820
const [token, user] = await createUserAndToken({
1921
twoFactorAuth: new UserTwoFactorAuth('blah')
20-
})
22+
}, organisation)
2123

2224
user.twoFactorAuth!.enabled = true
2325
user.recoveryCodes = new Collection<UserRecoveryCode>(user, generateRecoveryCodes(user))
@@ -39,7 +41,7 @@ describe('User public service - use recovery code', () => {
3941

4042
expect(res.body.user).toBeTruthy()
4143
expect(res.body.user.organisation).toBeTruthy()
42-
expect(res.body.user.organisation.games).toEqual([])
44+
expect(res.body.user.organisation.games).toHaveLength(1)
4345

4446
expect(res.body.accessToken).toBeTruthy()
4547
expect(res.body.newRecoveryCodes).toBeUndefined()

tests/services/_public/user-public/verify-2fa.test.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,15 @@ import request from 'supertest'
22
import { authenticator } from '@otplib/preset-default'
33
import UserTwoFactorAuth from '../../../../src/entities/user-two-factor-auth'
44
import createUserAndToken from '../../../utils/createUserAndToken'
5+
import createOrganisationAndGame from '../../../utils/createOrganisationAndGame'
56

67
describe('User public service - verify 2fa', () => {
78
it('should let users verify their 2fa code and login', async () => {
89
const twoFactorAuth = new UserTwoFactorAuth('blah')
910
twoFactorAuth.enabled = true
10-
const [token, user] = await createUserAndToken({ twoFactorAuth })
11+
12+
const [organisation] = await createOrganisationAndGame()
13+
const [token, user] = await createUserAndToken({ twoFactorAuth }, organisation)
1114

1215
await redis.set(`2fa:${user.id}`, 'true')
1316

@@ -21,7 +24,7 @@ describe('User public service - verify 2fa', () => {
2124

2225
expect(res.body.user).toBeTruthy()
2326
expect(res.body.user.organisation).toBeTruthy()
24-
expect(res.body.user.organisation.games).toEqual([])
27+
expect(res.body.user.organisation.games).toHaveLength(1)
2528

2629
expect(res.body.accessToken).toBeTruthy()
2730

tests/services/user/confirm-2fa.test.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@ import request from 'supertest'
33
import UserTwoFactorAuth from '../../../src/entities/user-two-factor-auth'
44
import { authenticator } from '@otplib/preset-default'
55
import createUserAndToken from '../../utils/createUserAndToken'
6+
import createOrganisationAndGame from '../../utils/createOrganisationAndGame'
67

78
describe('User service - confirm 2fa', () => {
89
it('should let users confirm enabling 2fa', async () => {
10+
const [organisation] = await createOrganisationAndGame()
911
const [token, user] = await createUserAndToken({
1012
twoFactorAuth: new UserTwoFactorAuth('blah')
11-
})
13+
}, organisation)
1214

1315
authenticator.check = vi.fn().mockReturnValueOnce(true)
1416

@@ -20,7 +22,7 @@ describe('User service - confirm 2fa', () => {
2022

2123
expect(res.body.user).toBeTruthy()
2224
expect(res.body.user.organisation).toBeTruthy()
23-
expect(res.body.user.organisation.games).toEqual([])
25+
expect(res.body.user.organisation.games).toHaveLength(1)
2426

2527
expect(res.body.recoveryCodes).toHaveLength(8)
2628

tests/services/user/confirm-email.test.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
import request from 'supertest'
22
import UserAccessCode from '../../../src/entities/user-access-code'
33
import createUserAndToken from '../../utils/createUserAndToken'
4+
import createOrganisationAndGame from '../../utils/createOrganisationAndGame'
45

56
describe('User service - confirm email', () => {
67
it('should let a user confirm their email', async () => {
7-
const [token, user] = await createUserAndToken()
8+
const [organisation] = await createOrganisationAndGame()
9+
const [token, user] = await createUserAndToken({}, organisation)
810

911
const date = new Date()
1012
date.setDate(date.getDate() + 1)
@@ -19,7 +21,7 @@ describe('User service - confirm email', () => {
1921

2022
expect(res.body.user.emailConfirmed).toBe(true)
2123
expect(res.body.user.organisation).toBeTruthy()
22-
expect(res.body.user.organisation.games).toEqual([])
24+
expect(res.body.user.organisation.games).toHaveLength(1)
2325

2426
const updatedAccessCode = await em.getRepository(UserAccessCode).findOne({ code: accessCode.code })
2527
expect(updatedAccessCode).toBeNull()

tests/services/user/disable-2fa.test.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,15 @@ import request from 'supertest'
22
import UserTwoFactorAuth from '../../../src/entities/user-two-factor-auth'
33
import UserRecoveryCode from '../../../src/entities/user-recovery-code'
44
import createUserAndToken from '../../utils/createUserAndToken'
5+
import createOrganisationAndGame from '../../utils/createOrganisationAndGame'
56

67
describe('User service - disable 2fa', () => {
78
it('should let users disable 2fa', async () => {
89
const twoFactorAuth = new UserTwoFactorAuth('blah')
910
twoFactorAuth.enabled = true
10-
const [token, user] = await createUserAndToken({ twoFactorAuth })
11+
12+
const [organisation] = await createOrganisationAndGame()
13+
const [token, user] = await createUserAndToken({ twoFactorAuth }, organisation)
1114

1215
const res = await request(app)
1316
.post('/users/2fa/disable')
@@ -17,7 +20,7 @@ describe('User service - disable 2fa', () => {
1720

1821
expect(res.body.user.has2fa).toBe(false)
1922
expect(res.body.user.organisation).toBeTruthy()
20-
expect(res.body.user.organisation.games).toEqual([])
23+
expect(res.body.user.organisation.games).toHaveLength(1)
2124

2225
const recoveryCodes = await em.getRepository(UserRecoveryCode).find({ user })
2326
expect(recoveryCodes).toHaveLength(0)

0 commit comments

Comments
 (0)