feat: adds tests for api keys endpoints
This commit is contained in:
1 parent
9e8be659c5
commit
12a7129e5a
10 files changed
+115
-20
No files matched your search
@@ -1,10 +1,10 @@
|
||||
import prisma from "@calcom/prisma";
|
||||
|
||||
import { ApiKey } from "@calcom/prisma/client";
|
||||
import { ApiKey } from "@prisma/client";
|
||||
import type { NextApiRequest, NextApiResponse } from "next";
|
||||
|
||||
import { schemaApiKey, withValidApiKey } from "@lib/validations/apiKey";
|
||||
import { schemaQueryId, withValidQueryIdString } from "@lib/validations/queryIdString";
|
||||
import { schemaQueryIdAsString, withValidQueryIdString } from "@lib/validations/queryIdString";
|
||||
|
||||
type ResponseData = {
|
||||
data?: ApiKey;
|
||||
@@ -14,7 +14,7 @@ type ResponseData = {
|
||||
|
||||
export async function editApiKey(req: NextApiRequest, res: NextApiResponse<ResponseData>) {
|
||||
const { query, body, method } = req;
|
||||
const safeQuery = await schemaQueryId.safeParse(query);
|
||||
const safeQuery = await schemaQueryIdAsString.safeParse(query);
|
||||
const safeBody = await schemaApiKey.safeParse(body);
|
||||
|
||||
if (method === "PATCH") {
|
||||
@@ -25,12 +25,13 @@ export async function editApiKey(req: NextApiRequest, res: NextApiResponse<Respo
|
||||
}).then(apiKey => {
|
||||
res.status(200).json({ data: apiKey });
|
||||
}).catch(error => {
|
||||
res.status(404).json({ message: `Event type with ID ${safeQuery.data.id} not found and wasn't updated`, error })
|
||||
console.log(error)
|
||||
res.status(404).json({ message: `apiKey with ID ${safeQuery.data.id} not found and wasn't updated`, error })
|
||||
});
|
||||
}
|
||||
} else {
|
||||
// Reject any other HTTP method than POST
|
||||
res.status(405).json({ message: "Only PATCH Method allowed for updating apiKey-types" });
|
||||
res.status(405).json({ message: "Only PATCH Method allowed for updating API keys" });
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
import prisma from "@calcom/prisma";
|
||||
|
||||
import { ApiKey } from "@calcom/prisma/client";
|
||||
import { ApiKey } from "@prisma/client";
|
||||
import type { NextApiRequest, NextApiResponse } from "next";
|
||||
|
||||
import { schemaQueryId, withValidQueryIdString } from "@lib/validations/queryIdString";
|
||||
import { schemaQueryIdAsString, withValidQueryIdString } from "@lib/validations/queryIdString";
|
||||
|
||||
type ResponseData = {
|
||||
data?: ApiKey;
|
||||
@@ -13,7 +13,7 @@ type ResponseData = {
|
||||
|
||||
export async function apiKey(req: NextApiRequest, res: NextApiResponse<ResponseData>) {
|
||||
const { query, method } = req;
|
||||
const safe = await schemaQueryId.safeParse(query);
|
||||
const safe = await schemaQueryIdAsString.safeParse(query);
|
||||
if (safe.success) {
|
||||
if (method === "GET") {
|
||||
const apiKey = await prisma.apiKey.findUnique({ where: { id: safe.data.id } });
|
||||
|
||||
@@ -12,7 +12,7 @@ export default async function apiKey(req: NextApiRequest, res: NextApiResponse<R
|
||||
const apiKeys = await prisma.apiKey.findMany({});
|
||||
res.status(200).json({ data: { ...apiKeys } });
|
||||
} catch (error) {
|
||||
console.log(error);
|
||||
// console.log(error);
|
||||
// FIXME: Add zod for validation/error handling
|
||||
res.status(400).json({ error: error });
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import prisma from "@calcom/prisma";
|
||||
|
||||
import { ApiKey } from "@calcom/prisma/client";import type { NextApiRequest, NextApiResponse } from "next";
|
||||
import { ApiKey } from "@calcom/prisma/client";
|
||||
import type { NextApiRequest, NextApiResponse } from "next";
|
||||
|
||||
import { schemaApiKey, withValidApiKey } from "@lib/validations/apiKey";
|
||||
|
||||
@@ -23,7 +24,7 @@ async function createApiKey(req: NextApiRequest, res: NextApiResponse<ResponseDa
|
||||
})
|
||||
.then((apiKey) => res.status(201).json({ data: apiKey }))
|
||||
.catch((error) => {
|
||||
console.log(error);
|
||||
// console.log(error);
|
||||
res.status(400).json({ message: "Could not create apiKey", error: error })
|
||||
}
|
||||
)
|
||||
|
||||
Reference in new issue
Block a user