Skip to content

Commit

Permalink
Add services api type filter to get method
Browse files Browse the repository at this point in the history
  • Loading branch information
javierbrea committed Nov 18, 2018
1 parent 5338ac8 commit 7b12e3a
Show file tree
Hide file tree
Showing 6 changed files with 108 additions and 2 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
## [unreleased]
### Added
- Send entity operations events to all registered plugins
- Add type filter to get services api
### Changed
- Allow plugin users to create and get operator users
### Fixed
- Fix role-based permissions in get user api
### Removed

## [1.0.0-alpha.8] - 2018-11-13
Expand Down
9 changes: 8 additions & 1 deletion lib/api/services.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
'use strict'

const { omitBy, isUndefined } = require('lodash')

const definition = require('./services.json')
const { roles } = require('../security/utils')
const events = require('../events')
Expand All @@ -8,7 +10,12 @@ const EVENT_ENTITY = 'service'

const Operations = (service, commands) => ({
getServices: {
handler: (params, body, res) => commands.service.getFiltered()
handler: (params, body, res) => {
const filter = omitBy({
type: params.query.type
}, isUndefined)
return commands.service.getFiltered(filter)
}
},
getService: {
handler: (params, body, res) => commands.service.getById(params.path.id)
Expand Down
11 changes: 11 additions & 0 deletions lib/api/services.json
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,17 @@
"paths": {
"/services": {
"get": {
"parameters": [
{
"in": "query",
"name": "type",
"schema": {
"type": "string",
"enum": ["module", "plugin"]
},
"description": "Service type"
}
],
"tags": ["service"],
"summary": "Get services",
"description": "List services",
Expand Down
40 changes: 40 additions & 0 deletions test/functional/specs/services-api.specs.js
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,46 @@ test.describe('services api', function () {
})
})

test.describe('get services', () => {
test.it('should return all services', () => {
return getServices()
.then(getResponse => {
return Promise.all([
test.expect(getResponse.statusCode).to.equal(200),
test.expect(getResponse.body.length).to.equal(2)
])
})
})

test.it('should return plugin services when filtering by plugin type', () => {
return getServices({
type: 'plugin'
})
.then(getResponse => {
const service = getResponse.body.find(service => service.type !== 'plugin')
return Promise.all([
test.expect(getResponse.statusCode).to.equal(200),
test.expect(service).to.be.undefined(),
test.expect(getResponse.body.length).to.equal(1)
])
})
})

test.it('should return module services when filtering by module type', () => {
return getServices({
type: 'module'
})
.then(getResponse => {
const service = getResponse.body.find(service => service.type !== 'module')
return Promise.all([
test.expect(getResponse.statusCode).to.equal(200),
test.expect(service).to.be.undefined(),
test.expect(getResponse.body.length).to.equal(1)
])
})
})
})

test.describe('update service', () => {
let moduleUserService

Expand Down
29 changes: 29 additions & 0 deletions test/functional/specs/users-api.specs.js
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,35 @@ test.describe('users api', function () {
})
})
})

test.it('should return users filtering by name', () => {
return getUsers({
name: newUser.name
})
.then(getResponse => {
const user1 = getResponse.body.find(user => user.name === operatorUser.name)
const user2 = getResponse.body.find(user => user.name === newUser.name)
return Promise.all([
test.expect(user1).to.be.undefined(),
test.expect(user2.role).to.equal(newUser.role),
test.expect(user2.email).to.equal(newUser.email),
test.expect(user2.name).to.equal(newUser.name)
])
})
})

test.it('should return users filtering by role', () => {
return getUsers({
role: 'admin'
})
.then(getResponse => {
const noAdminUser = getResponse.body.find(user => user.role !== 'admin')
return Promise.all([
test.expect(getResponse.statusCode).to.equal(200),
test.expect(noAdminUser).to.be.undefined()
])
})
})
})

test.describe('get user', () => {
Expand Down
18 changes: 17 additions & 1 deletion test/unit/lib/api/services.specs.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,30 @@ test.describe('services api', () => {
const fooResult = 'foo result'
commandsMocks.stubs.service.getFiltered.resolves(fooResult)

return operations.getServices.handler()
return operations.getServices.handler({query: {}})
.then((result) => {
return Promise.all([
test.expect(result).to.equal(fooResult),
test.expect(commandsMocks.stubs.service.getFiltered).to.have.been.calledWith()
])
})
})

test.it('should pass received query data to get services command', () => {
const fooResult = 'foo result'
const fooQuery = {
type: 'foo-type'
}
commandsMocks.stubs.service.getFiltered.resolves(fooResult)

return operations.getServices.handler({query: fooQuery})
.then((result) => {
return Promise.all([
test.expect(result).to.equal(fooResult),
test.expect(commandsMocks.stubs.service.getFiltered).to.have.been.calledWith(fooQuery)
])
})
})
})

test.describe('getService handler', () => {
Expand Down

0 comments on commit 7b12e3a

Please sign in to comment.