Skip to content

Commit

Permalink
Merge pull request #16 from Trazi-Ventures/fix-#47-audit-user-new-val…
Browse files Browse the repository at this point in the history
…-json

Fix json and buffer data audit values
  • Loading branch information
Phara0h committed Apr 4, 2022
2 parents a6f2b96 + 3d1f325 commit a882df8
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 17 deletions.
8 changes: 4 additions & 4 deletions include/utils/audit.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,16 +52,16 @@ function createAuditObject(opts, prop, oldPropValue, newPropValue) {
audit.prop = prop.toString();
}
if (oldPropValue) {
try {
if (typeof oldPropValue === 'object') {
audit.old_val = JSON.stringify(oldPropValue);
} catch {
} else {
audit.old_val = oldPropValue.toString();
}
}
if (newPropValue) {
try {
if (!newPropValue.buffer && typeof newPropValue === 'object') {
audit.new_val = JSON.stringify(newPropValue);
} catch {
} else {
audit.new_val = newPropValue.toString();
}
}
Expand Down
36 changes: 36 additions & 0 deletions tests/endpoints/audit.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const config = require('../../include/utils/config');
const User = require('../../include/database/models/user');
const Audit = require('../../include/database/models/audit');
const { Travelling } = require('../../sdk/node')('http://127.0.0.1:6969/' + config.serviceName, {
resolveWithFullResponse: true
});
Expand Down Expand Up @@ -421,6 +422,41 @@ module.exports = () => {
expect(res.statusCode).toEqual(200);
expect(res.body.length).toEqual(0);
});

test('Checking Audit of (Edit Test User Email Property [test2])', async () => {
const u = await User.findAllBy({ username: 'test2' });
const audit = await Audit.findAllBy({ of_user_id: u[0].id, action: 'EDIT', subaction: 'USER_PROPERTY' });

expect(audit[0]).toHaveProperty('id');
expect(audit[0].created_on).not.toBeNull();
expect(audit[0].prop).toEqual('email');
expect(audit[0].old_val).toMatch('[email protected]');
expect(audit[0].new_val).toMatch('[email protected]');

expect(audit[1]).toHaveProperty('id');
expect(audit[1].created_on).not.toBeNull();
expect(audit[1].prop).toEqual('email');
expect(audit[1].old_val).toMatch('[email protected]');
expect(audit[1].new_val).toMatch('[email protected]');

expect(audit[2]).toHaveProperty('id');
expect(audit[2].created_on).not.toBeNull();
expect(audit[2].prop).toEqual('email');
expect(audit[2].old_val).toMatch('[email protected]');
expect(audit[2].new_val).toMatch('[email protected]');

expect(audit[3]).toHaveProperty('id');
expect(audit[3].created_on).not.toBeNull();
expect(audit[3].prop).toEqual('user_data');
expect(audit[3].old_val).toBeNull();
expect(audit[3].new_val).toMatch('{"test":1,"foo":"bar"}');

expect(audit[4]).toHaveProperty('id');
expect(audit[4].created_on).not.toBeNull();
expect(audit[4].prop).toEqual('user_data');
expect(audit[4].old_val).toMatch('{"test":1,"foo":"bar"}');
expect(audit[4].new_val).toMatch('{"notes":"notey totey"}');
});
});

describe('Invalid', () => {
Expand Down
24 changes: 11 additions & 13 deletions tests/endpoints/user-edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,19 +58,6 @@ module.exports = () => {
expect(res.statusCode).toEqual(200);
});

test('Checking Audit of (Edit Test User Email Property [test2])', async () => {
if (config.audit.edit.enable === true) {
const u = await User.findAllBy({ username: 'test2' });
const audit = await Audit.findAllBy({ of_user_id: u[0].id, action: 'EDIT', subaction: 'USER_PROPERTY' });

expect(audit[0]).toHaveProperty('id');
expect(audit[0].created_on).not.toBeNull();
expect(audit[0].prop).toEqual('email');
expect(audit[0].old_val).toMatch('[email protected]');
expect(audit[0].new_val).toMatch('[email protected]');
}
});

test('Edit Test User 2 Email Property Value ', async () => {
var res = await Travelling.User.editPropertyValue('test2', 'email', '[email protected]', userContainer.user1Token);

Expand All @@ -89,6 +76,17 @@ module.exports = () => {
expect(res.body).toMatchObject({ email: '[email protected]', user_data: { test: 1, foo: 'bar' } });
});

test('Edit Test User Update Existing UserData', async () => {
var res = await Travelling.User.edit(
{ user_data: { notes: 'notey totey' } },
'test2',
userContainer.user1Token
);

expect(res.statusCode).toEqual(200);
expect(res.body).toMatchObject({ user_data: { notes: 'notey totey' } });
});

test('Edit Test User 1 Email Property By GroupRequest', async () => {
var res = await Travelling.Group.Request.User.editProperty(
'[email protected]',
Expand Down

0 comments on commit a882df8

Please sign in to comment.