Skip to content

Commit

Permalink
Merge pull request #234 from sinamics/tests
Browse files Browse the repository at this point in the history
Added tests for creating user via API
  • Loading branch information
sinamics authored Dec 11, 2023
2 parents 6019e4b + 2bf7f26 commit 2bd12a1
Showing 1 changed file with 119 additions and 0 deletions.
119 changes: 119 additions & 0 deletions src/pages/api/__tests__/v1/user/user.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
import { NextApiRequest, NextApiResponse } from "next";
import createUserHandler from "~/pages/api/v1/user";
import { prisma } from "~/server/db";
import * as encryptionModule from "~/utils/encryption";
import { appRouter } from "~/server/api/root";

jest.mock("~/server/api/root", () => ({
appRouter: {
createCaller: jest.fn(() => ({
auth: {
// Mock the mutation method used in your authRouter
register: jest.fn().mockImplementation(() => ({
mutation: jest.fn().mockResolvedValue({}),
})),
},
})),
},
}));

jest.mock("~/utils/encryption");
jest.mock("~/server/api/trpc");

jest.mock("~/server/db", () => ({
prisma: {
user: {
count: jest.fn(),
create: jest.fn(),
},
},
}));

describe("createUserHandler", () => {
afterEach(() => {
// Restores all mocks back to their original value.
// only works when the mock was created with jest.spyOn;
jest.restoreAllMocks();
});
// Helper function to create a mock response object
const createMockRes = () =>
({
status: jest.fn().mockReturnThis(),
json: jest.fn(),
end: jest.fn(),
setHeader: jest.fn(),
}) as unknown as NextApiResponse;

it("should create a user successfully", async () => {
prisma.user.count = jest.fn().mockResolvedValue(0);

// Mock the decryption to fail
(encryptionModule.decryptAndVerifyToken as jest.Mock).mockResolvedValue({
userId: "userId",
});

const mockRegister = jest.fn().mockResolvedValue({ id: "newUserId" });
appRouter.createCaller = jest
.fn()
.mockReturnValue({ auth: { register: mockRegister } });

const req = {
method: "POST",
headers: { "x-ztnet-auth": "validApiKey" },
body: { email: "[email protected]", password: "password123", name: "Test User" },
} as unknown as NextApiRequest;

const res = {
status: jest.fn().mockReturnThis(),
json: jest.fn(),
end: jest.fn(),
setHeader: jest.fn(),
} as unknown as NextApiResponse;

await createUserHandler(req, res);

expect(res.status).toHaveBeenCalledWith(200);
expect(res.json).toHaveBeenCalledWith({ id: "newUserId" });
});

it("should respond 401 when decryptAndVerifyToken fails", async () => {
prisma.user.count = jest.fn().mockResolvedValue(1);

// Mock the decryption to fail
(encryptionModule.decryptAndVerifyToken as jest.Mock).mockRejectedValue(
new Error("Invalid token"),
);

const req = {
method: "POST",
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 createUserHandler(req, res);

expect(res.status).toHaveBeenCalledWith(401);
expect(res.json).toHaveBeenCalledWith({ error: "Invalid token" });
});

it("should allow only POST method", async () => {
const methods = ["GET", "PUT", "DELETE", "PATCH", "OPTIONS", "HEAD"];
const req = {} as NextApiRequest;
const res = createMockRes();

for (const method of methods) {
req.method = method;
await createUserHandler(req, res);

expect(res.status).toHaveBeenCalledWith(405);
expect(res.end).toHaveBeenCalled();
}
});
});

0 comments on commit 2bd12a1

Please sign in to comment.