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);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
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-13 11:46:52 +02:00
|
|
|
});
|