Skip to content

Commit

Permalink
GET, POST, PATCH, DELETE /machine
Browse files Browse the repository at this point in the history
  • Loading branch information
Pierre-Gilles committed Sep 24, 2016
1 parent cf7b9d2 commit 23bbda2
Show file tree
Hide file tree
Showing 9 changed files with 170 additions and 3 deletions.
44 changes: 44 additions & 0 deletions api/controllers/MachineController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/**
* Gladys Project
* http://gladysproject.com
* Software under licence Creative Commons 3.0 France
* http://creativecommons.org/licenses/by-nc-sa/3.0/fr/
* You may not use this software for commercial purposes.
* @author :: Pierre-Gilles Leymarie
*/

/**
* LockController
*
* @description :: Server-side logic for managing locks
* @help :: See http://links.sailsjs.org/docs/controllers
*/

module.exports = {

create: function(req, res, next){
gladys.machine.create(req.body)
.then((machine) => res.json(machine))
.catch(next);
},

get: function(req, res, next){
gladys.machine.get()
.then((machines) => res.json(machines))
.catch(next);
},

update: function(req, res, next){
req.body.id = req.params.id;
gladys.machine.update(req.body)
.then((machine) => res.json(machine))
.catch(next);
},

delete: function(req, res, next){
gladys.machine.delete({id: req.params.id})
.then(() => res.json({success: true}))
.catch(next);
}

};
7 changes: 6 additions & 1 deletion api/models/Machine.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,13 @@ module.exports = {
model: 'House'
},

ip: {
host: {
type: 'string'
},

me: {
type: 'boolean',
defaultsTo: false
}

}
Expand Down
6 changes: 6 additions & 0 deletions config/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,12 @@ module.exports.routes = {
'post /location': 'LocationController.create',
'get /location/create': 'LocationController.create',

// Machine
'get /machine' : 'MachineController.get',
'post /machine': 'MachineController.create',
'patch /machine/:id': 'MachineController.update',
'delete /machine/:id': 'MachineController.delete',

// Mode
'get /mode': 'ModeController.index',
'post /mode': 'ModeController.create',
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/machine.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"id": 1,
"name": "Raspberry Pi kitchen",
"uuid": "72844455-8634-4131-9875-612b47ad4397",
"ip": "192.168.1.100",
"host": "http://192.168.1.100",
"house": 1
}
]
32 changes: 32 additions & 0 deletions test/unit/api/controllers/Machine/machine.create.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
var request = require('supertest');
var validateMachine = require('../../validator/machineValidator.js');

describe('MachineController', function() {

describe('create', function() {

it('should create a machine', function (done) {

var machine = {
name: 'trest',
host: 'http://192.168.1.100',
house: 1
};

request(sails.hooks.http.app)
.post('/machine?token=test')
.send(machine)
.expect(200)
.end(function(err, res) {
if(err) return done(err);

validateMachine(res.body);
done();
});

});

});


});
24 changes: 24 additions & 0 deletions test/unit/api/controllers/Machine/machine.delete.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
var request = require('supertest');
var validateMachine = require('../../validator/machineValidator.js');

describe('MachineController', function() {

describe('delete', function() {

it('should delete a machine', function (done) {

request(sails.hooks.http.app)
.delete('/machine/1?token=test')
.expect(200)
.end(function(err, res) {
if(err) return done(err);

done();
});

});

});


});
25 changes: 25 additions & 0 deletions test/unit/api/controllers/Machine/machine.get.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
var request = require('supertest');
var validateMachine = require('../../validator/machineValidator.js');

describe('MachineController', function() {

describe('get', function() {

it('should get all machines', function (done) {

request(sails.hooks.http.app)
.get('/machine?token=test')
.expect(200)
.end(function(err, res) {
if(err) return done(err);

validateMachine(res.body);
done();
});

});

});


});
31 changes: 31 additions & 0 deletions test/unit/api/controllers/Machine/machine.update.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
var request = require('supertest');
var validateMachine = require('../../validator/machineValidator.js');

describe('MachineController', function() {

describe('update', function() {

it('should update a machine', function (done) {

var machine = {
name: 'newName'
};

request(sails.hooks.http.app)
.patch('/machine/1?token=test')
.send(machine)
.expect(200)
.end(function(err, res) {
if(err) return done(err);

validateMachine(res.body);
res.body.name.should.equal(machine.name);
done();
});

});

});


});
2 changes: 1 addition & 1 deletion test/unit/api/validator/machineValidator.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@ function validate(machine) {
function validateMachine(machine) {
machine.should.be.instanceOf(Object);
machine.should.have.property('uuid');
machine.should.have.property('ip');
machine.should.have.property('host');
}

0 comments on commit 23bbda2

Please sign in to comment.