Skip to content

Commit ae5bc1d

Browse files
Tom KirkpatrickTom Kirkpatrick
authored andcommitted
test: add test cases for invocation via REST
1 parent 5ef2074 commit ae5bc1d

File tree

6 files changed

+78
-4
lines changed

6 files changed

+78
-4
lines changed

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
"dev": "nodemon test/fullcube-state-machine/server/server.js --ignore db.json --ext js,json",
3333
"lint": "eslint .",
3434
"pretest": "npm run lint",
35-
"test": "nyc --reporter=lcov --reporter=text --reporter=text-summary mocha test/*test.js",
35+
"test": "nyc --reporter=lcov --reporter=text --reporter=text-summary mocha test/*test.js",
3636
"test:watch": "npm run test -- -w",
3737
"coverage": "nyc report --reporter=text-lcov | coveralls",
3838
"semantic-release": "semantic-release pre && npm publish && semantic-release post"
@@ -53,7 +53,8 @@
5353
"loopback-boot": "2.25.0",
5454
"loopback-component-explorer": "4.2.0",
5555
"loopback-component-fixtures": "1.1.0",
56-
"loopback-datasource-juggler": "3.9.2",
56+
"loopback-context": "1.0.0",
57+
"loopback-testing": "1.4.0",
5758
"mocha": "3.4.2",
5859
"nodemon": "1.11.0",
5960
"nyc": "10.3.2",

test/fullcube-state-machine/common/models/order.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,10 @@ module.exports = function OrderModel(Order) {
3939
// Disable
4040
Order.observe('fsm:ondisable', ctx => {
4141
log.info(`Disabling order ${ctx.instance.id}`)
42-
return Promise.reject(new Error('not implemented'))
42+
const error = new Error('Disable method is not yet allowed')
43+
44+
error.statusCode = 405
45+
return Promise.reject(error)
4346
})
4447
Order.observe('fsm:onentereddisabled', ctx => {
4548
log.info(`Sucessfully disabled order ${ctx.instance.id}`)

test/fullcube-state-machine/common/models/order.json

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,19 @@
6767
"verb": "put"
6868
}
6969
]
70+
},
71+
"prototype.disable": {
72+
"returns": {
73+
"arg": "order",
74+
"type": "Order",
75+
"root": true,
76+
"description": "The updated order"
77+
},
78+
"description": "Disable a order",
79+
"http": [ {
80+
"path": "/disable",
81+
"verb": "put"
82+
} ]
7083
}
7184
},
7285
"mixins": {

test/fullcube-state-machine/server/middleware.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@
33
"loopback#favicon": {}
44
},
55
"initial": {
6+
"loopback-context#per-request": {
7+
"params": {
8+
"enableHttpContext": true
9+
}
10+
},
611
"compression": {},
712
"cors": {
813
"params": {

test/integration.test.js

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
'use strict'
2+
3+
const path = require('path')
4+
const chai = require('chai')
5+
const lt = require('loopback-testing')
6+
const request = require('supertest')
7+
8+
chai.use(require('dirty-chai'))
9+
chai.use(require('sinon-chai'))
10+
11+
const { expect } = chai
12+
13+
const TEST_APP = path.join(__dirname, 'fullcube-state-machine')
14+
const app = require(path.join(TEST_APP, 'server/server.js'))
15+
16+
chai.use(require('dirty-chai'))
17+
18+
// Helper function to make api requests.
19+
function json(verb, reqUrl) {
20+
return request(app)[verb](reqUrl)
21+
.set('Content-Type', 'application/json')
22+
.set('Accept', 'application/json')
23+
}
24+
25+
describe('REST', function() {
26+
27+
lt.beforeEach.withApp(app)
28+
29+
describe('Basic handling', function() {
30+
lt.beforeEach.givenModel('Order', {}, 'order')
31+
it('Should return the updated order', function() {
32+
return json('put', `/api/orders/${this.order.id}/cancel`)
33+
.send()
34+
.expect(200)
35+
.then(res => expect(res.body.status).to.equal('canceled'))
36+
})
37+
})
38+
39+
describe('Error handling', function() {
40+
lt.beforeEach.givenModel('Order', {}, 'order')
41+
it('Should return the rejected error', function() {
42+
return json('put', `/api/orders/${this.order.id}/disable`)
43+
.send()
44+
.expect(405)
45+
.then(res => {
46+
expect(res.error).to.have.property('message')
47+
expect(res.body.error).to.have.property('message', 'Disable method is not yet allowed')
48+
})
49+
})
50+
})
51+
52+
})

test/test.js renamed to test/unit.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -350,7 +350,7 @@ describe('Force status update', function() {
350350
return this.order.disable()
351351
.then(() => new Error('should not have completed event'))
352352
.catch(err => {
353-
expect(err).to.have.property('message', 'not implemented')
353+
expect(err).to.have.property('message', 'Disable method is not yet allowed')
354354
return this.order.reload().then(order => expect(order).to.have.property('status', 'prepared'))
355355
})
356356
.finally(() => expect(this.disabledSpy).to.not.have.been.called())

0 commit comments

Comments
 (0)