From 06639d53d9a9623ec801655e5a584dd59f71b691 Mon Sep 17 00:00:00 2001 From: Moriz Wahl Date: Wed, 27 Apr 2022 09:57:39 +0200 Subject: [PATCH] test delete user mutation --- .../graphql/resolver/AdminResolver.test.ts | 150 ++++++++++++++++++ 1 file changed, 150 insertions(+) create mode 100644 backend/src/graphql/resolver/AdminResolver.test.ts diff --git a/backend/src/graphql/resolver/AdminResolver.test.ts b/backend/src/graphql/resolver/AdminResolver.test.ts new file mode 100644 index 000000000..4460030fe --- /dev/null +++ b/backend/src/graphql/resolver/AdminResolver.test.ts @@ -0,0 +1,150 @@ +/* eslint-disable @typescript-eslint/no-explicit-any */ +/* eslint-disable @typescript-eslint/explicit-module-boundary-types */ + +import { testEnvironment, resetToken, cleanDB } from '@test/helpers' +import { userFactory } from '@/seeds/factory/user' +import { bibiBloxberg } from '@/seeds/users/bibi-bloxberg' +import { peterLustig } from '@/seeds/users/peter-lustig' +import { deleteUser } from '@/seeds/graphql/mutations' +import { GraphQLError } from 'graphql' +import { User } from '@entity/User' +/* eslint-disable-next-line @typescript-eslint/no-unused-vars */ +import { sendAccountActivationEmail } from '@/mailer/sendAccountActivationEmail' +import { login } from '@/seeds/graphql/queries' + +// mock account activation email to avoid console spam +jest.mock('@/mailer/sendAccountActivationEmail', () => { + return { + __esModule: true, + sendAccountActivationEmail: jest.fn(), + } +}) + +let mutate: any, query: any, con: any +let testEnv: any + +beforeAll(async () => { + testEnv = await testEnvironment() + mutate = testEnv.mutate + query = testEnv.query + con = testEnv.con + await cleanDB() +}) + +afterAll(async () => { + await cleanDB() + await con.close() +}) + +let admin: User +let user: User + +describe('AdminResolver', () => { + describe('delete user', () => { + describe('unauthenticated', () => { + it('returns an error', async () => { + await expect(mutate({ mutation: deleteUser, variables: { userId: 1 } })).resolves.toEqual( + expect.objectContaining({ + errors: [new GraphQLError('401 Unauthorized')], + }), + ) + }) + }) + + describe('authenticated', () => { + describe('with user rights', () => { + beforeAll(async () => { + user = await userFactory(testEnv, bibiBloxberg) + await query({ + query: login, + variables: { email: 'bibi@bloxberg.de', password: 'Aa12345_' }, + }) + }) + + afterAll(async () => { + await cleanDB() + resetToken() + }) + + it('returns an error', async () => { + await expect( + mutate({ mutation: deleteUser, variables: { userId: user.id + 1 } }), + ).resolves.toEqual( + expect.objectContaining({ + errors: [new GraphQLError('401 Unauthorized')], + }), + ) + }) + }) + + describe('with admin rights', () => { + beforeAll(async () => { + admin = await userFactory(testEnv, peterLustig) + await query({ + query: login, + variables: { email: 'peter@lustig.de', password: 'Aa12345_' }, + }) + }) + + afterAll(async () => { + await cleanDB() + resetToken() + }) + + describe('user to delete user does not exist', () => { + it('throws an error', async () => { + await expect( + mutate({ mutation: deleteUser, variables: { userId: admin.id + 1 } }), + ).resolves.toEqual( + expect.objectContaining({ + errors: [new GraphQLError(`Could not find user with userId: ${admin.id + 1}`)], + }), + ) + }) + }) + + describe('delete self', () => { + it('throws an error', async () => { + await expect( + mutate({ mutation: deleteUser, variables: { userId: admin.id } }), + ).resolves.toEqual( + expect.objectContaining({ + errors: [new GraphQLError('Moderator can not delete his own account!')], + }), + ) + }) + }) + + describe('delete with success', () => { + beforeAll(async () => { + user = await userFactory(testEnv, bibiBloxberg) + }) + + it('returns date string', async () => { + const result = await mutate({ mutation: deleteUser, variables: { userId: user.id } }) + expect(result).toEqual( + expect.objectContaining({ + data: { + deleteUser: expect.any(String), + }, + }), + ) + expect(new Date(result.data.deleteUser)).toEqual(expect.any(Date)) + }) + + describe('delete deleted user', () => { + it('throws an error', async () => { + await expect( + mutate({ mutation: deleteUser, variables: { userId: user.id } }), + ).resolves.toEqual( + expect.objectContaining({ + errors: [new GraphQLError(`Could not find user with userId: ${user.id}`)], + }), + ) + }) + }) + }) + }) + }) + }) +})