forked from AndrewKeig/express-validation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathabortEarly.test.js
123 lines (105 loc) · 3.56 KB
/
abortEarly.test.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
const Joi = require('joi');
const request = require('supertest');
const { createServer } = require('../../_mocks_/express');
const schema = {
headers: Joi.object({
accesstoken: Joi.string().required(),
userid: Joi.string().required(),
}).unknown(),
params: Joi.object({
id: Joi.number()
.integer()
.required(),
}),
body: Joi.object({
email: Joi.string()
.email()
.required(),
password: Joi.string()
.regex(/[a-zA-Z0-9]{3,30}/)
.required(),
}),
};
const app = createServer('put', '/user/:id', schema, {}, { abortEarly: false });
describe('Abort early', () => {
describe('when the request contains a valid payload and headers', () => {
it('should return a 200 ok response', async () => {
const login = {
email: 'andrew.keig@gmail.com',
password: '12356',
};
const response = await request(app)
.put('/user/1')
.set('accessToken', '4343434343')
.set('userId', '3243243242')
.send(login);
expect(response.statusCode).toBe(200);
});
});
describe('when the request contains a valid payload and headers, with invalid parameter', () => {
it('should return a 200 ok response', async () => {
const login = {
email: 'andrew.keig@gmail.com',
password: '12356',
};
const response = await request(app)
.put('/user/e')
.set('accessToken', '4343434343')
.set('userId', '3243243242')
.send(login);
expect(response.statusCode).toBe(400);
expect(response.body.details.params.length).toBe(1);
expect(response.body.details.params[0].path[0]).toBe('id');
});
});
describe('when the request contains a missing payload and valid headers', () => {
it('should return a 400 ok response and two errors', async () => {
const login = {
email: 'andrew.keig@gmail.com',
password: '',
};
const response = await request(app)
.put('/user/1')
.set('accessToken', '4343434343')
.set('userId', '3243243242')
.send(login);
expect(response.statusCode).toBe(400);
expect(response.body.details.body.length).toBe(1);
expect(response.body.details.body[0].path[0]).toBe('password');
});
});
describe('when the request contains a valid payload and missing headers', () => {
it('should return a 400 ok response and two errors', async () => {
const login = {
email: 'andrew.keig@gmail.com',
password: '32323432',
};
const response = await request(app)
.put('/user/1')
.set('accessToken', '')
.set('userId', '3243243242')
.send(login);
expect(response.statusCode).toBe(400);
expect(response.body.details.headers.length).toBe(1);
expect(response.body.details.headers[0].path[0]).toBe('accesstoken');
});
});
describe('when the request contains an invalid payload and missing headers', () => {
it('should return a 400 ok response and two errors', async () => {
const login = {
email: 'andrew.keig@gmail.com',
password: '',
};
const response = await request(app)
.put('/user/1')
.set('accessToken', '')
.set('userId', '3243243242')
.send(login);
expect(response.statusCode).toBe(400);
expect(response.body.details.headers.length).toBe(1);
expect(response.body.details.headers[0].path[0]).toBe('accesstoken');
expect(response.body.details.body.length).toBe(1);
expect(response.body.details.body[0].path[0]).toBe('password');
});
});
});