Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* jslint node: true */
- /* global describe, before, it, expect */
- 'use strict';
- const assert = require('assert');
- const async = require('async');
- const utils = require('../utils');
- const uuid = require('node-uuid');
- const app = require('../../server/server');
- const supertest = require('supertest');
- const server = supertest(app);
- const Account = app.models.Account;
- const Device = app.models.Device;
- const Media = app.models.Media;
- const App = app.models.App;
- const DeviceApp = app.models.DeviceApp;
- const AccessToken = app.models.AccessToken;
- const Filter = app.models.Filter;
- const CurfewBlock = app.models.CurfewBlock;
- const LocationLog = app.models.LocationLog;
- const Photo = app.models.Photo;
- const PhotoLike = app.models.PhotoLike;
- const PhotoComment = app.models.PhotoComment;
- const DeviceAppLog = app.models.DeviceAppLog;
- const Activity = app.models.Activity;
- const Geofence = app.models.Geofence;
- const GeofenceSummary = app.models.GeofenceSummary;
- const moment = require('moment-timezone');
- const queue = require('../../server/lib/queue.js');
- const sinon = require('sinon');
- const _ = require('lodash');
- const facebookToken = (`CAACEdEose0cBAJwdPzRD7WmCEqWv1wZCqNIFSv7qsztahYzjVUJoZAmGGgcodxw8m6Mo5xocAs
- 2PunZCrXXP7nR1MhIz467YeBwjP2XrQN26m7ZBQL5NNEyOCAIteYxyTS5NouzrIDxThUJE1LY
- ZBLOM8gLkuXlGVQrAYce6xhQAtaZA3ZAlrLiJQcHZBYgdVqNGEbqmlFD1bjgbfvIujvBL`);
- let userAccessToken1, userAccessToken2, devices;
- describe('Device', () => {
- // setup the test environment
- before(function(done) {
- this.timeout(5000);
- utils.resetDatabase(err => {
- if (err) {
- return done(err);
- }
- // create test data as appropriate
- async.series([
- callback => {
- Account.create({
- firstname: 'Ethan',
- lastname: 'Hunt',
- email: 'ehunt32@imf.us.gov',
- password: 'supersecret1',
- mediaId: 1
- }, callback);
- },
- callback => {
- AccessToken.create({
- userId: 1
- }, (err, accessToken) => {
- userAccessToken1 = accessToken.id;
- callback();
- });
- },
- callback => {
- Account.create({
- firstname: 'blah',
- lastname: 'blah',
- email: 'blah32@imf.us.gov',
- password: 'supersecret1',
- mediaId: 1
- }, callback);
- },
- callback => {
- AccessToken.create({
- userId: 2
- }, (err, accessToken) => {
- userAccessToken2 = accessToken.id;
- callback();
- });
- },
- callback => {
- Media.create({
- accountId: 1,
- filename: 'string',
- originalURL: 'string',
- largeURL: 'string',
- mediumURL: 'string',
- smallURL: 'string',
- created: new Date()
- }, callback);
- },
- callback => {
- App.create({
- name: 'Facebook',
- bundleID: 1,
- mediaId: 1,
- description: 'Social Media',
- flagged: false,
- created: new Date()
- }, callback);
- },
- callback => {
- App.create({
- name: 'Instagram',
- bundleID: 1,
- mediaId: 1,
- description: 'Social Media',
- flagged: true,
- created: new Date()
- }, callback);
- },
- callback => {
- App.create({
- name: 'Youtube',
- bundleID: 1,
- mediaId: 1,
- description: 'Streaming site/ Socal Media',
- flagged: false,
- created: new Date()
- }, callback);
- },
- callback => {
- async.eachSeries([1, 3], (value, callback) => {
- let deviceId = value;
- async.series([
- callback => {
- DeviceApp.create({
- deviceId: deviceId,
- appId: 1,
- installed: new Date(),
- uninstalled: null
- }, callback);
- },
- callback => {
- DeviceApp.create({
- deviceId: deviceId,
- appId: 2,
- installed: new Date(),
- uninstalled: null
- }, callback);
- },
- callback => {
- DeviceApp.create({
- deviceId: deviceId,
- appId: 3,
- installed: new Date(),
- uninstalled: null
- }, callback);
- }
- ], done => {
- callback();
- });
- }, done => {
- callback();
- });
- },
- callback => {
- Device.create({
- accountId: 1,
- name: 'string',
- mediaId: 1,
- color: 'string',
- type: 0,
- udid: uuid.v4(),
- facebookToken: facebookToken,
- facebookId: 'string',
- instagramToken: 'string',
- instagramId: 'string',
- mdmToken: 'string',
- mdmPushToken: 'string',
- mdmPushMagic: 'string',
- mdmUnlockToken: 'string',
- deleted: false,
- created: new Date(),
- timezone: 'GMT',
- facebookConnectedOn: new Date(),
- instagramConnectedOn: new Date(),
- facebookOk: true,
- instagramOk: true,
- osVersion: '4.4.2',
- appVersion: '1.10.6',
- updateFrequency: 30
- }, callback);
- },
- callback => {
- Device.create({
- accountId: 2,
- name: 'string',
- mediaId: 1,
- color: 'string',
- type: 1,
- udid: uuid.v4(),
- facebookToken: 'string',
- facebookId: 'string',
- instagramToken: 'string',
- instagramId: 'string',
- mdmToken: '',
- mdmPushToken: 'string',
- mdmPushMagic: 'string',
- mdmUnlockToken: 'string',
- deleted: false,
- created: new Date(),
- timezone: 'GMT',
- facebookConnectedOn: new Date(),
- instagramConnectedOn: new Date(),
- facebookOk: true,
- instagramOk: true,
- osVersion: '4.4.2',
- appVersion: '1.10.6',
- updateFrequency: 30
- }, callback);
- },
- callback => {
- Device.create({
- accountId: 1,
- name: 'string',
- mediaId: 1,
- color: 'string',
- type: 0,
- udid: uuid.v4(),
- facebookToken: 'string',
- facebookId: 'string',
- instagramToken: 'string',
- instagramId: 'string',
- mdmToken: '',
- mdmPushToken: 'string',
- mdmPushMagic: 'string',
- mdmUnlockToken: 'string',
- deleted: false,
- created: new Date(),
- timezone: 'GMT',
- facebookConnectedOn: new Date(),
- instagramConnectedOn: new Date(),
- facebookOk: true,
- instagramOk: true,
- osVersion: '4.4.2',
- appVersion: '1.10.6',
- updateFrequency: 30
- }, callback);
- },
- callback => {
- Device.find({}, (err, res) => {
- if (err) {
- return callback(err);
- }
- devices = res;
- return callback();
- });
- },
- callback => {
- Filter.create({
- deviceId: 1,
- phrase: 'stri',
- disabled: false
- }, callback);
- },
- callback => {
- Filter.create({
- deviceId: 1,
- phrase: 'STRinG',
- disabled: false
- }, callback);
- },
- callback => {
- Filter.create({
- phrase: 'STRinG',
- disabled: false
- }, callback);
- },
- callback => {
- Filter.create({
- deviceId: 1,
- phrase: 'strin',
- disabled: false
- }, callback);
- },
- callback => {
- Filter.create({
- phrase: 'strin',
- disabled: false
- }, callback);
- },
- callback => {
- Filter.create({
- deviceId: 1,
- phrase: 'this is a string',
- disabled: false
- }, callback);
- },
- callback => {
- Filter.create({
- deviceId: 1,
- phrase: 'test',
- disabled: false
- }, callback);
- },
- callback => {
- Filter.create({
- deviceId: 1,
- phrase: 'stringy',
- disabled: true
- }, callback);
- },
- callback => {
- CurfewBlock.create({
- deviceId: 1,
- day: 1,
- start: '11:00',
- end: '12:00'
- }, callback);
- },
- callback => {
- CurfewBlock.create({
- deviceId: 1,
- day: 1,
- start: '13:00',
- end: '14:00'
- }, callback);
- },
- callback => {
- CurfewBlock.create({
- deviceId: 1,
- day: 1,
- start: '15:00',
- end: '16:00'
- }, callback);
- },
- callback => {
- CurfewBlock.create({
- deviceId: 1,
- day: 2,
- start: '15:00',
- end: '16:00'
- }, callback);
- },
- callback => {
- CurfewBlock.create({
- deviceId: 2,
- day: 1,
- start: '15:00',
- end: '16:00'
- }, callback);
- },
- callback => {
- LocationLog.create({
- deviceId: 1,
- latitude: 30.000035762786865,
- longitude: 124.97748613357544,
- address: '123 Main St',
- created: moment('2016-02-16').tz('Asia/Manila').format('x')
- }, callback);
- },
- callback => {
- LocationLog.create({
- deviceId: 1,
- latitude: 29.999992847442627,
- longitude: 124.9774432182312,
- address: '456 Other St',
- created: moment('2016-02-17').tz('Asia/Manila').format('x')
- }, callback);
- },
- callback => {
- LocationLog.create({
- deviceId: 2,
- latitude: 29.999992847442627,
- longitude: 124.9774432182312,
- address: '456 Other St',
- created: moment('2016-02-17').tz('Asia/Manila').format('x')
- }, callback);
- },
- callback => {
- LocationLog.create({
- deviceId: 1,
- latitude: 24.000035762786865,
- longitude: 45.97748613357544,
- address: 'blah blah',
- created: moment('2016-02-19').tz('Asia/Manila').format('x')
- }, callback);
- },
- callback => {
- LocationLog.create({
- deviceId: 1,
- latitude: 24.000035762786865,
- longitude: 45.97748613357544,
- address: '789 South St',
- created: moment('2016-02-09').tz('Asia/Manila').format('x')
- }, callback);
- },
- callback => {
- DeviceAppLog.create({
- deviceId: 1,
- appId: 1,
- action: 1,
- created: new Date()
- }, callback);
- },
- callback => {
- DeviceAppLog.create({
- deviceId: 1,
- appId: 2,
- action: 1,
- created: new Date()
- }, callback);
- },
- callback => {
- DeviceAppLog.create({
- deviceId: 2,
- appId: 3,
- action: 1,
- created: new Date()
- }, callback);
- },
- callback => {
- Photo.create({
- id: '3cf95a90-d3c9-11e5-a8f5-dd5eeac92e98',
- deviceId: 1,
- description: 'string',
- originalURL: 'string',
- smallURL: 'string',
- mediumURL: 'string',
- largeURL: 'string',
- commentCount: 0,
- likeCount: 0,
- source: 0,
- sourceId: 'string',
- created: new Date(),
- imported: 0
- }, (err, resp) => {
- async.eachSeries([1, 2], (value, callback) => {
- PhotoLike.create({
- photoId: resp.id,
- sourceId: 'string',
- username: 'asdasdasd'
- }, callback);
- }, done => {
- PhotoComment.create({
- photoId: resp.id,
- sourceId: 'string',
- username: 'asdasdasd',
- comment: 'this is a test',
- created: new Date()
- }, callback);
- });
- });
- },
- callback => {
- Photo.create({
- id: '3cf95a90-d3c9-11e5-a8f5-dd5eeac92e99',
- deviceId: 1,
- description: 'string',
- originalURL: 'string',
- smallURL: 'string',
- mediumURL: 'string',
- largeURL: 'string',
- commentCount: 0,
- likeCount: 0,
- source: 0,
- sourceId: 'string',
- created: new Date(),
- imported: 0
- }, (err, resp) => {
- PhotoLike.create({
- photoId: resp.id,
- sourceId: 'string',
- username: 'asdasdasd'
- }, callback);
- });
- },
- callback => {
- Photo.create({
- id: '3cf95a90-d3c9-11e5-a8f5-dd5eeac92100',
- deviceId: 2,
- description: 'string',
- originalURL: 'string',
- smallURL: 'string',
- mediumURL: 'string',
- largeURL: 'string',
- commentCount: 0,
- likeCount: 0,
- source: 0,
- sourceId: 'string',
- created: new Date(),
- imported: 0
- }, (err, resp) => {
- PhotoLike.create({
- photoId: resp.id,
- sourceId: 'string',
- username: 'asdasdasd'
- }, callback);
- });
- },
- callback => {
- Geofence.create({
- deviceId: 1,
- name: 'Test Geofence Alert True',
- color: 'FFFFFF',
- center: {
- lat: 14.583091,
- lng: 121.060972
- },
- radius: 10,
- arrives: true,
- leaves: true,
- disabled: false
- }, callback);
- },
- callback => {
- Geofence.create({
- deviceId: 1,
- name: 'Test Geofence Alert True',
- color: 'FFFFFF',
- center: {
- lat: 24.583091,
- lng: 121.060972
- },
- radius: 10,
- arrives: true,
- leaves: true,
- disabled: false
- }, callback);
- }
- ],
- err => {
- done(err);
- });
- });
- });
- describe('ACL tests', () => {
- it('should allow you to view your own filters', done => {
- server.get('/api/v2/Devices/1/Filters/search?q=str')
- .set('Authorization', userAccessToken1)
- .expect(200)
- .end((err, res) => {
- assert.equal(res.status, 200, 'expects status equal to 200');
- done(err);
- });
- });
- it('should allow you to view Other Apps', done => {
- server.get('/api/v2/Devices/1/Apps/current/other')
- .set('Authorization', userAccessToken1)
- .expect(200)
- .end((err, res) => {
- assert.equal(res.status, 200, 'expects status equal to 200');
- done(err);
- });
- });
- it('should allow you to view your own apps of concern', done => {
- server.get('/api/v2/Devices/1/Apps/current/concern')
- .set('Authorization', userAccessToken1)
- .expect(200)
- .end((err, res) => {
- assert.equal(res.status, 200, 'expects status equal to 200');
- done(err);
- });
- });
- it('should allow you to view your own curfewblock', done => {
- server.get('/api/v2/Devices/1/CurfewBlocks/?day=1')
- .set('Authorization', userAccessToken1)
- .expect(200)
- .end((err, res) => {
- assert.equal(res.status, 200, 'expects status equal to 200');
- done(err);
- });
- });
- it('should NOT allow user to make a curfew', function(done) {
- this.timeout(10000);
- server.post('/api/v2/Devices/2/Curfew')
- .set('Authorization', userAccessToken2)
- .send({
- 'deviceId': 2,
- 'sunday': false,
- 'monday': false,
- 'tuesday': false,
- 'wednesday': false,
- 'thursday': false,
- 'friday': false,
- 'saturday': false
- })
- .expect(500)
- .end((err, res) => {
- assert.equal(res.status, 500, 'expects status equal to 500');
- done(err);
- });
- });
- it('should allow user to get curfew', function(done) {
- this.timeout(10000);
- server.get('/api/v2/Devices/2/Curfew')
- .set('Authorization', userAccessToken2)
- .expect(200)
- .end((err, res) => {
- assert.notEqual(res.body, null, 'expects curfew not equal to null');
- done(err);
- });
- });
- it('should NOT allow other user to get curfew', function(done) {
- this.timeout(10000);
- server.get('/api/v2/Devices/1/Curfew')
- .set('Authorization', userAccessToken2)
- .expect(401)
- .end((err, res) => {
- assert.equal(res.status, 401, 'expects status equal to 401');
- done(err);
- });
- });
- it('should NOT allow other user to add curfewBlocks', function(done) {
- this.timeout(10000);
- server.post('/api/v2/Devices/1/CurfewBlocks/0')
- .set('Authorization', userAccessToken2)
- .send({ curfewBlock: { start: '00:00:00', end: '01:00:00' } })
- .expect(401)
- .end((err, res) => {
- assert.equal(res.status, 401, 'expects status equal to 401');
- done(err);
- });
- });
- it('should allow user to add curfewBlocks', function(done) {
- this.timeout(10000);
- server.post('/api/v2/Devices/1/CurfewBlocks/4')
- .set('Authorization', userAccessToken1)
- .send({ curfewBlock: { start: '00:00:00', end: '01:00:00' } })
- .expect(200)
- .end((err, res) => {
- done(err);
- });
- });
- it('should NOT allow the public to view your filters', done => {
- server.get('/api/v2/Devices/1/Filters/search?q=str')
- .expect(401)
- .end((err, res) => {
- assert.equal(res.status, 401, 'expects status equal to 401');
- done(err);
- });
- });
- it('should NOT allow the public to view Other Apps', done => {
- server.get('/api/v2/Devices/1/Apps/current/other')
- .expect(401)
- .end((err, res) => {
- assert.equal(res.status, 401, 'expects status equal to 401');
- done(err);
- });
- });
- it('should NOT allow the public to view your apps of concern', done => {
- server.get('/api/v2/Devices/1/Apps/current/concern')
- .expect(401)
- .end((err, res) => {
- assert.equal(res.status, 401, 'expects status equal to 401');
- done(err);
- });
- });
- it('should NOT allow the public to view your curfewblock', done => {
- server.get('/api/v2/Devices/1/CurfewBlocks/?day=1')
- .expect(401)
- .end((err, res) => {
- assert.equal(res.status, 401, 'expects status equal to 401');
- done(err);
- });
- });
- it('should NOT allow other user to view your filters', done => {
- server.get('/api/v2/Devices/1/Filters/search?q=str')
- .set('Authorization', userAccessToken2)
- .expect(401)
- .end((err, res) => {
- assert.equal(res.status, 401, 'expects status equal to 401');
- done(err);
- });
- });
- it('should NOT allow the public to view your curfewblock', done => {
- server.get('/api/v2/Devices/1/CurfewBlocks/?day=1')
- .expect(401)
- .end((err, res) => {
- assert.equal(res.status, 401, 'expects status equal to 401');
- done(err);
- });
- });
- it('should NOT allow other user to view Other Apps', done => {
- server.get('/api/v2/Devices/1/Apps/current/other')
- .set('Authorization', userAccessToken2)
- .expect(401)
- .end((err, res) => {
- assert.equal(res.status, 401, 'expects status equal to 401');
- done(err);
- });
- });
- it('should NOT allow other user to view your own apps of concern', done => {
- server.get('/api/v2/Devices/1/Apps/current/concern')
- .set('Authorization', userAccessToken2)
- .expect(401)
- .end((err, res) => {
- assert.equal(res.status, 401, 'expects status equal to 401');
- done(err);
- });
- });
- it('should NOT allow other user to view your curfewblock', done => {
- server.get('/api/v2/Devices/1/CurfewBlocks/?day=1')
- .set('Authorization', userAccessToken2)
- .expect(401)
- .end((err, res) => {
- assert.equal(res.status, 401, 'expects status equal to 401');
- done(err);
- });
- });
- it('should allow you to view your current location', done => {
- server.get('/api/v2/Devices/1/Locations/current')
- .set('Authorization', userAccessToken1)
- .expect(200)
- .end((err, res) => {
- assert.equal(res.status, 200, 'expects status equal to 200');
- done(err);
- });
- });
- it('should NOT allow other user to view your current location', done => {
- server.get('/api/v2/Devices/1/Locations/current')
- .set('Authorization', userAccessToken2)
- .expect(401)
- .end((err, res) => {
- assert.equal(res.status, 401, 'expects status equal to 401');
- done(err);
- });
- });
- it('should not allow the public to view your apps log', done => {
- server.get('/api/v2/Devices/1/Apps/log')
- .expect(401)
- .end((err, res) => {
- assert.equal(res.status, 401, 'expects status equal to 401');
- done(err);
- });
- });
- it('should allow you to view your apps log', done => {
- server.get('/api/v2/Devices/1/Apps/log')
- .set('Authorization', userAccessToken1)
- .expect(200)
- .end((err, res) => {
- assert.equal(res.status, 200, 'expects status equal to 200');
- done(err);
- });
- });
- it('should NOT allow other user to view your apps log', done => {
- server.get('/api/v2/Devices/1/Apps/log')
- .set('Authorization', userAccessToken2)
- .expect(401)
- .end((err, res) => {
- assert.equal(res.status, 401, 'expects status equal to 401');
- done(err);
- });
- });
- it('should not allow the public to post your locations', done => {
- server.post('/api/v2/Devices/1/Locations/bulk')
- .expect(401)
- .end((err, res) => {
- assert.equal(res.status, 401, 'expects status equal to 401');
- done(err);
- });
- });
- it('should not allow the public to view your locations - map breadcrumbs', done => {
- server.get('/api/v2/Devices/1/Locations/search?startDate=2016-02-10' +
- '&endDate=2016-02-18&neLat=30.000035762786865&neLon=' +
- '124.97752904891968&swLat=29.99994993209839&swLon=124.9774432182312')
- .expect(401)
- .end((err, res) => {
- done(err);
- });
- });
- it('should not allow other user to post your locations', done => {
- server.post('/api/v2/Devices/1/Locations/bulk')
- .set('Authorization', userAccessToken2)
- .expect(401)
- .end((err, res) => {
- assert.equal(res.status, 401, 'expects status equal to 401');
- done(err);
- });
- });
- it('should NOT allow other user to view your locations - map breadcrumbs', done => {
- server.get('/api/v2/Devices/1/Locations/search?startDate=2016-02-10' +
- '&endDate=2016-02-18&neLat=30.000035762786865&neLon=' +
- '124.97752904891968&swLat=29.99994993209839&swLon=124.9774432182312')
- .set('Authorization', userAccessToken2)
- .expect(401)
- .end((err, res) => {
- done(err);
- });
- });
- it('should allow you to post your locations', done => {
- server.post('/api/v2/Devices/1/Locations/bulk')
- .set('Authorization', userAccessToken1)
- .send([])
- .expect(200)
- .end((err, res) => {
- assert.equal(res.status, 200, 'expects status equal to 200');
- done(err);
- });
- });
- it('should allow you to view your locations - map breadcrumbs', done => {
- server.get('/api/v2/Devices/1/Locations/search?startDate=2016-02-10' +
- '&endDate=2016-02-18&neLat=30.000035762786865&neLon=' +
- '124.97752904891968&swLat=29.99994993209839&swLon=124.9774432182312')
- .set('Authorization', userAccessToken1)
- .expect(200)
- .end((err, res) => {
- done(err);
- });
- });
- it('should allow device owner to connect an iCloud account', function(done) {
- const riCloud = require('ricloud');
- const riCloudLoginStub = sinon.stub(riCloud.prototype, 'login', function() {
- riCloudLoginStub.restore();
- riCloudLoginStub.yield(0, null);
- });
- server.post('/api/v2/Devices/1/connect/iCloud/?iCloudEmail=share@intelliminds.com&iCloudPassword=3415Sepulveda')
- .set('Authorization', userAccessToken1)
- .expect(200)
- .end((err, res) => {
- done(err);
- });
- });
- it('should NOT allow other user to connect an iCloud account', function(done) {
- server.post('/api/v2/Devices/1/connect/iCloud/?iCloudEmail=share@intelliminds.com&iCloudPassword=3415Sepulveda')
- .set('Authorization', userAccessToken2)
- .expect(401)
- .end((err, res) => {
- assert.equal(res.status, 401, 'expects status equal to 401');
- done(err);
- });
- });
- let deviceId = 0;
- it('should allow a user to create a new device', done => {
- server.post('/api/v2/Devices/')
- .send({
- name: 'string',
- mediaId: 1,
- color: 'FF0000',
- type: 1,
- timezone: 'GMT',
- osVersion: '4.4.2',
- appVersion: '1.10.6',
- updateFrequency: 30
- })
- .set('Authorization', userAccessToken1)
- .expect(200)
- .end((err, res) => {
- deviceId = res.body.id;
- done(err);
- });
- });
- it('should allow a user to update a device', done => {
- server.put('/api/v2/Devices/' + deviceId)
- .send({
- name: 'string 2'
- })
- .set('Authorization', userAccessToken1)
- .expect(200)
- .end((err, res) => {
- assert.equal(res.body.name, 'string 2');
- done(err);
- });
- });
- it('should NOT allow others to update a device', done=> {
- server.put('/api/v2/Devices/' + deviceId)
- .send({
- name: 'string 3'
- })
- .set('Authorization', userAccessToken2)
- .expect(401)
- .end((err, res) => {
- done(err);
- });
- });
- });
- describe('getFiltersByPhrase', () => {
- it('should return 5 filter records', done => {
- server.get('/api/v2/Devices/1/Filters/search?q=str')
- .set('Authorization', userAccessToken1)
- .expect(200)
- .end((err, res) => {
- assert.equal(res.body.length, 5, 'expects length equal 5');
- assert.notEqual(res.body[0].deviceId, null, 'expects deviceId not equal to null');
- assert.notEqual(res.body[1].deviceId, null, 'expects deviceId not equal to null');
- done(err);
- });
- });
- it('should return 0 filter records', done => {
- server.get('/api/v2/Devices/1/Filters/search?q=testtest')
- .set('Authorization', userAccessToken1)
- .expect(200)
- .end((err, res) => {
- assert.equal(res.body.length, 0, 'expects length equal 0');
- done(err);
- });
- });
- });
- describe('#getCurrentOtherApps for User1', () => {
- describe('#getCurrentOtherApps For Device1 of User1 having device_app data', () => {
- it('should provide current Other Apps', done => {
- server.get('/api/v2/Devices/1/Apps/current/other')
- .set('Authorization', userAccessToken1)
- .expect(200)
- .end((err, res) => {
- assert.equal(res.body.length, 2, 'expects length equal to 2');
- done(err);
- });
- });
- it('should NOT provide current Other Apps using different token', done => {
- server.get('/api/v2/Devices/1/Apps/current/other')
- .set('Authorization', userAccessToken2)
- .expect(401)
- .end((err, res) => {
- assert.equal(res.status, 401, 'expects status equal to 401');
- done(err);
- });
- });
- });
- describe('#getCurrentOtherApps For Device1 of User1 without having device_app data', () => {
- it('should NOT provide current Other Apps', done => {
- server.get('/api/v2/Devices/2/Apps/current/other')
- .set('Authorization', userAccessToken1)
- .expect(401)
- .end((err, res) => {
- assert.equal(res.status, 401, 'expects status equal to 401');
- done(err);
- });
- });
- });
- });
- describe('#getCurrentOtherApps For Non Existing Device', () => {
- it('should NOT provide current other apps for other devices', done => {
- server.get('/api/v2/Devices/0/Apps/current/other')
- .set('Authorization', userAccessToken1)
- .expect(401)
- .end((err, res) => {
- assert.equal(res.status, 401, 'expects status equal to 401');
- done(err);
- });
- });
- });
- describe('#getCurrentOtherApps For Non Existing Device', () => {
- it('should NOT provide current other apps for other devices', done => {
- server.get('/api/v2/Devices/-1/Apps/current/other')
- .set('Authorization', userAccessToken1)
- .expect(401)
- .end((err, res) => {
- assert.equal(res.status, 401, 'expects status equal to 401');
- done(err);
- });
- });
- });
- describe('getAllAppsOfConcern', () => {
- it('should should provide accurate stats when viewing your own', done => {
- server.get('/api/v2/Devices/1/Apps/current/concern')
- .set('Authorization', userAccessToken1)
- .expect(200)
- .end((err, res) => {
- assert.equal(res.body.length, 1, 'expects length equal 1');
- done(err);
- });
- });
- });
- describe('getCurfewBlockByDay', () => {
- it('should return 3 curfewblock records', done => {
- server.get('/api/v2/Devices/1/CurfewBlocks/?day=1')
- .set('Authorization', userAccessToken1)
- .expect(200)
- .end((err, res) => {
- assert.equal(res.body.length, 3, 'expects length equal 3');
- done(err);
- });
- });
- it('should return no curfewblock record', done => {
- server.get('/api/v2/Devices/1/CurfewBlocks/?day=3')
- .set('Authorization', userAccessToken1)
- .expect(200)
- .end((err, res) => {
- assert.equal(res.body.length, 0, 'expects length equal 0');
- done(err);
- });
- });
- });
- describe('getCurrentLocation', () => {
- it('should return the current location of the device', done => {
- server.get('/api/v2/Devices/1/Locations/current')
- .set('Authorization', userAccessToken1)
- .expect(200)
- .end((err, res) => {
- assert.equal(res.body.deviceId, 1, 'expects length equal 1');
- assert.equal(res.body.address, 'blah blah', 'expects address equal to blah blah');
- done(err);
- });
- });
- it('should return an error', done => {
- server.get('/api/v2/Devices/3/Locations/current')
- .set('Authorization', userAccessToken1)
- .expect(404)
- .end((err, res) => {
- done(err);
- });
- });
- });
- describe('getPhotos', () => {
- it('should return photo records', done => {
- server.get('/api/v2/Devices/1/Photos')
- .set('Authorization', userAccessToken1)
- .expect(200)
- .end((err, res) => {
- assert.equal(res.body.length, 2, 'expects length equal 2');
- done(err);
- });
- });
- });
- describe('getAppsLog', () => {
- it('should return 2 records', done => {
- server.get('/api/v2/Devices/1/Apps/log?limit=&before' + (new Date() - 10))
- .set('Authorization', userAccessToken1)
- .expect(200)
- .end((err, res) => {
- const isGreater = res.body[0].created > res.body[1].created ? true : false;
- assert.equal(res.body.length, 2, 'expects length equal 2');
- assert.equal(isGreater, true, 'expects isGreater equal to true');
- done(err);
- });
- });
- });
- describe('#connectFacebook', () => {
- it('should return an error when invalid token is provided', function(done) {
- server.post('/api/v2/Devices/1/connect/facebook')
- .set('Authorization', userAccessToken1)
- .send({ accessToken: facebookToken })
- .expect(400)
- .end((err, res) => {
- assert.equal(res.status, 400, 'expects status equal to 400');
- done(err);
- });
- });
- });
- describe('#connectInstagram', () => {
- it('should return an error when invalid token is provided', (done) => {
- server.post('/api/v2/Devices/1/connect/instagram')
- .set('Authorization', userAccessToken1)
- .send({ accessToken: '1234567890.1234567890' })
- .expect(400)
- .end((err, res) => {
- done(err);
- });
- });
- });
- describe('#connectInstagram', () => {
- it('should return status of 200', (done) => {
- server.post('/api/v2/Devices/1/connect/instagram')
- .set('Authorization', userAccessToken1)
- .send({ accessToken: '1773147737.badc866.74f684c73dc44e3a92c9deac4cb0b4a7' })
- .expect(200)
- .end((err, res) => {
- done(err);
- });
- });
- });
- describe('#addBulkLocations', function() {
- let locations = [
- {
- latitude: 14.583091,
- longitude: 121.060972,
- address: '6808 Viverra. St',
- created: moment('2016-02-16').tz('Asia/Manila').format('x')
- },
- {
- latitude: 14.583091,
- longitude: 121.060972,
- address: 'P.O. Box 476, 1915 Et Avenue',
- created: moment('2016-02-15').tz('Asia/Manila').format('x')
- },
- {
- latitude: 14.583091,
- longitude: 121.060972,
- address: '607-6002 Enim. St.',
- created: moment('2016-02-14').tz('Asia/Manila').format('x')
- },
- {
- latitude: 14.583091,
- longitude: 121.060972,
- address: 'P.O. Box 821, 4470 Vivamus Avenue',
- created: moment('2016-02-13').tz('Asia/Manila').format('x')
- },
- {
- latitude: 14.583091,
- longitude: 121.060972,
- address: '890-496 Tincidunt St.',
- created: moment('2016-02-13').tz('Asia/Manila').format('x')
- },
- {
- latitude: 14.583091,
- longitude: 121.060972,
- address: '890-496 Tincidunt St.',
- created: moment('2016-02-13').tz('Asia/Manila').format('x')
- },
- {
- latitude: 15.705522,
- longitude: 120.013430,
- address: 'Candelaria',
- created: moment('2016-02-13').tz('Asia/Manila').format('x')
- }
- ];
- it('should return status of 200', function(done) {
- server.post('/api/v2/Devices/1/Locations/bulk')
- .set('Authorization', userAccessToken1)
- .send({ locations: locations })
- .expect(200)
- .end((err, res) => {
- assert.equal(res.status, 200, 'expects status equal to 200');
- assert.equal(queue.queue.length, 1, 'expects length equal to 1');
- assert.equal(queue.queue[0].type, 'locations-bulk', 'expects type equal to locations-bulk');
- assert.equal(JSON.parse(queue.queue[0].message).length, 7, 'expects queue.message length equal to 7');
- done(err);
- });
- });
- //we already have 5 data on locationlog plus 6 data on top which has one duplicate so real data should only be 10
- it('LocationLogs.importLocations - should return record count of 10', function(done) {
- for (let location of locations) {
- location.deviceId = 1;
- }
- LocationLog.importLocations(locations, (err) => {
- LocationLog.count({ deviceId: 1 }, (err, resp) => {
- assert.equal(resp, 10, 'expects count equal to 10');
- GeofenceSummary.find({ where: { deviceId: 1 }, order: 'date' }, (err, res) => {
- assert.equal(res.length, 5);
- assert.equal(res[0].geofenceId, 1);
- assert.equal(res[1].geofenceId, null);
- assert.equal(res[2].geofenceId, 1);
- assert.equal(res[3].geofenceId, 1);
- assert.equal(res[4].geofenceId, 1);
- assert.equal(res[0].count, 3);
- const count = _.reduce(res, (x, y) => {
- x.count = x.count + y.count;
- return x;
- });
- assert.equal(count.count, 7);
- Activity.count({ type: Activity.TYPES.GEOFENCE_ARRIVED }, (err, count) => {
- assert.equal(count, 6);
- done(err);
- });
- });
- });
- });
- });
- it('LocationLogs.importLocations. Add more locations', function(done) {
- for (let location of locations) {
- location.deviceId = 1;
- }
- LocationLog.importLocations(locations, (err) => {
- LocationLog.count({ deviceId: 1 }, (err, resp) => {
- assert.equal(resp, 10, 'expects count equal to 10');
- GeofenceSummary.find({ where: { deviceId: 1 }, order: 'date' }, (err, res) => {
- assert.equal(res.length, 5);
- assert.equal(res[0].geofenceId, 1);
- assert.equal(res[1].geofenceId, null);
- assert.equal(res[2].geofenceId, 1);
- assert.equal(res[3].geofenceId, 1);
- assert.equal(res[4].geofenceId, 1);
- assert.equal(res[0].count, 6);
- //check order
- assert(new Date(res[0].date) <= new Date(res[1].date));
- assert(new Date(res[1].date) < new Date(res[2].date));
- assert(new Date(res[2].date) < new Date(res[3].date));
- assert(new Date(res[3].date) < new Date(res[4].date));
- const count = _.reduce(res, (x, y) => {
- x.count = x.count + y.count;
- return x;
- });
- assert.equal(count.count, 14);
- Activity.count({ type: Activity.TYPES.GEOFENCE_ARRIVED }, (err, count) => {
- assert.equal(count, 6);
- done(err);
- });
- });
- });
- });
- });
- });
- describe('getLocationByDateAndCoordinates', () => {
- describe('test not in range and not in bounding box', () => {
- it('should return empty array', done => {
- server.get('/api/v2/Devices/1/Locations/search?startDate=2016-02-10&endDate=2016-02-18' +
- '&neLat=47.83243417739868&neLon=' +
- '112.55842924118042&swLat=47.832348346710205&swLon=112.55834341049194')
- .set('Authorization', userAccessToken1)
- .end((err, res) => {
- assert.equal(res.body.length, 0, 'expects length equal 0');
- done(err);
- });
- });
- });
- describe('test in range and not in bounding box', () => {
- it('should return empty array', done => {
- server.get('/api/v2/Devices/1/Locations/search?startDate=2016-02-10&endDate=2016-02-18' +
- '&neLat=47.83243417739868&neLon=' +
- '112.55842924118042&swLat=47.832348346710205&swLon=112.55834341049194')
- .set('Authorization', userAccessToken1)
- .end((err, res) => {
- assert.equal(res.body.length, 0, 'expects length equal 0');
- done(err);
- });
- });
- });
- describe('test not in range and in bounding box', () => {
- it('should return empty array', done => {
- server.get('/api/v2/Devices/1/Locations/search?startDate=2016-02-01&endDate=2016-02-08' +
- '&neLat=30.000035762786865&neLon=' +
- '124.97752904891968&swLat=29.99994993209839&swLon=124.9774432182312')
- .set('Authorization', userAccessToken1)
- .end((err, res) => {
- assert.equal(res.body.length, 0, 'expects length equal 0');
- done(err);
- });
- });
- });
- describe('test in range and in bounding box', () => {
- it('should return 2 locations', done => {
- server.get('/api/v2/Devices/1/Locations/search?startDate=2016-02-10&endDate=2016-02-18' +
- '&neLat=30.000035762786865&neLon=' +
- '124.97752904891968&swLat=29.99994993209839&swLon=124.9774432182312')
- .set('Authorization', userAccessToken1)
- .end((err, res) => {
- const isGreater = res.body[0].created > res.body[1].created ? true : false;
- assert.equal(res.body.length, 2, 'expects length equal 2');
- assert.equal(isGreater, true, 'expects isGreater equal to true');
- assert.equal(res.body[0].deviceId, 1, 'expects deviceId equal to 1');
- assert.equal(res.body[1].deviceId, 1, 'expects deviceId equal to 1');
- done(err);
- });
- });
- });
- });
- describe('curfew', () => {
- let curfewBlocks = [
- {
- start: '00:00:00', //good
- end: '02:00:00'
- },
- {
- start: '00:00:00', //bad - overlapping front and end
- end: '05:00:00'
- },
- {
- start: '01:00:00', //bad - overlapping front
- end: '05:00:00'
- },
- {
- start: '02:00:00', //good - curfew can start when another ends
- end: '05:00:00'
- },
- {
- start: '10:00:00', //good - later hour to midnight
- end: '00:00:00'
- },
- {
- start: '08:00:00',//bad - overlapping end
- end: '11:00:00'
- }
- ];
- it('should return the correct curfew for the device', function(done) {
- this.timeout(10000);
- server.get('/api/v2/Devices/2/Curfew')
- .set('Authorization', userAccessToken2)
- .expect(200)
- .end((err, res) => {
- assert.notEqual(res.body, null, 'expects curfew to exist');
- assert.equal(res.body.deviceId, 2, 'expects curfew to belong to correct device');
- done(err);
- });
- });
- it('should return the newly created curfew block', function(done) {
- this.timeout(10000);
- server.post('/api/v2/Devices/2/CurfewBlocks/0')
- .set('Authorization', userAccessToken2)
- .send({ 'curfewBlock': curfewBlocks[0] })
- .expect(200)
- .end((err, res) => {
- assert.notEqual(res.body, null, 'expects curfewBlock to exist');
- assert.equal(res.body.deviceId, 2, 'expects curfewBlock to belong to correct device');
- assert.equal(res.body.start, curfewBlocks[0].start, 'expects curfewBlock start to equal start entered');
- assert.equal(res.body.end, curfewBlocks[0].end, 'expects curfewBlock end to equal end entered');
- done(err);
- });
- });
- it('should NOT allow start and end times to overlap with existing curfewBlocks', function(done) {
- this.timeout(10000);
- server.post('/api/v2/Devices/2/CurfewBlocks/0')
- .set('Authorization', userAccessToken2)
- .send({ 'curfewBlock': curfewBlocks[1] })
- .expect(422)
- .end((err, res) => {
- done(err);
- });
- });
- it('should NOT allow starting times to overlap with existing curfewBlocks', function(done) {
- this.timeout(10000);
- server.post('/api/v2/Devices/2/CurfewBlocks/0')
- .set('Authorization', userAccessToken2)
- .send({ 'curfewBlock': curfewBlocks[2] })
- .expect(422)
- .end((err, res) => {
- done(err);
- });
- });
- it('should allow adding curfewBlocks that start when other events end', function(done) {
- this.timeout(10000);
- server.post('/api/v2/Devices/2/CurfewBlocks/0')
- .set('Authorization', userAccessToken2)
- .send({ 'curfewBlock': curfewBlocks[3] })
- .expect(200)
- .end((err, res) => {
- assert.notEqual(res.body, null, 'expects curfewBlock to exist');
- assert.equal(res.body.deviceId, 2, 'expects curfewBlock to belong to correct device');
- assert.equal(res.body.start, curfewBlocks[3].start, 'expects curfewBlock start to equal start entered');
- assert.equal(res.body.end, curfewBlocks[3].end, 'expects curfewBlock end to equal end entered');
- done(err);
- });
- });
- it('should allow a non-overlapping curfew block to end at midnight', function(done) {
- this.timeout(10000);
- server.post('/api/v2/Devices/2/CurfewBlocks/0')
- .set('Authorization', userAccessToken2)
- .send({ 'curfewBlock': curfewBlocks[4] })
- .expect(200)
- .end((err, res) => {
- assert.notEqual(res.body, null, 'expects curfewBlock to exist');
- assert.equal(res.body.deviceId, 2, 'expects curfewBlock to belong to correct device');
- assert.equal(res.body.start, curfewBlocks[4].start, 'expects curfewBlock start to equal start entered');
- assert.equal(res.body.end, curfewBlocks[4].end, 'expects curfewBlock end to equal end entered');
- done(err);
- });
- });
- it('should not allow end times to overlap with existing curfewBlocks', function(done) {
- this.timeout(10000);
- server.post('/api/v2/Devices/2/CurfewBlocks/0')
- .set('Authorization', userAccessToken2)
- .send({ 'curfewBlock': curfewBlocks[5] })
- .expect(422)
- .end((err, res) => {
- done(err);
- });
- });
- });
- describe('connectICloud', () => {
- it('Should require icloud email', function(done) {
- server.post('/api/v2/Devices/1/connect/iCloud/?iCloudPassword=3415Sepulveda')
- .set('Authorization', userAccessToken1)
- .expect(400)
- .end((err, res) => {
- done(err);
- });
- });
- it('Should require icloud password', function(done) {
- server.post('/api/v2/Devices/1/connect/iCloud/?iCloudEmail=share@intelliminds.com')
- .set('Authorization', userAccessToken1)
- .expect(400)
- .end((err, res) => {
- done(err);
- });
- });
- it('should return the updated device', function(done) {
- const riCloud = require('ricloud');
- const riCloudLoginStub = sinon.stub(riCloud.prototype, 'login', function() {
- riCloudLoginStub.restore();
- riCloudLoginStub.yield(0, null);
- });
- server.post('/api/v2/Devices/2/connect/iCloud/?iCloudEmail=share@intelliminds.com&iCloudPassword=3415Sepulveda')
- .set('Authorization', userAccessToken2)
- .expect(200)
- .end((err, res) => {
- assert.notEqual(res.body.iCloudUsername, null, 'expects the icloud username to be updated');
- done(err);
- });
- });
- it('simulate invalid icloud credentials should return 403', function(done) {
- const res = {
- body: {
- message: {}
- },
- statusCode: 403
- };
- const riCloud = require('ricloud');
- const riCloudLoginStub = sinon.stub(riCloud.prototype, 'login', function() {
- riCloudLoginStub.restore();
- riCloudLoginStub.yield(1, res);
- });
- server.post('/api/v2/Devices/2/connect/iCloud/?iCloudEmail=share@intelliminds.com&iCloudPassword=3415Sepulveda')
- .set('Authorization', userAccessToken2)
- .expect(403)
- .end((err, res) => {
- done(err);
- });
- });
- it('simulate valid icloud credentials with session error should return 503', function(done) {
- const res = {
- body: {
- message: 'Your iCloud session has expired. To continue, please sign in again.'
- },
- statusCode: 503
- };
- const riCloud = require('ricloud');
- const riCloudLoginStub = sinon.stub(riCloud.prototype, 'login', function() {
- riCloudLoginStub.restore();
- riCloudLoginStub.yield(1, res);
- });
- server.post('/api/v2/Devices/2/connect/iCloud/?iCloudEmail=share@intelliminds.com&iCloudPassword=3415Sepulveda')
- .set('Authorization', userAccessToken2)
- .expect(503)
- .end((err, res) => {
- done(err);
- });
- });
- });
- describe('#mdmToken', () => {
- it('should vaidate mdmToken if it has value or not', (done) => {
- assert(devices[0].mdmToken === 'string', true, `ios' mdmtoken should not be empty.`);
- assert(devices[1].mdmToken == null, true, `android's mdmtoken should be empty.`);
- assert(devices[2].mdmToken !== null, true, `ios' mdmtoken should not be empty.`);
- done();
- });
- });
- describe('markAllActivitiesAsRead', () => {
- it('should NOT allow a user to mark other users activities as read', done => {
- server.post('/api/v2/Devices/1/Activities/markAllAsRead')
- .set('Authorization', userAccessToken2)
- .expect(401)
- .end((err, res) => {
- done(err);
- });
- });
- it('should allow a user to mark his own activities as read', done => {
- server.post('/api/v2/Devices/2/Activities/markAllAsRead')
- .set('Authorization', userAccessToken2)
- .expect(200)
- .end((err, res) => {
- done(err);
- });
- });
- it('should return 0 unreadActivityCount for deviceId 2', done => {
- server.get('/api/v2/Accounts/2')
- .set('Authorization', userAccessToken2)
- .expect(200)
- .end((err, res) => {
- const account = res.body;
- assert.equal(account.Devices[0].id, 2, 'expects deviceId 2');
- assert.equal(account.Devices[0].unreadActivityCount, 0, 'expects 0 unreadActivityCount');
- done(err);
- });
- });
- });
- describe('test messages' , () => {
- it('test sent message with object', done => {
- server.post('/api/v2/Devices/2/Chats/Messages/bulk')
- .set('Authorization', userAccessToken2)
- .send({
- 'message': 'Fuck You',
- 'fromMe': true,
- 'created': new Date(),
- 'flagged': true,
- 'flaggedWords' : ['fuck'],
- 'imported': 1,
- 'contact': {
- 'deviceId': 2,
- 'sourceId': 2,
- 'firstname': '09216185215',
- 'lastname': 'N/A',
- 'address': '09216185215'
- }
- })
- .expect(200)
- .end((err, res) => {
- done(err);
- });
- });
- });
- describe('test getDevicesForLocationLogSilentPush', () => {
- it('test different scenario', done => {
- let deviceInstance = {};
- const token = '2ddc9c60eb4310fe60e9eb84f0ce14bfa08e66814548dadcd01b1d6644854957';
- Device.findById(1).then(device => {
- deviceInstance = device;
- deviceInstance.pushToken = token;
- deviceInstance.type = 0;
- deviceInstance.deleted = false;
- return deviceInstance.save();
- }).then(device => {
- deviceInstance = device;
- return Device.getDevicesForLocationLogSilentPush();
- }).then(devices => {
- assert.equal(devices.length, 1, 'should return devices');
- deviceInstance.pushToken = null;
- return deviceInstance.save();
- }).then(device => {
- deviceInstance = device;
- return Device.getDevicesForLocationLogSilentPush();
- }).then(devices => {
- assert.equal(devices.length, 0, 'should return no devices');
- deviceInstance.pushToken = token;
- deviceInstance.type = 1;
- return deviceInstance.save();
- }).then(device => {
- deviceInstance = device;
- return Device.getDevicesForLocationLogSilentPush();
- }).then(devices => {
- assert.equal(devices.length, 0, 'should return no devices');
- deviceInstance.type = 0;
- deviceInstance.deleted = true;
- return deviceInstance.save();
- }).then(device => {
- deviceInstance = device;
- return Device.getDevicesForLocationLogSilentPush();
- }).then(devices => {
- assert.equal(devices.length, 0, 'should return no devices');
- done();
- }).catch(err => {
- console.log(err);
- done(err);
- });
- });
- });
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement