-
Notifications
You must be signed in to change notification settings - Fork 34
/
api.js
72 lines (58 loc) · 1.78 KB
/
api.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
'use strict';
const Hapi = require('hapi');
const jwtPlugin = require('hapi-auth-jwt2');
const authPlugin = require('../auth');
const apiPlugin = require('../api');
const Code = require('code');
const Lab = require('lab');
const lab = exports.lab = Lab.script();
const describe = lab.describe;
const it = lab.it;
const before = lab.before;
const expect = Code.expect;
describe('API', () => {
let server;
before((done) => {
const plugins = [jwtPlugin, authPlugin, apiPlugin];
server = new Hapi.Server();
server.connection({ port: 8000 });
server.register(plugins, (err) => {
if (err) {
return done(err);
}
server.initialize(done);
});
});
it('Known route should return http status 200', done => {
server.inject('/', response => {
expect(response.statusCode).to.equal(200);
done();
});
});
it('Restricted route should return http status 401 for anonymous user', done => {
server.inject('/restricted', response => {
expect(response.statusCode).to.equal(401);
done();
});
});
it('Restricted route should return http status 200 for authenticated user', done => {
var options = {
method: 'GET',
url: '/restricted',
headers: {
'Authorization': 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpZCI6MSwibmFtZSI6IkFudGhvbnkgVmFsaWQgVXNlciIsImlhdCI6MTQyNTQ3MzUzNX0.KA68l60mjiC8EXaC2odnjFwdIDxE__iDu5RwLdN1F2A',
'Content-Type': 'application/json; charset=utf-8'
}
};
server.inject(options, response => {
expect(response.statusCode).to.equal(200);
done();
});
});
it('Unknown route should return http status 404', done => {
server.inject('/unkownroute', response => {
expect(response.statusCode).to.equal(404);
done();
});
});
});