forked from GladysAssistant/Gladys
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Close GladysAssistant#34 : Add API routes to create, get, update and …
…delete scenes
- Loading branch information
1 parent
042ba53
commit 5b32337
Showing
5 changed files
with
154 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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), | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}); | ||
}); | ||
}); | ||
}); |