|
| 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 { fillDbMockWithUnallocatedItemRequestsForItemIdFiltering } from "@/test/util/dbMockUtils"; |
| 6 | +import { dbMock } from "@/test/dbMock"; |
| 7 | + |
| 8 | +test("Should return 401 for no session", async () => { |
| 9 | + await testApiHandler({ |
| 10 | + params: { unallocatedItemId: "1" }, |
| 11 | + appHandler, |
| 12 | + async test({ fetch }) { |
| 13 | + invalidateSession(); |
| 14 | + |
| 15 | + const res = await fetch({ method: "GET" }); |
| 16 | + expect(res.status).toBe(401); |
| 17 | + const json = await res.json(); |
| 18 | + expect(json).toEqual({ message: "Session required" }); |
| 19 | + }, |
| 20 | + }); |
| 21 | +}); |
| 22 | + |
| 23 | +test("Should return 403 for being PARTNER", async () => { |
| 24 | + await testApiHandler({ |
| 25 | + appHandler, |
| 26 | + async test({ fetch }) { |
| 27 | + validateSession("PARTNER"); |
| 28 | + |
| 29 | + const res = await fetch({ method: "GET" }); |
| 30 | + expect(res.status).toBe(403); |
| 31 | + const json = await res.json(); |
| 32 | + expect(json).toEqual({ message: "Unauthorized" }); |
| 33 | + }, |
| 34 | + }); |
| 35 | +}); |
| 36 | + |
| 37 | +test("Should return 200 for being STAFF", async () => { |
| 38 | + await testApiHandler({ |
| 39 | + params: { unallocatedItemId: "1" }, |
| 40 | + appHandler, |
| 41 | + async test({ fetch }) { |
| 42 | + validateSession("STAFF"); |
| 43 | + |
| 44 | + const res = await fetch({ method: "GET" }); |
| 45 | + expect(res.status).toBe(200); |
| 46 | + }, |
| 47 | + }); |
| 48 | +}); |
| 49 | + |
| 50 | +test("Should return 200 for being ADMIN", async () => { |
| 51 | + await testApiHandler({ |
| 52 | + params: { unallocatedItemId: "1" }, |
| 53 | + appHandler, |
| 54 | + async test({ fetch }) { |
| 55 | + validateSession("ADMIN"); |
| 56 | + |
| 57 | + const res = await fetch({ method: "GET" }); |
| 58 | + expect(res.status).toBe(200); |
| 59 | + }, |
| 60 | + }); |
| 61 | +}); |
| 62 | + |
| 63 | +test("Should return 200 for being SUPER_ADMIN", async () => { |
| 64 | + await testApiHandler({ |
| 65 | + params: { unallocatedItemId: "1" }, |
| 66 | + appHandler, |
| 67 | + async test({ fetch }) { |
| 68 | + validateSession("SUPER_ADMIN"); |
| 69 | + |
| 70 | + const res = await fetch({ method: "GET" }); |
| 71 | + expect(res.status).toBe(200); |
| 72 | + }, |
| 73 | + }); |
| 74 | +}); |
| 75 | + |
| 76 | +test("For an authorized session, should give all unallocated item requests pointed to the specificed item", async () => { |
| 77 | + await testApiHandler({ |
| 78 | + params: { unallocatedItemId: "1" }, |
| 79 | + appHandler, |
| 80 | + async test({ fetch }) { |
| 81 | + fillDbMockWithUnallocatedItemRequestsForItemIdFiltering(10); |
| 82 | + |
| 83 | + const unallocatedItemRequests = |
| 84 | + await dbMock.unallocatedItemRequest.findMany(); |
| 85 | + |
| 86 | + validateSession("STAFF"); |
| 87 | + const res = await fetch({ method: "GET" }); |
| 88 | + expect(res.status).toBe(200); |
| 89 | + const json = await res.json(); |
| 90 | + expect(json).toEqual({ unallocatedItemRequests }); |
| 91 | + // expect(json).toEqual({ |
| 92 | + // unallocatedItemRequests: unallocatedItemRequests.forEach((uir) => { |
| 93 | + // return { |
| 94 | + // id: uir.id, |
| 95 | + // quantity: uir.quantity, |
| 96 | + // comments: uir.comments, |
| 97 | + // }; |
| 98 | + // }), |
| 99 | + // }); |
| 100 | + }, |
| 101 | + }); |
| 102 | +}); |
0 commit comments