From 1a28d411d9c964c569ff8b94efd622e5fcdb358a Mon Sep 17 00:00:00 2001 From: tudor <7089284+tudddorrr@users.noreply.github.com> Date: Fri, 26 Jul 2024 16:33:51 +0100 Subject: [PATCH] update email with verification toggle --- src/services/api/player-auth-api.service.ts | 3 ++ .../toggleVerification.test.ts | 39 +++++++++++++++++++ 2 files changed, 42 insertions(+) diff --git a/src/services/api/player-auth-api.service.ts b/src/services/api/player-auth-api.service.ts index 07778cd6..c8db8a11 100644 --- a/src/services/api/player-auth-api.service.ts +++ b/src/services/api/player-auth-api.service.ts @@ -522,6 +522,9 @@ export default class PlayerAuthAPIService extends APIService { } alias.player.auth.verificationEnabled = Boolean(verificationEnabled) + if (email?.trim()) { + alias.player.auth.email = email + } createPlayerAuthActivity(req, alias.player, { type: PlayerAuthActivityType.VERFICIATION_TOGGLED, diff --git a/tests/services/_api/player-auth-api/toggleVerification.test.ts b/tests/services/_api/player-auth-api/toggleVerification.test.ts index 1578fbe3..99de10ce 100644 --- a/tests/services/_api/player-auth-api/toggleVerification.test.ts +++ b/tests/services/_api/player-auth-api/toggleVerification.test.ts @@ -125,6 +125,45 @@ describe('Player auth API service - toggle verification', () => { expect(activity).not.toBeNull() }) + it('should update the email of the player if one is sent', async () => { + const [apiKey, token] = await createAPIKeyAndToken([APIKeyScope.READ_PLAYERS, APIKeyScope.WRITE_PLAYERS]) + + const player = await new PlayerFactory([apiKey.game]).state('with talo alias').with(async () => ({ + auth: await new PlayerAuthFactory().with(async () => ({ + password: await bcrypt.hash('password', 10), + email: null, + verificationEnabled: false + })).one() + })).one() + const alias = player.aliases[0] + await (global.em).persistAndFlush(player) + + const sessionToken = await player.auth.createSession(alias) + await (global.em).flush() + + await request(global.app) + .patch('/v1/players/auth/toggle_verification') + .send({ currentPassword: 'password', verificationEnabled: true, email: 'bozzz@mail.com' }) + .auth(token, { type: 'bearer' }) + .set('x-talo-player', player.id) + .set('x-talo-alias', String(alias.id)) + .set('x-talo-session', sessionToken) + .expect(204) + + await (global.em).refresh(player.auth) + expect(player.auth.verificationEnabled).toBe(true) + expect(player.auth.email).toBe('bozzz@mail.com') + + const activity = await (global.em).getRepository(PlayerAuthActivity).findOne({ + type: PlayerAuthActivityType.VERFICIATION_TOGGLED, + player: player.id, + extra: { + verificationEnabled: true + } + }) + expect(activity).not.toBeNull() + }) + it('should not toggle verification if the current password is incorrect', async () => { const [apiKey, token] = await createAPIKeyAndToken([APIKeyScope.READ_PLAYERS, APIKeyScope.WRITE_PLAYERS])