Add unit tests to the admin hash generation, fix minor requests

This commit is contained in:
ivanThePleasant 2022-10-28 14:01:37 +03:00
parent efb4125181
commit 6c1b0010e6
6 changed files with 36 additions and 4 deletions

View File

@ -78,7 +78,7 @@ const AuthenticatedApp = () => {
const shouldShowLoader = isLoading || shouldShowNotDependentQueriesLoader;
const adminUserId = userInfo ? hashAdminUserEmail(userInfo) : '';
const adminUserId = hashAdminUserEmail(userInfo);
const appInfosValue = useMemo(() => {
return {
...appInfos,

View File

@ -10,6 +10,9 @@ async function digestMessage(message) {
}
export default async function hashAdminUserEmail(payload) {
if (!payload) {
return '';
}
try {
return await digestMessage(payload.email);
} catch (error) {

View File

@ -57,7 +57,7 @@ describe('Metrics', () => {
expect(getLanguagesInUse).toHaveBeenCalledWith();
expect(send).toHaveBeenCalledWith('didChangeInterfaceLanguage', {
groupProperties: {
userProperties: {
languagesInUse: ['en', 'fr', 'en'],
},
});

View File

@ -17,7 +17,7 @@ const sendDidUpdateRolePermissions = async () => {
const sendDidChangeInterfaceLanguage = async () => {
const languagesInUse = await getService('user').getLanguagesInUse();
// This event is anonymous
strapi.telemetry.send('didChangeInterfaceLanguage', { groupProperties: { languagesInUse } });
strapi.telemetry.send('didChangeInterfaceLanguage', { userProperties: { languagesInUse } });
};
module.exports = {

View File

@ -0,0 +1,29 @@
'use strict';
const crypto = require('crypto');
const { generateAdminUserHash } = require('../admin-user-hash');
const createContext = require('../../../../../../../test/helpers/create-context');
describe('user email hash', () => {
test('should create a hash from admin email', () => {
const state = {
user: {
email: 'testemail@strapi.io',
},
};
const ctx = createContext({}, { state });
const hash = crypto.createHash('sha256').update('testemail@strapi.io').digest('hex');
const adminUserId = generateAdminUserHash(ctx);
expect(adminUserId).toBe(hash);
});
test('should return empty string if user is not available on ctx', () => {
const ctx = createContext({}, {});
const adminUserId = generateAdminUserHash(ctx);
expect(adminUserId).toBe('');
});
});

View File

@ -74,7 +74,7 @@ module.exports = (strapi) => {
event,
adminUserId,
deviceId,
eventProperties: adminUserId ? payload.eventProperties : {},
eventProperties: payload.eventProperties,
userProperties: adminUserId
? { ...anonymousUserProperties, ...payload.userProperties }
: {},