-
Notifications
You must be signed in to change notification settings - Fork 98
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added function for safe cloning user object
- Loading branch information
1 parent
70852ce
commit bb08bef
Showing
2 changed files
with
86 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
const assert = require('chai').assert; | ||
const helpers = require('../src/helpers'); | ||
|
||
describe('helpers - sanitization', () => { | ||
it('allows to stringify sanitized user object', () => { | ||
const user = { | ||
id: 1, | ||
email: 'test@test.test', | ||
password: '0000000000', | ||
resetToken: 'aaa', | ||
}; | ||
|
||
const result1 = helpers.sanitizeUserForClient(user); | ||
const result2 = helpers.sanitizeUserForNotifier(user); | ||
|
||
assert.doesNotThrow(() => JSON.stringify(result1)); | ||
assert.doesNotThrow(() => JSON.stringify(result2)); | ||
}); | ||
|
||
it('throws error when stringifying sanitized object with circular reference', () => { | ||
const user = { | ||
id: 1, | ||
email: 'test@test.test', | ||
password: '0000000000', | ||
resetToken: 'aaa' | ||
}; | ||
|
||
user.self = user; | ||
|
||
const result1 = helpers.sanitizeUserForClient(user); | ||
const result2 = helpers.sanitizeUserForNotifier(user); | ||
|
||
assert.throws(() => JSON.stringify(result1), TypeError); | ||
assert.throws(() => JSON.stringify(result2), TypeError); | ||
}); | ||
|
||
it('allows to stringify sanitized object with circular reference and custom toJSON()', () => { | ||
const user = { | ||
id: 1, | ||
email: 'test@test.test', | ||
password: '0000000000', | ||
resetToken: 'aaa', | ||
toJSON: function() { | ||
return Object.assign({}, this, { self: undefined }); | ||
} | ||
}; | ||
|
||
user.self = user; | ||
|
||
const result1 = helpers.sanitizeUserForClient(user); | ||
const result2 = helpers.sanitizeUserForNotifier(user); | ||
|
||
assert.doesNotThrow(() => JSON.stringify(result1)); | ||
assert.doesNotThrow(() => JSON.stringify(result2)); | ||
}); | ||
}); |