BVB Source Codes

parse-server Show Parse.Push.spec.js Source code

Return Download parse-server: download Parse.Push.spec.js Source code - Download parse-server Source code - Type:.js
  1. 'use strict';
  2.  
  3. const request = require('request');
  4.  
  5. const delayPromise = (delay) => {
  6.   return new Promise((resolve) => {
  7.     setTimeout(resolve, delay);
  8.   });
  9. }
  10.  
  11. describe('Parse.Push', () => {
  12.   var setup = function() {
  13.     var sendToInstallationSpy = jasmine.createSpy();
  14.  
  15.     var pushAdapter = {
  16.       send: function(body, installations) {
  17.         var badge = body.data.badge;
  18.         const promises = installations.map((installation) => {
  19.           sendToInstallationSpy(installation);
  20.  
  21.           if (installation.deviceType == "ios") {
  22.             expect(installation.badge).toEqual(badge);
  23.             expect(installation.originalBadge + 1).toEqual(installation.badge);
  24.           } else {
  25.             expect(installation.badge).toBeUndefined();
  26.           }
  27.           return Promise.resolve({
  28.             err: null,
  29.             device: installation,
  30.             transmitted: true
  31.           })
  32.         });
  33.         return Promise.all(promises);
  34.       },
  35.       getValidPushTypes: function() {
  36.         return ["ios", "android"];
  37.       }
  38.     }
  39.  
  40.     return reconfigureServer({
  41.       appId: Parse.applicationId,
  42.       masterKey: Parse.masterKey,
  43.       serverURL: Parse.serverURL,
  44.       push: {
  45.         adapter: pushAdapter
  46.       }
  47.     })
  48.     .then(() => {
  49.       var installations = [];
  50.       while(installations.length != 10) {
  51.         var installation = new Parse.Object("_Installation");
  52.         installation.set("installationId", "installation_" + installations.length);
  53.         installation.set("deviceToken","device_token_" + installations.length)
  54.         installation.set("badge", installations.length);
  55.         installation.set("originalBadge", installations.length);
  56.         installation.set("deviceType", "ios");
  57.         installations.push(installation);
  58.       }
  59.       return Parse.Object.saveAll(installations);
  60.     })
  61.     .then(() => {
  62.       return {
  63.         sendToInstallationSpy,
  64.       };
  65.     })
  66.     .catch((err) => {
  67.       console.error(err);
  68.  
  69.       throw err;
  70.     })
  71.   }
  72.  
  73.   it('should properly send push', (done) => {
  74.     return setup().then(({ sendToInstallationSpy }) => {
  75.       return Parse.Push.send({
  76.         where: {
  77.           deviceType: 'ios'
  78.         },
  79.         data: {
  80.           badge: 'Increment',
  81.           alert: 'Hello world!'
  82.         }
  83.       }, {useMasterKey: true})
  84.       .then(() => {
  85.         return delayPromise(500);
  86.       })
  87.       .then(() => {
  88.         expect(sendToInstallationSpy.calls.count()).toEqual(10);
  89.       })
  90.     }).then(() => {
  91.       done();
  92.     }).catch((err) => {
  93.       jfail(err);
  94.       done();
  95.     });
  96.   });
  97.  
  98.   it('should properly send push with lowercaseIncrement', (done) => {
  99.     return setup().then(() => {
  100.       return Parse.Push.send({
  101.         where: {
  102.           deviceType: 'ios'
  103.         },
  104.         data: {
  105.           badge: 'increment',
  106.           alert: 'Hello world!'
  107.         }
  108.       }, {useMasterKey: true})
  109.     }).then(() => {
  110.       return delayPromise(500);
  111.     }).then(() => {
  112.       done();
  113.     }).catch((err) => {
  114.       jfail(err);
  115.       done();
  116.     });
  117.   });
  118.  
  119.   it('should not allow clients to query _PushStatus', done => {
  120.     setup()
  121.     .then(() => Parse.Push.send({
  122.       where: {
  123.         deviceType: 'ios'
  124.       },
  125.       data: {
  126.         badge: 'increment',
  127.         alert: 'Hello world!'
  128.       }
  129.     }, {useMasterKey: true}))
  130.     .then(() => {
  131.       request.get({
  132.         url: 'http://localhost:8378/1/classes/_PushStatus',
  133.         json: true,
  134.         headers: {
  135.           'X-Parse-Application-Id': 'test',
  136.         },
  137.       }, (error, response, body) => {
  138.         expect(body.error).toEqual('unauthorized');
  139.         done();
  140.       });
  141.     }).catch((err) => {
  142.       jfail(err);
  143.       done();
  144.     });
  145.   });
  146.  
  147.   it('should allow master key to query _PushStatus', done => {
  148.     setup()
  149.     .then(() => Parse.Push.send({
  150.       where: {
  151.         deviceType: 'ios'
  152.       },
  153.       data: {
  154.         badge: 'increment',
  155.         alert: 'Hello world!'
  156.       }
  157.     }, {useMasterKey: true}))
  158.     .then(() => {
  159.       request.get({
  160.         url: 'http://localhost:8378/1/classes/_PushStatus',
  161.         json: true,
  162.         headers: {
  163.           'X-Parse-Application-Id': 'test',
  164.           'X-Parse-Master-Key': 'test',
  165.         },
  166.       }, (error, response, body) => {
  167.         try {
  168.           expect(body.results.length).toEqual(1);
  169.           expect(body.results[0].query).toEqual('{"deviceType":"ios"}');
  170.           expect(body.results[0].payload).toEqual('{"badge":"increment","alert":"Hello world!"}');
  171.         } catch(e) {
  172.           jfail(e);
  173.         }
  174.         done();
  175.       });
  176.     }).catch((err) => {
  177.       jfail(err);
  178.       done();
  179.     });
  180.   });
  181.  
  182.   it('should throw error if missing push configuration', done => {
  183.     reconfigureServer({push: null})
  184.     .then(() => {
  185.       return Parse.Push.send({
  186.         where: {
  187.           deviceType: 'ios'
  188.         },
  189.         data: {
  190.           badge: 'increment',
  191.           alert: 'Hello world!'
  192.         }
  193.       }, {useMasterKey: true})
  194.     }).then(() => {
  195.       fail('should not succeed');
  196.     }, (err) => {
  197.       expect(err.code).toEqual(Parse.Error.PUSH_MISCONFIGURED);
  198.       done();
  199.     }).catch((err) => {
  200.       jfail(err);
  201.       done();
  202.     });
  203.   });
  204. });
  205.  
downloadParse.Push.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