|
| 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 { fillDbMockWithUnallocatedItemRequestsForPartnerIdFilter } from "@/test/util/dbMockUtils"; |
| 6 | +import { dbMock } from "@/test/dbMock"; |
| 7 | + |
| 8 | +test("Should return 401 for invalid session", async () => { |
| 9 | + await testApiHandler({ |
| 10 | + params: { partnerId: "1" }, |
| 11 | + appHandler, |
| 12 | + async test({ fetch }) { |
| 13 | + invalidateSession(); |
| 14 | + |
| 15 | + const res = await fetch({ method: "GET" }); |
| 16 | + await expect(res.status).toBe(401); |
| 17 | + const json = await res.json(); |
| 18 | + await expect(json).toEqual({ message: "Session required" }); |
| 19 | + }, |
| 20 | + }); |
| 21 | +}); |
| 22 | + |
| 23 | +test("Should return 403 if not STAFF, ADMIN, or SUPER_ADMIN", async () => { |
| 24 | + await testApiHandler({ |
| 25 | + params: { partnerId: "1" }, |
| 26 | + appHandler, |
| 27 | + async test({ fetch }) { |
| 28 | + validateSession("PARTNER"); |
| 29 | + |
| 30 | + const res = await fetch({ method: "GET" }); |
| 31 | + await expect(res.status).toBe(403); |
| 32 | + const json = await res.json(); |
| 33 | + await expect(json).toEqual({ message: "Unauthorized" }); |
| 34 | + }, |
| 35 | + }); |
| 36 | +}); |
| 37 | + |
| 38 | +test("Should return 200 if STAFF", async () => { |
| 39 | + await testApiHandler({ |
| 40 | + params: { partnerId: "1" }, |
| 41 | + appHandler, |
| 42 | + async test({ fetch }) { |
| 43 | + validateSession("STAFF"); |
| 44 | + |
| 45 | + const res = await fetch({ method: "GET" }); |
| 46 | + await expect(res.status).toBe(200); |
| 47 | + }, |
| 48 | + }); |
| 49 | +}); |
| 50 | + |
| 51 | +test("Should return 200 if ADMIN", async () => { |
| 52 | + await testApiHandler({ |
| 53 | + params: { partnerId: "1" }, |
| 54 | + appHandler, |
| 55 | + async test({ fetch }) { |
| 56 | + validateSession("ADMIN"); |
| 57 | + |
| 58 | + const res = await fetch({ method: "GET" }); |
| 59 | + await expect(res.status).toBe(200); |
| 60 | + }, |
| 61 | + }); |
| 62 | +}); |
| 63 | + |
| 64 | +test("Should return 200 if SUPER_ADMIN", async () => { |
| 65 | + await testApiHandler({ |
| 66 | + params: { partnerId: "1" }, |
| 67 | + appHandler, |
| 68 | + async test({ fetch }) { |
| 69 | + validateSession("SUPER_ADMIN"); |
| 70 | + |
| 71 | + const res = await fetch({ method: "GET" }); |
| 72 | + await expect(res.status).toBe(200); |
| 73 | + }, |
| 74 | + }); |
| 75 | +}); |
| 76 | + |
| 77 | +// This method doesn't have a mock db implemented such that it verifies that the only items being pulled are those associated with the partner id. |
| 78 | +test("Should return 200 and unallocated item requests associated with partnerId", async () => { |
| 79 | + await testApiHandler({ |
| 80 | + params: { partnerId: "1" }, |
| 81 | + appHandler, |
| 82 | + async test({ fetch }) { |
| 83 | + validateSession("STAFF"); |
| 84 | + |
| 85 | + fillDbMockWithUnallocatedItemRequestsForPartnerIdFilter(10); |
| 86 | + const expectedResponse = await dbMock.unallocatedItemRequest.findMany(); |
| 87 | + |
| 88 | + const res = await fetch({ method: "GET" }); |
| 89 | + expect(res.status).toBe(200); |
| 90 | + |
| 91 | + const json = await res.json(); |
| 92 | + expect(json).toEqual({ unallocatedItemRequests: expectedResponse }); |
| 93 | + }, |
| 94 | + }); |
| 95 | +}); |
0 commit comments