-
Notifications
You must be signed in to change notification settings - Fork 0
/
commonSetups.js
109 lines (96 loc) · 2.14 KB
/
commonSetups.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
const bcrypt = require("bcrypt");
const db = require("./database");
const { bcryptWorkFactor } = require("./config");
const createToken = require("./helpers/createToken");
// setup the database before testing
const commonBeforeAll = async () => {
// clear out all the tables
await db.query("DELETE FROM products");
await db.query("DELETE FROM users");
await db.query("DELETE FROM carts");
await db.query("DELETE FROM orders");
// fill out the tables with sample data
await db.query(
`INSERT INTO products
(id, name, image, price)
VALUES
('p1', 'name', 'image', 100),
('p2', 'name', 'image', 100),
('p3', 'name', 'image', 100),
('p4', 'name', 'image', 100),
('p5', 'name', 'image', 100),
('p6', 'name', 'image', 100)
`
);
const hash = await bcrypt.hash("password", bcryptWorkFactor);
await db.query(
`INSERT INTO users
(id, email, name, password, is_admin)
VALUES
(1, 'u@1', 'name', '${hash}', TRUE),
(2, 'u@2', 'name', '${hash}', TRUE),
(3, 'u@3', 'name', '${hash}', FALSE),
(4, 'u@4', 'name', '${hash}', FALSE),
(5, 'u@5', 'name', '${hash}', FALSE),
(6, 'u@6', 'name', '${hash}', FALSE)
`
);
await db.query(
`INSERT INTO carts
(id, user_id)
VALUES
(1, 1),
(2, 3),
(3, 3),
(4, 3),
(5, 5)
`
);
await db.query(
`INSERT INTO carts_products
(cart_id, product_id)
VALUES
(2, 'p1'),
(2, 'p2'),
(1, 'p1'),
(4, 'p3'),
(4, 'p6')`
);
await db.query(
`INSERT INTO orders
(id, user_id)
VALUES
(1, 1),
(2, 3),
(3, 3)
`
);
await db.query(
`INSERT INTO orders_products
(order_id, product_id)
VALUES
(3, 'p1'),
(3, 'p2'),
(1, 'p1')`
);
}
// begin a DB transaction
const commonBeforeEach = async () => {
await db.query("BEGIN");
}
// undo the transaction
const commonAfterEach = async () => {
await db.query("ROLLBACK");
}
// end the DB connection
const commonAfterAll = async () => {
await db.end();
}
// JWTs for testing admin / non admin
const userToken = createToken({ id: 3, isAdmin: false });
const adminToken = createToken({ id: 1, isAdmin: true });
module.exports = {
commonBeforeAll, commonBeforeEach,
commonAfterEach, commonAfterAll,
userToken, adminToken
};