-
-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathexpress.test.js
215 lines (186 loc) · 4.51 KB
/
express.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
import { describe, test, beforeAll, afterAll, expect } from 'vitest';
import { fileURLToPath } from 'url';
const map = require('./db');
const express = require('express');
import { json } from 'body-parser';
const initSqlite = require('./initSqlite');
const dateToISOString = require('../src/dateToISOString');
const port = 3008;
let server;
afterAll(async () => {
return new Promise((res) => {
if (server)
server.close(res);
else
res();
});
});
beforeAll(async () => {
await insertData('sqlite2');
hostExpress();
async function insertData(dbName) {
const { db, init } = getDb(dbName);
await init(db);
const george = await db.customer.insert({
name: 'George',
balance: 177,
isActive: true
});
const john = await db.customer.insert({
name: 'Harry',
balance: 200,
isActive: true
});
const date1 = new Date(2022, 0, 11, 9, 24, 47);
const date2 = new Date(2021, 0, 11, 12, 22, 45);
await db.order.insert([
{
orderDate: date1,
customer: george,
deliveryAddress: {
name: 'George',
street: 'Node street 1',
postalCode: '7059',
postalPlace: 'Jakobsli',
countryCode: 'NO'
},
lines: [
{
product: 'Bicycle',
packages: [
{ sscc: 'aaaa' }
]
},
{
product: 'Small guitar',
packages: [
{ sscc: 'bbbb' }
]
}
]
},
{
customer: john,
orderDate: date2,
deliveryAddress: {
name: 'Harry Potter',
street: '4 Privet Drive, Little Whinging',
postalCode: 'GU4',
postalPlace: 'Surrey',
countryCode: 'UK'
},
lines: [
{
product: 'Magic wand',
packages: [
{ sscc: '1234' }
]
}
]
}
]);
}
function hostExpress() {
const { db } = getDb('sqlite2');
let app = express();
app.disable('x-powered-by')
.use(json({ limit: '100mb' }))
.use('/rdb', validateToken)
.use('/rdb', db.express({
order: {
baseFilter: (db, req, _res) => {
const customerId = Number.parseInt(req.headers.authorization.split(' ')[1]);
return db.order.customerId.eq(Number.parseInt(customerId));
}
}
}));
server = app.listen(port, () => console.log(`Example app listening on port ${port}!`));
}
});
function validateToken(req, res, next) {
const authHeader = req.headers.authorization;
if (authHeader) {
return next();
} else
return res.status(401).json({ error: 'Authorization header missing' });
}
describe('express update with basefilter and interceptors', () => {
test('http', async () => await verify('http'));
async function verify(dbName) {
const { db } = getDb(dbName);
db.interceptors.request.use((config) => {
config.headers.Authorization = 'Bearer 2';
return config;
});
db.interceptors.response.use(
response => response,
error => {
return Promise.reject(error);
}
);
let row = await db.order.getOne(null, {
where: x => x.lines.exists(),
lines: { orderBy: 'id' },
customer: true,
deliveryAddress: true
});
row.lines.push({ product: 'Broomstick', amount: 300 });
await row.saveChanges();
await row.refresh();
row.orderDate = dateToISOString(new Date(row.orderDate));
const date2 = new Date(2021, 0, 11, 12, 22, 45);
const expected = {
id: 2,
customerId: 2,
customer: {
id: 2,
name: 'Harry',
balance: 200,
isActive: true
},
orderDate: dateToISOString(date2),
deliveryAddress: {
id: 2,
orderId: 2,
name: 'Harry Potter',
street: '4 Privet Drive, Little Whinging',
postalCode: 'GU4',
postalPlace: 'Surrey',
countryCode: 'UK'
},
lines: [
{ product: 'Magic wand', amount: null, id: 3, orderId: 2 },
{ product: 'Broomstick', amount: 300, id: 4, orderId: 2 }
]
};
expect(row).toEqual(expected);
}
});
const pathSegments = fileURLToPath(import.meta.url).split('/');
const lastSegment = pathSegments[pathSegments.length - 1];
const fileNameWithoutExtension = lastSegment.split('.')[0];
const sqliteName = `demo.${fileNameWithoutExtension}.db`;
const sqliteName2 = `demo.${fileNameWithoutExtension}2.db`;
const connections = {
sqlite: {
db: map({ db: (con) => con.sqlite(sqliteName) }),
init: initSqlite
},
sqlite2: {
db: map({ db: (con) => con.sqlite(sqliteName2) }),
init: initSqlite
},
http: {
db: map.http(`http://localhost:${port}/rdb`),
}
};
function getDb(name) {
if (name === 'sqlite')
return connections.sqlite;
else if (name === 'sqlite2')
return connections.sqlite2;
else if (name === 'http')
return connections.http;
else
throw new Error('unknown');
}