cal.pub0.org/tests/api-keys/api-key.new.test.ts

78 lines
2.3 KiB
TypeScript
Raw Normal View History

2022-03-26 21:29:30 +00:00
import handleNewApiKey from "@api/api-keys/new";
import { createMocks } from "node-mocks-http";
describe("POST /api/api-keys/new with a note", () => {
it("returns a 201, and the created api key", async () => {
const { req, res } = createMocks({
method: "POST", // This POST method is not allowed
body: {
note: "Updated note",
},
});
await handleNewApiKey(req, res);
expect(res._getStatusCode()).toBe(201);
expect(JSON.parse(res._getData()).data.note).toStrictEqual("Updated note");
});
});
describe("POST /api/api-keys/new with a slug param", () => {
it("returns error 400, and the details about invalid slug body param", async () => {
const { req, res } = createMocks({
method: "POST", // This POST method is not allowed
body: {
note: "Updated note",
slug: "slug",
},
});
await handleNewApiKey(req, res);
expect(res._getStatusCode()).toBe(400);
2022-03-30 12:17:55 +00:00
expect(JSON.parse(res._getData())).toStrictEqual([
{
code: "unrecognized_keys",
keys: ["slug"],
message: "Unrecognized key(s) in object: 'slug'",
path: [],
},
]);
2022-03-26 21:29:30 +00:00
});
});
describe("GET /api/api-keys/new fails, only POST allowed", () => {
it("returns a message with the specified apiKeys", async () => {
const { req, res } = createMocks({
method: "GET", // This POST method is not allowed
});
await handleNewApiKey(req, res);
expect(res._getStatusCode()).toBe(405);
expect(JSON.parse(res._getData())).toStrictEqual({ error: "Only POST Method allowed" });
});
});
// FIXME: test 405 when prisma fails look for how to test prisma errors
describe("GET /api/api-keys/new fails, only POST allowed", () => {
it("returns a message with the specified apiKeys", async () => {
const { req, res } = createMocks({
method: "POST", // This POST method is not allowed
body: {
2022-03-30 12:17:55 +00:00
nonExistentParam: true,
2022-03-26 21:29:30 +00:00
// note: '123',
// slug: 12,
},
});
await handleNewApiKey(req, res);
expect(res._getStatusCode()).toBe(400);
2022-03-30 12:17:55 +00:00
expect(JSON.parse(res._getData())).toStrictEqual([
{
code: "unrecognized_keys",
keys: ["nonExistentParam"],
message: "Unrecognized key(s) in object: 'nonExistentParam'",
path: [],
},
]);
2022-03-26 21:29:30 +00:00
});
2022-03-30 12:17:55 +00:00
});