forked from sinamics/ztnet
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request sinamics#538 from sinamics/api
Implemented Zod schemas for improved API input validation
- Loading branch information
Showing
23 changed files
with
373 additions
and
145 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -68,6 +68,7 @@ describe("organization api validation", () => { | |
.mockResolvedValue({ id: "newUserId", name: "Ztnet", email: "[email protected]" }); | ||
|
||
mockRequest.headers["x-ztnet-auth"] = "not valid token"; | ||
mockRequest.query = {}; | ||
|
||
await GET_userOrganization( | ||
mockRequest as NextApiRequest, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
import { NextApiRequest, NextApiResponse } from "next"; | ||
import createUserHandler, { POST_createUser } from "~/pages/api/v1/user"; | ||
import createUserHandler from "~/pages/api/v1/user"; | ||
import { prisma } from "~/server/db"; | ||
import { appRouter } from "~/server/api/root"; | ||
import { API_TOKEN_SECRET, encrypt, generateInstanceSecret } from "~/utils/encryption"; | ||
|
@@ -18,7 +18,12 @@ jest.mock("~/server/api/root", () => ({ | |
})), | ||
}, | ||
})); | ||
|
||
jest.mock("~/utils/rateLimit", () => ({ | ||
__esModule: true, | ||
default: () => ({ | ||
check: jest.fn().mockResolvedValue(true), | ||
}), | ||
})); | ||
jest.mock("~/server/api/trpc"); | ||
|
||
jest.mock("~/server/db", () => ({ | ||
|
@@ -126,9 +131,19 @@ describe("createUserHandler", () => { | |
}), | ||
})); | ||
|
||
mockRequest.method = "POST"; | ||
mockRequest.headers["x-ztnet-auth"] = "not defined"; | ||
mockRequest.body = { | ||
email: "[email protected]", | ||
password: "password123", | ||
name: "Ztnet", | ||
}; | ||
|
||
await createUserHandler( | ||
mockRequest as NextApiRequest, | ||
mockResponse as NextApiResponse, | ||
); | ||
|
||
await POST_createUser(mockRequest as NextApiRequest, mockResponse as NextApiResponse); | ||
expect(mockResponse.status).toHaveBeenCalledWith(200); | ||
|
||
// Check if the response is as expected | ||
|
@@ -166,6 +181,7 @@ describe("createUserHandler", () => { | |
method: "POST", | ||
headers: { "x-ztnet-auth": tokenWithIdHash }, | ||
body: { email: "[email protected]", password: "password123", name: "Test User" }, | ||
query: {}, | ||
} as unknown as NextApiRequest; | ||
|
||
const res = { | ||
|
@@ -208,7 +224,9 @@ describe("createUserHandler", () => { | |
|
||
it("should allow only POST method", async () => { | ||
const methods = ["GET", "PUT", "DELETE", "PATCH", "OPTIONS", "HEAD"]; | ||
const req = {} as NextApiRequest; | ||
const req = { | ||
query: {}, | ||
} as NextApiRequest; | ||
const res = createMockRes(); | ||
|
||
for (const method of methods) { | ||
|
40 changes: 40 additions & 0 deletions
40
src/pages/api/v1/network/[id]/member/[memberId]/_schema.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { z } from "zod"; | ||
|
||
// Schema for updateable fields metadata | ||
export const updateableFieldsMetaSchema = z | ||
.object({ | ||
name: z.string().optional(), | ||
authorized: z.boolean().optional(), | ||
}) | ||
.strict(); | ||
|
||
// Schema for the context passed to the handler | ||
export const handlerContextSchema = z.object({ | ||
body: z.record(z.unknown()), | ||
userId: z.string(), | ||
networkId: z.string(), | ||
memberId: z.string(), | ||
ctx: z.object({ | ||
prisma: z.any(), | ||
session: z.object({ | ||
user: z.object({ | ||
id: z.string(), | ||
}), | ||
}), | ||
}), | ||
}); | ||
|
||
// Schema for the context passed to the DELETE handler | ||
export const deleteHandlerContextSchema = z.object({ | ||
userId: z.string(), | ||
networkId: z.string(), | ||
memberId: z.string(), | ||
ctx: z.object({ | ||
prisma: z.any(), | ||
session: z.object({ | ||
user: z.object({ | ||
id: z.string(), | ||
}), | ||
}), | ||
}), | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { z } from "zod"; | ||
|
||
// Schema for the request body when creating a new network | ||
export const createNetworkBodySchema = z | ||
.object({ | ||
name: z.string().optional(), | ||
}) | ||
.strict(); | ||
|
||
// Schema for the context passed to the handler | ||
export const createNetworkContextSchema = z.object({ | ||
body: createNetworkBodySchema, | ||
ctx: z.object({ | ||
prisma: z.any(), | ||
session: z.object({ | ||
user: z.object({ | ||
id: z.string(), | ||
}), | ||
}), | ||
}), | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.