| 
									
										
										
										
											2020-05-13 11:46:52 +02:00
										 |  |  | 'use strict'; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-14 12:30:04 +02:00
										 |  |  | const _ = require('lodash'); | 
					
						
							|  |  |  | const userService = require('../user'); | 
					
						
							| 
									
										
										
										
											2020-05-13 11:46:52 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | describe('User', () => { | 
					
						
							|  |  |  |   describe('sanitizeUser', () => { | 
					
						
							|  |  |  |     test('Removes password and resetPasswordToken', () => { | 
					
						
							| 
									
										
										
										
											2020-05-14 12:30:04 +02:00
										 |  |  |       const res = userService.sanitizeUser({ | 
					
						
							| 
									
										
										
										
											2020-05-13 11:46:52 +02:00
										 |  |  |         id: 1, | 
					
						
							|  |  |  |         firstname: 'Test', | 
					
						
							|  |  |  |         otherField: 'Hello', | 
					
						
							|  |  |  |         password: '$5IAZUDB871', | 
					
						
							|  |  |  |         resetPasswordToken: '3456-5678-6789-789', | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       expect(res).toEqual({ | 
					
						
							|  |  |  |         id: 1, | 
					
						
							|  |  |  |         firstname: 'Test', | 
					
						
							|  |  |  |         otherField: 'Hello', | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  |   }); | 
					
						
							| 
									
										
										
										
											2020-05-14 12:30:04 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |   describe('create', () => { | 
					
						
							|  |  |  |     test('Creates a user by merging given and default attributes', async () => { | 
					
						
							|  |  |  |       const create = jest.fn(user => Promise.resolve(user)); | 
					
						
							| 
									
										
										
										
											2020-05-15 10:12:50 +02:00
										 |  |  |       const createToken = jest.fn(() => 'token'); | 
					
						
							| 
									
										
										
										
											2020-05-14 12:30:04 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |       global.strapi = { | 
					
						
							| 
									
										
										
										
											2020-05-15 10:12:50 +02:00
										 |  |  |         admin: { | 
					
						
							|  |  |  |           services: { | 
					
						
							|  |  |  |             token: { createToken }, | 
					
						
							|  |  |  |           }, | 
					
						
							|  |  |  |         }, | 
					
						
							| 
									
										
										
										
											2020-05-14 12:30:04 +02:00
										 |  |  |         query() { | 
					
						
							|  |  |  |           return { create }; | 
					
						
							|  |  |  |         }, | 
					
						
							|  |  |  |       }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       const input = { firstname: 'John', lastname: 'Doe', email: 'johndoe@email.com' }; | 
					
						
							| 
									
										
										
										
											2020-05-15 10:12:50 +02:00
										 |  |  |       const expected = { ...input, isActive: false, roles: [], registrationToken: 'token' }; | 
					
						
							| 
									
										
										
										
											2020-05-14 12:30:04 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |       const result = await userService.create(input); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-15 10:12:50 +02:00
										 |  |  |       expect(create).toHaveBeenCalled(); | 
					
						
							|  |  |  |       expect(createToken).toHaveBeenCalled(); | 
					
						
							| 
									
										
										
										
											2020-05-14 12:30:04 +02:00
										 |  |  |       expect(result).toStrictEqual(expected); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     test('Creates a user by using given attributes', async () => { | 
					
						
							| 
									
										
										
										
											2020-05-15 10:12:50 +02:00
										 |  |  |       const create = jest.fn(user => Promise.resolve(user)); | 
					
						
							|  |  |  |       const createToken = jest.fn(() => 'token'); | 
					
						
							| 
									
										
										
										
											2020-05-14 12:30:04 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |       global.strapi = { | 
					
						
							| 
									
										
										
										
											2020-05-15 10:12:50 +02:00
										 |  |  |         admin: { | 
					
						
							|  |  |  |           services: { | 
					
						
							|  |  |  |             token: { createToken }, | 
					
						
							|  |  |  |           }, | 
					
						
							|  |  |  |         }, | 
					
						
							| 
									
										
										
										
											2020-05-14 12:30:04 +02:00
										 |  |  |         query() { | 
					
						
							| 
									
										
										
										
											2020-05-15 10:12:50 +02:00
										 |  |  |           return { create }; | 
					
						
							| 
									
										
										
										
											2020-05-14 12:30:04 +02:00
										 |  |  |         }, | 
					
						
							|  |  |  |       }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       const input = { | 
					
						
							|  |  |  |         firstname: 'John', | 
					
						
							|  |  |  |         lastname: 'Doe', | 
					
						
							|  |  |  |         email: 'johndoe@email.com', | 
					
						
							|  |  |  |         roles: [2], | 
					
						
							|  |  |  |         isActive: true, | 
					
						
							| 
									
										
										
										
											2020-05-15 10:12:50 +02:00
										 |  |  |         registrationToken: 'another-token', | 
					
						
							| 
									
										
										
										
											2020-05-14 12:30:04 +02:00
										 |  |  |       }; | 
					
						
							|  |  |  |       const expected = _.clone(input); | 
					
						
							|  |  |  |       const result = await userService.create(input); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       expect(result).toStrictEqual(expected); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-18 17:16:49 +02:00
										 |  |  |   describe('update', () => { | 
					
						
							|  |  |  |     test('Forwards call to the query layer', async () => { | 
					
						
							|  |  |  |       const user = { | 
					
						
							|  |  |  |         email: 'test@strapi.io', | 
					
						
							|  |  |  |       }; | 
					
						
							|  |  |  |       const update = jest.fn(() => Promise.resolve(user)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       global.strapi = { | 
					
						
							|  |  |  |         query() { | 
					
						
							|  |  |  |           return { update }; | 
					
						
							|  |  |  |         }, | 
					
						
							|  |  |  |       }; | 
					
						
							|  |  |  |       const params = { id: 1 }; | 
					
						
							|  |  |  |       const input = { email: 'test@strapi.io' }; | 
					
						
							|  |  |  |       const result = await userService.update(params, input); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       expect(update).toHaveBeenCalledWith(params, input); | 
					
						
							|  |  |  |       expect(result).toBe(user); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-14 12:30:04 +02:00
										 |  |  |   describe('exists', () => { | 
					
						
							|  |  |  |     test('Return true if the user already exists', async () => { | 
					
						
							|  |  |  |       const count = jest.fn(() => Promise.resolve(1)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       global.strapi = { | 
					
						
							|  |  |  |         query: () => { | 
					
						
							|  |  |  |           return { count }; | 
					
						
							|  |  |  |         }, | 
					
						
							|  |  |  |       }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       const result = await userService.exists(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       expect(result).toBeTruthy(); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     test('Return false if the user does not exists', async () => { | 
					
						
							|  |  |  |       const count = jest.fn(() => Promise.resolve(0)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       global.strapi = { | 
					
						
							|  |  |  |         query: () => { | 
					
						
							|  |  |  |           return { count }; | 
					
						
							|  |  |  |         }, | 
					
						
							|  |  |  |       }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       const result = await userService.exists(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       expect(result).toBeFalsy(); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  |   }); | 
					
						
							| 
									
										
										
										
											2020-05-18 16:07:37 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |   describe('findRegistrationInfo', () => { | 
					
						
							|  |  |  |     test('Returns undefined if not found', async () => { | 
					
						
							|  |  |  |       const findOne = jest.fn(() => Promise.resolve()); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       global.strapi = { | 
					
						
							|  |  |  |         query: () => { | 
					
						
							|  |  |  |           return { findOne }; | 
					
						
							|  |  |  |         }, | 
					
						
							|  |  |  |       }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       const res = await userService.findRegistrationInfo('ABCD'); | 
					
						
							|  |  |  |       expect(res).toBeUndefined(); | 
					
						
							|  |  |  |       expect(findOne).toHaveBeenCalledWith({ registrationToken: 'ABCD' }); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     test('Returns correct user registration info', async () => { | 
					
						
							|  |  |  |       const user = { | 
					
						
							|  |  |  |         email: 'test@strapi.io', | 
					
						
							|  |  |  |         firstname: 'Test', | 
					
						
							|  |  |  |         lastname: 'Strapi', | 
					
						
							|  |  |  |         otherField: 'ignored', | 
					
						
							|  |  |  |       }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       const findOne = jest.fn(() => Promise.resolve(user)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       global.strapi = { | 
					
						
							|  |  |  |         query: () => { | 
					
						
							|  |  |  |           return { findOne }; | 
					
						
							|  |  |  |         }, | 
					
						
							|  |  |  |       }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       const res = await userService.findRegistrationInfo('ABCD'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       expect(res).toEqual({ | 
					
						
							|  |  |  |         email: user.email, | 
					
						
							|  |  |  |         firstname: user.firstname, | 
					
						
							|  |  |  |         lastname: user.lastname, | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  |   }); | 
					
						
							| 
									
										
										
										
											2020-05-18 17:16:49 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |   describe('register', () => { | 
					
						
							|  |  |  |     test('Fails if no matching user is found', async () => { | 
					
						
							|  |  |  |       const findOne = jest.fn(() => Promise.resolve(undefined)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       global.strapi = { | 
					
						
							|  |  |  |         query() { | 
					
						
							|  |  |  |           return { | 
					
						
							|  |  |  |             findOne, | 
					
						
							|  |  |  |           }; | 
					
						
							|  |  |  |         }, | 
					
						
							|  |  |  |         errors: { | 
					
						
							|  |  |  |           badRequest(msg) { | 
					
						
							|  |  |  |             throw new Error(msg); | 
					
						
							|  |  |  |           }, | 
					
						
							|  |  |  |         }, | 
					
						
							|  |  |  |       }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       const input = { | 
					
						
							|  |  |  |         registrationToken: '123', | 
					
						
							|  |  |  |         userInfo: { | 
					
						
							|  |  |  |           firstname: 'test', | 
					
						
							|  |  |  |           lastname: 'Strapi', | 
					
						
							|  |  |  |           password: 'Test1234', | 
					
						
							|  |  |  |         }, | 
					
						
							|  |  |  |       }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       expect(userService.register(input)).rejects.toThrowError('Invalid registration info'); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     test('Create a password hash', async () => { | 
					
						
							|  |  |  |       const findOne = jest.fn(() => Promise.resolve({ id: 1 })); | 
					
						
							|  |  |  |       const update = jest.fn(user => Promise.resolve(user)); | 
					
						
							|  |  |  |       const hashPassword = jest.fn(() => Promise.resolve('123456789')); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       global.strapi = { | 
					
						
							|  |  |  |         query() { | 
					
						
							|  |  |  |           return { | 
					
						
							|  |  |  |             findOne, | 
					
						
							|  |  |  |           }; | 
					
						
							|  |  |  |         }, | 
					
						
							|  |  |  |         admin: { | 
					
						
							|  |  |  |           services: { | 
					
						
							|  |  |  |             user: { update }, | 
					
						
							|  |  |  |             auth: { hashPassword }, | 
					
						
							|  |  |  |           }, | 
					
						
							|  |  |  |         }, | 
					
						
							|  |  |  |       }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       const input = { | 
					
						
							|  |  |  |         registrationToken: '123', | 
					
						
							|  |  |  |         userInfo: { | 
					
						
							|  |  |  |           firstname: 'test', | 
					
						
							|  |  |  |           lastname: 'Strapi', | 
					
						
							|  |  |  |           password: 'Test1234', | 
					
						
							|  |  |  |         }, | 
					
						
							|  |  |  |       }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       await userService.register(input); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       expect(hashPassword).toHaveBeenCalledWith('Test1234'); | 
					
						
							|  |  |  |       expect(update).toHaveBeenCalledWith( | 
					
						
							|  |  |  |         { id: 1 }, | 
					
						
							|  |  |  |         expect.objectContaining({ password: '123456789' }) | 
					
						
							|  |  |  |       ); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     test('Set user firstname and lastname', async () => { | 
					
						
							|  |  |  |       const findOne = jest.fn(() => Promise.resolve({ id: 1 })); | 
					
						
							|  |  |  |       const update = jest.fn(user => Promise.resolve(user)); | 
					
						
							|  |  |  |       const hashPassword = jest.fn(() => Promise.resolve('123456789')); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       global.strapi = { | 
					
						
							|  |  |  |         query() { | 
					
						
							|  |  |  |           return { | 
					
						
							|  |  |  |             findOne, | 
					
						
							|  |  |  |           }; | 
					
						
							|  |  |  |         }, | 
					
						
							|  |  |  |         admin: { | 
					
						
							|  |  |  |           services: { | 
					
						
							|  |  |  |             user: { update }, | 
					
						
							|  |  |  |             auth: { hashPassword }, | 
					
						
							|  |  |  |           }, | 
					
						
							|  |  |  |         }, | 
					
						
							|  |  |  |       }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       const input = { | 
					
						
							|  |  |  |         registrationToken: '123', | 
					
						
							|  |  |  |         userInfo: { | 
					
						
							|  |  |  |           firstname: 'test', | 
					
						
							|  |  |  |           lastname: 'Strapi', | 
					
						
							|  |  |  |           password: 'Test1234', | 
					
						
							|  |  |  |         }, | 
					
						
							|  |  |  |       }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       await userService.register(input); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       expect(hashPassword).toHaveBeenCalledWith('Test1234'); | 
					
						
							|  |  |  |       expect(update).toHaveBeenCalledWith( | 
					
						
							|  |  |  |         { id: 1 }, | 
					
						
							|  |  |  |         expect.objectContaining({ firstname: 'test', lastname: 'Strapi' }) | 
					
						
							|  |  |  |       ); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     test('Set user to active', async () => { | 
					
						
							|  |  |  |       const findOne = jest.fn(() => Promise.resolve({ id: 1 })); | 
					
						
							|  |  |  |       const update = jest.fn(user => Promise.resolve(user)); | 
					
						
							|  |  |  |       const hashPassword = jest.fn(() => Promise.resolve('123456789')); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       global.strapi = { | 
					
						
							|  |  |  |         query() { | 
					
						
							|  |  |  |           return { | 
					
						
							|  |  |  |             findOne, | 
					
						
							|  |  |  |           }; | 
					
						
							|  |  |  |         }, | 
					
						
							|  |  |  |         admin: { | 
					
						
							|  |  |  |           services: { | 
					
						
							|  |  |  |             user: { update }, | 
					
						
							|  |  |  |             auth: { hashPassword }, | 
					
						
							|  |  |  |           }, | 
					
						
							|  |  |  |         }, | 
					
						
							|  |  |  |       }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       const input = { | 
					
						
							|  |  |  |         registrationToken: '123', | 
					
						
							|  |  |  |         userInfo: { | 
					
						
							|  |  |  |           firstname: 'test', | 
					
						
							|  |  |  |           lastname: 'Strapi', | 
					
						
							|  |  |  |           password: 'Test1234', | 
					
						
							|  |  |  |         }, | 
					
						
							|  |  |  |       }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       await userService.register(input); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       expect(hashPassword).toHaveBeenCalledWith('Test1234'); | 
					
						
							|  |  |  |       expect(update).toHaveBeenCalledWith({ id: 1 }, expect.objectContaining({ isActive: true })); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  |   }); | 
					
						
							| 
									
										
										
										
											2020-05-13 11:46:52 +02:00
										 |  |  | }); |