From 4fd5ae3e28ceb9acd153d13e263fc24735b2d398 Mon Sep 17 00:00:00 2001 From: Antoine Cormouls Date: Thu, 26 Dec 2019 19:49:31 +0100 Subject: [PATCH] Reset and Send verification email --- spec/ParseGraphQLServer.spec.js | 84 ++++++++++++++++++ src/GraphQL/loaders/usersMutations.js | 123 +++++++++++++++++++++++--- 2 files changed, 196 insertions(+), 11 deletions(-) diff --git a/spec/ParseGraphQLServer.spec.js b/spec/ParseGraphQLServer.spec.js index 08f492966f..6d119c47d9 100644 --- a/spec/ParseGraphQLServer.spec.js +++ b/spec/ParseGraphQLServer.spec.js @@ -285,6 +285,7 @@ describe('ParseGraphQLServer', () => { user1 = new Parse.User(); user1.setUsername('user1'); user1.setPassword('user1'); + user1.setEmail('user1@user1.user1'); await user1.signUp(); user2 = new Parse.User(); @@ -7149,6 +7150,89 @@ describe('ParseGraphQLServer', () => { }); } }); + + it('should send reset password', async () => { + const clientMutationId = uuidv4(); + const emailAdapter = { + sendVerificationEmail: () => {}, + sendPasswordResetEmail: () => Promise.resolve(), + sendMail: () => {}, + }; + parseServer = await global.reconfigureServer({ + appName: 'test', + emailAdapter: emailAdapter, + publicServerURL: 'http://test.test', + }); + const user = new Parse.User(); + user.setUsername('user1'); + user.setPassword('user1'); + user.setEmail('user1@user1.user1'); + await user.signUp(); + await Parse.User.logOut(); + const result = await apolloClient.mutate({ + mutation: gql` + mutation ResetPassword($input: ResetPasswordInput!) { + resetPassword(input: $input) { + clientMutationId + ok + } + } + `, + variables: { + input: { + clientMutationId, + email: 'user1@user1.user1', + }, + }, + }); + + expect(result.data.resetPassword.clientMutationId).toEqual( + clientMutationId + ); + expect(result.data.resetPassword.ok).toBeTruthy(); + }); + it('should send verification email again', async () => { + const clientMutationId = uuidv4(); + const emailAdapter = { + sendVerificationEmail: () => {}, + sendPasswordResetEmail: () => Promise.resolve(), + sendMail: () => {}, + }; + parseServer = await global.reconfigureServer({ + appName: 'test', + emailAdapter: emailAdapter, + publicServerURL: 'http://test.test', + }); + const user = new Parse.User(); + user.setUsername('user1'); + user.setPassword('user1'); + user.setEmail('user1@user1.user1'); + await user.signUp(); + await Parse.User.logOut(); + const result = await apolloClient.mutate({ + mutation: gql` + mutation SendVerificationEmail( + $input: SendVerificationEmailInput! + ) { + sendVerificationEmail(input: $input) { + clientMutationId + ok + } + } + `, + variables: { + input: { + clientMutationId, + email: 'user1@user1.user1', + }, + }, + }); + + expect(result.data.sendVerificationEmail.clientMutationId).toEqual( + clientMutationId + ); + expect(result.data.sendVerificationEmail.ok).toBeTruthy(); + }); }); describe('Session Token', () => { diff --git a/src/GraphQL/loaders/usersMutations.js b/src/GraphQL/loaders/usersMutations.js index 0a8677b610..a25db157e1 100644 --- a/src/GraphQL/loaders/usersMutations.js +++ b/src/GraphQL/loaders/usersMutations.js @@ -1,4 +1,4 @@ -import { GraphQLNonNull, GraphQLString } from 'graphql'; +import { GraphQLNonNull, GraphQLString, GraphQLBoolean } from 'graphql'; import { mutationWithClientMutationId } from 'graphql-relay'; import UsersRouter from '../../Routers/UsersRouter'; import * as objectsMutations from '../helpers/objectsMutations'; @@ -93,16 +93,18 @@ const load = parseGraphQLSchema => { const { username, password } = args; const { config, auth, info } = context; - const { sessionToken } = (await usersRouter.handleLogIn({ - body: { - username, - password, - }, - query: {}, - config, - auth, - info, - })).response; + const { sessionToken } = ( + await usersRouter.handleLogIn({ + body: { + username, + password, + }, + query: {}, + config, + auth, + info, + }) + ).response; info.sessionToken = sessionToken; @@ -171,6 +173,105 @@ const load = parseGraphQLSchema => { ); parseGraphQLSchema.addGraphQLType(logOutMutation.type, true, true); parseGraphQLSchema.addGraphQLMutation('logOut', logOutMutation, true, true); + + const resetPasswordMutation = mutationWithClientMutationId({ + name: 'ResetPassword', + description: + 'The resetPassword mutation can be used to reset the password of an existing user.', + inputFields: { + email: { + descriptions: 'Email of the user that should receive the reset email', + type: new GraphQLNonNull(GraphQLString), + }, + }, + outputFields: { + ok: { + description: "It's always true.", + type: new GraphQLNonNull(GraphQLBoolean), + }, + }, + mutateAndGetPayload: async ({ email }, context) => { + const { config, auth, info } = context; + + await usersRouter.handleResetRequest({ + body: { + email, + }, + config, + auth, + info, + }); + + return { ok: true }; + }, + }); + + parseGraphQLSchema.addGraphQLType( + resetPasswordMutation.args.input.type.ofType, + true, + true + ); + parseGraphQLSchema.addGraphQLType(resetPasswordMutation.type, true, true); + parseGraphQLSchema.addGraphQLMutation( + 'resetPassword', + resetPasswordMutation, + true, + true + ); + + const sendVerificationEmailMutation = mutationWithClientMutationId({ + name: 'SendVerificationEmail', + description: + 'The sendVerificationEmail mutation can be used to send the verification email again.', + inputFields: { + email: { + descriptions: + 'Email of the user that should receive the verification email', + type: new GraphQLNonNull(GraphQLString), + }, + }, + outputFields: { + ok: { + description: "It's always true.", + type: new GraphQLNonNull(GraphQLBoolean), + }, + }, + mutateAndGetPayload: async ({ email }, context) => { + try { + const { config, auth, info } = context; + + await usersRouter.handleVerificationEmailRequest({ + body: { + email, + }, + config, + auth, + info, + }); + + return { ok: true }; + } catch (e) { + parseGraphQLSchema.handleError(e); + } + }, + }); + + parseGraphQLSchema.addGraphQLType( + sendVerificationEmailMutation.args.input.type.ofType, + true, + true + ); + parseGraphQLSchema.addGraphQLType( + sendVerificationEmailMutation.type, + true, + true + ); + parseGraphQLSchema.addGraphQLMutation( + 'sendVerificationEmail', + sendVerificationEmailMutation, + true, + true + ); }; export { load };