@arisan/data-api
Version:
The Universal Database API Gateway for CLIO's Modules
524 lines (519 loc) • 18.2 kB
JavaScript
'use strict'
/*** Import Packages ***
***********************/
var async = require('async');
var bunyan = require('bunyan');
var chai = require('chai');
var request = require('supertest');
var mongodb = require('mongodb');
/*** Initialize Imported Objects ***
***********************************/
var assert = chai.assert;
var MongoClient = mongodb.MongoClient;
var ObjectID = mongodb.ObjectID;
/*** File Scope Variables ***
****************************/
var db;
var nodeTypes = [
//{
// title: 'Recorder',
// path: 'recorders',
// collectionName: 'recorders'
//},
//{
// title: 'Streamer',
// path: 'streamers',
// collectionName: 'streamers'
//},
//{
// title: 'Camera',
// path: 'cameras',
// collectionName: 'cameras'
//}
];
/*** Tests ***
*************/
// describe('Data API RESTful Interface', function () {
// var app = require('../src/app');
// before(function (done) {
// this.timeout(0);
// async.series([
//
// function (callback) {
// dropClioCollections(callback);
// },
// function (callback) {
// app.init({
// mongoUrl : process.env.CLIO_MONGO_URL,
// serverPort: process.env.CLIO_DATA_API_PORT || 15925,
// logLevel : app.LOG_LEVEL.FATAL
// });
// callback();
// },
// function (callback) {
// app.launch(function (err) {
// if (!err) callback();
// });
// }
// ], function (err, results) {
// if (!err) done();
// });
// });
// nodeTypes.forEach(function (nodeType) {
// if (nodeType.title === 'Camera') {
// return;
// }
// var title = nodeType.title;
// var path = nodeType.path;
// var collectionName = nodeType.collectionName;
// var id;
// describe('POST /' + path, function () {
// it('should respond 400 Bad Request if port not supplied',
// function (done) {
// request(app).post('/' + path)
// .expect(400, done);
// });
// it('should respond 201 Created and ID if new ' + title + ' registered',
// function (done) {
// request(app).post('/' + path)
// .send({
// port: 15925
// })
// .expect(201)
// .expect(function (res) {
// id = res.text;
// var idRegExp = new RegExp('^[a-fA-F0-9]{24}$');
// if (!idRegExp.test(id)) {
// throw new Error('Invalid ID (' + id + ')');
// }
// })
// .end(done);
// });
// it('should respond 200 OK and ID if ' + title + ' already registered',
// function (done) {
// request(app).post('/' + path)
// .send({
// port: 15925
// })
// .expect(200)
// .expect(function (res) {
// var newId = res.text;
// var idRegExp = new RegExp('^[a-fA-F0-9]{24}$');
// if (!idRegExp.test(id)) {
// throw new Error('Invalid ID (' + id + ')');
// }
// })
// .end(done);
// });
// });
// describe('PUT /' + path + '/:id', function () {
// it('should respond 400 Bad Request if body not supplied',
// function (done) {
// request(app).put('/' + path + '/' + id)
// .expect(400, done);
// });
// it('should respond 400 Bad Request if ID invalid',
// function (done) {
// var body = {};
// if (path === 'camera-agents') {
// body.cameraIds = [];
// } else {
// body.streamIds = [];
// }
// request(app).put('/' + path + '/invalidid')
// .send(body)
// .expect(400, done);
// });
// it('should respond 200 OK if ' + title + ' updated',
// function (done) {
// var lastUpdated;
// async.series([
//
// function (callback) {
// db.collection(collectionName).find({
// _id: new ObjectID(id)
// }).limit(1).next(function (err, result) {
// lastUpdated = result.updated;
// callback();
// });
// },
// function (callback) {
// var body = {};
// if (path === 'camera-agents') {
// body.cameraIds = [];
// } else {
// body.streamIds = ['123', '234', '345'];
// }
// request(app).put('/' + path + '/' + id)
// .send(body)
// .expect(200)
// .end(function (err, result) {
// if (!err) {
// callback();
// } else {
// body.port = 123;
// if (path === 'camera-agents') {
// body.cameraIds = [];
// } else {
// body.streamIds = ['456'];
// }
// request(app).post('/' + path)
// .send(body)
// .expect(202, done);
// }
// });
// },
// function (callback) {
// db.collection(collectionName).find({
// _id: new ObjectID(id)
// }).limit(1).next(function (err, result) {
// if (lastUpdated.getTime() === result.updated.getTime()) {
// throw new Error('updated Field Not Updated');
// }
// done();
// callback();
// });
// }
// ]);
// });
// it('should respond 404 Not Found if ' + title + ' not found',
// function (done) {
// var nonexistentId = '12345678901234567890abcd';
// var body = {};
// if (path === 'camera-agents') {
// body.cameraIds = [];
// } else {
// body.streamIds = [];
// }
// request(app).put('/' + path + '/' + nonexistentId)
// .send(body)
// .expect(404, done);
// });
// });
// });
// var cameraId;
// //describe('POST /cameras', function () {
// // it('should respond 400 Bad Request if publicAddress unspecified',
// // function (done) {
// // request(app).post('/cameras')
// // .send({
// // webPort: 1001
// // })
// // .expect(400, done);
// // });
// // it('should respond 400 Bad Request if webPort unspecified',
// // function (done) {
// // request(app).post('/cameras')
// // .send({
// // publicAddress: '210.61.67.37'
// // })
// // .expect(400, done);
// // });
// // it('should respond 201 Created and ID if all information supplied',
// // function (done) {
// // request(app).post('/cameras')
// // .send({
// // projectId: '123',
// // publicAddress: '210.61.67.37',
// // webPort: 1001,
// // title: '101',
// // username: 'Admin',
// // password: '1234',
// // publicRtspPort: 10001,
// // usePublicRtspPort: true,
// // privateAddress: '192.168.1.101',
// // privateMjpegPort: 8001
// // })
// // .expect(201)
// // .expect(function (res) {
// // cameraId = res.text;
// // var idRegExp = new RegExp('^[a-fA-F0-9]{24}$');
// // if (!idRegExp.test(cameraId)) {
// // throw new Error('Invalid ID (' + cameraId + ')');
// // }
// // })
// // .end(done);
// // });
// // it('should respond 201 Created and ID with Only Project ID, Public Address and Web Port',
// // function (done) {
// // request(app).post('/cameras')
// // .send({
// // projectId: '123',
// // publicAddress: '210.61.67.37',
// // webPort: 1002
// // })
// // .expect(201)
// // .expect(function (res) {
// // var id = res.text;
// // var idRegExp = new RegExp('^[a-fA-F0-9]{24}$');
// // if (!idRegExp.test(id)) {
// // throw new Error('Invalid ID (' + id + ')');
// // }
// // })
// // .end(done);
// // });
// // it('should respond 409 Conflict if the camera already exists',
// // function (done) {
// // request(app).post('/cameras')
// // .send({
// // projectId: '123',
// // publicAddress: '210.61.67.37',
// // webPort: 1002
// // })
// // .expect(409, done);
// // });
// //});
// //describe('PUT /cameras/:id', function () {
// // it('shoudl respond 400 Bad Request if ID invalid',
// // function (done) {
// // request(app).put('/cameras/invalidid')
// // .send({
// // publicAddress: '211.21.26.67'
// // })
// // .expect(400, done);
// // });
// // it('should respond 400 Bad Request if no request body',
// // function (done) {
// // request(app).put('/cameras/' + cameraId)
// // .expect(400, done);
// // });
// // it('should respond 404 Not found if not found', function (done) {
// // request(app).put('/cameras/' + cameraId.substring(1) + '0')
// // .send({
// // title: 'Working Title'
// // })
// // .expect(404, done);
// // });
// // it('should respond 200 OK if title specified',
// // function (done) {
// // request(app).put('/cameras/' + cameraId)
// // .send({
// // title: 'Working Title'
// // })
// // .expect(200, done);
// // });
// // it('should respond 200 OK if all information specified',
// // function (done) {
// // request(app).put('/cameras/' + cameraId)
// // .send({
// // publicAddress: '123.123.123.123',
// // webPort: '1234',
// // title: 'Some Title',
// // privateAddress: '192.168.1.1',
// // publicRtspPort: 20000,
// // userPublicRtspPort: true,
// // privateMjpegPort: 8080,
// // username: 'Admin',
// // password: '1234',
// // streams: [{
// // rtspUrl: 'rtsp://Admin:1234@192.168.1.1',
// // availablePlans: [
// // '720p30@922k',
// // '720p15@922k'
// // ],
// // intendedPlan: '720p15@922k',
// // currentPlan: '720p30@922k'
// // }]
// // })
// // .expect(200, done);
// // });
// //});
// //describe('GET /cameras/:id', function () {
// // it('should respond 400 Bad Request if ID invalid', function (done) {
// // request(app).get('/cameras/invalidid')
// // .expect(400, done);
// // });
// // it('should respond 404 Not Found if not found', function (done) {
// // var fakeId = '6' + cameraId.substring(1);
// // request(app).get('/cameras/' + fakeId)
// // .expect(404, done);
// // });
// // it('should respond 200 OK and camera object', function (done) {
// // request(app).get('/cameras/' + cameraId)
// // .expect(200, done);
// // });
// //});
// //describe('GET /cameras/:id/streams/:index/live-info', function () {
// // it('should get streamer with least number of streamers',
// // function (done) {
// // request(app).get('/cameras/' + cameraId + '/streams/0/live-info')
// // .expect(200)
// // .end(function (err, res) {
// // if (err) {
// // done(err);
// // } else {
// // done();
// // }
// // });
// // });
// //});
// describe('POST Unknown Node Types', function () {
// it('should respond 404 Not Found', function (done) {
// request(app).post('/unknown')
// .send({
// port: 15925
// })
// .expect(404, done);
// });
// });
// describe('PUT Unknown Node Types', function () {
// it('should respond 404 Not Found', function (done) {
// request(app).put('/unknown/12345678901234567890abcdef')
// .send({
// streams: []
// })
// .expect(404, done);
// });
// });
// //describe('Update Archive Status with Text', function () {
// // it('should respond 200', function (done) {
// // request(app).put('/cameras/' + cameraId + '/streams/0/archive-status')
// // .type('text/plain')
// // .send('Activating')
// // .expect(200, done);
// // });
// //});
// //describe('GET Last Video Info', function () {
// // it('should respond index 0 if empty', function (done) {
// // request(app).get('/cameras/' + cameraId + '/streams/0/video-info/-1')
// // .expect(200)
// // .expect(function (res) {
// // if (res.body.index !== 0) {
// // throw new Error();
// // }
// // })
// // .end(done);
// // });
// //});
// //describe('PUT Video Info', function () {
// // it('should respond 200', function (done) {
// // request(app).put('/cameras/' + cameraId + '/streams/0/video-info')
// // .send({
// // startTime: new Date(),
// // endTime: new Date(),
// // objectName: '123',
// // index: 1
// // }).expect(200, done);
// // });
// //});
// //describe('GET Last Video Info', function () {
// // it('should respond latest video info index', function (done) {
// // request(app).get('/cameras/' + cameraId + '/streams/0/video-info/-1')
// // .expect(200)
// // .expect(function (res) {
// // if (res.body.index !== 1) {
// // throw new Error();
// // }
// // })
// // .end(done);
// // });
// //});
// after(function () {
// app.shutdown(function () {
// app = null;
// });
// });
// });
//
// describe('Data API app Module', function () {
// var app = require('../src/app');
// describe('init()', function () {
// it('should return false if MongoDB URL not specified',
// function (done) {
// var result = app.init({
// serverPort: 15925,
// logLevel : app.LOG_LEVEL.NONE
// });
// assert.isFalse(result);
// done();
// });
// it('should return false if MongoDB URL not beginning with "mongodb://"',
// function (done) {
// var result = app.init({
// mongoUrl : "localhost/test",
// serverPort: 15925,
// logLevel : app.LOG_LEVEL.NONE
// });
// assert.isFalse(result);
// done();
// });
// it('should return false if serverPort specified out of range',
// function (done) {
// var result = app.init({
// serverPort: 65536,
// mongoUrl : process.env.CLIO_MONGO_URL,
// logLevel : app.LOG_LEVEL.NONE
// });
// assert.isFalse(result);
// done();
// });
// it('should use INFO log level if unspecified', function (done) {
// app.init({
// serverPort: 15925,
// mongoUrl : process.env.CLIO_MONGO_URL
// });
// assert.equal(app.getLogLevel(), app.LOG_LEVEL.INFO);
// done();
// });
// });
// describe('launch()', function () {
// //it('should abort if already launched.', function (done) {
// // this.timeout(0);
// // app.init({
// // mongoUrl: process.env.CLIO_MONGO_URL,
// // serverPort: 15925,
// // logLevel: app.LOG_LEVEL.NONE
// // });
// // app.launch(function (err) {
// // assert.equal(err, null);
// // app.launch(function (err) {
// // assert.notEqual(err, null);
// // app.shutdown(function () {
// // done();
// // });
// // });
// // });
// //});
// it('should exit if unable to connect to database', function (done) {
// app.init({
// mongoUrl : 'mongodb://nonexistentMongoUrl',
// serverPort: 15925,
// logLevel : app.LOG_LEVEL.NONE
// });
// app.launch(function (err) {
// assert.notEqual(err, null);
// done();
// });
// });
// });
// describe('getMongoUrl()', function () {
// before(function (done) {
// app.init({
// mongoUrl : process.env.CLIO_MONGO_URL,
// serverPort: process.env.CLIO_DATA_API_PORT || 15925,
// logLevel : app.LOG_LEVEL.FATAL
// });
// done();
// });
// it('should return provided database url', function (done) {
// assert.equal(app.getMongoUrl(), process.env.CLIO_MONGO_URL);
// done();
// });
// });
// });
//
// function dropClioCollections(callback) {
// mongoClient.connect(process.env.CLIO_MONGO_URL, function (err, connectedDb) {
// if (!err) {
// db = connectedDb;
// var dropCollection = function (nodeType, done) {
// db.dropCollection(nodeType.collectionName);
// done();
// }
// async.forEach(nodeTypes, dropCollection);
// callback();
// } else {
// callback(new Error('Database Connection Error'));
// }
// });
// }