Repository navigation
Expand file tree
/
Copy pathtest.js
More file actions
221 lines (188 loc) · 7.38 KB
/
Copy pathtest.js
File metadata and controls
221 lines (188 loc) · 7.38 KB
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
216
217
218
219
220
221
const request = require("supertest");
const app = require("./server");
const pool = require("./config/db");
// ── Inventory ──────────────────────────────────────────────────────────────
describe("GET /inventory", () => {
it("returns paginated inventory with only active books", async () => {
const res = await request(app).get("/inventory");
expect(res.statusCode).toBe(200);
expect(res.body).toHaveProperty("data");
expect(res.body).toHaveProperty("pagination");
expect(Array.isArray(res.body.data)).toBe(true);
});
});
describe("POST /inventory/add", () => {
it("blocks non-librarians", async () => {
const res = await request(app)
.post("/inventory/add")
.send({
userId: 1,
title: "Test Book",
author: "Author",
isbn: "123",
category: "Test",
total_stock: 3,
});
expect(res.statusCode).toBe(403);
});
it("allows librarians to add a book", async () => {
const res = await request(app)
.post("/inventory/add")
.send({
userId: 999,
title: "Test Book",
author: "Author",
isbn: "123",
category: "Test",
total_stock: 3,
});
expect(res.statusCode).toBe(201);
expect(res.body.message).toMatch(/Test Book/);
});
});
describe("DELETE /inventory/delete/:inventoryId", () => {
it("blocks non-librarians", async () => {
const res = await request(app)
.delete("/inventory/delete/101")
.send({ userId: 1 });
expect(res.statusCode).toBe(403);
});
it("soft-deletes a book so it no longer appears in inventory", async () => {
// First add a book to delete
await request(app)
.post("/inventory/add")
.send({
userId: 999,
title: "To Be Deleted",
author: "Author",
isbn: "999",
category: "Test",
total_stock: 1,
});
// Get its ID
const [rows] = await pool.query(
"SELECT inventory_id FROM library_inventory WHERE title = 'To Be Deleted'"
);
const id = rows[0].inventory_id;
const res = await request(app)
.delete(`/inventory/delete/${id}`)
.send({ userId: 999 });
expect(res.statusCode).toBe(200);
expect(res.body.message).toMatch(/archived/i);
// Confirm it's hidden from GET
const listRes = await request(app).get("/inventory");
const titles = listRes.body.data.map((b) => b.title);
expect(titles).not.toContain("To Be Deleted");
});
});
// ── Loans ──────────────────────────────────────────────────────────────────
describe("POST /loan/borrow", () => {
it("successfully borrows a book and decrements stock", async () => {
const [before] = await pool.query(
"SELECT available_stock FROM library_inventory WHERE inventory_id = 101"
);
const stockBefore = before[0].available_stock;
const res = await request(app)
.post("/loan/borrow")
.send({ borrowerId: 2, inventoryId: 101 });
expect(res.statusCode).toBe(200);
expect(res.body.message).toMatch(/borrowed/i);
const [after] = await pool.query(
"SELECT available_stock FROM library_inventory WHERE inventory_id = 101"
);
expect(after[0].available_stock).toBe(stockBefore - 1);
});
it("fails when book is out of stock", async () => {
const res = await request(app)
.post("/loan/borrow")
.send({ borrowerId: 1, inventoryId: 102 }); // 102 has 0 stock
expect(res.statusCode).toBe(400);
expect(res.body.error).toMatch(/out of stock/i);
});
});
describe("POST /loan/return", () => {
it("returns a book and increments stock", async () => {
const [before] = await pool.query(
"SELECT available_stock FROM library_inventory WHERE inventory_id = 101"
);
const stockBefore = before[0].available_stock;
const res = await request(app)
.post("/loan/return")
.send({ borrowerId: 2, inventoryId: 101 });
expect(res.statusCode).toBe(200);
const [after] = await pool.query(
"SELECT available_stock FROM library_inventory WHERE inventory_id = 101"
);
expect(after[0].available_stock).toBe(stockBefore + 1);
});
it("fails when no active loan exists", async () => {
const res = await request(app)
.post("/loan/return")
.send({ borrowerId: 1, inventoryId: 101 }); // Alice never borrowed
expect(res.statusCode).toBe(400);
expect(res.body.error).toMatch(/no active loan/i);
});
it("charges a late fee for overdue returns", async () => {
// Manually create an overdue loan
const pastDate = new Date("2020-01-01");
await pool.query(
'INSERT INTO loan_records (borrower_id, inventory_id, status, borrowed_at, due_date) VALUES (1, 101, "active", NOW(), ?)',
[pastDate]
);
await pool.query(
"UPDATE library_inventory SET available_stock = available_stock - 1 WHERE inventory_id = 101"
);
const res = await request(app)
.post("/loan/return")
.send({ borrowerId: 1, inventoryId: 101 });
expect(res.statusCode).toBe(200);
expect(res.body.message).toMatch(/fee/i);
expect(res.body.message).toMatch(/\$/);
});
});
describe("GET /loan/my-loans/:borrowerId", () => {
it("returns active loans for a borrower", async () => {
// Borrow a book first so there's something to show
await request(app)
.post("/loan/borrow")
.send({ borrowerId: 1, inventoryId: 101 });
const res = await request(app).get("/loan/my-loans/1");
expect(res.statusCode).toBe(200);
expect(res.body).toHaveProperty("activeLoans");
expect(Array.isArray(res.body.activeLoans)).toBe(true);
expect(res.body.activeLoans.length).toBeGreaterThan(0);
});
});
// ── Reservations ───────────────────────────────────────────────────────────
describe("POST /loan/reserve", () => {
it("blocks reservation when book is in stock", async () => {
const res = await request(app)
.post("/loan/reserve")
.send({ borrowerId: 2, inventoryId: 101 }); // 101 has stock
expect(res.statusCode).toBe(400);
expect(res.body.message).toMatch(/available/i);
});
it("places a reservation when book is out of stock", async () => {
// Clear any existing reservation first
await pool.query(
"DELETE FROM reservations WHERE borrower_id = 1 AND inventory_id = 102"
);
const res = await request(app)
.post("/loan/reserve")
.send({ borrowerId: 1, inventoryId: 102 }); // 102 has 0 stock
expect(res.statusCode).toBe(200);
expect(res.body.message).toMatch(/reservation placed/i);
});
it("blocks a duplicate reservation", async () => {
// First reservation already exists from test above
const res = await request(app)
.post("/loan/reserve")
.send({ borrowerId: 1, inventoryId: 102 });
expect(res.statusCode).toBe(400);
expect(res.body.message).toMatch(/already have a pending reservation/i);
});
});
// ── Cleanup ────────────────────────────────────────────────────────────────
afterAll(async () => {
await pool.end();
});