diff --git a/src/resolvers/user_management.js b/src/resolvers/user_management.js index 3a7698e24..36865646f 100644 --- a/src/resolvers/user_management.js +++ b/src/resolvers/user_management.js @@ -30,32 +30,28 @@ export default { // throw new Error('Already logged in.') // } const session = driver.session() - return session - .run( - 'MATCH (user:User {email: $userEmail}) ' + - 'RETURN user {.id, .slug, .name, .avatar, .email, .password, .role} as user LIMIT 1', - { - userEmail: email - } - ) - .then(async result => { - session.close() - const [currentUser] = await result.records.map(function (record) { - return record.get('user') - }) + const result = await session.run( + 'MATCH (user:User {email: $userEmail}) ' + + 'RETURN user {.id, .slug, .name, .avatar, .email, .password, .role} as user LIMIT 1', + { + userEmail: email + } + ) - if ( - currentUser && - (await bcrypt.compareSync(password, currentUser.password)) - ) { - delete currentUser.password - return encode(currentUser) - } else { - throw new AuthenticationError( - 'Incorrect email address or password.' - ) - } - }) + session.close() + const [currentUser] = await result.records.map(function (record) { + return record.get('user') + }) + + if ( + currentUser && + (await bcrypt.compareSync(password, currentUser.password)) + ) { + delete currentUser.password + return encode(currentUser) + } else { + throw new AuthenticationError('Incorrect email address or password.') + } }, changePassword: async ( _, @@ -76,12 +72,12 @@ export default { }) if (!(await bcrypt.compareSync(oldPassword, currentUser.password))) { - throw new AuthenticationError('Old password isn\'t valid') + throw new AuthenticationError('Old password is not correct') } if (await bcrypt.compareSync(newPassword, currentUser.password)) { throw new AuthenticationError( - 'Old password and New password should not be same' + 'Old password and new password should be different' ) } else { const newHashedPassword = await bcrypt.hashSync(newPassword, 10) diff --git a/src/resolvers/user_management.spec.js b/src/resolvers/user_management.spec.js index 8007737f0..c4b09df37 100644 --- a/src/resolvers/user_management.spec.js +++ b/src/resolvers/user_management.spec.js @@ -28,6 +28,7 @@ const jennyRostocksHeaders = { beforeEach(async () => { await factory.create('User', { + avatar: 'https://s3.amazonaws.com/uifaces/faces/twitter/jimmuirhead/128.jpg', id: 'acb2d923-f3af-479e-9f00-61b12e864666', name: 'Matilde Hermiston', slug: 'matilde-hermiston', @@ -126,8 +127,7 @@ describe('currentUser', () => { it('returns the whole user object', async () => { const expected = { currentUser: { - avatar: - 'https://s3.amazonaws.com/uifaces/faces/twitter/seyedhossein1/128.jpg', + avatar: 'https://s3.amazonaws.com/uifaces/faces/twitter/jimmuirhead/128.jpg', email: 'test@example.org', id: 'acb2d923-f3af-479e-9f00-61b12e864666', name: 'Matilde Hermiston', @@ -216,7 +216,7 @@ describe('change password', () => { } describe('should be authenticated before changing password', () => { - it('should throw not "Not Authorised!', async () => { + it('throws not "Not Authorised!', async () => { await expect( request( host, @@ -230,7 +230,7 @@ describe('change password', () => { }) describe('old and new password should not match', () => { - it('responds with "Old password and New password should not be same"', async () => { + it('responds with "Old password and new password should be different"', async () => { await expect( client.request( mutation({ @@ -238,7 +238,7 @@ describe('change password', () => { newPassword: '1234' }) ) - ).rejects.toThrow('Old password and New password should not be same') + ).rejects.toThrow('Old password and new password should be different') }) }) @@ -251,7 +251,23 @@ describe('change password', () => { newPassword: '12345' }) ) - ).rejects.toThrow('Old password isn\'t valid') + ).rejects.toThrow('Old password is not correct') + }) + }) + + describe('correct password', () => { + it('changes the password if given correct credentials "', async () => { + let response = await client.request( + mutation({ + oldPassword: '1234', + newPassword: '12345' + }) + ) + await expect( + response + ).toEqual(expect.objectContaining({ + changePassword: expect.any(String) + })) }) }) })