Skip to content

Commit

Permalink
tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sinamics committed Aug 30, 2024
1 parent a2e0c44 commit a9d083d
Showing 1 changed file with 132 additions and 132 deletions.
264 changes: 132 additions & 132 deletions src/pages/api/__tests__/v1/network/networkById.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.",
});
});
});

0 comments on commit a9d083d

Please sign in to comment.