Skip to content

Commit

Permalink
enhanced test and corrected sendNodeReq to be equivalent to sendReq (…
Browse files Browse the repository at this point in the history
…from @DavidBruant comments)
  • Loading branch information
vallettea committed Oct 26, 2015
1 parent d17127e commit 7f579f2
Show file tree
Hide file tree
Showing 9 changed files with 107 additions and 145 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ npm install
* Build container

```
docker-compose -f compose-init-db.yml build
docker-compose -f compose-init-db-dev.yml build
docker-compose -f compose-dev.yml build
```

Expand Down
23 changes: 11 additions & 12 deletions api/clients/Admin/src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,17 @@ var topLevelStore = {
sendCommand: sendCommand
};

var urlParams;
(window.onpopstate = function () {
var match,
pl = /\+/g, // Regex for replacing addition symbol with a space
search = /([^&=]+)=?([^&]*)/g,
decode = function (s) { return decodeURIComponent(s.replace(pl, " ")); },
query = window.location.search.substring(1);

urlParams = {};
while (match = search.exec(query))
urlParams[decode(match[1])] = decode(match[2]);
})();
// this is used to parse the url and get the secret tocken so admin can call the api
var match,
pl = /\+/g, // Regex for replacing addition symbol with a space
search = /([^&=]+)=?([^&]*)/g,
decode = function (s) { return decodeURIComponent(s.replace(pl, " ")); },
query = window.location.search.substring(1);

var urlParams = {};
while (match = search.exec(query))
urlParams[decode(match[1])] = decode(match[2]);


var api = prepareAPI(sendReq, '', urlParams.s);

Expand Down
2 changes: 1 addition & 1 deletion api/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ module.exports = function(app, debug){
res.status(200).send(data);
})
.catch(function(error){
res.status(500).send('Couldn\'t place measurements from database');
res.status(500).send('Couldn\'t get place measurements from database');
console.log('error in /measurements/place/' + placeId, error);
});
});
Expand Down
4 changes: 2 additions & 2 deletions compose-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ api:
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=elements
- PGPASSWORD=elements
- NODE_ENV=production
- NODE_ENV=test
volumes:
- ./:/pheromon
log_driver: "none"
Expand All @@ -25,7 +25,7 @@ broker:
- "5100:5100"
environment:
- VIRTUAL_PORT=5100
- NODE_ENV=production
- NODE_ENV=test
- POSTGRES_PASSWORD=elements
volumes:
- ./:/pheromon
Expand Down
4 changes: 2 additions & 2 deletions tests/mocha/api/complexQueries.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ var io = require('socket.io-client');
var request = require('request');

var database = require('../../../database');
var sendReq = require('../../../tools/sendNodeReq');
var makeMap = require('../../../tools/makeMap');
var sendReq = require('../../../tools/sendNodeReq.js');
var makeMap = require('../../../tools/makeMap.js');

var prepareAPI = require('../../../tools/prepareAPI.js');
var apiOrigin = 'http://api:4000';
Expand Down
111 changes: 8 additions & 103 deletions tests/mocha/api/security.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,30 +36,13 @@ describe('Verify API refuses unauthorized operation with no token', function() {
lon: -0.570468
};

return api.createPlace(place)
.then(function(res){
expect(res.message).to.deep.equal('No token provided.');
});
return expect(api.createPlace(place)).to.be.rejectedWith('HTTP error');

This comment has been minimized.

Copy link
@vallettea

vallettea Oct 26, 2015

Author Owner

I wanted to use

expect(api.deletePlace(id)).to.be.rejectedWith('HTTP error').and.be.an.instanceOf(Error).and.have.property("HTTPstatus", 403);

but it seems there is a bug in chai as promised since this doesn't work see chaijs/chai-as-promised#47

});
});

describe('Update', function(){
var id;

before('Creating place to be updated', function(){

var place = {
name: 'Place1',
lat: 44.840450,
lon: -0.570468
};

return api.createPlace(place)
.then(function(res){
expect(res.message).to.deep.equal('No token provided.');
});
});

it('/update/place', function () {
this.timeout(2000);

Expand All @@ -74,67 +57,24 @@ describe('Verify API refuses unauthorized operation with no token', function() {
delta: delta
};

return api.updatePlace(updateData)
.then(function(res){
expect(res.message).to.deep.equal('No token provided.');
});
return expect(api.updatePlace(updateData)).to.be.rejectedWith('HTTP error');
});

});

describe('Deletion', function(){
var id;

before('Creating place to be deleted', function(){

var place = {
name: 'Place1',
lat: 44.840450,
lon: -0.570468
};

return api.createPlace(place)
.then(function(res){
expect(res.message).to.deep.equal('No token provided.');
});
});

it('/place/delete', function () {

return api.deletePlace(id)
.then(function(res){
expect(res.message).to.deep.equal('No token provided.');
});
return expect(api.deletePlace(id)).to.be.rejectedWith('HTTP error');
});

});

describe('Delete All Places', function(){

before('Creating places to be deleted', function(){

var creationPs = [0, 1, 2].map(function(item){

var place = {
name: 'Place' + item,
lat: Math.random(),
lon: Math.random()
};

return api.createPlace(place);
});

return Promise.all(creationPs);

});

it('/place/deleteAll', function () {

return api.deleteAllPlaces()
.then(function(res){
expect(res.message).to.deep.equal('No token provided.');
});

return expect(api.deleteAllPlaces()).to.be.rejectedWith('HTTP error');
});

});
Expand All @@ -152,10 +92,7 @@ describe('Verify API refuses unauthorized operation with no token', function() {
outputs: ['type1', 'type2']
};

return api.createSensor(sensor)
.then(function(res){
expect(res.message).to.deep.equal('No token provided.');
});
return expect(api.createSensor(sensor)).to.be.rejectedWith('HTTP error');

});
});
Expand All @@ -166,9 +103,6 @@ describe('Verify API refuses unauthorized operation with no token', function() {
sim: '290'
};

before('Creating sensor to be updated', function(){
return api.createSensor(sensor);
});

it('/sensor/update', function () {

Expand All @@ -181,10 +115,7 @@ describe('Verify API refuses unauthorized operation with no token', function() {
delta: delta
};

return api.updateSensor(updateData)
.then(function(res){
expect(res.message).to.deep.equal('No token provided.');
});
return expect(api.updateSensor(updateData)).to.be.rejectedWith('HTTP error');

});

Expand All @@ -196,48 +127,22 @@ describe('Verify API refuses unauthorized operation with no token', function() {
sim: '290'
};

before('Creating sensor to be deleted', function(){
return api.createSensor(sensor)
});

it('/sensor/delete', function () {

var deleteData = {
sim: sensor.sim
};

return api.deleteSensor(deleteData.sim)
.then(function(res){
expect(res.message).to.deep.equal('No token provided.');
});
return expect(api.deleteSensor(deleteData.sim)).to.be.rejectedWith('HTTP error');

});
});

describe('Delete All Sensors', function(){

before('Creating sensors to be deleted', function(){

var creationPs = [0, 1, 2].map(function(item){

var sensor = {
name: 'Sensor' + item,
sim: 'sim' + item * 10
};

return api.createSensor(sensor);
});

return Promise.all(creationPs);

});

it('/sensor/deleteAll', function () {

return api.deleteAllSensors()
.then(function(res){
expect(res.message).to.deep.equal('No token provided.');
});
return expect(api.deleteAllSensors()).to.be.rejectedWith('HTTP error');

});
});
Expand Down
6 changes: 0 additions & 6 deletions tests/mocha/maestro.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,11 @@ describe('Maestro testing', function(){

// before all tests, clear the table
before('Clearing Sensor table', function(){
console.log('Clearing Sensor table');
return database.Sensors.deleteAll();
});

// after all tests, clear the table
after('Clearing Sensor table', function(){
console.log('Clearing Sensor table');

return database.Measurements.deleteAll()
.then(function(){
Expand All @@ -66,7 +64,6 @@ describe('Maestro testing', function(){
describe('Maestro utils', function(){
// after each test, clear the table
afterEach('Clearing Sensor Table', function(){
console.log('After: Clearing Sensor table');
return database.Sensors.deleteAll();
});

Expand Down Expand Up @@ -128,7 +125,6 @@ describe('Maestro testing', function(){
beforeEach('Creating Fake Sensor', function(){
i++;
simId = 'simNumber' + i;
console.log('Before: creating fake sensor');
return createFakeSensor(simId)
.then(function(sensor){
fakeSensor = sensor;
Expand Down Expand Up @@ -211,7 +207,6 @@ describe('Maestro testing', function(){

return sigCodec.encode(measurement)
.then(function(encoded){
console.log('SENT', encoded);
fakeSensor.publish('measurement/' + simId + '/wifi', encoded);

var data = {
Expand All @@ -224,7 +219,6 @@ describe('Maestro testing', function(){

resolve(api.getMeasurements(data)
.then(function(measurements){
console.log('measurements', measurements);
expect(measurements[0].value[0]).to.deep.equal(-39); // signal strengths are sorted when encoded.
expect(measurements[0].entry).to.equal(3);
expect(Date.parse(measurements[0].date)).to.be.a('number');
Expand Down
Loading

0 comments on commit 7f579f2

Please sign in to comment.