From 00c70311a1ff5023537efa5f96fb4ca654a621dc Mon Sep 17 00:00:00 2001 From: Moriz Wahl Date: Tue, 15 Mar 2022 11:19:19 +0100 Subject: [PATCH] use factory to create user in test, use bibi bloxberg as seeded user (no admin) --- .../src/graphql/resolver/UserResolver.test.ts | 37 +++++-------------- backend/src/seeds/factory/user.ts | 22 ++++++----- backend/src/seeds/users/UserInterface.ts | 1 + backend/src/seeds/users/bibi-bloxberg.ts | 1 + 4 files changed, 24 insertions(+), 37 deletions(-) diff --git a/backend/src/graphql/resolver/UserResolver.test.ts b/backend/src/graphql/resolver/UserResolver.test.ts index d22ef5f99..1e43a60b2 100644 --- a/backend/src/graphql/resolver/UserResolver.test.ts +++ b/backend/src/graphql/resolver/UserResolver.test.ts @@ -2,7 +2,8 @@ /* eslint-disable @typescript-eslint/explicit-module-boundary-types */ import { testEnvironment, headerPushMock, cleanDB, resetToken } from '@test/helpers' -import { createConfirmedUser } from '@/seeds/factory/user' +import { createUserFactory } from '@/seeds/factory/user' +import { bibiBloxberg } from '@/seeds/users/bibi-bloxberg' import { createUser, setPassword } from '@/seeds/graphql/mutations' import { login, logout } from '@/seeds/graphql/queries' import { GraphQLError } from 'graphql' @@ -284,7 +285,7 @@ describe('UserResolver', () => { describe('login', () => { const variables = { - email: 'peter@lustig.de', + email: 'bibi@bloxberg.de', password: 'Aa12345_', publisherId: 1234, } @@ -311,13 +312,7 @@ describe('UserResolver', () => { describe('user is in database and correct login data', () => { beforeAll(async () => { - await createConfirmedUser(mutate, { - email: 'peter@lustig.de', - firstName: 'Peter', - lastName: 'Lustig', - language: 'de', - publisherId: 1234, - }) + await createUserFactory(mutate, bibiBloxberg) result = await query({ query: login, variables }) }) @@ -331,15 +326,15 @@ describe('UserResolver', () => { data: { login: { coinanimation: true, - email: 'peter@lustig.de', - firstName: 'Peter', + email: 'bibi@bloxberg.de', + firstName: 'Bibi', hasElopage: false, isAdmin: false, klickTipp: { newsletterState: false, }, language: 'de', - lastName: 'Lustig', + lastName: 'Bloxberg', publisherId: 1234, }, }, @@ -354,13 +349,7 @@ describe('UserResolver', () => { describe('user is in database and wrong password', () => { beforeAll(async () => { - await createConfirmedUser(mutate, { - email: 'peter@lustig.de', - firstName: 'Peter', - lastName: 'Lustig', - language: 'de', - publisherId: 1234, - }) + await createUserFactory(mutate, bibiBloxberg) }) afterAll(async () => { @@ -393,18 +382,12 @@ describe('UserResolver', () => { describe('authenticated', () => { const variables = { - email: 'peter@lustig.de', + email: 'bibi@bloxberg.de', password: 'Aa12345_', } beforeAll(async () => { - await createConfirmedUser(mutate, { - email: 'peter@lustig.de', - firstName: 'Peter', - lastName: 'Lustig', - language: 'de', - publisherId: 1234, - }) + await createUserFactory(mutate, bibiBloxberg) await query({ query: login, variables }) }) diff --git a/backend/src/seeds/factory/user.ts b/backend/src/seeds/factory/user.ts index e38cb9a2d..e46cb173c 100644 --- a/backend/src/seeds/factory/user.ts +++ b/backend/src/seeds/factory/user.ts @@ -4,16 +4,18 @@ import { createUser, setPassword } from '@/seeds/graphql/mutations' import { User } from '@entity/User' import { LoginEmailOptIn } from '@entity/LoginEmailOptIn' +import { UserInterface } from '@/seeds/users/UserInterface' -export const createConfirmedUser = async (mutate: any, user: any) => { - // resetToken() +export const createUserFactory = async (mutate: any, user: UserInterface): Promise => { await mutate({ mutation: createUser, variables: user }) - const dbUser = await User.findOne({ where: { email: user.email } }) - if (!dbUser) throw new Error('Ups, no user found') - const optin = await LoginEmailOptIn.findOne({ where: { userId: dbUser.id } }) - if (!optin) throw new Error('Ups, no optin found') - await mutate({ - mutation: setPassword, - variables: { password: 'Aa12345_', code: optin.verificationCode }, - }) + if (user.emailChecked) { + const dbUser = await User.findOne({ where: { email: user.email } }) + if (!dbUser) throw new Error('Ups, no user found') + const optin = await LoginEmailOptIn.findOne({ where: { userId: dbUser.id } }) + if (!optin) throw new Error('Ups, no optin found') + await mutate({ + mutation: setPassword, + variables: { password: 'Aa12345_', code: optin.verificationCode }, + }) + } } diff --git a/backend/src/seeds/users/UserInterface.ts b/backend/src/seeds/users/UserInterface.ts index b2379c8d5..08aa5d19d 100644 --- a/backend/src/seeds/users/UserInterface.ts +++ b/backend/src/seeds/users/UserInterface.ts @@ -7,5 +7,6 @@ export interface UserInterface { emailChecked?: boolean language?: string deletedAt?: Date + publisherId?: number isAdmin?: boolean } diff --git a/backend/src/seeds/users/bibi-bloxberg.ts b/backend/src/seeds/users/bibi-bloxberg.ts index 81364eb03..7c372848e 100644 --- a/backend/src/seeds/users/bibi-bloxberg.ts +++ b/backend/src/seeds/users/bibi-bloxberg.ts @@ -7,4 +7,5 @@ export const bibiBloxberg: UserInterface = { // description: 'Hex Hex', emailChecked: true, language: 'de', + publisherId: 1234, }