diff --git a/backend/src/graphql/resolver/UserResolver.test.ts b/backend/src/graphql/resolver/UserResolver.test.ts index 6e9a21b86..36a22c386 100644 --- a/backend/src/graphql/resolver/UserResolver.test.ts +++ b/backend/src/graphql/resolver/UserResolver.test.ts @@ -19,6 +19,7 @@ import { setUserRole, deleteUser, unDeleteUser, + sendActivationEmail, } from '@/seeds/graphql/mutations' import { verifyLogin, queryOptIn, searchAdminUsers, searchUsers } from '@/seeds/graphql/queries' import { GraphQLError } from 'graphql' @@ -176,7 +177,7 @@ describe('UserResolver', () => { }) }) - it('stores the register event in the database', async () => { + it('stores the REGISTER event in the database', async () => { const userConatct = await UserContact.findOneOrFail( { email: 'peter@lustig.de' }, { relations: ['user'] }, @@ -209,7 +210,7 @@ describe('UserResolver', () => { }) }) - it('stores the send confirmation event in the database', () => { + it('stores the SEND_CONFIRMATION_EMAIL event in the database', () => { expect(EventProtocol.find()).resolves.toContainEqual( expect.objectContaining({ type: EventProtocolType.SEND_CONFIRMATION_EMAIL, @@ -250,7 +251,7 @@ describe('UserResolver', () => { ) }) - it('stores the send account multi registration email event in the database', async () => { + it('stores the SEND_ACCOUNT_MULTIREGISTRATION_EMAIL event in the database', async () => { const userConatct = await UserContact.findOneOrFail( { email: 'peter@lustig.de' }, { relations: ['user'] }, @@ -354,7 +355,7 @@ describe('UserResolver', () => { ) }) - it('stores the account activated event in the database', () => { + it('stores the ACTIVATE_ACCOUNT event in the database', () => { expect(EventProtocol.find()).resolves.toContainEqual( expect.objectContaining({ type: EventProtocolType.ACTIVATE_ACCOUNT, @@ -363,7 +364,7 @@ describe('UserResolver', () => { ) }) - it('stores the redeem register event in the database', () => { + it('stores the REDEEM_REGISTER event in the database', () => { expect(EventProtocol.find()).resolves.toContainEqual( expect.objectContaining({ type: EventProtocolType.REDEEM_REGISTER, @@ -447,7 +448,7 @@ describe('UserResolver', () => { ) }) - it('stores the redeem register event in the database', async () => { + it('stores the REDEEM_REGISTER event in the database', async () => { await expect(EventProtocol.find()).resolves.toContainEqual( expect.objectContaining({ type: EventProtocolType.REDEEM_REGISTER, @@ -674,7 +675,7 @@ describe('UserResolver', () => { expect(headerPushMock).toBeCalledWith({ key: 'token', value: expect.any(String) }) }) - it('stores the login event in the database', async () => { + it('stores the LOGIN event in the database', async () => { const userConatct = await UserContact.findOneOrFail( { email: 'bibi@bloxberg.de' }, { relations: ['user'] }, @@ -926,7 +927,7 @@ describe('UserResolver', () => { ) }) - it('stores the login event in the database', () => { + it('stores the LOGIN event in the database', () => { expect(EventProtocol.find()).resolves.toContainEqual( expect.objectContaining({ type: EventProtocolType.LOGIN, @@ -1707,6 +1708,135 @@ describe('UserResolver', () => { }) }) + /// + + describe('sendActivationEmail', () => { + describe('unauthenticated', () => { + it('returns an error', async () => { + await expect( + mutate({ mutation: sendActivationEmail, variables: { email: 'bibi@bloxberg.de' } }), + ).resolves.toEqual( + expect.objectContaining({ + errors: [new GraphQLError('401 Unauthorized')], + }), + ) + }) + }) + + describe('authenticated', () => { + describe('without admin rights', () => { + beforeAll(async () => { + user = await userFactory(testEnv, bibiBloxberg) + await mutate({ + mutation: login, + variables: { email: 'bibi@bloxberg.de', password: 'Aa12345_' }, + }) + }) + + afterAll(async () => { + await cleanDB() + resetToken() + }) + + it('returns an error', async () => { + await expect( + mutate({ mutation: sendActivationEmail, variables: { email: 'bibi@bloxberg.de' } }), + ).resolves.toEqual( + expect.objectContaining({ + errors: [new GraphQLError('401 Unauthorized')], + }), + ) + }) + }) + + describe('with admin rights', () => { + beforeAll(async () => { + admin = await userFactory(testEnv, peterLustig) + await mutate({ + mutation: login, + variables: { email: 'peter@lustig.de', password: 'Aa12345_' }, + }) + }) + + afterAll(async () => { + await cleanDB() + resetToken() + }) + + describe('user does not exist', () => { + it('throws an error', async () => { + jest.clearAllMocks() + await expect( + mutate({ mutation: sendActivationEmail, variables: { email: 'INVALID' } }), + ).resolves.toEqual( + expect.objectContaining({ + errors: [new GraphQLError('No user with this credentials')], + }), + ) + }) + + it('logs the error thrown', () => { + expect(logger.error).toBeCalledWith('No user with this credentials', 'invalid') + }) + }) + + describe('user is deleted', () => { + it('throws an error', async () => { + jest.clearAllMocks() + await userFactory(testEnv, stephenHawking) + await expect( + mutate({ mutation: sendActivationEmail, variables: { email: 'stephen@hawking.uk' } }), + ).resolves.toEqual( + expect.objectContaining({ + errors: [new GraphQLError('User with given email contact is deleted')], + }), + ) + }) + + it('logs the error thrown', () => { + expect(logger.error).toBeCalledWith( + 'User with given email contact is deleted', + 'stephen@hawking.uk', + ) + }) + }) + + describe('sendActivationEmail with success', () => { + beforeAll(async () => { + user = await userFactory(testEnv, bibiBloxberg) + }) + + it('returns true', async () => { + const result = await mutate({ + mutation: sendActivationEmail, + variables: { email: 'bibi@bloxberg.de' }, + }) + expect(result).toEqual( + expect.objectContaining({ + data: { + sendActivationEmail: true, + }, + }), + ) + }) + + it('stores the ADMIN_SEND_CONFIRMATION_EMAIL event in the database', async () => { + const userConatct = await UserContact.findOneOrFail( + { email: 'bibi@bloxberg.de' }, + { relations: ['user'] }, + ) + expect(EventProtocol.find()).resolves.toContainEqual( + expect.objectContaining({ + type: EventProtocolType.ADMIN_SEND_CONFIRMATION_EMAIL, + userId: userConatct.user.id, + }), + ) + }) + }) + }) + }) + }) + describe('unDelete user', () => { describe('unauthenticated', () => { it('returns an error', async () => { diff --git a/backend/src/graphql/resolver/UserResolver.ts b/backend/src/graphql/resolver/UserResolver.ts index d3397c720..aa7a94f9a 100644 --- a/backend/src/graphql/resolver/UserResolver.ts +++ b/backend/src/graphql/resolver/UserResolver.ts @@ -781,19 +781,12 @@ export class UserResolver { email = email.trim().toLowerCase() // const user = await dbUser.findOne({ id: emailContact.userId }) const user = await findUserByEmail(email) - if (!user) { - throw new LogError('Could not find user to given email contact', email) - } - if (user.deletedAt) { + if (user.deletedAt || user.emailContact.deletedAt) { throw new LogError('User with given email contact is deleted', email) } - const emailContact = user.emailContact - if (emailContact.deletedAt) { - throw new LogError('The given email contact for this user is deleted', email) - } - emailContact.emailResendCount++ - await emailContact.save() + user.emailContact.emailResendCount++ + await user.emailContact.save() // eslint-disable-next-line @typescript-eslint/no-unused-vars const emailSent = await sendAccountActivationEmail({ @@ -801,7 +794,7 @@ export class UserResolver { lastName: user.lastName, email, language: user.language, - activationLink: activationLink(emailContact.emailVerificationCode), + activationLink: activationLink(user.emailContact.emailVerificationCode), timeDurationObject: getTimeDurationObject(CONFIG.EMAIL_CODE_VALID_TIME), }) diff --git a/backend/src/seeds/graphql/mutations.ts b/backend/src/seeds/graphql/mutations.ts index 2b4ed6656..5c05a4de9 100644 --- a/backend/src/seeds/graphql/mutations.ts +++ b/backend/src/seeds/graphql/mutations.ts @@ -68,6 +68,12 @@ export const createUser = gql` } ` +export const sendActivationEmail = gql` + mutation ($email: String!) { + sendActivationEmail(email: $email) + } +` + export const sendCoins = gql` mutation ($email: String!, $amount: Decimal!, $memo: String!) { sendCoins(email: $email, amount: $amount, memo: $memo)