Add telemetry events for user creation

Signed-off-by: Convly <jean-sebastien.herbaux@epitech.eu>
This commit is contained in:
Convly 2020-08-03 11:20:42 +02:00 committed by Pierre Noël
parent 7b19532728
commit ba8954794b
5 changed files with 100 additions and 11 deletions

View File

@ -276,6 +276,33 @@ describe('Role', () => {
});
});
describe('Count roles', () => {
test('Count roles without params', async () => {
const count = jest.fn(() => Promise.resolve(2));
global.strapi = {
query: () => ({ count }),
};
const amount = await roleService.count();
expect(amount).toBe(2);
expect(count).toHaveBeenCalledWith({});
});
test('Count roles with params', async () => {
const count = jest.fn(() => Promise.resolve(2));
global.strapi = {
query: () => ({ count }),
};
const params = { foo: 'bar' };
const amount = await roleService.count(params);
expect(amount).toBe(2);
expect(count).toHaveBeenCalledWith(params);
});
});
describe('createRolesIfNoneExist', () => {
test("Don't create roles if one already exist", async () => {
const count = jest.fn(() => Promise.resolve(1));

View File

@ -26,20 +26,25 @@ describe('User', () => {
});
describe('create', () => {
const count = jest.fn(() => Promise.resolve(1));
const send = jest.fn();
test('Creates a user by merging given and default attributes', async () => {
const create = jest.fn(user => Promise.resolve(user));
const createToken = jest.fn(() => 'token');
const hashPassword = jest.fn(() => Promise.resolve('123456789'));
global.strapi = {
telemetry: { send },
admin: {
services: {
token: { createToken },
auth: { hashPassword },
role: { count },
},
},
query() {
return { create };
return { create, count };
},
};
@ -59,14 +64,16 @@ describe('User', () => {
const hashPassword = jest.fn(() => Promise.resolve('123456789'));
global.strapi = {
telemetry: { send },
admin: {
services: {
token: { createToken },
auth: { hashPassword },
role: { count },
},
},
query() {
return { create };
return { create, count };
},
};
@ -99,14 +106,16 @@ describe('User', () => {
const hashPassword = jest.fn(() => Promise.resolve('123456789'));
global.strapi = {
telemetry: { send },
admin: {
services: {
token: { createToken },
auth: { hashPassword },
role: { count },
},
},
query() {
return { create };
return { create, count };
},
};
@ -125,6 +134,33 @@ describe('User', () => {
});
});
describe('Count users', () => {
test('Count users without params', async () => {
const count = jest.fn(() => Promise.resolve(2));
global.strapi = {
query: () => ({ count }),
};
const amount = await userService.count();
expect(amount).toBe(2);
expect(count).toHaveBeenCalledWith({});
});
test('Count users with params', async () => {
const count = jest.fn(() => Promise.resolve(2));
global.strapi = {
query: () => ({ count }),
};
const params = { foo: 'bar' };
const amount = await userService.count(params);
expect(amount).toBe(2);
expect(count).toHaveBeenCalledWith(params);
});
});
describe('update', () => {
test('Hash password', async () => {
const hash = 'aoizdnoaizndoainzodiaz';

View File

@ -119,6 +119,15 @@ const exists = async params => {
return foundCount > 0;
};
/**
* Count the number of users based on params
* @param params
* @returns {Promise<*>}
*/
const count = async (params = {}) => {
return strapi.query('role', 'admin').count(params);
};
/**
* Delete roles in database if they have no user assigned
* @param ids query params to find the roles
@ -265,6 +274,7 @@ module.exports = {
findAllWithUsersCount,
update,
exists,
count,
deleteByIds,
getUsersCount,
getSuperAdmin,

View File

@ -29,17 +29,20 @@ const create = async attributes => {
...attributes,
});
// hash password if a new one is sent
if (_.has(user, 'password')) {
const hashedPassword = await strapi.admin.services.auth.hashPassword(user.password);
return strapi.query('user', 'admin').create({
...user,
password: hashedPassword,
});
user.password = await strapi.admin.services.auth.hashPassword(user.password);
}
return strapi.query('user', 'admin').create(user);
const createdUser = await strapi.query('user', 'admin').create(user);
if (createdUser) {
const numberOfUsers = await count();
const numberOfRoles = await strapi.admin.services.role.count();
await strapi.telemetry.send('didInviteUser', { numberOfRoles, numberOfUsers });
}
return createdUser;
};
/**
@ -237,6 +240,15 @@ const countUsersWithoutRole = async () => {
return count;
};
/**
* Count number of users based on attributes
* @param attributes the filters used to filter the search
* @returns {Promise<number>}
*/
const count = async (attributes = {}) => {
return strapi.query('user', 'admin').count(attributes);
};
/** Assign some roles to several users
* @returns {undefined}
*/
@ -308,6 +320,7 @@ module.exports = {
deleteById,
deleteByIds,
countUsersWithoutRole,
count,
assignARoleToAll,
displayWarningIfUsersDontHaveRole,
migrateUsers,

View File

@ -6,6 +6,7 @@ const isDocker = require('is-docker');
const { machineIdSync } = require('node-machine-id');
const fetch = require('node-fetch');
const ciEnv = require('ci-info');
const ee = require('../../utils/ee');
const defaultQueryOpts = {
timeout: 1000,
@ -22,6 +23,7 @@ const ANALYTICS_URI = 'https://analytics.strapi.io';
module.exports = strapi => {
const uuid = strapi.config.uuid;
const deviceId = machineIdSync();
const isEE = strapi.EE === true && ee.isEE === true;
const anonymous_metadata = {
environment: strapi.config.environment,
@ -33,6 +35,7 @@ module.exports = strapi => {
isCI: ciEnv.isCI,
version: strapi.config.info.strapi,
strapiVersion: strapi.config.info.strapi,
projectType: isEE ? 'Enterprise' : 'Community',
};
return async (event, payload = {}, opts = {}) => {