BVB Source Codes

parse-server Show PushController.spec.js Source code

Return Download parse-server: download PushController.spec.js Source code - Download parse-server Source code - Type:.js
  1. "use strict";
  2. var PushController = require('../src/Controllers/PushController').PushController;
  3. var StatusHandler = require('../src/StatusHandler');
  4. var Config = require('../src/Config');
  5. var validatePushType = require('../src/Push/utils').validatePushType;
  6.  
  7. const successfulTransmissions = function(body, installations) {
  8.  
  9.   const promises = installations.map((device) => {
  10.     return Promise.resolve({
  11.       transmitted: true,
  12.       device: device,
  13.     })
  14.   });
  15.  
  16.   return Promise.all(promises);
  17. }
  18.  
  19. const successfulIOS = function(body, installations) {
  20.  
  21.   const promises = installations.map((device) => {
  22.     return Promise.resolve({
  23.       transmitted: device.deviceType == "ios",
  24.       device: device,
  25.     })
  26.   });
  27.  
  28.   return Promise.all(promises);
  29. }
  30.  
  31. describe('PushController', () => {
  32.   it('can validate device type when no device type is set', (done) => {
  33.     // Make query condition
  34.     var where = {
  35.     };
  36.     var validPushTypes = ['ios', 'android'];
  37.  
  38.     expect(function(){
  39.       validatePushType(where, validPushTypes);
  40.     }).not.toThrow();
  41.     done();
  42.   });
  43.  
  44.   it('can validate device type when single valid device type is set', (done) => {
  45.     // Make query condition
  46.     var where = {
  47.       'deviceType': 'ios'
  48.     };
  49.     var validPushTypes = ['ios', 'android'];
  50.  
  51.     expect(function(){
  52.       validatePushType(where, validPushTypes);
  53.     }).not.toThrow();
  54.     done();
  55.   });
  56.  
  57.   it('can validate device type when multiple valid device types are set', (done) => {
  58.     // Make query condition
  59.     var where = {
  60.       'deviceType': {
  61.         '$in': ['android', 'ios']
  62.       }
  63.     };
  64.     var validPushTypes = ['ios', 'android'];
  65.  
  66.     expect(function(){
  67.       validatePushType(where, validPushTypes);
  68.     }).not.toThrow();
  69.     done();
  70.   });
  71.  
  72.   it('can throw on validateDeviceType when single invalid device type is set', (done) => {
  73.     // Make query condition
  74.     var where = {
  75.       'deviceType': 'osx'
  76.     };
  77.     var validPushTypes = ['ios', 'android'];
  78.  
  79.     expect(function(){
  80.       validatePushType(where, validPushTypes);
  81.     }).toThrow();
  82.     done();
  83.   });
  84.  
  85.   it('can throw on validateDeviceType when single invalid device type is set', (done) => {
  86.     // Make query condition
  87.     var where = {
  88.       'deviceType': 'osx'
  89.     };
  90.     var validPushTypes = ['ios', 'android'];
  91.  
  92.     expect(function(){
  93.       validatePushType(where, validPushTypes);
  94.     }).toThrow();
  95.     done();
  96.   });
  97.  
  98.   it('can get expiration time in string format', (done) => {
  99.     // Make mock request
  100.     var timeStr = '2015-03-19T22:05:08Z';
  101.     var body = {
  102.       'expiration_time': timeStr
  103.     }
  104.  
  105.     var time = PushController.getExpirationTime(body);
  106.     expect(time).toEqual(new Date(timeStr).valueOf());
  107.     done();
  108.   });
  109.  
  110.   it('can get expiration time in number format', (done) => {
  111.     // Make mock request
  112.     var timeNumber = 1426802708;
  113.     var body = {
  114.       'expiration_time': timeNumber
  115.     }
  116.  
  117.     var time = PushController.getExpirationTime(body);
  118.     expect(time).toEqual(timeNumber * 1000);
  119.     done();
  120.   });
  121.  
  122.   it('can throw on getExpirationTime in invalid format', (done) => {
  123.     // Make mock request
  124.     var body = {
  125.       'expiration_time': 'abcd'
  126.     }
  127.  
  128.     expect(function(){
  129.       PushController.getExpirationTime(body);
  130.     }).toThrow();
  131.     done();
  132.   });
  133.  
  134.   it('can get push time in string format', (done) => {
  135.     // Make mock request
  136.     var timeStr = '2015-03-19T22:05:08Z';
  137.     var body = {
  138.       'push_time': timeStr
  139.     }
  140.  
  141.     var time = PushController.getPushTime(body);
  142.     expect(time).toEqual(new Date(timeStr));
  143.     done();
  144.   });
  145.  
  146.   it('can get push time in number format', (done) => {
  147.     // Make mock request
  148.     var timeNumber = 1426802708;
  149.     var body = {
  150.       'push_time': timeNumber
  151.     }
  152.  
  153.     var time = PushController.getPushTime(body).valueOf();
  154.     expect(time).toEqual(timeNumber * 1000);
  155.     done();
  156.   });
  157.  
  158.   it('can throw on getPushTime in invalid format', (done) => {
  159.     // Make mock request
  160.     var body = {
  161.       'push_time': 'abcd'
  162.     }
  163.  
  164.     expect(function(){
  165.       PushController.getPushTime(body);
  166.     }).toThrow();
  167.     done();
  168.   });
  169.  
  170.   it('properly increment badges', (done) => {
  171.     var pushAdapter = {
  172.       send: function(body, installations) {
  173.         var badge = body.data.badge;
  174.         installations.forEach((installation) => {
  175.           if (installation.deviceType == "ios") {
  176.             expect(installation.badge).toEqual(badge);
  177.             expect(installation.originalBadge + 1).toEqual(installation.badge);
  178.           } else {
  179.             expect(installation.badge).toBeUndefined();
  180.           }
  181.         })
  182.         return successfulTransmissions(body, installations);
  183.       },
  184.       getValidPushTypes: function() {
  185.         return ["ios", "android"];
  186.       }
  187.     }
  188.     var payload = {data:{
  189.       alert: "Hello World!",
  190.       badge: "Increment",
  191.     }}
  192.     var installations = [];
  193.     while(installations.length != 10) {
  194.       const installation = new Parse.Object("_Installation");
  195.       installation.set("installationId", "installation_" + installations.length);
  196.       installation.set("deviceToken","device_token_" + installations.length)
  197.       installation.set("badge", installations.length);
  198.       installation.set("originalBadge", installations.length);
  199.       installation.set("deviceType", "ios");
  200.       installations.push(installation);
  201.     }
  202.  
  203.     while(installations.length != 15) {
  204.       const installation = new Parse.Object("_Installation");
  205.       installation.set("installationId", "installation_" + installations.length);
  206.       installation.set("deviceToken","device_token_" + installations.length)
  207.       installation.set("deviceType", "android");
  208.       installations.push(installation);
  209.     }
  210.     var config = new Config(Parse.applicationId);
  211.     var auth = {
  212.       isMaster: true
  213.     }
  214.  
  215.     var pushController = new PushController();
  216.     reconfigureServer({
  217.       push: { adapter: pushAdapter }
  218.     }).then(() => {
  219.       return Parse.Object.saveAll(installations)
  220.     }).then(() => {
  221.       return pushController.sendPush(payload, {}, config, auth);
  222.     }).then(() => {
  223.       // Wait so the push is completed.
  224.       return new Promise((resolve) => { setTimeout(() => { resolve(); }, 1000); });
  225.     }).then(() => {
  226.       // Check we actually sent 15 pushes.
  227.       const query = new Parse.Query('_PushStatus');
  228.       return query.find({ useMasterKey: true })
  229.     }).then((results) => {
  230.       expect(results.length).toBe(1);
  231.       const pushStatus = results[0];
  232.       expect(pushStatus.get('numSent')).toBe(15);
  233.     }).then(() => {
  234.       // Check that the installations were actually updated.
  235.       const query = new Parse.Query('_Installation');
  236.       return query.find({ useMasterKey: true })
  237.     }).then((results) => {
  238.       expect(results.length).toBe(15);
  239.       for (var i = 0; i < 15; i++) {
  240.         const installation = results[i];
  241.         if (installation.get('deviceType') == 'ios') {
  242.           expect(installation.get('badge')).toBe(parseInt(installation.get('originalBadge')) + 1);
  243.         } else {
  244.           expect(installation.get('badge')).toBe(undefined);
  245.           expect(installation.get('originalBadge')).toBe(undefined);
  246.         }
  247.       }
  248.       done()
  249.     }).catch((err) => {
  250.       jfail(err);
  251.       done();
  252.     });
  253.   });
  254.  
  255.   it('properly set badges to 1', (done) => {
  256.  
  257.     var pushAdapter = {
  258.       send: function(body, installations) {
  259.         var badge = body.data.badge;
  260.         installations.forEach((installation) => {
  261.           expect(installation.badge).toEqual(badge);
  262.           expect(1).toEqual(installation.badge);
  263.         })
  264.         return successfulTransmissions(body, installations);
  265.       },
  266.       getValidPushTypes: function() {
  267.         return ["ios"];
  268.       }
  269.     }
  270.  
  271.     var payload = {data: {
  272.       alert: "Hello World!",
  273.       badge: 1,
  274.     }}
  275.     var installations = [];
  276.     while(installations.length != 10) {
  277.       var installation = new Parse.Object("_Installation");
  278.       installation.set("installationId", "installation_" + installations.length);
  279.       installation.set("deviceToken","device_token_" + installations.length)
  280.       installation.set("badge", installations.length);
  281.       installation.set("originalBadge", installations.length);
  282.       installation.set("deviceType", "ios");
  283.       installations.push(installation);
  284.     }
  285.  
  286.     var config = new Config(Parse.applicationId);
  287.     var auth = {
  288.       isMaster: true
  289.     }
  290.  
  291.     var pushController = new PushController();
  292.     reconfigureServer({
  293.       push: { adapter: pushAdapter }
  294.     }).then(() => {
  295.       return Parse.Object.saveAll(installations)
  296.     }).then(() => {
  297.       return pushController.sendPush(payload, {}, config, auth);
  298.     }).then(() => {
  299.       // Wait so the push is completed.
  300.       return new Promise((resolve) => { setTimeout(() => { resolve(); }, 1000); });
  301.     }).then(() => {
  302.       // Check we actually sent the pushes.
  303.       const query = new Parse.Query('_PushStatus');
  304.       return query.find({ useMasterKey: true })
  305.     }).then((results) => {
  306.       expect(results.length).toBe(1);
  307.       const pushStatus = results[0];
  308.       expect(pushStatus.get('numSent')).toBe(10);
  309.     }).then(() => {
  310.       // Check that the installations were actually updated.
  311.       const query = new Parse.Query('_Installation');
  312.       return query.find({ useMasterKey: true })
  313.     }).then((results) => {
  314.       expect(results.length).toBe(10);
  315.       for (var i = 0; i < 10; i++) {
  316.         const installation = results[i];
  317.         expect(installation.get('badge')).toBe(1);
  318.       }
  319.       done()
  320.     }).catch((err) => {
  321.       jfail(err);
  322.       done();
  323.     });
  324.   });
  325.  
  326.   it('properly set badges to 1 with complex query #2903 #3022', (done) => {
  327.  
  328.     var payload = {
  329.       data: {
  330.         alert: "Hello World!",
  331.         badge: 1,
  332.       }
  333.     }
  334.     var installations = [];
  335.     while(installations.length != 10) {
  336.       var installation = new Parse.Object("_Installation");
  337.       installation.set("installationId", "installation_" + installations.length);
  338.       installation.set("deviceToken","device_token_" + installations.length)
  339.       installation.set("badge", installations.length);
  340.       installation.set("originalBadge", installations.length);
  341.       installation.set("deviceType", "ios");
  342.       installations.push(installation);
  343.     }
  344.     let matchedInstallationsCount = 0;
  345.     var pushAdapter = {
  346.       send: function(body, installations) {
  347.         matchedInstallationsCount += installations.length;
  348.         var badge = body.data.badge;
  349.         installations.forEach((installation) => {
  350.           expect(installation.badge).toEqual(badge);
  351.           expect(1).toEqual(installation.badge);
  352.         })
  353.         return successfulTransmissions(body, installations);
  354.       },
  355.       getValidPushTypes: function() {
  356.         return ["ios"];
  357.       }
  358.     }
  359.  
  360.     var config = new Config(Parse.applicationId);
  361.     var auth = {
  362.       isMaster: true
  363.     }
  364.     var pushController = new PushController();
  365.     reconfigureServer({
  366.       push: {
  367.         adapter: pushAdapter
  368.       }
  369.     }).then(() => {
  370.       return Parse.Object.saveAll(installations)
  371.     }).then((installations) => {
  372.       const objectIds = installations.map(installation => {
  373.         return installation.id;
  374.       })
  375.       const where = {
  376.         objectId: {'$in': objectIds.slice(0, 5)}
  377.       }
  378.       return pushController.sendPush(payload, where, config, auth);
  379.     }).then(() => {
  380.       return new Promise((res) => {
  381.         setTimeout(res, 300);
  382.       });
  383.     }).then(() => {
  384.       expect(matchedInstallationsCount).toBe(5);
  385.       const query = new Parse.Query(Parse.Installation);
  386.       query.equalTo('badge', 1);
  387.       return query.find({useMasterKey: true});
  388.     }).then((installations) => {
  389.       expect(installations.length).toBe(5);
  390.       done();
  391.     }).catch(() => {
  392.       fail("should not fail");
  393.       done();
  394.     });
  395.   });
  396.  
  397.   it('properly creates _PushStatus', (done) => {
  398.  
  399.     var installations = [];
  400.     while(installations.length != 10) {
  401.       const installation = new Parse.Object("_Installation");
  402.       installation.set("installationId", "installation_" + installations.length);
  403.       installation.set("deviceToken","device_token_" + installations.length)
  404.       installation.set("badge", installations.length);
  405.       installation.set("originalBadge", installations.length);
  406.       installation.set("deviceType", "ios");
  407.       installations.push(installation);
  408.     }
  409.  
  410.     while(installations.length != 15) {
  411.       const installation = new Parse.Object("_Installation");
  412.       installation.set("installationId", "installation_" + installations.length);
  413.       installation.set("deviceToken","device_token_" + installations.length)
  414.       installation.set("deviceType", "android");
  415.       installations.push(installation);
  416.     }
  417.     var payload = {data: {
  418.       alert: "Hello World!",
  419.       badge: 1,
  420.     }}
  421.  
  422.     var pushAdapter = {
  423.       send: function(body, installations) {
  424.         return successfulIOS(body, installations);
  425.       },
  426.       getValidPushTypes: function() {
  427.         return ["ios"];
  428.       }
  429.     }
  430.  
  431.     var config = new Config(Parse.applicationId);
  432.     var auth = {
  433.       isMaster: true
  434.     }
  435.     var pushController = new PushController();
  436.     reconfigureServer({
  437.       push: { adapter: pushAdapter }
  438.     }).then(() => {
  439.       return Parse.Object.saveAll(installations)
  440.     })
  441.    .then(() => {
  442.      return pushController.sendPush(payload, {}, config, auth);
  443.    }).then(() => {
  444.      // it is enqueued so it can take time
  445.      return new Promise((resolve) => {
  446.        setTimeout(() => {
  447.          resolve();
  448.        }, 1000);
  449.      });
  450.    }).then(() => {
  451.      const query = new Parse.Query('_PushStatus');
  452.      return query.find({useMasterKey: true});
  453.    }).then((results) => {
  454.      expect(results.length).toBe(1);
  455.      const result = results[0];
  456.      expect(result.createdAt instanceof Date).toBe(true);
  457.      expect(result.updatedAt instanceof Date).toBe(true);
  458.      expect(result.id.length).toBe(10);
  459.      expect(result.get('source')).toEqual('rest');
  460.      expect(result.get('query')).toEqual(JSON.stringify({}));
  461.      expect(typeof result.get('payload')).toEqual("string");
  462.      expect(JSON.parse(result.get('payload'))).toEqual(payload.data);
  463.      expect(result.get('status')).toEqual('succeeded');
  464.      expect(result.get('numSent')).toEqual(10);
  465.      expect(result.get('sentPerType')).toEqual({
  466.        'ios': 10 // 10 ios
  467.      });
  468.      expect(result.get('numFailed')).toEqual(5);
  469.      expect(result.get('failedPerType')).toEqual({
  470.        'android': 5 // android
  471.      });
  472.      // Try to get it without masterKey
  473.      const query = new Parse.Query('_PushStatus');
  474.      return query.find();
  475.    }).then((results) => {
  476.      expect(results.length).toBe(0);
  477.      done();
  478.    });
  479.   });
  480.  
  481.   it('should properly report failures in _PushStatus', (done) => {
  482.     var pushAdapter = {
  483.       send: function(body, installations) {
  484.         return installations.map((installation) => {
  485.           return Promise.resolve({
  486.             deviceType: installation.deviceType
  487.           })
  488.         })
  489.       },
  490.       getValidPushTypes: function() {
  491.         return ["ios"];
  492.       }
  493.     }
  494.     const where = { 'channels': {
  495.       '$ins': ['Giants', 'Mets']
  496.     }};
  497.     var payload = {data: {
  498.       alert: "Hello World!",
  499.       badge: 1,
  500.     }}
  501.     var config = new Config(Parse.applicationId);
  502.     var auth = {
  503.       isMaster: true
  504.     }
  505.     var pushController = new PushController();
  506.     reconfigureServer({
  507.       push: { adapter: pushAdapter }
  508.     }).then(() => {
  509.       return pushController.sendPush(payload, where, config, auth)
  510.     }).then(() => {
  511.       fail('should not succeed');
  512.       done();
  513.     }).catch(() => {
  514.       const query = new Parse.Query('_PushStatus');
  515.       query.find({useMasterKey: true}).then((results) => {
  516.         expect(results.length).toBe(1);
  517.         const pushStatus = results[0];
  518.         expect(pushStatus.get('status')).toBe('failed');
  519.         done();
  520.       });
  521.     });
  522.   });
  523.  
  524.   it('should support full RESTQuery for increment', (done) => {
  525.     var payload = {data: {
  526.       alert: "Hello World!",
  527.       badge: 'Increment',
  528.     }}
  529.  
  530.     var pushAdapter = {
  531.       send: function(body, installations) {
  532.         return successfulTransmissions(body, installations);
  533.       },
  534.       getValidPushTypes: function() {
  535.         return ["ios"];
  536.       }
  537.     }
  538.     var config = new Config(Parse.applicationId);
  539.     var auth = {
  540.       isMaster: true
  541.     }
  542.  
  543.     const where = {
  544.       'deviceToken': {
  545.         '$in': ['device_token_0', 'device_token_1', 'device_token_2']
  546.       }
  547.     }
  548.  
  549.     var pushController = new PushController();
  550.     reconfigureServer({
  551.       push: { adapter: pushAdapter }
  552.     }).then(() => {
  553.       var installations = [];
  554.       while (installations.length != 5) {
  555.         const installation = new Parse.Object("_Installation");
  556.         installation.set("installationId", "installation_" + installations.length);
  557.         installation.set("deviceToken", "device_token_" + installations.length)
  558.         installation.set("badge", installations.length);
  559.         installation.set("originalBadge", installations.length);
  560.         installation.set("deviceType", "ios");
  561.         installations.push(installation);
  562.       }
  563.       return Parse.Object.saveAll(installations);
  564.     }).then(() => {
  565.       return pushController.sendPush(payload, where, config, auth);
  566.     }).then(() => {
  567.       // Wait so the push is completed.
  568.       return new Promise((resolve) => { setTimeout(() => { resolve(); }, 1000); });
  569.     }).then(() => {
  570.       const query = new Parse.Query('_PushStatus');
  571.       return query.find({ useMasterKey: true })
  572.     }).then((results) => {
  573.       expect(results.length).toBe(1);
  574.       const pushStatus = results[0];
  575.       expect(pushStatus.get('numSent')).toBe(3);
  576.       done();
  577.     }).catch((err) => {
  578.       jfail(err);
  579.       done();
  580.     });
  581.   });
  582.  
  583.   it('should support object type for alert', (done) => {
  584.     var payload = {data: {
  585.       alert: {
  586.         'loc-key': 'hello_world',
  587.       },
  588.     }}
  589.  
  590.     var pushAdapter = {
  591.       send: function(body, installations) {
  592.         return successfulTransmissions(body, installations);
  593.       },
  594.       getValidPushTypes: function() {
  595.         return ["ios"];
  596.       }
  597.     }
  598.  
  599.     var config = new Config(Parse.applicationId);
  600.     var auth = {
  601.       isMaster: true
  602.     }
  603.  
  604.     const where = {
  605.       'deviceType': 'ios'
  606.     }
  607.  
  608.     var pushController = new PushController();
  609.     reconfigureServer({
  610.       push: { adapter: pushAdapter }
  611.     }).then(() => {
  612.       var installations = [];
  613.       while (installations.length != 5) {
  614.         const installation = new Parse.Object("_Installation");
  615.         installation.set("installationId", "installation_" + installations.length);
  616.         installation.set("deviceToken", "device_token_" + installations.length)
  617.         installation.set("badge", installations.length);
  618.         installation.set("originalBadge", installations.length);
  619.         installation.set("deviceType", "ios");
  620.         installations.push(installation);
  621.       }
  622.       return Parse.Object.saveAll(installations);
  623.     }).then(() => {
  624.       return pushController.sendPush(payload, where, config, auth)
  625.     }).then(() => {
  626.       // Wait so the push is completed.
  627.       return new Promise((resolve) => { setTimeout(() => { resolve(); }, 1000); });
  628.     }).then(() => {
  629.       const query = new Parse.Query('_PushStatus');
  630.       return query.find({ useMasterKey: true })
  631.     }).then((results) => {
  632.       expect(results.length).toBe(1);
  633.       const pushStatus = results[0];
  634.       expect(pushStatus.get('numSent')).toBe(5);
  635.       done();
  636.     }).catch(() => {
  637.       fail('should not fail');
  638.       done();
  639.     });
  640.   });
  641.  
  642.   it('should flatten', () => {
  643.     var res = StatusHandler.flatten([1, [2], [[3, 4], 5], [[[6]]]])
  644.     expect(res).toEqual([1,2,3,4,5,6]);
  645.   });
  646.  
  647.   it('properly transforms push time', () => {
  648.     expect(PushController.getPushTime()).toBe(undefined);
  649.     expect(PushController.getPushTime({
  650.       'push_time': 1000
  651.     })).toEqual(new Date(1000 * 1000));
  652.     expect(PushController.getPushTime({
  653.       'push_time': '2017-01-01'
  654.     })).toEqual(new Date('2017-01-01'));
  655.     expect(() => {PushController.getPushTime({
  656.       'push_time': 'gibberish-time'
  657.     })}).toThrow();
  658.     expect(() => {PushController.getPushTime({
  659.       'push_time': Number.NaN
  660.     })}).toThrow();
  661.   });
  662.  
  663.   it('should not schedule push when not configured', (done) => {
  664.     var config = new Config(Parse.applicationId);
  665.     var auth = {
  666.       isMaster: true
  667.     }
  668.     var pushAdapter = {
  669.       send: function(body, installations) {
  670.         return successfulTransmissions(body, installations);
  671.       },
  672.       getValidPushTypes: function() {
  673.         return ["ios"];
  674.       }
  675.     }
  676.  
  677.     var pushController = new PushController();
  678.     const payload = {
  679.       data: {
  680.         alert: 'hello',
  681.       },
  682.       push_time: new Date().getTime()
  683.     }
  684.  
  685.     var installations = [];
  686.     while(installations.length != 10) {
  687.       const installation = new Parse.Object("_Installation");
  688.       installation.set("installationId", "installation_" + installations.length);
  689.       installation.set("deviceToken","device_token_" + installations.length)
  690.       installation.set("badge", installations.length);
  691.       installation.set("originalBadge", installations.length);
  692.       installation.set("deviceType", "ios");
  693.       installations.push(installation);
  694.     }
  695.  
  696.     reconfigureServer({
  697.       push: { adapter: pushAdapter }
  698.     }).then(() => {
  699.       return Parse.Object.saveAll(installations).then(() => {
  700.         return pushController.sendPush(payload, {}, config, auth);
  701.       });
  702.     }).then(() => {
  703.       const query = new Parse.Query('_PushStatus');
  704.       return query.find({useMasterKey: true}).then((results) => {
  705.         expect(results.length).toBe(1);
  706.         const pushStatus = results[0];
  707.         expect(pushStatus.get('status')).not.toBe('scheduled');
  708.         done();
  709.       });
  710.     }).catch((err) => {
  711.       console.error(err);
  712.       fail('should not fail');
  713.       done();
  714.     });
  715.   });
  716.  
  717.   it('should schedule push when configured', (done) => {
  718.     var auth = {
  719.       isMaster: true
  720.     }
  721.     var pushAdapter = {
  722.       send: function(body, installations) {
  723.         const promises = installations.map((device) => {
  724.           if (!device.deviceToken) {
  725.             // Simulate error when device token is not set
  726.             return Promise.reject();
  727.           }
  728.           return Promise.resolve({
  729.             transmitted: true,
  730.             device: device,
  731.           })
  732.         });
  733.  
  734.         return Promise.all(promises);
  735.       },
  736.       getValidPushTypes: function() {
  737.         return ["ios"];
  738.       }
  739.     }
  740.  
  741.     var pushController = new PushController();
  742.     const payload = {
  743.       data: {
  744.         alert: 'hello',
  745.       },
  746.       push_time: new Date().getTime() / 1000
  747.     }
  748.  
  749.     var installations = [];
  750.     while(installations.length != 10) {
  751.       const installation = new Parse.Object("_Installation");
  752.       installation.set("installationId", "installation_" + installations.length);
  753.       installation.set("deviceToken","device_token_" + installations.length)
  754.       installation.set("badge", installations.length);
  755.       installation.set("originalBadge", installations.length);
  756.       installation.set("deviceType", "ios");
  757.       installations.push(installation);
  758.     }
  759.  
  760.     reconfigureServer({
  761.       push: { adapter: pushAdapter },
  762.       scheduledPush: true
  763.     }).then(() => {
  764.       var config = new Config(Parse.applicationId);
  765.       return Parse.Object.saveAll(installations).then(() => {
  766.         return pushController.sendPush(payload, {}, config, auth);
  767.       }).then(() => new Promise(resolve => setTimeout(resolve, 100)));
  768.     }).then(() => {
  769.       const query = new Parse.Query('_PushStatus');
  770.       return query.find({useMasterKey: true}).then((results) => {
  771.         expect(results.length).toBe(1);
  772.         const pushStatus = results[0];
  773.         expect(pushStatus.get('status')).toBe('scheduled');
  774.         done();
  775.       });
  776.     }).catch((err) => {
  777.       console.error(err);
  778.       fail('should not fail');
  779.       done();
  780.     });
  781.   });
  782.  
  783.   it('should not enqueue push when device token is not set', (done) => {
  784.     var auth = {
  785.       isMaster: true
  786.     }
  787.     var pushAdapter = {
  788.       send: function(body, installations) {
  789.         const promises = installations.map((device) => {
  790.           if (!device.deviceToken) {
  791.             // Simulate error when device token is not set
  792.             return Promise.reject();
  793.           }
  794.           return Promise.resolve({
  795.             transmitted: true,
  796.             device: device,
  797.           })
  798.         });
  799.  
  800.         return Promise.all(promises);
  801.       },
  802.       getValidPushTypes: function() {
  803.         return ["ios"];
  804.       }
  805.     }
  806.  
  807.     var pushController = new PushController();
  808.     const payload = {
  809.       data: {
  810.         alert: 'hello',
  811.       },
  812.       push_time: new Date().getTime() / 1000
  813.     }
  814.  
  815.     var installations = [];
  816.     while(installations.length != 5) {
  817.       const installation = new Parse.Object("_Installation");
  818.       installation.set("installationId", "installation_" + installations.length);
  819.       installation.set("deviceToken","device_token_" + installations.length)
  820.       installation.set("badge", installations.length);
  821.       installation.set("originalBadge", installations.length);
  822.       installation.set("deviceType", "ios");
  823.       installations.push(installation);
  824.     }
  825.  
  826.     while(installations.length != 15) {
  827.       const installation = new Parse.Object("_Installation");
  828.       installation.set("installationId", "installation_" + installations.length);
  829.       installation.set("badge", installations.length);
  830.       installation.set("originalBadge", installations.length);
  831.       installation.set("deviceType", "ios");
  832.       installations.push(installation);
  833.     }
  834.  
  835.     reconfigureServer({
  836.       push: { adapter: pushAdapter }
  837.     }).then(() => {
  838.       var config = new Config(Parse.applicationId);
  839.       return Parse.Object.saveAll(installations).then(() => {
  840.         return pushController.sendPush(payload, {}, config, auth);
  841.       }).then(() => new Promise(resolve => setTimeout(resolve, 100)));
  842.     }).then(() => {
  843.       const query = new Parse.Query('_PushStatus');
  844.       return query.find({useMasterKey: true}).then((results) => {
  845.         expect(results.length).toBe(1);
  846.         const pushStatus = results[0];
  847.         expect(pushStatus.get('numSent')).toBe(5);
  848.         expect(pushStatus.get('status')).toBe('succeeded');
  849.         done();
  850.       });
  851.     }).catch((err) => {
  852.       console.error(err);
  853.       fail('should not fail');
  854.       done();
  855.     });
  856.  
  857.   });
  858. });
  859.  
downloadPushController.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