-
Notifications
You must be signed in to change notification settings - Fork 6
/
route-errors.js
175 lines (150 loc) · 4.92 KB
/
route-errors.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
const Hapi = require('hapi');
const Lab = require('lab');
const Joi = require('joi');
const Errors = require('plugins/route-errors');
const Assets = require('plugins/assets');
const Auth = require('test/fixtures/auth-plugin');
const Logger = require('test/fixtures/logger-plugin');
const { beforeEach, describe, expect, it } = (exports.lab = Lab.script());
describe('Plugin: route-errors', () => {
let server;
beforeEach(async () => {
server = Hapi.server();
server.register(Logger);
await server.register(Errors);
});
it('should not redirect on valid route with no errors', async () => {
// setup
const fakeResult = 'ok';
const fakeRoute = { path: '/', method: 'GET', handler: () => fakeResult };
server.route(fakeRoute);
// exercise
const response = await server.inject(fakeRoute.path);
// verify
expect(response.statusCode).to.equal(200);
expect(response.result).to.equals(fakeResult);
});
it('should redirect 404 not found errors to root', async () => {
// exercise
const response = await server.inject('/invalid-route');
// validate
expect(response.statusCode).to.equal(301);
expect(response.statusMessage).to.equal('Moved Permanently');
expect(response.headers.location).to.equal('/');
});
it('should not redirect assets', async () => {
// setup
await server.register(Assets);
// exercise
const response = await server.inject('/img/invalid');
// validate
expect(response.statusCode).to.equal(404);
expect(response.statusMessage).to.equal('Not Found');
});
it('should return 400 malformed data on post errors', async () => {
// setup
const fakeRoute = {
path: '/',
method: 'POST',
handler: () => null,
options: {
validate: {
payload: {
arg: Joi.string().required()
}
}
}
};
server.route(fakeRoute);
// exercise
const response = await server.inject({ method: 'POST', url: fakeRoute.path });
// validate
expect(response.statusCode).to.equal(400);
expect(response.statusMessage).to.equal('Bad Request');
expect(response.result.message).to.equal('Malformed Data Entered');
});
it('should redirect on insufficient scope', async () => {
// setup
const fakeRoute = {
path: '/',
method: 'GET',
handler: () => null,
options: {
auth: {
scope: 'admin'
}
}
};
await server.register(Auth);
server.route(fakeRoute);
// exercise
const response = await server.inject({
method: 'GET',
url: fakeRoute.path,
auth: {
credentials: { scope: 'user' },
strategy: 'default'
}
});
// validate
expect(response.statusCode).to.equal(302);
expect(response.statusMessage).to.equal('Found');
expect(response.headers.location).to.equal('/');
});
it('should not redirect on insufficient scope when redirect set to false', async () => {
// setup
const fakeRoute = {
path: '/',
method: 'GET',
handler: () => null,
options: {
auth: {
scope: 'admin',
strategy: 'default'
},
app: {
redirect: false
}
}
};
await server.register(Auth);
server.route(fakeRoute);
// exercise
const response = await server.inject({
method: 'GET',
url: fakeRoute.path,
auth: {
credentials: { scope: 'user' },
strategy: 'default'
}
});
// validate
expect(response.statusCode).to.equal(403);
expect(response.statusMessage).to.equal('Forbidden');
});
it('should not redirect on failed authentication', async flags => {
// cleanup
flags.onCleanup = function() {
Auth.authenticate = true;
};
// setup
Auth.authenticate = false;
await server.register(Auth);
const fakeRoute = {
path: '/',
method: 'GET',
handler: () => null,
options: {
auth: {
scope: 'admin'
}
}
};
server.route(fakeRoute);
// exercise
const response = await server.inject({ method: 'GET', url: fakeRoute.path });
// validate
expect(response.statusCode).to.equal(401);
expect(response.statusMessage).to.equal('Unauthorized');
});
});