Advertisement
Guest User

Untitled

a guest
Aug 15th, 2017
480
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 51.33 KB | None | 0 0
  1. /* jslint node: true */
  2. /* global describe, before, it, expect */
  3. 'use strict';
  4.  
  5. const assert = require('assert');
  6. const async = require('async');
  7. const utils = require('../utils');
  8. const uuid = require('node-uuid');
  9. const app = require('../../server/server');
  10. const supertest = require('supertest');
  11. const server = supertest(app);
  12. const Account = app.models.Account;
  13. const Device = app.models.Device;
  14. const Media = app.models.Media;
  15. const App = app.models.App;
  16. const DeviceApp = app.models.DeviceApp;
  17. const AccessToken = app.models.AccessToken;
  18. const Filter = app.models.Filter;
  19. const CurfewBlock = app.models.CurfewBlock;
  20. const LocationLog = app.models.LocationLog;
  21. const Photo = app.models.Photo;
  22. const PhotoLike = app.models.PhotoLike;
  23. const PhotoComment = app.models.PhotoComment;
  24. const DeviceAppLog = app.models.DeviceAppLog;
  25. const Activity = app.models.Activity;
  26. const Geofence = app.models.Geofence;
  27. const GeofenceSummary = app.models.GeofenceSummary;
  28. const moment = require('moment-timezone');
  29. const queue = require('../../server/lib/queue.js');
  30. const sinon = require('sinon');
  31. const _ = require('lodash');
  32.  
  33. const facebookToken = (`CAACEdEose0cBAJwdPzRD7WmCEqWv1wZCqNIFSv7qsztahYzjVUJoZAmGGgcodxw8m6Mo5xocAs
  34. 2PunZCrXXP7nR1MhIz467YeBwjP2XrQN26m7ZBQL5NNEyOCAIteYxyTS5NouzrIDxThUJE1LY
  35. ZBLOM8gLkuXlGVQrAYce6xhQAtaZA3ZAlrLiJQcHZBYgdVqNGEbqmlFD1bjgbfvIujvBL`);
  36.  
  37. let userAccessToken1, userAccessToken2, devices;
  38.  
  39. describe('Device', () => {
  40.  
  41. // setup the test environment
  42. before(function(done) {
  43. this.timeout(5000);
  44. utils.resetDatabase(err => {
  45. if (err) {
  46. return done(err);
  47. }
  48. // create test data as appropriate
  49. async.series([
  50. callback => {
  51. Account.create({
  52. firstname: 'Ethan',
  53. lastname: 'Hunt',
  54. email: 'ehunt32@imf.us.gov',
  55. password: 'supersecret1',
  56. mediaId: 1
  57. }, callback);
  58. },
  59. callback => {
  60. AccessToken.create({
  61. userId: 1
  62. }, (err, accessToken) => {
  63. userAccessToken1 = accessToken.id;
  64. callback();
  65. });
  66. },
  67. callback => {
  68. Account.create({
  69. firstname: 'blah',
  70. lastname: 'blah',
  71. email: 'blah32@imf.us.gov',
  72. password: 'supersecret1',
  73. mediaId: 1
  74. }, callback);
  75. },
  76. callback => {
  77. AccessToken.create({
  78. userId: 2
  79. }, (err, accessToken) => {
  80. userAccessToken2 = accessToken.id;
  81. callback();
  82. });
  83. },
  84. callback => {
  85. Media.create({
  86. accountId: 1,
  87. filename: 'string',
  88. originalURL: 'string',
  89. largeURL: 'string',
  90. mediumURL: 'string',
  91. smallURL: 'string',
  92. created: new Date()
  93. }, callback);
  94. },
  95. callback => {
  96. App.create({
  97. name: 'Facebook',
  98. bundleID: 1,
  99. mediaId: 1,
  100. description: 'Social Media',
  101. flagged: false,
  102. created: new Date()
  103. }, callback);
  104. },
  105. callback => {
  106. App.create({
  107. name: 'Instagram',
  108. bundleID: 1,
  109. mediaId: 1,
  110. description: 'Social Media',
  111. flagged: true,
  112. created: new Date()
  113. }, callback);
  114. },
  115. callback => {
  116. App.create({
  117. name: 'Youtube',
  118. bundleID: 1,
  119. mediaId: 1,
  120. description: 'Streaming site/ Socal Media',
  121. flagged: false,
  122. created: new Date()
  123. }, callback);
  124. },
  125. callback => {
  126. async.eachSeries([1, 3], (value, callback) => {
  127. let deviceId = value;
  128. async.series([
  129. callback => {
  130. DeviceApp.create({
  131. deviceId: deviceId,
  132. appId: 1,
  133. installed: new Date(),
  134. uninstalled: null
  135. }, callback);
  136. },
  137. callback => {
  138. DeviceApp.create({
  139. deviceId: deviceId,
  140. appId: 2,
  141. installed: new Date(),
  142. uninstalled: null
  143. }, callback);
  144. },
  145. callback => {
  146. DeviceApp.create({
  147. deviceId: deviceId,
  148. appId: 3,
  149. installed: new Date(),
  150. uninstalled: null
  151. }, callback);
  152. }
  153. ], done => {
  154. callback();
  155. });
  156. }, done => {
  157. callback();
  158. });
  159. },
  160. callback => {
  161. Device.create({
  162. accountId: 1,
  163. name: 'string',
  164. mediaId: 1,
  165. color: 'string',
  166. type: 0,
  167. udid: uuid.v4(),
  168. facebookToken: facebookToken,
  169. facebookId: 'string',
  170. instagramToken: 'string',
  171. instagramId: 'string',
  172. mdmToken: 'string',
  173. mdmPushToken: 'string',
  174. mdmPushMagic: 'string',
  175. mdmUnlockToken: 'string',
  176. deleted: false,
  177. created: new Date(),
  178. timezone: 'GMT',
  179. facebookConnectedOn: new Date(),
  180. instagramConnectedOn: new Date(),
  181. facebookOk: true,
  182. instagramOk: true,
  183. osVersion: '4.4.2',
  184. appVersion: '1.10.6',
  185. updateFrequency: 30
  186. }, callback);
  187. },
  188. callback => {
  189. Device.create({
  190. accountId: 2,
  191. name: 'string',
  192. mediaId: 1,
  193. color: 'string',
  194. type: 1,
  195. udid: uuid.v4(),
  196. facebookToken: 'string',
  197. facebookId: 'string',
  198. instagramToken: 'string',
  199. instagramId: 'string',
  200. mdmToken: '',
  201. mdmPushToken: 'string',
  202. mdmPushMagic: 'string',
  203. mdmUnlockToken: 'string',
  204. deleted: false,
  205. created: new Date(),
  206. timezone: 'GMT',
  207. facebookConnectedOn: new Date(),
  208. instagramConnectedOn: new Date(),
  209. facebookOk: true,
  210. instagramOk: true,
  211. osVersion: '4.4.2',
  212. appVersion: '1.10.6',
  213. updateFrequency: 30
  214. }, callback);
  215. },
  216. callback => {
  217. Device.create({
  218. accountId: 1,
  219. name: 'string',
  220. mediaId: 1,
  221. color: 'string',
  222. type: 0,
  223. udid: uuid.v4(),
  224. facebookToken: 'string',
  225. facebookId: 'string',
  226. instagramToken: 'string',
  227. instagramId: 'string',
  228. mdmToken: '',
  229. mdmPushToken: 'string',
  230. mdmPushMagic: 'string',
  231. mdmUnlockToken: 'string',
  232. deleted: false,
  233. created: new Date(),
  234. timezone: 'GMT',
  235. facebookConnectedOn: new Date(),
  236. instagramConnectedOn: new Date(),
  237. facebookOk: true,
  238. instagramOk: true,
  239. osVersion: '4.4.2',
  240. appVersion: '1.10.6',
  241. updateFrequency: 30
  242. }, callback);
  243. },
  244. callback => {
  245. Device.find({}, (err, res) => {
  246. if (err) {
  247. return callback(err);
  248. }
  249.  
  250. devices = res;
  251. return callback();
  252. });
  253. },
  254. callback => {
  255. Filter.create({
  256. deviceId: 1,
  257. phrase: 'stri',
  258. disabled: false
  259. }, callback);
  260. },
  261. callback => {
  262. Filter.create({
  263. deviceId: 1,
  264. phrase: 'STRinG',
  265. disabled: false
  266. }, callback);
  267. },
  268. callback => {
  269. Filter.create({
  270. phrase: 'STRinG',
  271. disabled: false
  272. }, callback);
  273. },
  274. callback => {
  275. Filter.create({
  276. deviceId: 1,
  277. phrase: 'strin',
  278. disabled: false
  279. }, callback);
  280. },
  281. callback => {
  282. Filter.create({
  283. phrase: 'strin',
  284. disabled: false
  285. }, callback);
  286. },
  287. callback => {
  288. Filter.create({
  289. deviceId: 1,
  290. phrase: 'this is a string',
  291. disabled: false
  292. }, callback);
  293. },
  294. callback => {
  295. Filter.create({
  296. deviceId: 1,
  297. phrase: 'test',
  298. disabled: false
  299. }, callback);
  300. },
  301. callback => {
  302. Filter.create({
  303. deviceId: 1,
  304. phrase: 'stringy',
  305. disabled: true
  306. }, callback);
  307. },
  308. callback => {
  309. CurfewBlock.create({
  310. deviceId: 1,
  311. day: 1,
  312. start: '11:00',
  313. end: '12:00'
  314. }, callback);
  315. },
  316. callback => {
  317. CurfewBlock.create({
  318. deviceId: 1,
  319. day: 1,
  320. start: '13:00',
  321. end: '14:00'
  322. }, callback);
  323. },
  324. callback => {
  325. CurfewBlock.create({
  326. deviceId: 1,
  327. day: 1,
  328. start: '15:00',
  329. end: '16:00'
  330. }, callback);
  331. },
  332. callback => {
  333. CurfewBlock.create({
  334. deviceId: 1,
  335. day: 2,
  336. start: '15:00',
  337. end: '16:00'
  338. }, callback);
  339. },
  340. callback => {
  341. CurfewBlock.create({
  342. deviceId: 2,
  343. day: 1,
  344. start: '15:00',
  345. end: '16:00'
  346. }, callback);
  347. },
  348. callback => {
  349. LocationLog.create({
  350. deviceId: 1,
  351. latitude: 30.000035762786865,
  352. longitude: 124.97748613357544,
  353. address: '123 Main St',
  354. created: moment('2016-02-16').tz('Asia/Manila').format('x')
  355. }, callback);
  356. },
  357. callback => {
  358. LocationLog.create({
  359. deviceId: 1,
  360. latitude: 29.999992847442627,
  361. longitude: 124.9774432182312,
  362. address: '456 Other St',
  363. created: moment('2016-02-17').tz('Asia/Manila').format('x')
  364. }, callback);
  365. },
  366. callback => {
  367. LocationLog.create({
  368. deviceId: 2,
  369. latitude: 29.999992847442627,
  370. longitude: 124.9774432182312,
  371. address: '456 Other St',
  372. created: moment('2016-02-17').tz('Asia/Manila').format('x')
  373. }, callback);
  374. },
  375. callback => {
  376. LocationLog.create({
  377. deviceId: 1,
  378. latitude: 24.000035762786865,
  379. longitude: 45.97748613357544,
  380. address: 'blah blah',
  381. created: moment('2016-02-19').tz('Asia/Manila').format('x')
  382. }, callback);
  383. },
  384. callback => {
  385. LocationLog.create({
  386. deviceId: 1,
  387. latitude: 24.000035762786865,
  388. longitude: 45.97748613357544,
  389. address: '789 South St',
  390. created: moment('2016-02-09').tz('Asia/Manila').format('x')
  391. }, callback);
  392. },
  393. callback => {
  394. DeviceAppLog.create({
  395. deviceId: 1,
  396. appId: 1,
  397. action: 1,
  398. created: new Date()
  399. }, callback);
  400. },
  401. callback => {
  402. DeviceAppLog.create({
  403. deviceId: 1,
  404. appId: 2,
  405. action: 1,
  406. created: new Date()
  407. }, callback);
  408. },
  409. callback => {
  410. DeviceAppLog.create({
  411. deviceId: 2,
  412. appId: 3,
  413. action: 1,
  414. created: new Date()
  415. }, callback);
  416. },
  417. callback => {
  418. Photo.create({
  419. id: '3cf95a90-d3c9-11e5-a8f5-dd5eeac92e98',
  420. deviceId: 1,
  421. description: 'string',
  422. originalURL: 'string',
  423. smallURL: 'string',
  424. mediumURL: 'string',
  425. largeURL: 'string',
  426. commentCount: 0,
  427. likeCount: 0,
  428. source: 0,
  429. sourceId: 'string',
  430. created: new Date(),
  431. imported: 0
  432. }, (err, resp) => {
  433. async.eachSeries([1, 2], (value, callback) => {
  434. PhotoLike.create({
  435. photoId: resp.id,
  436. sourceId: 'string',
  437. username: 'asdasdasd'
  438. }, callback);
  439. }, done => {
  440. PhotoComment.create({
  441. photoId: resp.id,
  442. sourceId: 'string',
  443. username: 'asdasdasd',
  444. comment: 'this is a test',
  445. created: new Date()
  446. }, callback);
  447. });
  448. });
  449. },
  450. callback => {
  451. Photo.create({
  452. id: '3cf95a90-d3c9-11e5-a8f5-dd5eeac92e99',
  453. deviceId: 1,
  454. description: 'string',
  455. originalURL: 'string',
  456. smallURL: 'string',
  457. mediumURL: 'string',
  458. largeURL: 'string',
  459. commentCount: 0,
  460. likeCount: 0,
  461. source: 0,
  462. sourceId: 'string',
  463. created: new Date(),
  464. imported: 0
  465. }, (err, resp) => {
  466. PhotoLike.create({
  467. photoId: resp.id,
  468. sourceId: 'string',
  469. username: 'asdasdasd'
  470. }, callback);
  471. });
  472. },
  473. callback => {
  474. Photo.create({
  475. id: '3cf95a90-d3c9-11e5-a8f5-dd5eeac92100',
  476. deviceId: 2,
  477. description: 'string',
  478. originalURL: 'string',
  479. smallURL: 'string',
  480. mediumURL: 'string',
  481. largeURL: 'string',
  482. commentCount: 0,
  483. likeCount: 0,
  484. source: 0,
  485. sourceId: 'string',
  486. created: new Date(),
  487. imported: 0
  488. }, (err, resp) => {
  489. PhotoLike.create({
  490. photoId: resp.id,
  491. sourceId: 'string',
  492. username: 'asdasdasd'
  493. }, callback);
  494. });
  495. },
  496. callback => {
  497. Geofence.create({
  498. deviceId: 1,
  499. name: 'Test Geofence Alert True',
  500. color: 'FFFFFF',
  501. center: {
  502. lat: 14.583091,
  503. lng: 121.060972
  504. },
  505. radius: 10,
  506. arrives: true,
  507. leaves: true,
  508. disabled: false
  509. }, callback);
  510. },
  511. callback => {
  512. Geofence.create({
  513. deviceId: 1,
  514. name: 'Test Geofence Alert True',
  515. color: 'FFFFFF',
  516. center: {
  517. lat: 24.583091,
  518. lng: 121.060972
  519. },
  520. radius: 10,
  521. arrives: true,
  522. leaves: true,
  523. disabled: false
  524. }, callback);
  525. }
  526. ],
  527. err => {
  528. done(err);
  529. });
  530. });
  531. });
  532.  
  533. describe('ACL tests', () => {
  534. it('should allow you to view your own filters', done => {
  535. server.get('/api/v2/Devices/1/Filters/search?q=str')
  536. .set('Authorization', userAccessToken1)
  537. .expect(200)
  538. .end((err, res) => {
  539. assert.equal(res.status, 200, 'expects status equal to 200');
  540. done(err);
  541. });
  542. });
  543.  
  544. it('should allow you to view Other Apps', done => {
  545. server.get('/api/v2/Devices/1/Apps/current/other')
  546. .set('Authorization', userAccessToken1)
  547. .expect(200)
  548. .end((err, res) => {
  549. assert.equal(res.status, 200, 'expects status equal to 200');
  550. done(err);
  551. });
  552. });
  553.  
  554. it('should allow you to view your own apps of concern', done => {
  555. server.get('/api/v2/Devices/1/Apps/current/concern')
  556. .set('Authorization', userAccessToken1)
  557. .expect(200)
  558. .end((err, res) => {
  559. assert.equal(res.status, 200, 'expects status equal to 200');
  560. done(err);
  561. });
  562. });
  563.  
  564. it('should allow you to view your own curfewblock', done => {
  565. server.get('/api/v2/Devices/1/CurfewBlocks/?day=1')
  566. .set('Authorization', userAccessToken1)
  567. .expect(200)
  568. .end((err, res) => {
  569. assert.equal(res.status, 200, 'expects status equal to 200');
  570. done(err);
  571. });
  572. });
  573.  
  574. it('should NOT allow user to make a curfew', function(done) {
  575. this.timeout(10000);
  576. server.post('/api/v2/Devices/2/Curfew')
  577. .set('Authorization', userAccessToken2)
  578. .send({
  579. 'deviceId': 2,
  580. 'sunday': false,
  581. 'monday': false,
  582. 'tuesday': false,
  583. 'wednesday': false,
  584. 'thursday': false,
  585. 'friday': false,
  586. 'saturday': false
  587. })
  588. .expect(500)
  589. .end((err, res) => {
  590. assert.equal(res.status, 500, 'expects status equal to 500');
  591. done(err);
  592. });
  593. });
  594.  
  595. it('should allow user to get curfew', function(done) {
  596. this.timeout(10000);
  597. server.get('/api/v2/Devices/2/Curfew')
  598. .set('Authorization', userAccessToken2)
  599. .expect(200)
  600. .end((err, res) => {
  601. assert.notEqual(res.body, null, 'expects curfew not equal to null');
  602. done(err);
  603. });
  604. });
  605.  
  606. it('should NOT allow other user to get curfew', function(done) {
  607. this.timeout(10000);
  608. server.get('/api/v2/Devices/1/Curfew')
  609. .set('Authorization', userAccessToken2)
  610. .expect(401)
  611. .end((err, res) => {
  612. assert.equal(res.status, 401, 'expects status equal to 401');
  613. done(err);
  614. });
  615. });
  616.  
  617. it('should NOT allow other user to add curfewBlocks', function(done) {
  618. this.timeout(10000);
  619. server.post('/api/v2/Devices/1/CurfewBlocks/0')
  620. .set('Authorization', userAccessToken2)
  621. .send({ curfewBlock: { start: '00:00:00', end: '01:00:00' } })
  622. .expect(401)
  623. .end((err, res) => {
  624. assert.equal(res.status, 401, 'expects status equal to 401');
  625. done(err);
  626. });
  627. });
  628.  
  629. it('should allow user to add curfewBlocks', function(done) {
  630. this.timeout(10000);
  631. server.post('/api/v2/Devices/1/CurfewBlocks/4')
  632. .set('Authorization', userAccessToken1)
  633. .send({ curfewBlock: { start: '00:00:00', end: '01:00:00' } })
  634. .expect(200)
  635. .end((err, res) => {
  636. done(err);
  637. });
  638. });
  639.  
  640. it('should NOT allow the public to view your filters', done => {
  641. server.get('/api/v2/Devices/1/Filters/search?q=str')
  642. .expect(401)
  643. .end((err, res) => {
  644. assert.equal(res.status, 401, 'expects status equal to 401');
  645. done(err);
  646. });
  647. });
  648.  
  649. it('should NOT allow the public to view Other Apps', done => {
  650. server.get('/api/v2/Devices/1/Apps/current/other')
  651. .expect(401)
  652. .end((err, res) => {
  653. assert.equal(res.status, 401, 'expects status equal to 401');
  654. done(err);
  655. });
  656. });
  657.  
  658. it('should NOT allow the public to view your apps of concern', done => {
  659. server.get('/api/v2/Devices/1/Apps/current/concern')
  660. .expect(401)
  661. .end((err, res) => {
  662. assert.equal(res.status, 401, 'expects status equal to 401');
  663. done(err);
  664. });
  665. });
  666.  
  667. it('should NOT allow the public to view your curfewblock', done => {
  668. server.get('/api/v2/Devices/1/CurfewBlocks/?day=1')
  669. .expect(401)
  670. .end((err, res) => {
  671. assert.equal(res.status, 401, 'expects status equal to 401');
  672. done(err);
  673. });
  674. });
  675.  
  676. it('should NOT allow other user to view your filters', done => {
  677. server.get('/api/v2/Devices/1/Filters/search?q=str')
  678. .set('Authorization', userAccessToken2)
  679. .expect(401)
  680. .end((err, res) => {
  681. assert.equal(res.status, 401, 'expects status equal to 401');
  682. done(err);
  683. });
  684. });
  685.  
  686. it('should NOT allow the public to view your curfewblock', done => {
  687. server.get('/api/v2/Devices/1/CurfewBlocks/?day=1')
  688. .expect(401)
  689. .end((err, res) => {
  690. assert.equal(res.status, 401, 'expects status equal to 401');
  691. done(err);
  692. });
  693. });
  694.  
  695. it('should NOT allow other user to view Other Apps', done => {
  696. server.get('/api/v2/Devices/1/Apps/current/other')
  697. .set('Authorization', userAccessToken2)
  698. .expect(401)
  699. .end((err, res) => {
  700. assert.equal(res.status, 401, 'expects status equal to 401');
  701. done(err);
  702. });
  703. });
  704.  
  705. it('should NOT allow other user to view your own apps of concern', done => {
  706. server.get('/api/v2/Devices/1/Apps/current/concern')
  707. .set('Authorization', userAccessToken2)
  708. .expect(401)
  709. .end((err, res) => {
  710. assert.equal(res.status, 401, 'expects status equal to 401');
  711. done(err);
  712. });
  713. });
  714.  
  715. it('should NOT allow other user to view your curfewblock', done => {
  716. server.get('/api/v2/Devices/1/CurfewBlocks/?day=1')
  717. .set('Authorization', userAccessToken2)
  718. .expect(401)
  719. .end((err, res) => {
  720. assert.equal(res.status, 401, 'expects status equal to 401');
  721. done(err);
  722. });
  723. });
  724.  
  725. it('should allow you to view your current location', done => {
  726. server.get('/api/v2/Devices/1/Locations/current')
  727. .set('Authorization', userAccessToken1)
  728. .expect(200)
  729. .end((err, res) => {
  730. assert.equal(res.status, 200, 'expects status equal to 200');
  731. done(err);
  732. });
  733. });
  734.  
  735. it('should NOT allow other user to view your current location', done => {
  736. server.get('/api/v2/Devices/1/Locations/current')
  737. .set('Authorization', userAccessToken2)
  738. .expect(401)
  739. .end((err, res) => {
  740. assert.equal(res.status, 401, 'expects status equal to 401');
  741. done(err);
  742. });
  743. });
  744.  
  745. it('should not allow the public to view your apps log', done => {
  746. server.get('/api/v2/Devices/1/Apps/log')
  747. .expect(401)
  748. .end((err, res) => {
  749. assert.equal(res.status, 401, 'expects status equal to 401');
  750. done(err);
  751. });
  752. });
  753.  
  754. it('should allow you to view your apps log', done => {
  755. server.get('/api/v2/Devices/1/Apps/log')
  756. .set('Authorization', userAccessToken1)
  757. .expect(200)
  758. .end((err, res) => {
  759. assert.equal(res.status, 200, 'expects status equal to 200');
  760. done(err);
  761. });
  762. });
  763.  
  764. it('should NOT allow other user to view your apps log', done => {
  765. server.get('/api/v2/Devices/1/Apps/log')
  766. .set('Authorization', userAccessToken2)
  767. .expect(401)
  768. .end((err, res) => {
  769. assert.equal(res.status, 401, 'expects status equal to 401');
  770. done(err);
  771. });
  772. });
  773.  
  774. it('should not allow the public to post your locations', done => {
  775. server.post('/api/v2/Devices/1/Locations/bulk')
  776. .expect(401)
  777. .end((err, res) => {
  778. assert.equal(res.status, 401, 'expects status equal to 401');
  779. done(err);
  780. });
  781. });
  782.  
  783. it('should not allow the public to view your locations - map breadcrumbs', done => {
  784. server.get('/api/v2/Devices/1/Locations/search?startDate=2016-02-10' +
  785. '&endDate=2016-02-18&neLat=30.000035762786865&neLon=' +
  786. '124.97752904891968&swLat=29.99994993209839&swLon=124.9774432182312')
  787. .expect(401)
  788. .end((err, res) => {
  789. done(err);
  790. });
  791. });
  792.  
  793. it('should not allow other user to post your locations', done => {
  794. server.post('/api/v2/Devices/1/Locations/bulk')
  795. .set('Authorization', userAccessToken2)
  796. .expect(401)
  797. .end((err, res) => {
  798. assert.equal(res.status, 401, 'expects status equal to 401');
  799. done(err);
  800. });
  801. });
  802.  
  803. it('should NOT allow other user to view your locations - map breadcrumbs', done => {
  804. server.get('/api/v2/Devices/1/Locations/search?startDate=2016-02-10' +
  805. '&endDate=2016-02-18&neLat=30.000035762786865&neLon=' +
  806. '124.97752904891968&swLat=29.99994993209839&swLon=124.9774432182312')
  807. .set('Authorization', userAccessToken2)
  808. .expect(401)
  809. .end((err, res) => {
  810. done(err);
  811. });
  812. });
  813.  
  814. it('should allow you to post your locations', done => {
  815. server.post('/api/v2/Devices/1/Locations/bulk')
  816. .set('Authorization', userAccessToken1)
  817. .send([])
  818. .expect(200)
  819. .end((err, res) => {
  820. assert.equal(res.status, 200, 'expects status equal to 200');
  821. done(err);
  822. });
  823. });
  824.  
  825. it('should allow you to view your locations - map breadcrumbs', done => {
  826. server.get('/api/v2/Devices/1/Locations/search?startDate=2016-02-10' +
  827. '&endDate=2016-02-18&neLat=30.000035762786865&neLon=' +
  828. '124.97752904891968&swLat=29.99994993209839&swLon=124.9774432182312')
  829. .set('Authorization', userAccessToken1)
  830. .expect(200)
  831. .end((err, res) => {
  832. done(err);
  833. });
  834. });
  835.  
  836. it('should allow device owner to connect an iCloud account', function(done) {
  837. const riCloud = require('ricloud');
  838. const riCloudLoginStub = sinon.stub(riCloud.prototype, 'login', function() {
  839. riCloudLoginStub.restore();
  840. riCloudLoginStub.yield(0, null);
  841. });
  842. server.post('/api/v2/Devices/1/connect/iCloud/?iCloudEmail=share@intelliminds.com&iCloudPassword=3415Sepulveda')
  843. .set('Authorization', userAccessToken1)
  844. .expect(200)
  845. .end((err, res) => {
  846. done(err);
  847. });
  848. });
  849. it('should NOT allow other user to connect an iCloud account', function(done) {
  850. server.post('/api/v2/Devices/1/connect/iCloud/?iCloudEmail=share@intelliminds.com&iCloudPassword=3415Sepulveda')
  851. .set('Authorization', userAccessToken2)
  852. .expect(401)
  853. .end((err, res) => {
  854. assert.equal(res.status, 401, 'expects status equal to 401');
  855. done(err);
  856. });
  857. });
  858.  
  859. let deviceId = 0;
  860. it('should allow a user to create a new device', done => {
  861. server.post('/api/v2/Devices/')
  862. .send({
  863. name: 'string',
  864. mediaId: 1,
  865. color: 'FF0000',
  866. type: 1,
  867. timezone: 'GMT',
  868. osVersion: '4.4.2',
  869. appVersion: '1.10.6',
  870. updateFrequency: 30
  871. })
  872. .set('Authorization', userAccessToken1)
  873. .expect(200)
  874. .end((err, res) => {
  875. deviceId = res.body.id;
  876. done(err);
  877. });
  878. });
  879.  
  880. it('should allow a user to update a device', done => {
  881. server.put('/api/v2/Devices/' + deviceId)
  882. .send({
  883. name: 'string 2'
  884. })
  885. .set('Authorization', userAccessToken1)
  886. .expect(200)
  887. .end((err, res) => {
  888. assert.equal(res.body.name, 'string 2');
  889. done(err);
  890. });
  891. });
  892.  
  893. it('should NOT allow others to update a device', done=> {
  894. server.put('/api/v2/Devices/' + deviceId)
  895. .send({
  896. name: 'string 3'
  897. })
  898. .set('Authorization', userAccessToken2)
  899. .expect(401)
  900. .end((err, res) => {
  901. done(err);
  902. });
  903. });
  904. });
  905.  
  906. describe('getFiltersByPhrase', () => {
  907. it('should return 5 filter records', done => {
  908. server.get('/api/v2/Devices/1/Filters/search?q=str')
  909. .set('Authorization', userAccessToken1)
  910. .expect(200)
  911. .end((err, res) => {
  912. assert.equal(res.body.length, 5, 'expects length equal 5');
  913. assert.notEqual(res.body[0].deviceId, null, 'expects deviceId not equal to null');
  914. assert.notEqual(res.body[1].deviceId, null, 'expects deviceId not equal to null');
  915. done(err);
  916. });
  917. });
  918. it('should return 0 filter records', done => {
  919. server.get('/api/v2/Devices/1/Filters/search?q=testtest')
  920. .set('Authorization', userAccessToken1)
  921. .expect(200)
  922. .end((err, res) => {
  923. assert.equal(res.body.length, 0, 'expects length equal 0');
  924. done(err);
  925. });
  926. });
  927. });
  928.  
  929. describe('#getCurrentOtherApps for User1', () => {
  930. describe('#getCurrentOtherApps For Device1 of User1 having device_app data', () => {
  931. it('should provide current Other Apps', done => {
  932. server.get('/api/v2/Devices/1/Apps/current/other')
  933. .set('Authorization', userAccessToken1)
  934. .expect(200)
  935. .end((err, res) => {
  936. assert.equal(res.body.length, 2, 'expects length equal to 2');
  937. done(err);
  938. });
  939. });
  940.  
  941. it('should NOT provide current Other Apps using different token', done => {
  942. server.get('/api/v2/Devices/1/Apps/current/other')
  943. .set('Authorization', userAccessToken2)
  944. .expect(401)
  945. .end((err, res) => {
  946. assert.equal(res.status, 401, 'expects status equal to 401');
  947. done(err);
  948. });
  949. });
  950. });
  951.  
  952. describe('#getCurrentOtherApps For Device1 of User1 without having device_app data', () => {
  953. it('should NOT provide current Other Apps', done => {
  954. server.get('/api/v2/Devices/2/Apps/current/other')
  955. .set('Authorization', userAccessToken1)
  956. .expect(401)
  957. .end((err, res) => {
  958. assert.equal(res.status, 401, 'expects status equal to 401');
  959. done(err);
  960. });
  961. });
  962. });
  963. });
  964.  
  965. describe('#getCurrentOtherApps For Non Existing Device', () => {
  966. it('should NOT provide current other apps for other devices', done => {
  967. server.get('/api/v2/Devices/0/Apps/current/other')
  968. .set('Authorization', userAccessToken1)
  969. .expect(401)
  970. .end((err, res) => {
  971. assert.equal(res.status, 401, 'expects status equal to 401');
  972. done(err);
  973. });
  974. });
  975. });
  976.  
  977. describe('#getCurrentOtherApps For Non Existing Device', () => {
  978. it('should NOT provide current other apps for other devices', done => {
  979. server.get('/api/v2/Devices/-1/Apps/current/other')
  980. .set('Authorization', userAccessToken1)
  981. .expect(401)
  982. .end((err, res) => {
  983. assert.equal(res.status, 401, 'expects status equal to 401');
  984. done(err);
  985. });
  986. });
  987. });
  988.  
  989. describe('getAllAppsOfConcern', () => {
  990. it('should should provide accurate stats when viewing your own', done => {
  991. server.get('/api/v2/Devices/1/Apps/current/concern')
  992. .set('Authorization', userAccessToken1)
  993. .expect(200)
  994. .end((err, res) => {
  995. assert.equal(res.body.length, 1, 'expects length equal 1');
  996. done(err);
  997. });
  998. });
  999. });
  1000.  
  1001. describe('getCurfewBlockByDay', () => {
  1002. it('should return 3 curfewblock records', done => {
  1003. server.get('/api/v2/Devices/1/CurfewBlocks/?day=1')
  1004. .set('Authorization', userAccessToken1)
  1005. .expect(200)
  1006. .end((err, res) => {
  1007. assert.equal(res.body.length, 3, 'expects length equal 3');
  1008. done(err);
  1009. });
  1010. });
  1011.  
  1012. it('should return no curfewblock record', done => {
  1013. server.get('/api/v2/Devices/1/CurfewBlocks/?day=3')
  1014. .set('Authorization', userAccessToken1)
  1015. .expect(200)
  1016. .end((err, res) => {
  1017. assert.equal(res.body.length, 0, 'expects length equal 0');
  1018. done(err);
  1019. });
  1020. });
  1021. });
  1022.  
  1023. describe('getCurrentLocation', () => {
  1024. it('should return the current location of the device', done => {
  1025. server.get('/api/v2/Devices/1/Locations/current')
  1026. .set('Authorization', userAccessToken1)
  1027. .expect(200)
  1028. .end((err, res) => {
  1029. assert.equal(res.body.deviceId, 1, 'expects length equal 1');
  1030. assert.equal(res.body.address, 'blah blah', 'expects address equal to blah blah');
  1031. done(err);
  1032. });
  1033. });
  1034.  
  1035. it('should return an error', done => {
  1036. server.get('/api/v2/Devices/3/Locations/current')
  1037. .set('Authorization', userAccessToken1)
  1038. .expect(404)
  1039. .end((err, res) => {
  1040. done(err);
  1041. });
  1042. });
  1043. });
  1044.  
  1045. describe('getPhotos', () => {
  1046. it('should return photo records', done => {
  1047. server.get('/api/v2/Devices/1/Photos')
  1048. .set('Authorization', userAccessToken1)
  1049. .expect(200)
  1050. .end((err, res) => {
  1051. assert.equal(res.body.length, 2, 'expects length equal 2');
  1052. done(err);
  1053. });
  1054. });
  1055. });
  1056.  
  1057. describe('getAppsLog', () => {
  1058. it('should return 2 records', done => {
  1059. server.get('/api/v2/Devices/1/Apps/log?limit=&before' + (new Date() - 10))
  1060. .set('Authorization', userAccessToken1)
  1061. .expect(200)
  1062. .end((err, res) => {
  1063. const isGreater = res.body[0].created > res.body[1].created ? true : false;
  1064. assert.equal(res.body.length, 2, 'expects length equal 2');
  1065. assert.equal(isGreater, true, 'expects isGreater equal to true');
  1066. done(err);
  1067. });
  1068. });
  1069. });
  1070.  
  1071. describe('#connectFacebook', () => {
  1072. it('should return an error when invalid token is provided', function(done) {
  1073. server.post('/api/v2/Devices/1/connect/facebook')
  1074. .set('Authorization', userAccessToken1)
  1075. .send({ accessToken: facebookToken })
  1076. .expect(400)
  1077. .end((err, res) => {
  1078. assert.equal(res.status, 400, 'expects status equal to 400');
  1079. done(err);
  1080. });
  1081. });
  1082. });
  1083.  
  1084. describe('#connectInstagram', () => {
  1085. it('should return an error when invalid token is provided', (done) => {
  1086. server.post('/api/v2/Devices/1/connect/instagram')
  1087. .set('Authorization', userAccessToken1)
  1088. .send({ accessToken: '1234567890.1234567890' })
  1089. .expect(400)
  1090. .end((err, res) => {
  1091. done(err);
  1092. });
  1093. });
  1094. });
  1095.  
  1096. describe('#connectInstagram', () => {
  1097. it('should return status of 200', (done) => {
  1098. server.post('/api/v2/Devices/1/connect/instagram')
  1099. .set('Authorization', userAccessToken1)
  1100. .send({ accessToken: '1773147737.badc866.74f684c73dc44e3a92c9deac4cb0b4a7' })
  1101. .expect(200)
  1102. .end((err, res) => {
  1103. done(err);
  1104. });
  1105. });
  1106. });
  1107.  
  1108. describe('#addBulkLocations', function() {
  1109. let locations = [
  1110. {
  1111. latitude: 14.583091,
  1112. longitude: 121.060972,
  1113. address: '6808 Viverra. St',
  1114. created: moment('2016-02-16').tz('Asia/Manila').format('x')
  1115. },
  1116. {
  1117. latitude: 14.583091,
  1118. longitude: 121.060972,
  1119. address: 'P.O. Box 476, 1915 Et Avenue',
  1120. created: moment('2016-02-15').tz('Asia/Manila').format('x')
  1121. },
  1122. {
  1123. latitude: 14.583091,
  1124. longitude: 121.060972,
  1125. address: '607-6002 Enim. St.',
  1126. created: moment('2016-02-14').tz('Asia/Manila').format('x')
  1127. },
  1128. {
  1129. latitude: 14.583091,
  1130. longitude: 121.060972,
  1131. address: 'P.O. Box 821, 4470 Vivamus Avenue',
  1132. created: moment('2016-02-13').tz('Asia/Manila').format('x')
  1133. },
  1134. {
  1135. latitude: 14.583091,
  1136. longitude: 121.060972,
  1137. address: '890-496 Tincidunt St.',
  1138. created: moment('2016-02-13').tz('Asia/Manila').format('x')
  1139. },
  1140. {
  1141. latitude: 14.583091,
  1142. longitude: 121.060972,
  1143. address: '890-496 Tincidunt St.',
  1144. created: moment('2016-02-13').tz('Asia/Manila').format('x')
  1145. },
  1146. {
  1147. latitude: 15.705522,
  1148. longitude: 120.013430,
  1149. address: 'Candelaria',
  1150. created: moment('2016-02-13').tz('Asia/Manila').format('x')
  1151. }
  1152. ];
  1153.  
  1154. it('should return status of 200', function(done) {
  1155. server.post('/api/v2/Devices/1/Locations/bulk')
  1156. .set('Authorization', userAccessToken1)
  1157. .send({ locations: locations })
  1158. .expect(200)
  1159. .end((err, res) => {
  1160. assert.equal(res.status, 200, 'expects status equal to 200');
  1161. assert.equal(queue.queue.length, 1, 'expects length equal to 1');
  1162. assert.equal(queue.queue[0].type, 'locations-bulk', 'expects type equal to locations-bulk');
  1163. assert.equal(JSON.parse(queue.queue[0].message).length, 7, 'expects queue.message length equal to 7');
  1164. done(err);
  1165. });
  1166. });
  1167.  
  1168. //we already have 5 data on locationlog plus 6 data on top which has one duplicate so real data should only be 10
  1169. it('LocationLogs.importLocations - should return record count of 10', function(done) {
  1170. for (let location of locations) {
  1171. location.deviceId = 1;
  1172. }
  1173.  
  1174. LocationLog.importLocations(locations, (err) => {
  1175. LocationLog.count({ deviceId: 1 }, (err, resp) => {
  1176. assert.equal(resp, 10, 'expects count equal to 10');
  1177. GeofenceSummary.find({ where: { deviceId: 1 }, order: 'date' }, (err, res) => {
  1178. assert.equal(res.length, 5);
  1179. assert.equal(res[0].geofenceId, 1);
  1180. assert.equal(res[1].geofenceId, null);
  1181. assert.equal(res[2].geofenceId, 1);
  1182. assert.equal(res[3].geofenceId, 1);
  1183. assert.equal(res[4].geofenceId, 1);
  1184. assert.equal(res[0].count, 3);
  1185. const count = _.reduce(res, (x, y) => {
  1186. x.count = x.count + y.count;
  1187. return x;
  1188. });
  1189. assert.equal(count.count, 7);
  1190. Activity.count({ type: Activity.TYPES.GEOFENCE_ARRIVED }, (err, count) => {
  1191. assert.equal(count, 6);
  1192. done(err);
  1193. });
  1194. });
  1195. });
  1196. });
  1197. });
  1198.  
  1199. it('LocationLogs.importLocations. Add more locations', function(done) {
  1200. for (let location of locations) {
  1201. location.deviceId = 1;
  1202. }
  1203.  
  1204. LocationLog.importLocations(locations, (err) => {
  1205. LocationLog.count({ deviceId: 1 }, (err, resp) => {
  1206. assert.equal(resp, 10, 'expects count equal to 10');
  1207. GeofenceSummary.find({ where: { deviceId: 1 }, order: 'date' }, (err, res) => {
  1208. assert.equal(res.length, 5);
  1209. assert.equal(res[0].geofenceId, 1);
  1210. assert.equal(res[1].geofenceId, null);
  1211. assert.equal(res[2].geofenceId, 1);
  1212. assert.equal(res[3].geofenceId, 1);
  1213. assert.equal(res[4].geofenceId, 1);
  1214. assert.equal(res[0].count, 6);
  1215. //check order
  1216. assert(new Date(res[0].date) <= new Date(res[1].date));
  1217. assert(new Date(res[1].date) < new Date(res[2].date));
  1218. assert(new Date(res[2].date) < new Date(res[3].date));
  1219. assert(new Date(res[3].date) < new Date(res[4].date));
  1220. const count = _.reduce(res, (x, y) => {
  1221. x.count = x.count + y.count;
  1222. return x;
  1223. });
  1224. assert.equal(count.count, 14);
  1225. Activity.count({ type: Activity.TYPES.GEOFENCE_ARRIVED }, (err, count) => {
  1226. assert.equal(count, 6);
  1227. done(err);
  1228. });
  1229. });
  1230. });
  1231. });
  1232. });
  1233. });
  1234.  
  1235. describe('getLocationByDateAndCoordinates', () => {
  1236. describe('test not in range and not in bounding box', () => {
  1237. it('should return empty array', done => {
  1238. server.get('/api/v2/Devices/1/Locations/search?startDate=2016-02-10&endDate=2016-02-18' +
  1239. '&neLat=47.83243417739868&neLon=' +
  1240. '112.55842924118042&swLat=47.832348346710205&swLon=112.55834341049194')
  1241. .set('Authorization', userAccessToken1)
  1242. .end((err, res) => {
  1243. assert.equal(res.body.length, 0, 'expects length equal 0');
  1244. done(err);
  1245. });
  1246. });
  1247. });
  1248.  
  1249. describe('test in range and not in bounding box', () => {
  1250. it('should return empty array', done => {
  1251. server.get('/api/v2/Devices/1/Locations/search?startDate=2016-02-10&endDate=2016-02-18' +
  1252. '&neLat=47.83243417739868&neLon=' +
  1253. '112.55842924118042&swLat=47.832348346710205&swLon=112.55834341049194')
  1254. .set('Authorization', userAccessToken1)
  1255. .end((err, res) => {
  1256. assert.equal(res.body.length, 0, 'expects length equal 0');
  1257. done(err);
  1258. });
  1259. });
  1260. });
  1261.  
  1262. describe('test not in range and in bounding box', () => {
  1263. it('should return empty array', done => {
  1264. server.get('/api/v2/Devices/1/Locations/search?startDate=2016-02-01&endDate=2016-02-08' +
  1265. '&neLat=30.000035762786865&neLon=' +
  1266. '124.97752904891968&swLat=29.99994993209839&swLon=124.9774432182312')
  1267. .set('Authorization', userAccessToken1)
  1268. .end((err, res) => {
  1269. assert.equal(res.body.length, 0, 'expects length equal 0');
  1270. done(err);
  1271. });
  1272. });
  1273. });
  1274.  
  1275. describe('test in range and in bounding box', () => {
  1276. it('should return 2 locations', done => {
  1277. server.get('/api/v2/Devices/1/Locations/search?startDate=2016-02-10&endDate=2016-02-18' +
  1278. '&neLat=30.000035762786865&neLon=' +
  1279. '124.97752904891968&swLat=29.99994993209839&swLon=124.9774432182312')
  1280. .set('Authorization', userAccessToken1)
  1281. .end((err, res) => {
  1282. const isGreater = res.body[0].created > res.body[1].created ? true : false;
  1283. assert.equal(res.body.length, 2, 'expects length equal 2');
  1284. assert.equal(isGreater, true, 'expects isGreater equal to true');
  1285. assert.equal(res.body[0].deviceId, 1, 'expects deviceId equal to 1');
  1286. assert.equal(res.body[1].deviceId, 1, 'expects deviceId equal to 1');
  1287. done(err);
  1288. });
  1289. });
  1290. });
  1291. });
  1292.  
  1293. describe('curfew', () => {
  1294. let curfewBlocks = [
  1295. {
  1296. start: '00:00:00', //good
  1297. end: '02:00:00'
  1298. },
  1299. {
  1300. start: '00:00:00', //bad - overlapping front and end
  1301. end: '05:00:00'
  1302. },
  1303. {
  1304. start: '01:00:00', //bad - overlapping front
  1305. end: '05:00:00'
  1306. },
  1307. {
  1308. start: '02:00:00', //good - curfew can start when another ends
  1309. end: '05:00:00'
  1310. },
  1311. {
  1312. start: '10:00:00', //good - later hour to midnight
  1313. end: '00:00:00'
  1314. },
  1315. {
  1316. start: '08:00:00',//bad - overlapping end
  1317. end: '11:00:00'
  1318. }
  1319. ];
  1320.  
  1321. it('should return the correct curfew for the device', function(done) {
  1322. this.timeout(10000);
  1323. server.get('/api/v2/Devices/2/Curfew')
  1324. .set('Authorization', userAccessToken2)
  1325. .expect(200)
  1326. .end((err, res) => {
  1327. assert.notEqual(res.body, null, 'expects curfew to exist');
  1328. assert.equal(res.body.deviceId, 2, 'expects curfew to belong to correct device');
  1329. done(err);
  1330. });
  1331. });
  1332.  
  1333. it('should return the newly created curfew block', function(done) {
  1334. this.timeout(10000);
  1335. server.post('/api/v2/Devices/2/CurfewBlocks/0')
  1336. .set('Authorization', userAccessToken2)
  1337. .send({ 'curfewBlock': curfewBlocks[0] })
  1338. .expect(200)
  1339. .end((err, res) => {
  1340. assert.notEqual(res.body, null, 'expects curfewBlock to exist');
  1341. assert.equal(res.body.deviceId, 2, 'expects curfewBlock to belong to correct device');
  1342. assert.equal(res.body.start, curfewBlocks[0].start, 'expects curfewBlock start to equal start entered');
  1343. assert.equal(res.body.end, curfewBlocks[0].end, 'expects curfewBlock end to equal end entered');
  1344. done(err);
  1345. });
  1346. });
  1347.  
  1348. it('should NOT allow start and end times to overlap with existing curfewBlocks', function(done) {
  1349. this.timeout(10000);
  1350. server.post('/api/v2/Devices/2/CurfewBlocks/0')
  1351. .set('Authorization', userAccessToken2)
  1352. .send({ 'curfewBlock': curfewBlocks[1] })
  1353. .expect(422)
  1354. .end((err, res) => {
  1355. done(err);
  1356. });
  1357. });
  1358.  
  1359. it('should NOT allow starting times to overlap with existing curfewBlocks', function(done) {
  1360. this.timeout(10000);
  1361. server.post('/api/v2/Devices/2/CurfewBlocks/0')
  1362. .set('Authorization', userAccessToken2)
  1363. .send({ 'curfewBlock': curfewBlocks[2] })
  1364. .expect(422)
  1365. .end((err, res) => {
  1366. done(err);
  1367. });
  1368. });
  1369.  
  1370. it('should allow adding curfewBlocks that start when other events end', function(done) {
  1371. this.timeout(10000);
  1372. server.post('/api/v2/Devices/2/CurfewBlocks/0')
  1373. .set('Authorization', userAccessToken2)
  1374. .send({ 'curfewBlock': curfewBlocks[3] })
  1375. .expect(200)
  1376. .end((err, res) => {
  1377. assert.notEqual(res.body, null, 'expects curfewBlock to exist');
  1378. assert.equal(res.body.deviceId, 2, 'expects curfewBlock to belong to correct device');
  1379. assert.equal(res.body.start, curfewBlocks[3].start, 'expects curfewBlock start to equal start entered');
  1380. assert.equal(res.body.end, curfewBlocks[3].end, 'expects curfewBlock end to equal end entered');
  1381. done(err);
  1382. });
  1383. });
  1384.  
  1385. it('should allow a non-overlapping curfew block to end at midnight', function(done) {
  1386. this.timeout(10000);
  1387. server.post('/api/v2/Devices/2/CurfewBlocks/0')
  1388. .set('Authorization', userAccessToken2)
  1389. .send({ 'curfewBlock': curfewBlocks[4] })
  1390. .expect(200)
  1391. .end((err, res) => {
  1392. assert.notEqual(res.body, null, 'expects curfewBlock to exist');
  1393. assert.equal(res.body.deviceId, 2, 'expects curfewBlock to belong to correct device');
  1394. assert.equal(res.body.start, curfewBlocks[4].start, 'expects curfewBlock start to equal start entered');
  1395. assert.equal(res.body.end, curfewBlocks[4].end, 'expects curfewBlock end to equal end entered');
  1396. done(err);
  1397. });
  1398. });
  1399.  
  1400. it('should not allow end times to overlap with existing curfewBlocks', function(done) {
  1401. this.timeout(10000);
  1402. server.post('/api/v2/Devices/2/CurfewBlocks/0')
  1403. .set('Authorization', userAccessToken2)
  1404. .send({ 'curfewBlock': curfewBlocks[5] })
  1405. .expect(422)
  1406. .end((err, res) => {
  1407. done(err);
  1408. });
  1409. });
  1410. });
  1411.  
  1412. describe('connectICloud', () => {
  1413. it('Should require icloud email', function(done) {
  1414. server.post('/api/v2/Devices/1/connect/iCloud/?iCloudPassword=3415Sepulveda')
  1415. .set('Authorization', userAccessToken1)
  1416. .expect(400)
  1417. .end((err, res) => {
  1418. done(err);
  1419. });
  1420. });
  1421. it('Should require icloud password', function(done) {
  1422. server.post('/api/v2/Devices/1/connect/iCloud/?iCloudEmail=share@intelliminds.com')
  1423. .set('Authorization', userAccessToken1)
  1424. .expect(400)
  1425. .end((err, res) => {
  1426. done(err);
  1427. });
  1428. });
  1429. it('should return the updated device', function(done) {
  1430. const riCloud = require('ricloud');
  1431. const riCloudLoginStub = sinon.stub(riCloud.prototype, 'login', function() {
  1432. riCloudLoginStub.restore();
  1433. riCloudLoginStub.yield(0, null);
  1434. });
  1435. server.post('/api/v2/Devices/2/connect/iCloud/?iCloudEmail=share@intelliminds.com&iCloudPassword=3415Sepulveda')
  1436. .set('Authorization', userAccessToken2)
  1437. .expect(200)
  1438. .end((err, res) => {
  1439. assert.notEqual(res.body.iCloudUsername, null, 'expects the icloud username to be updated');
  1440. done(err);
  1441. });
  1442. });
  1443.  
  1444. it('simulate invalid icloud credentials should return 403', function(done) {
  1445. const res = {
  1446. body: {
  1447. message: {}
  1448. },
  1449. statusCode: 403
  1450. };
  1451. const riCloud = require('ricloud');
  1452. const riCloudLoginStub = sinon.stub(riCloud.prototype, 'login', function() {
  1453. riCloudLoginStub.restore();
  1454. riCloudLoginStub.yield(1, res);
  1455. });
  1456. server.post('/api/v2/Devices/2/connect/iCloud/?iCloudEmail=share@intelliminds.com&iCloudPassword=3415Sepulveda')
  1457. .set('Authorization', userAccessToken2)
  1458. .expect(403)
  1459. .end((err, res) => {
  1460. done(err);
  1461. });
  1462. });
  1463.  
  1464. it('simulate valid icloud credentials with session error should return 503', function(done) {
  1465. const res = {
  1466. body: {
  1467. message: 'Your iCloud session has expired. To continue, please sign in again.'
  1468. },
  1469. statusCode: 503
  1470. };
  1471. const riCloud = require('ricloud');
  1472. const riCloudLoginStub = sinon.stub(riCloud.prototype, 'login', function() {
  1473. riCloudLoginStub.restore();
  1474. riCloudLoginStub.yield(1, res);
  1475. });
  1476. server.post('/api/v2/Devices/2/connect/iCloud/?iCloudEmail=share@intelliminds.com&iCloudPassword=3415Sepulveda')
  1477. .set('Authorization', userAccessToken2)
  1478. .expect(503)
  1479. .end((err, res) => {
  1480. done(err);
  1481. });
  1482. });
  1483. });
  1484.  
  1485. describe('#mdmToken', () => {
  1486. it('should vaidate mdmToken if it has value or not', (done) => {
  1487. assert(devices[0].mdmToken === 'string', true, `ios' mdmtoken should not be empty.`);
  1488. assert(devices[1].mdmToken == null, true, `android's mdmtoken should be empty.`);
  1489. assert(devices[2].mdmToken !== null, true, `ios' mdmtoken should not be empty.`);
  1490. done();
  1491. });
  1492. });
  1493.  
  1494. describe('markAllActivitiesAsRead', () => {
  1495. it('should NOT allow a user to mark other users activities as read', done => {
  1496. server.post('/api/v2/Devices/1/Activities/markAllAsRead')
  1497. .set('Authorization', userAccessToken2)
  1498. .expect(401)
  1499. .end((err, res) => {
  1500. done(err);
  1501. });
  1502. });
  1503. it('should allow a user to mark his own activities as read', done => {
  1504. server.post('/api/v2/Devices/2/Activities/markAllAsRead')
  1505. .set('Authorization', userAccessToken2)
  1506. .expect(200)
  1507. .end((err, res) => {
  1508. done(err);
  1509. });
  1510. });
  1511. it('should return 0 unreadActivityCount for deviceId 2', done => {
  1512. server.get('/api/v2/Accounts/2')
  1513. .set('Authorization', userAccessToken2)
  1514. .expect(200)
  1515. .end((err, res) => {
  1516. const account = res.body;
  1517. assert.equal(account.Devices[0].id, 2, 'expects deviceId 2');
  1518. assert.equal(account.Devices[0].unreadActivityCount, 0, 'expects 0 unreadActivityCount');
  1519. done(err);
  1520. });
  1521. });
  1522. });
  1523.  
  1524. describe('test messages' , () => {
  1525. it('test sent message with object', done => {
  1526. server.post('/api/v2/Devices/2/Chats/Messages/bulk')
  1527. .set('Authorization', userAccessToken2)
  1528. .send({
  1529. 'message': 'Fuck You',
  1530. 'fromMe': true,
  1531. 'created': new Date(),
  1532. 'flagged': true,
  1533. 'flaggedWords' : ['fuck'],
  1534. 'imported': 1,
  1535. 'contact': {
  1536. 'deviceId': 2,
  1537. 'sourceId': 2,
  1538. 'firstname': '09216185215',
  1539. 'lastname': 'N/A',
  1540. 'address': '09216185215'
  1541. }
  1542. })
  1543. .expect(200)
  1544. .end((err, res) => {
  1545. done(err);
  1546. });
  1547. });
  1548. });
  1549.  
  1550. describe('test getDevicesForLocationLogSilentPush', () => {
  1551. it('test different scenario', done => {
  1552. let deviceInstance = {};
  1553. const token = '2ddc9c60eb4310fe60e9eb84f0ce14bfa08e66814548dadcd01b1d6644854957';
  1554. Device.findById(1).then(device => {
  1555. deviceInstance = device;
  1556. deviceInstance.pushToken = token;
  1557. deviceInstance.type = 0;
  1558. deviceInstance.deleted = false;
  1559. return deviceInstance.save();
  1560. }).then(device => {
  1561. deviceInstance = device;
  1562. return Device.getDevicesForLocationLogSilentPush();
  1563. }).then(devices => {
  1564. assert.equal(devices.length, 1, 'should return devices');
  1565. deviceInstance.pushToken = null;
  1566. return deviceInstance.save();
  1567. }).then(device => {
  1568. deviceInstance = device;
  1569. return Device.getDevicesForLocationLogSilentPush();
  1570. }).then(devices => {
  1571. assert.equal(devices.length, 0, 'should return no devices');
  1572. deviceInstance.pushToken = token;
  1573. deviceInstance.type = 1;
  1574. return deviceInstance.save();
  1575. }).then(device => {
  1576. deviceInstance = device;
  1577. return Device.getDevicesForLocationLogSilentPush();
  1578. }).then(devices => {
  1579. assert.equal(devices.length, 0, 'should return no devices');
  1580. deviceInstance.type = 0;
  1581. deviceInstance.deleted = true;
  1582. return deviceInstance.save();
  1583. }).then(device => {
  1584. deviceInstance = device;
  1585. return Device.getDevicesForLocationLogSilentPush();
  1586. }).then(devices => {
  1587. assert.equal(devices.length, 0, 'should return no devices');
  1588. done();
  1589. }).catch(err => {
  1590. console.log(err);
  1591. done(err);
  1592. });
  1593. });
  1594. });
  1595. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement