-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathdisable_route.test.js
180 lines (151 loc) · 4.56 KB
/
disable_route.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
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
176
177
178
179
180
'use strict';
const t = require('tap');
const { test } = t;
const mongoose = require('mongoose');
const BackwardWrapper = require('./BackwardWrapper.js');
const bw = new BackwardWrapper(t);
test('mongoose db initialization', async () => {
await bw.createConnection();
});
test('schema initialization', async t => {
const schema = mongoose.Schema({
name: String,
appleCount: Number,
bananaCount: Number,
fieldAvailableIfYouAskGood: { type: Number, default: 999 }
});
schema.methods.apiValues = function (request) {
if (!request.headers['givememoredataplease']) {
return {
name: this.name,
appleCount: this.appleCount,
bananaCount: this.bananaCount
};
}
// or return this.toObject();
//
return {
name: this.name,
appleCount: this.appleCount,
bananaCount: this.bananaCount,
fieldAvailableIfYouAskGood: this.fieldAvailableIfYouAskGood
};
};
schema.methods.apiPut = async function () {
// disable the Put completely
throw new Error('PUT is disabled for this route');
};
schema.statics.apiPost = async function (data, request) {
// lets POST only with specific header
// possible option is to check user rights here
// if (!request.user.hasRightToPost()) {
// throw new Error('Lol, you cant');
// }
if (!request.headers['letmepostplease']) {
throw new Error('POST is disabled for you!');
}
let doc = new bw.conn.models.WhereTest();
bw.conn.models.WhereTest.schema.eachPath(pathname => {
if (data[pathname] !== undefined) {
doc[pathname] = data[pathname];
}
});
await doc.save();
return doc;
};
schema.methods.apiDelete = async function () {
// disable the Put completely
throw new Error('DELETE is disabled for this route');
};
bw.conn.model('WhereTest', schema);
t.ok(bw.conn.models.WhereTest);
});
test('clean up test collections', async () => {
await bw.conn.models.WhereTest.deleteMany({}).exec();
});
test('initialization of API server', async t => {
await bw.createServer({
models: bw.conn.models
});
t.equal(
bw.fastify.mongooseAPI.apiRouters.WhereTest.collectionName,
'wheretests',
'Collection name used in API path'
);
});
test('Disabled POST item test', async t => {
let response = null;
response = await bw.inject(
t,
{
method: 'POST',
url: '/api/wheretests',
payload: { name: 'Bob', appleCount: 1, bananaCount: 2 }
},
500
);
t.equal(
response.statusCode,
500,
"doesn't let you post without extra header"
);
response = await bw.inject(t, {
method: 'POST',
url: '/api/wheretests',
headers: { letmepostplease: 'pleaaaaase' },
payload: { name: 'Bob', appleCount: 1, bananaCount: 2 }
});
t.match(
response.json(),
{ name: 'Bob', appleCount: 1, bananaCount: 2 },
'POST with header ok'
);
t.has(
response.json(),
{ fieldAvailableIfYouAskGood: undefined },
'does not have fieldAvailableIfYouAskGood field by default'
);
});
test('Has Extra field in response by apiValues', async t => {
const response = await bw.inject(t, {
method: 'POST',
url: '/api/wheretests',
headers: {
letmepostplease: 'pleaaaaase',
givememoredataplease: 'pleaaaaase'
},
payload: { name: 'Bob', appleCount: 1, bananaCount: 2 }
});
t.match(
response.json(),
{ name: 'Bob', appleCount: 1, bananaCount: 2 },
'POST with header ok'
);
t.ok(
response.json().fieldAvailableIfYouAskGood !== undefined,
'fieldAvailableIfYouAskGood is present'
);
});
test('Disabled PUT test', async t => {
let itemFromDb = await bw.conn.models.WhereTest.findOne({});
await bw.inject(
t,
{
method: 'PUT',
url: '/api/wheretests/' + itemFromDb.id,
payload: { name: 'Bob22', appleCount: 21, bananaCount: 22 }
},
500
);
});
test('Disabled DELETE test', async t => {
let itemFromDb = await bw.conn.models.WhereTest.findOne({});
await bw.inject(
t,
{
method: 'DELETE',
url: '/api/wheretests/' + itemFromDb.id
},
500
);
});