Skip to content

Commit af4a34e

Browse files
committed
test stub for controller
1 parent 46c492c commit af4a34e

File tree

4 files changed

+117
-29
lines changed

4 files changed

+117
-29
lines changed

‎package.json‎

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
},
1313
"scripts": {
1414
"//": "Gulp installs css and javascript",
15-
"test": "echo \"TODO: Write some tests...\"",
15+
"test": "mocha test/unit/*.js",
1616
"precommit": "standard && npm run test",
1717
"docker": "npm install --development && npm run build && npm prune --production",
1818
"build": "NODE_ENV=production gulp build",
@@ -32,26 +32,21 @@
3232
"jquery": "^3.1.0",
3333
"knockout": "^3.4.0",
3434
"knockout.viewmodel": "https://github.com/KTH/knockout.viewmodel.git#v2.0.3",
35-
"kth-client-ajax": "https://github.com/KTH/kth-client-ajax.git#v1.0.2",
3635
"kth-client-logging": "https://github.com/KTH/kth-client-logging.git#v1.0.0",
37-
"kth-node-access-log": "https://github.com/KTH/kth-node-access-log.git#v1.0.0",
36+
"kth-node-access-log": "^0.2.0",
3837
"kth-node-api-call": "^1.2.2",
39-
"kth-node-cache": "https://github.com/KTH/kth-node-cache.git#v1.0.0",
4038
"kth-node-configuration": "^1.4.0",
41-
"kth-node-cortina-block": "^1.0.4",
4239
"kth-node-express-routing": "^0.3.1",
4340
"kth-node-i18n": "^1.0.2",
4441
"kth-node-ldap": "^2.0.1",
4542
"kth-node-log": "^2.0.3",
4643
"kth-node-monitor": "^0.1.5",
4744
"kth-node-passport-cas": "^2.0.0",
4845
"kth-node-redis": "^1.2.0",
49-
"kth-node-response": "https://github.com/KTH/kth-node-response.git#v1.0.0",
5046
"kth-node-server": "^3.0.3",
5147
"kth-node-session": "^1.0.0",
5248
"kth-node-web-common": "^0.1.1",
5349
"kth-style": "^1.2.12",
54-
"ldapjs": "^1.0.0",
5550
"locale": "^0.1.0",
5651
"passport": "^0.3.2",
5752
"safe-utils": "^0.1.1"
@@ -61,13 +56,17 @@
6156
"babel-loader": "^6.2.4",
6257
"babel-preset-es2015": "^6.9.0",
6358
"browser-sync": "^2.12.8",
59+
"chai": "^4.0.2",
6460
"concurrently": "^2.1.0",
6561
"cross-env": "^2.0.0",
6662
"gulp": "^3.9.1",
6763
"husky": "^0.14.0",
6864
"kth-node-build-commons": "^3.0.0",
6965
"merge-stream": "^1.0.1",
7066
"mocha": "3.2.0",
67+
"mockery": "^2.0.0",
68+
"nock": "^9.0.13",
69+
"node-mocks-http": "^1.6.3",
7170
"nodemon": "^1.11.0",
7271
"standard": "^7.1.1",
7372
"zombie": "5.0.5"

‎server/controllers/sampleCtrl.js‎

Lines changed: 4 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -11,35 +11,17 @@ module.exports = {
1111

1212
function * getIndex (req, res, next) {
1313
try {
14-
let resp = {}
15-
// safeGet returns the value after the return keyword if it exists, otherwise a default value
16-
let apiUp = safeGet(() => { return api.sampleApi.connected }, false) // is our api up?
17-
18-
if (apiUp) {
19-
const cachedApi = getCachedApi()
20-
const client = cachedApi.client
21-
const paths = cachedApi.paths
22-
resp = yield client.getAsync(client.resolve(paths.getDataById.uri, { id: '123' }))
23-
}
14+
const client = api.nodeApi.client
15+
const paths = api.nodeApi.paths
16+
const resp = yield client.getAsync(client.resolve(paths.getDataById.uri, { id: '123' }), { useCache: true })
2417

2518
res.render('sample/index', {
2619
debug: 'debug' in req.query,
2720
data: resp.statusCode === 200 ? safeGet(() => { return resp.body.name }) : '',
2821
error: resp.statusCode !== 200 ? safeGet(() => { return resp.body.message }) : ''
2922
})
3023
} catch (err) {
24+
log.error('Error in getIndex', { error: err })
3125
next(err)
3226
}
3327
}
34-
35-
/*
36-
* Get cache api, if not configured, use default api to avoid problems
37-
*/
38-
function getCachedApi () {
39-
if (api.sampleApiCached === undefined) {
40-
log.info('No cached api configured, using deafult api')
41-
return api.sampleApi
42-
}
43-
44-
return api.sampleApiCached
45-
}

‎test/mocks/apipaths.json‎

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
{
2+
"system": {
3+
"monitor": {
4+
"uri": "/api/node/_monitor",
5+
"method": "get"
6+
},
7+
"about": {
8+
"uri": "/api/node/_about",
9+
"method": "get"
10+
},
11+
"paths": {
12+
"uri": "/api/node/_paths",
13+
"method": "get"
14+
},
15+
"robots": {
16+
"uri": "/robots.txt",
17+
"method": "get"
18+
},
19+
"swagger": {
20+
"uri": "/api/node/swagger.json",
21+
"method": "get"
22+
}
23+
},
24+
"api": {
25+
"checkAPIkey": {
26+
"uri": "/api/node/_checkAPIkey",
27+
"method": "GET",
28+
"apikey": {
29+
"scope_required": true,
30+
"scopes": [
31+
"read"
32+
],
33+
"type": "api_key"
34+
}
35+
},
36+
"getDataById": {
37+
"uri": "/api/node/v1/data/:id",
38+
"method": "GET",
39+
"apikey": {
40+
"scope_required": true,
41+
"scopes": [
42+
"read"
43+
],
44+
"type": "api_key"
45+
}
46+
},
47+
"postDataById": {
48+
"uri": "/api/node/v1/data/:id",
49+
"method": "POST",
50+
"apikey": {
51+
"scope_required": true,
52+
"scopes": [
53+
"write"
54+
],
55+
"type": "api_key"
56+
}
57+
}
58+
}
59+
}

‎test/unit/test-sampleCtrl.js‎

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/* eslint-env mocha */
2+
/* eslint-disable no-unused-expressions */
3+
4+
process.env['LDAP_URI'] = 'ldaps://mockuser@mockdomain.com@mockldapdomain.com'
5+
process.env['LDAP_PASSWORD'] = 'mockldappassword'
6+
const expect = require('chai').expect
7+
const nock = require('nock')
8+
const mockery = require('mockery')
9+
const httpMocks = require('node-mocks-http')
10+
11+
const mockLogger = {}
12+
mockLogger.debug = mockLogger.info = mockLogger.error = mockLogger.warn = console.log
13+
mockLogger.init = () => {}
14+
15+
mockery.registerMock('kth-node-log', mockLogger)
16+
mockery.enable({
17+
warnOnReplace: false,
18+
warnOnUnregistered: false
19+
})
20+
21+
const paths = require('../mocks/apipaths.json')
22+
const api = nock('http://localhost:3001/api/node')
23+
.get('/_paths')
24+
.reply(200, paths)
25+
.get('/_checkAPIkey')
26+
.reply(200, {})
27+
28+
describe('Index page', function () {
29+
before((done) => {
30+
require('../../server/api')
31+
setTimeout(() => {
32+
done()
33+
}, 500)
34+
})
35+
it('should get the index page', done => {
36+
api.get('/v1/data/123').reply(200, {
37+
id: '123',
38+
name: 'asdasd'
39+
})
40+
const ctrl = require('../../server/controllers/sampleCtrl')
41+
const { req, res } = httpMocks.createMocks()
42+
res.render = function (view, data) {
43+
expect(data).to.be.not.undefined
44+
done()
45+
}
46+
ctrl.getIndex(req, res, console.log)
47+
})
48+
})

0 commit comments

Comments
 (0)