processNested.spec.js 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. 'use strict';
  2. const assert = require('assert');
  3. const processNested = require('../lib/processNested');
  4. describe('Test Convert Flatten object to Nested object', function() {
  5. it('With no nested data', () => {
  6. const data = {
  7. 'firstname': 'John',
  8. 'lastname': 'Doe',
  9. 'age': 22
  10. },
  11. excerpt = { firstname: 'John', lastname: 'Doe', age: 22 },
  12. processed = processNested(data);
  13. assert.deepEqual(processed, excerpt);
  14. });
  15. it('With nested data', () => {
  16. const data = {
  17. 'firstname': 'John',
  18. 'lastname': 'Doe',
  19. 'age': 22,
  20. 'hobbies[0]': 'Cinema',
  21. 'hobbies[1]': 'Bike',
  22. 'address[line]': '78 Lynch Street',
  23. 'address[city]': 'Milwaukee',
  24. 'friends[0][name]': 'Jane',
  25. 'friends[0][lastname]': 'Doe',
  26. 'friends[1][name]': 'Joe',
  27. 'friends[1][lastname]': 'Doe'
  28. },
  29. excerpt = {
  30. firstname: 'John',
  31. lastname: 'Doe',
  32. age: 22,
  33. hobbies: [ 'Cinema', 'Bike' ],
  34. address: { line: '78 Lynch Street', city: 'Milwaukee' },
  35. friends: [
  36. { name: 'Jane', lastname: 'Doe' },
  37. { name: 'Joe', lastname: 'Doe' }
  38. ]
  39. },
  40. processed = processNested(data);
  41. assert.deepEqual(processed, excerpt);
  42. });
  43. it('Do not allow prototype pollution', () => {
  44. const pollutionOb1 = JSON.parse(`{"__proto__.POLLUTED1": "FOOBAR"}`);
  45. const pollutionOb2 = JSON.parse(`{"constructor.prototype.POLLUTED2": "FOOBAR"}`);
  46. processNested(pollutionOb1);
  47. processNested(pollutionOb2);
  48. assert.equal(global.POLLUTED1, undefined);
  49. assert.equal(global.POLLUTED2, undefined);
  50. });
  51. });