diff --git a/src/pages/api/__tests__/v1/network/networkById.test.ts b/src/pages/api/__tests__/v1/network/networkById.test.ts index 6a1187e8..31846e07 100644 --- a/src/pages/api/__tests__/v1/network/networkById.test.ts +++ b/src/pages/api/__tests__/v1/network/networkById.test.ts @@ -37,78 +37,78 @@ describe("/api/getNetworkById", () => { beforeEach(() => { jest.clearAllMocks(); }); - // it("should respond 200 when network is found", async () => { - // // Mock the decryption to return a valid user ID - // (encryptionModule.decryptAndVerifyToken as jest.Mock).mockResolvedValue({ - // userId: "userId", - // }); - - // // Mock the database to return a network - // prisma.network.findUnique = jest.fn().mockResolvedValue({ - // nwid: "test_nw_id", - // nwname: "credent_second", - // authorId: "userId", - // }); - - // // Mock the ztController to return a network detail - // (ztController.local_network_detail as jest.Mock).mockResolvedValue({ - // network: { id: "networkId", name: "networkName" }, - // }); - - // const req = { - // method: "GET", - // headers: { "x-ztnet-auth": "validApiKey" }, - // query: { id: "networkId" }, - // } as unknown as NextApiRequest; - - // const res = { - // status: jest.fn().mockReturnThis(), - // json: jest.fn(), - // end: jest.fn(), - // setHeader: jest.fn(), // Mock `setHeader` rate limiter uses it - // } as unknown as NextApiResponse; - - // await apiNetworkByIdHandler(req, res); - - // expect(res.status).toHaveBeenCalledWith(200); - // expect(res.json).toHaveBeenCalledWith({ - // id: "networkId", - // name: "networkName", - // authorId: "userId", - // nwid: "test_nw_id", - // nwname: "credent_second", - // }); - // }); - - // it("should respond 401 when network is not found", async () => { - // // Mock the decryption to return a valid user ID - // (encryptionModule.decryptAndVerifyToken as jest.Mock).mockResolvedValue({ - // userId: "userId", - // }); - - // // Mock the database to return a network - // prisma.network.findUnique = jest.fn().mockResolvedValue(null); - - // const req = { - // method: "GET", - // headers: { "x-ztnet-auth": "validApiKey" }, - // query: { id: "networkId" }, - // } as unknown as NextApiRequest; - - // const res = { - // status: jest.fn().mockReturnThis(), - // json: jest.fn(), - // end: jest.fn(), - // setHeader: jest.fn(), // Mock `setHeader` rate limiter uses it - // } as unknown as NextApiResponse; - - // await apiNetworkByIdHandler(req, res); - - // expect(res.status).toHaveBeenCalledWith(401); - // expect(res.json).toHaveBeenCalledWith({ - // error: "Network not found or access denied.", - // }); - // }); + it("should respond 200 when network is found", async () => { + // Mock the decryption to return a valid user ID + (encryptionModule.decryptAndVerifyToken as jest.Mock).mockResolvedValue({ + userId: "userId", + }); + + // Mock the database to return a network + prisma.network.findUnique = jest.fn().mockResolvedValue({ + nwid: "test_nw_id", + nwname: "credent_second", + authorId: "userId", + }); + + // Mock the ztController to return a network detail + (ztController.local_network_detail as jest.Mock).mockResolvedValue({ + network: { id: "networkId", name: "networkName" }, + }); + + const req = { + method: "GET", + headers: { "x-ztnet-auth": "validApiKey" }, + query: { id: "networkId" }, + } as unknown as NextApiRequest; + + const res = { + status: jest.fn().mockReturnThis(), + json: jest.fn(), + end: jest.fn(), + setHeader: jest.fn(), // Mock `setHeader` rate limiter uses it + } as unknown as NextApiResponse; + + await apiNetworkByIdHandler(req, res); + + expect(res.status).toHaveBeenCalledWith(200); + expect(res.json).toHaveBeenCalledWith({ + id: "networkId", + name: "networkName", + authorId: "userId", + nwid: "test_nw_id", + nwname: "credent_second", + }); + }); + + it("should respond 401 when network is not found", async () => { + // Mock the decryption to return a valid user ID + (encryptionModule.decryptAndVerifyToken as jest.Mock).mockResolvedValue({ + userId: "userId", + }); + + // Mock the database to return a network + prisma.network.findUnique = jest.fn().mockResolvedValue(null); + + const req = { + method: "GET", + headers: { "x-ztnet-auth": "validApiKey" }, + query: { id: "networkId" }, + } as unknown as NextApiRequest; + + const res = { + status: jest.fn().mockReturnThis(), + json: jest.fn(), + end: jest.fn(), + setHeader: jest.fn(), // Mock `setHeader` rate limiter uses it + } as unknown as NextApiResponse; + + await apiNetworkByIdHandler(req, res); + + expect(res.status).toHaveBeenCalledWith(401); + expect(res.json).toHaveBeenCalledWith({ + error: "Network not found or access denied.", + }); + }); it("should respond with an error when ztController throws an error", async () => { // Mock the decryption to return a valid user ID @@ -151,64 +151,64 @@ describe("/api/getNetworkById", () => { expect(res.json).toHaveBeenCalledWith({ error: error.message }); }); - // it("should respond 401 when decryptAndVerifyToken fails", async () => { - // // Mock the decryption to fail - // (encryptionModule.decryptAndVerifyToken as jest.Mock).mockRejectedValue( - // new Error("Invalid token"), - // ); - - // const req = { - // method: "GET", - // headers: { "x-ztnet-auth": "invalidApiKey" }, - // query: { id: "networkId" }, - // } as unknown as NextApiRequest; - - // const res = { - // status: jest.fn().mockReturnThis(), - // json: jest.fn(), - // end: jest.fn(), - // setHeader: jest.fn(), // Mock `setHeader` if rate limiter uses it - // } as unknown as NextApiResponse; - - // await apiNetworkByIdHandler(req, res); - - // expect(res.status).toHaveBeenCalledWith(401); - // expect(res.json).toHaveBeenCalledWith( - // expect.objectContaining({ error: expect.any(String) }), - // ); - // }); - - // it("should respond 401 when user is not the author of the network", async () => { - // // Mock the decryption to return a valid user ID - // (encryptionModule.decryptAndVerifyToken as jest.Mock).mockResolvedValue({ - // userId: "userId", - // }); - - // const req = { - // method: "GET", - // headers: { "x-ztnet-auth": "validApiKey" }, - // query: { id: "networkIdThatUserDoesNotOwn" }, - // } as unknown as NextApiRequest; - - // const res = { - // status: jest.fn().mockReturnThis(), - // end: jest.fn(), - // json: jest.fn().mockReturnThis(), - // setHeader: jest.fn(), // Mock `setHeader` rate limiter uses it - // } as unknown as NextApiResponse; - - // // Mock the database to return a network - // prisma.network.findUnique = jest.fn().mockResolvedValue({ - // nwid: "networkIdThatUserDoesNotOwn", - // nwname: "Some Network", - // authorId: "anotherUserId", - // }); - - // await apiNetworkByIdHandler(req, res); - - // expect(res.status).toHaveBeenCalledWith(401); - // expect(res.json).toHaveBeenCalledWith({ - // error: "Network not found or access denied.", - // }); - // }); + it("should respond 401 when decryptAndVerifyToken fails", async () => { + // Mock the decryption to fail + (encryptionModule.decryptAndVerifyToken as jest.Mock).mockRejectedValue( + new Error("Invalid token"), + ); + + const req = { + method: "GET", + headers: { "x-ztnet-auth": "invalidApiKey" }, + query: { id: "networkId" }, + } as unknown as NextApiRequest; + + const res = { + status: jest.fn().mockReturnThis(), + json: jest.fn(), + end: jest.fn(), + setHeader: jest.fn(), // Mock `setHeader` if rate limiter uses it + } as unknown as NextApiResponse; + + await apiNetworkByIdHandler(req, res); + + expect(res.status).toHaveBeenCalledWith(401); + expect(res.json).toHaveBeenCalledWith( + expect.objectContaining({ error: expect.any(String) }), + ); + }); + + it("should respond 401 when user is not the author of the network", async () => { + // Mock the decryption to return a valid user ID + (encryptionModule.decryptAndVerifyToken as jest.Mock).mockResolvedValue({ + userId: "userId", + }); + + const req = { + method: "GET", + headers: { "x-ztnet-auth": "validApiKey" }, + query: { id: "networkIdThatUserDoesNotOwn" }, + } as unknown as NextApiRequest; + + const res = { + status: jest.fn().mockReturnThis(), + end: jest.fn(), + json: jest.fn().mockReturnThis(), + setHeader: jest.fn(), // Mock `setHeader` rate limiter uses it + } as unknown as NextApiResponse; + + // Mock the database to return a network + prisma.network.findUnique = jest.fn().mockResolvedValue({ + nwid: "networkIdThatUserDoesNotOwn", + nwname: "Some Network", + authorId: "anotherUserId", + }); + + await apiNetworkByIdHandler(req, res); + + expect(res.status).toHaveBeenCalledWith(401); + expect(res.json).toHaveBeenCalledWith({ + error: "Network not found or access denied.", + }); + }); });