BVB Source Codes

parse-server Show rest.spec.js Source code

Return Download parse-server: download rest.spec.js Source code - Download parse-server Source code - Type:.js
  1. "use strict";
  2. // These tests check the "create" / "update" functionality of the REST API.
  3. var auth = require('../src/Auth');
  4. var Config = require('../src/Config');
  5. var Parse = require('parse/node').Parse;
  6. var rest = require('../src/rest');
  7. var request = require('request');
  8.  
  9. let config;
  10. let database;
  11.  
  12. describe('rest create', () => {
  13.  
  14.   beforeEach(() => {
  15.     config = new Config('test');
  16.     database = config.database;
  17.   });
  18.  
  19.   it('handles _id', done => {
  20.     rest.create(config, auth.nobody(config), 'Foo', {})
  21.     .then(() => database.adapter.find('Foo', { fields: {} }, {}, {}))
  22.     .then(results => {
  23.       expect(results.length).toEqual(1);
  24.       var obj = results[0];
  25.       expect(typeof obj.objectId).toEqual('string');
  26.       expect(obj._id).toBeUndefined();
  27.       done();
  28.     });
  29.   });
  30.  
  31.   it('handles array, object, date', (done) => {
  32.     const now = new Date();
  33.     var obj = {
  34.       array: [1, 2, 3],
  35.       object: {foo: 'bar'},
  36.       date: Parse._encode(now),
  37.     };
  38.     rest.create(config, auth.nobody(config), 'MyClass', obj)
  39.     .then(() => database.adapter.find('MyClass', { fields: {
  40.       array: { type: 'Array' },
  41.       object: { type: 'Object' },
  42.       date: { type: 'Date' },
  43.     } }, {}, {}))
  44.     .then(results => {
  45.       expect(results.length).toEqual(1);
  46.       var mob = results[0];
  47.       expect(mob.array instanceof Array).toBe(true);
  48.       expect(typeof mob.object).toBe('object');
  49.       expect(mob.date.__type).toBe('Date');
  50.       expect(new Date(mob.date.iso).getTime()).toBe(now.getTime());
  51.       done();
  52.     });
  53.   });
  54.  
  55.   it('handles object and subdocument', done => {
  56.     const obj = { subdoc: {foo: 'bar', wu: 'tan'} };
  57.     rest.create(config, auth.nobody(config), 'MyClass', obj)
  58.     .then(() => database.adapter.find('MyClass', { fields: {} }, {}, {}))
  59.     .then(results => {
  60.       expect(results.length).toEqual(1);
  61.       const mob = results[0];
  62.       expect(typeof mob.subdoc).toBe('object');
  63.       expect(mob.subdoc.foo).toBe('bar');
  64.       expect(mob.subdoc.wu).toBe('tan');
  65.       expect(typeof mob.objectId).toEqual('string');
  66.       const obj = { 'subdoc.wu': 'clan' };
  67.       return rest.update(config, auth.nobody(config), 'MyClass', { objectId: mob.objectId }, obj)
  68.     })
  69.     .then(() => database.adapter.find('MyClass', { fields: {} }, {}, {}))
  70.     .then(results => {
  71.       expect(results.length).toEqual(1);
  72.       const mob = results[0];
  73.       expect(typeof mob.subdoc).toBe('object');
  74.       expect(mob.subdoc.foo).toBe('bar');
  75.       expect(mob.subdoc.wu).toBe('clan');
  76.       done();
  77.     })
  78.     .catch(error => {
  79.       console.log(error);
  80.       fail();
  81.       done();
  82.     });
  83.   });
  84.  
  85.   it('handles create on non-existent class when disabled client class creation', (done) => {
  86.     var customConfig = Object.assign({}, config, {allowClientClassCreation: false});
  87.     rest.create(customConfig, auth.nobody(customConfig), 'ClientClassCreation', {})
  88.       .then(() => {
  89.         fail('Should throw an error');
  90.         done();
  91.       }, (err) => {
  92.         expect(err.code).toEqual(Parse.Error.OPERATION_FORBIDDEN);
  93.         expect(err.message).toEqual('This user is not allowed to access ' +
  94.                                     'non-existent class: ClientClassCreation');
  95.         done();
  96.       });
  97.   });
  98.  
  99.   it('handles create on existent class when disabled client class creation', (done) => {
  100.     var customConfig = Object.assign({}, config, {allowClientClassCreation: false});
  101.     config.database.loadSchema()
  102.     .then(schema => schema.addClassIfNotExists('ClientClassCreation', {}))
  103.     .then(actualSchema => {
  104.       expect(actualSchema.className).toEqual('ClientClassCreation');
  105.       return rest.create(customConfig, auth.nobody(customConfig), 'ClientClassCreation', {});
  106.     })
  107.     .then(() => {
  108.       done();
  109.     }, () => {
  110.       fail('Should not throw error')
  111.     });
  112.   });
  113.  
  114.   it('handles user signup', (done) => {
  115.     var user = {
  116.       username: 'asdf',
  117.       password: 'zxcv',
  118.       foo: 'bar',
  119.     };
  120.     rest.create(config, auth.nobody(config), '_User', user)
  121.       .then((r) => {
  122.         expect(Object.keys(r.response).length).toEqual(3);
  123.         expect(typeof r.response.objectId).toEqual('string');
  124.         expect(typeof r.response.createdAt).toEqual('string');
  125.         expect(typeof r.response.sessionToken).toEqual('string');
  126.         done();
  127.       });
  128.   });
  129.  
  130.   it('handles anonymous user signup', (done) => {
  131.     var data1 = {
  132.       authData: {
  133.         anonymous: {
  134.           id: '00000000-0000-0000-0000-000000000001'
  135.         }
  136.       }
  137.     };
  138.     var data2 = {
  139.       authData: {
  140.         anonymous: {
  141.           id: '00000000-0000-0000-0000-000000000002'
  142.         }
  143.       }
  144.     };
  145.     var username1;
  146.     rest.create(config, auth.nobody(config), '_User', data1)
  147.       .then((r) => {
  148.         expect(typeof r.response.objectId).toEqual('string');
  149.         expect(typeof r.response.createdAt).toEqual('string');
  150.         expect(typeof r.response.sessionToken).toEqual('string');
  151.         expect(typeof r.response.username).toEqual('string');
  152.         return rest.create(config, auth.nobody(config), '_User', data1);
  153.       }).then((r) => {
  154.         expect(typeof r.response.objectId).toEqual('string');
  155.         expect(typeof r.response.createdAt).toEqual('string');
  156.         expect(typeof r.response.username).toEqual('string');
  157.         expect(typeof r.response.updatedAt).toEqual('string');
  158.         username1 = r.response.username;
  159.         return rest.create(config, auth.nobody(config), '_User', data2);
  160.       }).then((r) => {
  161.         expect(typeof r.response.objectId).toEqual('string');
  162.         expect(typeof r.response.createdAt).toEqual('string');
  163.         expect(typeof r.response.sessionToken).toEqual('string');
  164.         return rest.create(config, auth.nobody(config), '_User', data2);
  165.       }).then((r) => {
  166.         expect(typeof r.response.objectId).toEqual('string');
  167.         expect(typeof r.response.createdAt).toEqual('string');
  168.         expect(typeof r.response.username).toEqual('string');
  169.         expect(typeof r.response.updatedAt).toEqual('string');
  170.         expect(r.response.username).not.toEqual(username1);
  171.         done();
  172.       });
  173.   });
  174.  
  175.   it('handles anonymous user signup and upgrade to new user', (done) => {
  176.     var data1 = {
  177.       authData: {
  178.         anonymous: {
  179.           id: '00000000-0000-0000-0000-000000000001'
  180.         }
  181.       }
  182.     };
  183.  
  184.     var updatedData = {
  185.       authData: { anonymous: null },
  186.       username: 'hello',
  187.       password: 'world'
  188.     }
  189.     var objectId;
  190.     rest.create(config, auth.nobody(config), '_User', data1)
  191.       .then((r) => {
  192.         expect(typeof r.response.objectId).toEqual('string');
  193.         expect(typeof r.response.createdAt).toEqual('string');
  194.         expect(typeof r.response.sessionToken).toEqual('string');
  195.         objectId = r.response.objectId;
  196.         return auth.getAuthForSessionToken({config, sessionToken: r.response.sessionToken })
  197.       }).then((sessionAuth) => {
  198.         return rest.update(config, sessionAuth, '_User', { objectId }, updatedData);
  199.       }).then(() => {
  200.         return Parse.User.logOut().then(() => {
  201.           return Parse.User.logIn('hello', 'world');
  202.         })
  203.       }).then((r) => {
  204.         expect(r.id).toEqual(objectId);
  205.         expect(r.get('username')).toEqual('hello');
  206.         done();
  207.       }).catch((err) => {
  208.         jfail(err);
  209.         done();
  210.       })
  211.   });
  212.  
  213.   it('handles no anonymous users config', (done) => {
  214.     var NoAnnonConfig = Object.assign({}, config);
  215.     NoAnnonConfig.authDataManager.setEnableAnonymousUsers(false);
  216.     var data1 = {
  217.       authData: {
  218.         anonymous: {
  219.           id: '00000000-0000-0000-0000-000000000001'
  220.         }
  221.       }
  222.     };
  223.     rest.create(NoAnnonConfig, auth.nobody(NoAnnonConfig), '_User', data1).then(() => {
  224.       fail("Should throw an error");
  225.       done();
  226.     }, (err) => {
  227.       expect(err.code).toEqual(Parse.Error.UNSUPPORTED_SERVICE);
  228.       expect(err.message).toEqual('This authentication method is unsupported.');
  229.       NoAnnonConfig.authDataManager.setEnableAnonymousUsers(true);
  230.       done();
  231.     })
  232.   });
  233.  
  234.   it('test facebook signup and login', (done) => {
  235.     var data = {
  236.       authData: {
  237.         facebook: {
  238.           id: '8675309',
  239.           access_token: 'jenny'
  240.         }
  241.       }
  242.     };
  243.     var newUserSignedUpByFacebookObjectId;
  244.     rest.create(config, auth.nobody(config), '_User', data)
  245.       .then((r) => {
  246.         expect(typeof r.response.objectId).toEqual('string');
  247.         expect(typeof r.response.createdAt).toEqual('string');
  248.         expect(typeof r.response.sessionToken).toEqual('string');
  249.         newUserSignedUpByFacebookObjectId = r.response.objectId;
  250.         return rest.create(config, auth.nobody(config), '_User', data);
  251.       }).then((r) => {
  252.         expect(typeof r.response.objectId).toEqual('string');
  253.         expect(typeof r.response.createdAt).toEqual('string');
  254.         expect(typeof r.response.username).toEqual('string');
  255.         expect(typeof r.response.updatedAt).toEqual('string');
  256.         expect(r.response.objectId).toEqual(newUserSignedUpByFacebookObjectId);
  257.         return rest.find(config, auth.master(config),
  258.                           '_Session', {sessionToken: r.response.sessionToken});
  259.       }).then((response) => {
  260.         expect(response.results.length).toEqual(1);
  261.         var output = response.results[0];
  262.         expect(output.user.objectId).toEqual(newUserSignedUpByFacebookObjectId);
  263.         done();
  264.       }).catch(err => {
  265.         jfail(err);
  266.         done();
  267.       })
  268.   });
  269.  
  270.   it('stores pointers', done => {
  271.     const obj = {
  272.       foo: 'bar',
  273.       aPointer: {
  274.         __type: 'Pointer',
  275.         className: 'JustThePointer',
  276.         objectId: 'qwerty1234' // make it 10 chars to match PG storage
  277.       }
  278.     };
  279.     rest.create(config, auth.nobody(config), 'APointerDarkly', obj)
  280.     .then(() => database.adapter.find('APointerDarkly', { fields: {
  281.       foo: { type: 'String' },
  282.       aPointer: { type: 'Pointer', targetClass: 'JustThePointer' },
  283.     }}, {}, {}))
  284.     .then(results => {
  285.       expect(results.length).toEqual(1);
  286.       const output = results[0];
  287.       expect(typeof output.foo).toEqual('string');
  288.       expect(typeof output._p_aPointer).toEqual('undefined');
  289.       expect(output._p_aPointer).toBeUndefined();
  290.       expect(output.aPointer).toEqual({
  291.         __type: 'Pointer',
  292.         className: 'JustThePointer',
  293.         objectId: 'qwerty1234'
  294.       });
  295.       done();
  296.     });
  297.   });
  298.  
  299.   it("cannot set objectId", (done) => {
  300.     var headers = {
  301.       'Content-Type': 'application/octet-stream',
  302.       'X-Parse-Application-Id': 'test',
  303.       'X-Parse-REST-API-Key': 'rest'
  304.     };
  305.     request.post({
  306.       headers: headers,
  307.       url: 'http://localhost:8378/1/classes/TestObject',
  308.       body: JSON.stringify({
  309.         'foo': 'bar',
  310.         'objectId': 'hello'
  311.       })
  312.     }, (error, response, body) => {
  313.       var b = JSON.parse(body);
  314.       expect(b.code).toEqual(105);
  315.       expect(b.error).toEqual('objectId is an invalid field name.');
  316.       done();
  317.     });
  318.   });
  319.  
  320.   it("test default session length", (done) => {
  321.     var user = {
  322.       username: 'asdf',
  323.       password: 'zxcv',
  324.       foo: 'bar',
  325.     };
  326.     var now = new Date();
  327.  
  328.     rest.create(config, auth.nobody(config), '_User', user)
  329.       .then((r) => {
  330.         expect(Object.keys(r.response).length).toEqual(3);
  331.         expect(typeof r.response.objectId).toEqual('string');
  332.         expect(typeof r.response.createdAt).toEqual('string');
  333.         expect(typeof r.response.sessionToken).toEqual('string');
  334.         return rest.find(config, auth.master(config),
  335.                           '_Session', {sessionToken: r.response.sessionToken});
  336.       })
  337.       .then((r) => {
  338.         expect(r.results.length).toEqual(1);
  339.  
  340.         var session = r.results[0];
  341.         var actual = new Date(session.expiresAt.iso);
  342.         var expected = new Date(now.getTime() + (1000 * 3600 * 24 * 365));
  343.  
  344.         expect(actual.getFullYear()).toEqual(expected.getFullYear());
  345.         expect(actual.getMonth()).toEqual(expected.getMonth());
  346.         expect(actual.getDate()).toEqual(expected.getDate());
  347.         // less than a minute, if test happen at the wrong time :/
  348.         expect(actual.getMinutes() - expected.getMinutes() <= 1).toBe(true);
  349.  
  350.         done();
  351.       });
  352.   });
  353.  
  354.   it("test specified session length", (done) => {
  355.     var user = {
  356.       username: 'asdf',
  357.       password: 'zxcv',
  358.       foo: 'bar',
  359.     };
  360.     var sessionLength = 3600, // 1 Hour ahead
  361.       now = new Date(); // For reference later
  362.     config.sessionLength = sessionLength;
  363.  
  364.     rest.create(config, auth.nobody(config), '_User', user)
  365.       .then((r) => {
  366.         expect(Object.keys(r.response).length).toEqual(3);
  367.         expect(typeof r.response.objectId).toEqual('string');
  368.         expect(typeof r.response.createdAt).toEqual('string');
  369.         expect(typeof r.response.sessionToken).toEqual('string');
  370.         return rest.find(config, auth.master(config),
  371.                           '_Session', {sessionToken: r.response.sessionToken});
  372.       })
  373.       .then((r) => {
  374.         expect(r.results.length).toEqual(1);
  375.  
  376.         var session = r.results[0];
  377.         var actual = new Date(session.expiresAt.iso);
  378.         var expected = new Date(now.getTime() + (sessionLength * 1000));
  379.  
  380.         expect(actual.getFullYear()).toEqual(expected.getFullYear());
  381.         expect(actual.getMonth()).toEqual(expected.getMonth());
  382.         expect(actual.getDate()).toEqual(expected.getDate());
  383.         expect(actual.getHours()).toEqual(expected.getHours());
  384.         expect(actual.getMinutes()).toEqual(expected.getMinutes());
  385.  
  386.         done();
  387.       }).catch(err => {
  388.         jfail(err);
  389.         done();
  390.       });
  391.   });
  392.  
  393.   it("can create a session with no expiration", (done) => {
  394.     var user = {
  395.       username: 'asdf',
  396.       password: 'zxcv',
  397.       foo: 'bar'
  398.     };
  399.     config.expireInactiveSessions = false;
  400.  
  401.     rest.create(config, auth.nobody(config), '_User', user)
  402.       .then((r) => {
  403.         expect(Object.keys(r.response).length).toEqual(3);
  404.         expect(typeof r.response.objectId).toEqual('string');
  405.         expect(typeof r.response.createdAt).toEqual('string');
  406.         expect(typeof r.response.sessionToken).toEqual('string');
  407.         return rest.find(config, auth.master(config),
  408.           '_Session', {sessionToken: r.response.sessionToken});
  409.       })
  410.       .then((r) => {
  411.         expect(r.results.length).toEqual(1);
  412.  
  413.         var session = r.results[0];
  414.         expect(session.expiresAt).toBeUndefined();
  415.  
  416.         done();
  417.       }).catch(err => {
  418.         console.error(err);
  419.         fail(err);
  420.         done();
  421.       })
  422.   });
  423. });
  424.  
  425. describe('rest update', () => {
  426.  
  427.   it('ignores createdAt', done => {
  428.     const nobody = auth.nobody(config);
  429.     const className = 'Foo';
  430.     const newCreatedAt = new Date('1970-01-01T00:00:00.000Z');
  431.  
  432.     rest.create(config, nobody, className, {}).then(res => {
  433.       const objectId = res.response.objectId;
  434.       const restObject = {
  435.         createdAt: {__type: "Date", iso: newCreatedAt}, // should be ignored
  436.       };
  437.  
  438.       return rest.update(config, nobody, className, { objectId }, restObject).then(() => {
  439.         const restWhere = {
  440.           objectId: objectId,
  441.         };
  442.         return rest.find(config, nobody, className, restWhere, {});
  443.       });
  444.     }).then(res2 => {
  445.       const updatedObject = res2.results[0];
  446.       expect(new Date(updatedObject.createdAt)).not.toEqual(newCreatedAt);
  447.       done();
  448.     }).then(done).catch(err => {
  449.       fail(err);
  450.       done();
  451.     });
  452.   });
  453.  
  454. });
  455.  
downloadrest.spec.js Source code - Download parse-server Source code
Related Source Codes/Software:
react-boilerplate - 2017-06-07
webtorrent - Streaming torrent client for the web ... 2017-06-06
machine-learning-for-software-engineers - A complete daily plan for studying to become a mac... 2017-06-06
upterm - A terminal emulator for the 21st century. 2017-06-06
lottie-android - Render After Effects animations natively on Androi... 2017-06-07
AsyncDisplayKit - Smooth asynchronous user interfaces for iOS apps. ... 2017-06-07
ionicons - The premium icon font for Ionic ... 2017-06-07
storybook - 2017-06-07
prettier - Prettier is an opinionated JavaScript formatter. ... 2017-06-08
CRYENGINE - CRYENGINE is a powerful real-time game development... 2017-06-11
postal - 2017-06-11
reactide - Reactide is the first dedicated IDE for React web ... 2017-06-11
rkt - rkt is a pod-native container engine for Linux. It... 2017-06-11
uWebSockets - Tiny WebSockets https://for... 2017-06-11
realworld - TodoMVC for the RealWorld - Exemplary fullstack Me... 2017-06-11
goreplay - GoReplay is an open-source tool for capturing and ... 2017-06-10
pyenv - Simple Python version management 2017-06-10
redux-saga - An alternative side effect model for Redux apps ... 2017-06-10
angular-starter - 2017-06-10

 Back to top