1
|
|
|
/** |
2
|
|
|
* Tests for middleware |
3
|
|
|
*/ |
4
|
1 |
|
var expect = require('chai').expect; |
5
|
1 |
|
var request = require('request'); |
6
|
1 |
|
var app = require('./server'); |
7
|
|
|
|
8
|
|
|
var server; |
9
|
1 |
|
const url = 'http://localhost:' + app.get('port') |
10
|
|
|
|
11
|
2 |
|
app.set('port', process.env.DBWEBB_PORT || 3052); |
12
|
|
|
|
13
|
1 |
|
var errorNoTokenProvided = JSON.stringify({ |
14
|
|
|
"success": false, |
15
|
|
|
"status": 403, |
16
|
|
|
"title": "NoTokenProvided", |
17
|
|
|
"description": "Forbidden, missing token." |
18
|
|
|
}); |
19
|
|
|
|
20
|
1 |
|
var errorJsonWebTokenError = JSON.stringify({ |
21
|
|
|
"success": false, |
22
|
|
|
"status": 403, |
23
|
|
|
"title": "JsonWebTokenError", |
24
|
|
|
"description": "Forbidden. invalid token" |
25
|
|
|
}); |
26
|
|
|
|
27
|
1 |
|
describe('Test open and closed routes', function () { |
28
|
1 |
|
before(function () { |
29
|
1 |
|
server = app.listen(app.get('port')); |
30
|
|
|
}); |
31
|
|
|
|
32
|
1 |
|
it('/ should return status 200', function (done) { |
33
|
1 |
|
request.get(url, function (err, res, body) { |
|
|
|
|
34
|
1 |
|
expect(res.statusCode).to.equal(200); |
35
|
1 |
|
done(); |
36
|
|
|
}); |
37
|
|
|
}); |
38
|
1 |
|
it('/gettoken should return status 200', function (done) { |
39
|
1 |
|
request.get(url + '/gettoken', function (err, res, body) { |
|
|
|
|
40
|
1 |
|
expect(res.statusCode).to.equal(200); |
41
|
1 |
|
done(); |
42
|
|
|
}); |
43
|
|
|
}); |
44
|
1 |
|
it('/test should return status 403 and NoToken-error', function (done) { |
45
|
1 |
|
request.get(url + '/test', function (err, res, body) { |
46
|
1 |
|
expect(res.statusCode).to.equal(403); |
47
|
1 |
|
expect(res.body).to.deep.equal(errorNoTokenProvided); |
48
|
1 |
|
done(); |
49
|
|
|
}); |
50
|
|
|
}); |
51
|
|
|
|
52
|
1 |
|
it('/test?token=1.1.1 should return status 403 and JsonWebTokenError', function (done) { |
53
|
1 |
|
request.get(url + '/test?token=1.1.1', function (err, res, body) { |
54
|
1 |
|
expect(res.statusCode).to.equal(403); |
55
|
1 |
|
expect(res.body).to.deep.equal(errorJsonWebTokenError); |
56
|
1 |
|
done(); |
57
|
|
|
}); |
58
|
|
|
}); |
59
|
|
|
|
60
|
|
|
|
61
|
1 |
|
after(function () { |
62
|
1 |
|
server.close(); |
63
|
|
|
}); |
64
|
|
|
}); |
65
|
|
|
|
66
|
|
|
var tokenBody; |
|
|
|
|
67
|
|
|
|
68
|
1 |
|
describe('Test JWT', function () { |
69
|
1 |
|
before(function () { |
70
|
1 |
|
server = app.listen(app.get('port')); |
71
|
|
|
}); |
72
|
|
|
|
73
|
1 |
|
it('expect statuscode 200 when using the created token', function (done) { |
74
|
1 |
|
request.get(url + '/gettoken', function (err, res, body) { |
75
|
1 |
|
request.get(url + '/test?token=1.1.1', function (err, res, body) { |
|
|
|
|
76
|
1 |
|
expect(res.statusCode).to.equal(403); |
77
|
|
|
}); |
78
|
|
|
|
79
|
1 |
|
request.get(url + '/test?token=' + JSON.parse(res.body).token, function (err, res, body) { |
80
|
1 |
|
expect(res.statusCode).to.equal(200); |
81
|
1 |
|
expect(JSON.parse(res.body).username).to.equal('doe'); |
82
|
1 |
|
done(); |
83
|
|
|
}); |
84
|
|
|
}); |
85
|
|
|
}); |
86
|
|
|
|
87
|
1 |
|
after(function () { |
88
|
1 |
|
server.close(); |
89
|
|
|
}); |
90
|
|
|
}); |
91
|
|
|
|
This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.