|
| 1 | +import { testApiHandler } from "next-test-api-route-handler"; |
| 2 | +import * as appHandler from "./route"; |
| 3 | +import { expect, test } from "@jest/globals"; |
| 4 | +import { validateSession, invalidateSession } from "@/test/util/authMockUtils"; |
| 5 | +import { dbMock } from "@/test/dbMock"; |
| 6 | +import { createUnclaimedItem } from "@/test/util/dbMockUtils"; |
| 7 | + |
| 8 | +/** |
| 9 | + * Form data: { |
| 10 | + * unallocatedItemId: "1", |
| 11 | + * quantity: "1", |
| 12 | + * comment: "comment" |
| 13 | + * } |
| 14 | + * @returns FormData with good data by default, but can be modified |
| 15 | + */ |
| 16 | +function getFormData({ |
| 17 | + unallocatedItemId = "1", |
| 18 | + quantity = "1", |
| 19 | + comment = "comment", |
| 20 | +}: { |
| 21 | + unallocatedItemId?: string; |
| 22 | + quantity?: string; |
| 23 | + comment?: string; |
| 24 | +} = {}) { |
| 25 | + const formData = new FormData(); |
| 26 | + formData.append("unallocatedItemId", unallocatedItemId); |
| 27 | + formData.append("quantity", quantity); |
| 28 | + formData.append("comment", comment); |
| 29 | + return formData; |
| 30 | +} |
| 31 | + |
| 32 | +test("Should return 401 for invalid session", async () => { |
| 33 | + await testApiHandler({ |
| 34 | + appHandler, |
| 35 | + async test({ fetch }) { |
| 36 | + invalidateSession(); |
| 37 | + |
| 38 | + const res = await fetch({ method: "POST" }); |
| 39 | + expect(res.status).toBe(401); |
| 40 | + const json = await res.json(); |
| 41 | + expect(json).toEqual({ message: "Session required" }); |
| 42 | + }, |
| 43 | + }); |
| 44 | +}); |
| 45 | + |
| 46 | +test("Should return 403 if not a partner", async () => { |
| 47 | + await testApiHandler({ |
| 48 | + appHandler, |
| 49 | + async test({ fetch }) { |
| 50 | + validateSession("STAFF"); |
| 51 | + |
| 52 | + const res = await fetch({ method: "POST" }); |
| 53 | + expect(res.status).toBe(403); |
| 54 | + const json = await res.json(); |
| 55 | + expect(json).toEqual({ message: "Unauthorized" }); |
| 56 | + }, |
| 57 | + }); |
| 58 | +}); |
| 59 | + |
| 60 | +test("Should return 404 if unallocated item not found", async () => { |
| 61 | + await testApiHandler({ |
| 62 | + appHandler, |
| 63 | + async test({ fetch }) { |
| 64 | + validateSession("PARTNER"); |
| 65 | + |
| 66 | + dbMock.item.findUnique.mockResolvedValue(null); |
| 67 | + |
| 68 | + const res = await fetch({ method: "POST", body: getFormData() }); |
| 69 | + expect(res.status).toBe(404); |
| 70 | + const json = await res.json(); |
| 71 | + expect(json).toEqual({ message: "Unallocated item not found" }); |
| 72 | + }, |
| 73 | + }); |
| 74 | +}); |
| 75 | + |
| 76 | +test("Should return 400 for bad form data", async () => { |
| 77 | + await testApiHandler({ |
| 78 | + appHandler, |
| 79 | + async test({ fetch }) { |
| 80 | + validateSession("PARTNER"); |
| 81 | + |
| 82 | + const badFormData = new FormData(); |
| 83 | + badFormData.append("unallocatedItemIdBad", "1"); |
| 84 | + badFormData.append("quantity", "1"); |
| 85 | + badFormData.append("comment", "comment"); |
| 86 | + |
| 87 | + const res = await fetch({ method: "POST", body: badFormData }); |
| 88 | + expect(res.status).toBe(400); |
| 89 | + const json = await res.json(); |
| 90 | + expect(json).toEqual({ message: "Invalid form data" }); |
| 91 | + }, |
| 92 | + }); |
| 93 | +}); |
| 94 | + |
| 95 | +test("Should return 400 for too low quantity", async () => { |
| 96 | + await testApiHandler({ |
| 97 | + appHandler, |
| 98 | + async test({ fetch }) { |
| 99 | + validateSession("PARTNER"); |
| 100 | + |
| 101 | + const res = await fetch({ |
| 102 | + method: "POST", |
| 103 | + body: getFormData({ quantity: "0" }), |
| 104 | + }); |
| 105 | + expect(res.status).toBe(400); |
| 106 | + const json = await res.json(); |
| 107 | + expect(json).toEqual({ message: "Invalid form data" }); |
| 108 | + }, |
| 109 | + }); |
| 110 | +}); |
| 111 | + |
| 112 | +test("Should return 400 for requesting too many items", async () => { |
| 113 | + await testApiHandler({ |
| 114 | + appHandler, |
| 115 | + async test({ fetch }) { |
| 116 | + validateSession("PARTNER"); |
| 117 | + |
| 118 | + dbMock.item.findUnique.mockResolvedValue( |
| 119 | + await createUnclaimedItem({ id: 1, quantity: 1 }) |
| 120 | + ); |
| 121 | + |
| 122 | + const res = await fetch({ |
| 123 | + method: "POST", |
| 124 | + body: getFormData({ quantity: "2" }), |
| 125 | + }); |
| 126 | + expect(res.status).toBe(400); |
| 127 | + const json = await res.json(); |
| 128 | + expect(json).toEqual({ message: "Not enough items for request" }); |
| 129 | + }, |
| 130 | + }); |
| 131 | +}); |
| 132 | + |
| 133 | +test("Should return 200 for successful request", async () => { |
| 134 | + await testApiHandler({ |
| 135 | + appHandler, |
| 136 | + async test({ fetch }) { |
| 137 | + validateSession("PARTNER"); |
| 138 | + |
| 139 | + dbMock.item.findUnique.mockResolvedValue( |
| 140 | + await createUnclaimedItem({ id: 1, quantity: 2 }) |
| 141 | + ); |
| 142 | + |
| 143 | + const res = await fetch({ method: "POST", body: getFormData() }); |
| 144 | + expect(res.status).toBe(200); |
| 145 | + }, |
| 146 | + }); |
| 147 | +}); |
| 148 | + |
| 149 | +test("Should create unallocated item request on success", async () => { |
| 150 | + await testApiHandler({ |
| 151 | + appHandler, |
| 152 | + async test({ fetch }) { |
| 153 | + const session = await validateSession("PARTNER"); |
| 154 | + |
| 155 | + dbMock.item.findUnique.mockResolvedValue( |
| 156 | + await createUnclaimedItem({ id: 1, quantity: 2 }) |
| 157 | + ); |
| 158 | + |
| 159 | + const res = await fetch({ |
| 160 | + method: "POST", |
| 161 | + body: getFormData({ |
| 162 | + unallocatedItemId: "1", |
| 163 | + quantity: "1", |
| 164 | + comment: "comment", |
| 165 | + }), |
| 166 | + }); |
| 167 | + expect(res.status).toBe(200); |
| 168 | + |
| 169 | + // For now, this is the only way I know to check if the create method was called |
| 170 | + expect(dbMock.unallocatedItemRequest.create).toHaveBeenCalledWith({ |
| 171 | + data: { |
| 172 | + itemId: 1, |
| 173 | + partnerId: parseInt(session.user.id), |
| 174 | + quantity: 1, |
| 175 | + comments: "comment", |
| 176 | + }, |
| 177 | + }); |
| 178 | + }, |
| 179 | + }); |
| 180 | +}); |
0 commit comments