Skip to content

Commit

Permalink
Close GladysAssistant#34 : Add API routes to create, get, update and …
Browse files Browse the repository at this point in the history
…delete scenes
  • Loading branch information
Pierre-Gilles committed Apr 9, 2019
1 parent 042ba53 commit 5b32337
Show file tree
Hide file tree
Showing 5 changed files with 154 additions and 4 deletions.
2 changes: 1 addition & 1 deletion server/api/controllers/room.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ module.exports = function RoomController(gladys) {
*
*/
async function destroy(req, res) {
await gladys.room.destroy(req.params.room_selector, req.body);
await gladys.room.destroy(req.params.room_selector);
res.json({
success: true,
});
Expand Down
57 changes: 57 additions & 0 deletions server/api/controllers/scene.controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
const asyncMiddleware = require('../middlewares/asyncMiddleware');


module.exports = function SceneController(gladys) {
/**
* @api {post} /api/v1/scene create
* @apiName create
* @apiGroup Scene
*
*/
async function create(req, res) {
const newScene = await gladys.scene.create(req.body);
res.status(201).json(newScene);
}

/**
* @api {patch} /api/v1/scene/:scene_selector update
* @apiName update
* @apiGroup Scene
*
*/
async function update(req, res) {
const newScene = await gladys.scene.update(req.params.scene_selector, req.body);
res.json(newScene);
}

/**
* @api {get} /api/v1/scene get
* @apiName get
* @apiGroup Scene
*
*/
async function get(req, res) {
const scenes = await gladys.scene.get(req.query);
res.json(scenes);
}

/**
* @api {delete} /api/v1/scene/:scene_selector delete
* @apiName delete
* @apiGroup Scene
*
*/
async function destroy(req, res) {
await gladys.scene.destroy(req.params.scene_selector);
res.json({
success: true,
});
}

return Object.freeze({
create: asyncMiddleware(create),
destroy: asyncMiddleware(destroy),
get: asyncMiddleware(get),
update: asyncMiddleware(update),
});
};
8 changes: 8 additions & 0 deletions server/api/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const MessageController = require('./controllers/message.controller');
const RoomController = require('./controllers/room.controller');
const SessionController = require('./controllers/session.controller');
const ServiceController = require('./controllers/service.controller');
const SceneController = require('./controllers/scene.controller');
const VariableController = require('./controllers/variable.controller');
const WeatherController = require('./controllers/weather.controller');
const AuthMiddleware = require('./middlewares/authMiddleware');
Expand All @@ -34,6 +35,7 @@ function setupRoutes(gladys) {
const variableController = VariableController(gladys);
const sessionController = SessionController(gladys);
const serviceController = ServiceController(gladys);
const sceneController = SceneController(gladys);
const weatherController = WeatherController(gladys);
const authMiddleware = AuthMiddleware(gladys.config.jwtSecret, 'dashboard:write', gladys.cache, gladys.user);
const resetPasswordAuthMiddleware = AuthMiddleware(gladys.config.jwtSecret, 'reset-password:write', gladys.cache, gladys.user);
Expand Down Expand Up @@ -103,6 +105,12 @@ function setupRoutes(gladys) {
// light
router.post('/api/v1/light/:device_selector/on', lightController.turnOn);

// scene
router.post('/api/v1/scene', sceneController.create);
router.get('/api/v1/scene', sceneController.get);
router.patch('/api/v1/scene/:scene_selector', sceneController.update);
router.delete('/api/v1/scene/:scene_selector', sceneController.destroy);

// weather
router.get('/api/v1/user/:user_selector/weather', weatherController.getByUser);
router.get('/api/v1/house/:house_selector/weather', weatherController.getByHouse);
Expand Down
7 changes: 4 additions & 3 deletions server/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const User = require('./user');
const Device = require('./device');
const Room = require('./room');
const StateManager = require('./state');
const SceneManager = require('./scene');
const Scene = require('./scene');
const TriggerManager = require('./trigger');
const Variable = require('./variable');
const services = require('../services');
Expand Down Expand Up @@ -45,8 +45,8 @@ function Gladys(config = {}) {
const session = new Session(config.jwtSecret, cache);
const user = new User(session);
const device = new Device(event, message, stateManager, service);
const sceneManager = new SceneManager(stateManager);
const triggerManager = new TriggerManager(event, stateManager, sceneManager);
const scene = new Scene(stateManager);
const triggerManager = new TriggerManager(event, stateManager, scene);
const variable = new Variable();
const weather = new Weather(service);

Expand All @@ -59,6 +59,7 @@ function Gladys(config = {}) {
message,
user,
service,
scene,
session,
cache,
config,
Expand Down
84 changes: 84 additions & 0 deletions server/test/controllers/scene/scene.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
const { expect } = require('chai');
const { authenticatedRequest } = require('../request.test');
const { ACTIONS } = require('../../../utils/constants');

describe('POST /api/v1/scene', () => {
it('should create scene', async () => {
await authenticatedRequest
.post('/api/v1/scene')
.send({
name: 'New Scene',
actions: [{
type: ACTIONS.HOUSE_ALARM.ARM,
}],
})
.expect('Content-Type', /json/)
.expect(201)
.then((res) => {
expect(res.body).to.have.property('name', 'New Scene');
expect(res.body).to.have.property('selector', 'new-scene');
});
});
});

describe('GET /api/v1/scene', () => {
it('should get scene', async () => {
await authenticatedRequest
.get('/api/v1/scene')
.query({
search: 'Test scene',
})
.expect('Content-Type', /json/)
.expect(200)
.then((res) => {
expect(res.body).to.deep.equal([{
id: '3a30636c-b3f0-4251-a347-90787f0fe940',
name: 'Test scene',
selector: 'test-scene',
last_executed: null,
updated_at: '2019-02-12T07:49:07.556Z',
}]);
});
});
it('should return 0 scenes', async () => {
await authenticatedRequest
.get('/api/v1/scene')
.query({
search: 'UNKNOWN SCENE',
})
.expect('Content-Type', /json/)
.expect(200)
.then((res) => {
expect(res.body).to.deep.equal([]);
});
});
});

describe('PATCH /api/v1/scene/:scene_selector', () => {
it('should update scene', async () => {
await authenticatedRequest
.patch('/api/v1/scene/test-scene')
.send({
name: 'New name',
})
.expect('Content-Type', /json/)
.expect(200)
.then((res) => {
expect(res.body).to.have.property('name', 'New name');
});
});
});

describe('DELETE /api/v1/scene/:scene_selector', () => {
it('should delete scene', async () => {
await authenticatedRequest
.delete('/api/v1/scene/test-scene')
.expect('Content-Type', /json/)
.expect(200)
.then((res) => {
expect(res.body).to.deep.equal({
success: true,
});
});
});
});

0 comments on commit 5b32337

Please sign in to comment.