diff --git a/backend/src/apis/KlicktippController.ts b/backend/src/apis/KlicktippController.ts index 544b39d97..0777211ad 100644 --- a/backend/src/apis/KlicktippController.ts +++ b/backend/src/apis/KlicktippController.ts @@ -5,7 +5,7 @@ import CONFIG from '../config' const klicktippConnector = new KlicktippConnector() -export const signIn = async ( +export const klicktippSignIn = async ( email: string, language: string, firstName?: string, diff --git a/backend/src/graphql/resolver/KlicktippResolver.ts b/backend/src/graphql/resolver/KlicktippResolver.ts index fdffb940a..0ba2387e3 100644 --- a/backend/src/graphql/resolver/KlicktippResolver.ts +++ b/backend/src/graphql/resolver/KlicktippResolver.ts @@ -6,7 +6,7 @@ import { getKlickTippUser, getKlicktippTagMap, unsubscribe, - signIn, + klicktippSignIn, } from '../../apis/KlicktippController' import { RIGHTS } from '../../auth/RIGHTS' import SubscribeNewsletterArgs from '../arg/SubscribeNewsletterArgs' @@ -36,6 +36,6 @@ export class KlicktippResolver { async subscribeNewsletter( @Args() { email, language }: SubscribeNewsletterArgs, ): Promise { - return await signIn(email, language) + return await klicktippSignIn(email, language) } } diff --git a/backend/src/graphql/resolver/UserResolver.test.ts b/backend/src/graphql/resolver/UserResolver.test.ts index 1f0bce30f..fb4e42bf6 100644 --- a/backend/src/graphql/resolver/UserResolver.test.ts +++ b/backend/src/graphql/resolver/UserResolver.test.ts @@ -13,6 +13,7 @@ import { LoginEmailOptIn } from '@entity/LoginEmailOptIn' import { User } from '@entity/User' import CONFIG from '../../config' import { sendAccountActivationEmail } from '../../mailer/sendAccountActivationEmail' +import { klicktippSignIn } from '../../apis/KlicktippController' jest.mock('../../mailer/sendAccountActivationEmail', () => { return { @@ -21,6 +22,13 @@ jest.mock('../../mailer/sendAccountActivationEmail', () => { } }) +jest.mock('../../apis/KlicktippController', () => { + return { + __esModule: true, + klicktippSignIn: jest.fn(), + } +}) + let mutate: any let con: any @@ -220,6 +228,101 @@ describe('UserResolver', () => { }) }) }) + + describe('setPassword', () => { + const createUserMutation = gql` + mutation ( + $email: String! + $firstName: String! + $lastName: String! + $language: String! + $publisherId: Int + ) { + createUser( + email: $email + firstName: $firstName + lastName: $lastName + language: $language + publisherId: $publisherId + ) + } + ` + + const createUserVariables = { + email: 'peter@lustig.de', + firstName: 'Peter', + lastName: 'Lustig', + language: 'de', + publisherId: 1234, + } + + const setPasswordMutation = gql` + mutation ($code: String!, $password: String!) { + setPassword(code: $code, password: $password) + } + ` + + describe('valid optin code and valid password', () => { + let emailOptIn: string + let result: any + let loginUser: any + let newLoginUser: any + let newUser: any + + beforeAll(async () => { + await mutate({ mutation: createUserMutation, variables: createUserVariables }) + const loginEmailOptIn = await getRepository(LoginEmailOptIn) + .createQueryBuilder('login_email_optin') + .getMany() + loginUser = await getRepository(LoginUser).createQueryBuilder('login_user').getMany() + emailOptIn = loginEmailOptIn[0].verificationCode.toString() + result = await mutate({ + mutation: setPasswordMutation, + variables: { code: emailOptIn, password: 'Aa12345_' }, + }) + newLoginUser = await getRepository(LoginUser).createQueryBuilder('login_user').getMany() + newUser = await getRepository(User).createQueryBuilder('state_user').getMany() + }) + + afterAll(async () => { + await resetDB() + }) + + it('updates the password', () => { + expect(newLoginUser[0].password).toEqual('3917921995996627700') + }) + + it('updates the public Key on both user tables', () => { + expect(newLoginUser[0].pubKey).toEqual(expect.any(Buffer)) + expect(newLoginUser[0].pubKey).not.toEqual(loginUser[0].pubKey) + expect(newLoginUser[0].pubKey).toEqual(newUser[0].pubkey) + }) + + it('updates the private Key', () => { + expect(newLoginUser[0].privKey).toEqual(expect.any(Buffer)) + expect(newLoginUser[0].privKey).not.toEqual(loginUser[0].privKey) + }) + + it('removes the optin', async () => { + await expect( + getRepository(LoginEmailOptIn).createQueryBuilder('login_email_optin').getMany(), + ).resolves.toHaveLength(0) + }) + + it('calls the klicktipp API', () => { + expect(klicktippSignIn).toBeCalledWith( + loginUser[0].email, + loginUser[0].language, + loginUser[0].firstName, + loginUser[0].lastName, + ) + }) + + it('returns true', () => { + expect(result).toBeTruthy() + }) + }) + }) }) afterAll(async () => { diff --git a/backend/src/graphql/resolver/UserResolver.ts b/backend/src/graphql/resolver/UserResolver.ts index b751eb633..c17bb06e9 100644 --- a/backend/src/graphql/resolver/UserResolver.ts +++ b/backend/src/graphql/resolver/UserResolver.ts @@ -23,7 +23,7 @@ import { LoginEmailOptIn } from '@entity/LoginEmailOptIn' import { sendResetPasswordEmail } from '../../mailer/sendResetPasswordEmail' import { sendAccountActivationEmail } from '../../mailer/sendAccountActivationEmail' import { LoginElopageBuysRepository } from '../../typeorm/repository/LoginElopageBuys' -import { signIn } from '../../apis/KlicktippController' +import { klicktippSignIn } from '../../apis/KlicktippController' import { RIGHTS } from '../../auth/RIGHTS' import { ServerUserRepository } from '../../typeorm/repository/ServerUser' import { ROLE_ADMIN } from '../../auth/ROLES' @@ -641,7 +641,12 @@ export class UserResolver { // TODO do we always signUp the user? How to handle things with old users? if (optInCode.emailOptInTypeId === EMAIL_OPT_IN_REGISTER) { try { - await signIn(loginUser.email, loginUser.language, loginUser.firstName, loginUser.lastName) + await klicktippSignIn( + loginUser.email, + loginUser.language, + loginUser.firstName, + loginUser.lastName, + ) } catch { // TODO is this a problem? // eslint-disable-next-line no-console diff --git a/backend/src/middleware/klicktippMiddleware.ts b/backend/src/middleware/klicktippMiddleware.ts index 69a74480d..d0fde8195 100644 --- a/backend/src/middleware/klicktippMiddleware.ts +++ b/backend/src/middleware/klicktippMiddleware.ts @@ -1,5 +1,5 @@ import { MiddlewareFn } from 'type-graphql' -import { /* signIn, */ getKlickTippUser } from '../apis/KlicktippController' +import { /* klicktippSignIn, */ getKlickTippUser } from '../apis/KlicktippController' import { KlickTipp } from '../graphql/model/KlickTipp' import CONFIG from '../config/index' @@ -12,7 +12,7 @@ import CONFIG from '../config/index' // // Do Something here before resolver is called // const result = await next() // // Do Something here after resolver is completed -// await signIn(result.email, result.language, result.firstName, result.lastName) +// await klicktippSignIn(result.email, result.language, result.firstName, result.lastName) // return result // }