tests/testVerifyToken.js   A
last analyzed

Complexity

Total Complexity 18
Complexity/F 1

Size

Lines of Code 87
Function Count 18

Duplication

Duplicated Lines 0
Ratio 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 0
c 1
b 0
f 0
nc 2
dl 0
loc 87
ccs 43
cts 43
cp 1
crap 0
rs 10
wmc 18
mnd 0
bc 18
fnc 18
bpm 1
cpm 1
noi 4

2 Functions

Rating   Name   Duplication   Size   Complexity  
A testVerifyToken.js ➔ describe(ꞌTest JWTꞌ) 0 23 1
B testVerifyToken.js ➔ describe(ꞌTest open and closed routesꞌ) 0 38 1
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) {
0 ignored issues
show
Unused Code introduced by
The parameter body is not used and could be removed.

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.

Loading history...
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) {
0 ignored issues
show
Unused Code introduced by
The parameter body is not used and could be removed.

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.

Loading history...
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;
0 ignored issues
show
Unused Code introduced by
The variable tokenBody seems to be never used. Consider removing it.
Loading history...
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) {
0 ignored issues
show
Unused Code introduced by
The parameter body is not used and could be removed.

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.

Loading history...
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