Skip to content

Commit

Permalink
Add test coverage for KeyCacheBase
Browse files Browse the repository at this point in the history
  • Loading branch information
itssimon committed Nov 21, 2023
1 parent 6e5af51 commit fb683fb
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 8 deletions.
23 changes: 20 additions & 3 deletions tests/express/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,32 @@ import { Joi, Segments, celebrate, errors } from "celebrate";
import express from "express";
import { query, validationResult } from "express-validator";

import { KeyCacheBase } from "../../src/common/keyRegistry";
import { requireApiKey, useApitally } from "../../src/express";
import { CLIENT_ID, ENV } from "../utils";

class TestKeyCache extends KeyCacheBase {
private data: string | null = JSON.stringify({ salt: "xxx", keys: {} });

store(data: string): void {
this.cacheKey; // test getter
this.data = data;
}

retrieve(): string | null {
this.cacheKey; // test getter
return this.data;
}
}

export const getAppWithCelebrate = () => {
const app = express();

useApitally(app, {
clientId: CLIENT_ID,
env: ENV,
syncApiKeys: true,
keyCacheClass: TestKeyCache,
});

app.get(
Expand Down Expand Up @@ -39,7 +55,7 @@ export const getAppWithCelebrate = () => {
res.send(`Hello ID ${req.params.id}!`);
},
);
app.get("/error", requireApiKey(), (req, res) => {
app.get("/error", requireApiKey(), () => {
throw new Error("Error");
});

Expand All @@ -49,6 +65,7 @@ export const getAppWithCelebrate = () => {

export const getAppWithValidator = () => {
const app = express();
app.use(express.json());

useApitally(app, {
clientId: CLIENT_ID,
Expand Down Expand Up @@ -79,7 +96,7 @@ export const getAppWithValidator = () => {
res.send(`Hello ID ${req.params.id}!`);
},
);
app.get("/error", requireApiKey({ customHeader: "ApiKey" }), (req, res) => {
app.get("/error", requireApiKey({ customHeader: "ApiKey" }), () => {
throw new Error("Error");
});

Expand Down Expand Up @@ -119,7 +136,7 @@ export const getNestJsApp = () => {
res.send(`Hello ID ${req.params.id}!`);
},
);
app.get("/error", requireApiKey({ customHeader: "ApiKey" }), (req, res) => {
app.get("/error", requireApiKey({ customHeader: "ApiKey" }), () => {
throw new Error("Error");
});

Expand Down
6 changes: 3 additions & 3 deletions tests/fastify/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,23 +40,23 @@ export const getApp = async (customHeader?: string) => {
},
preValidation: requireApiKey({ scopes: "hello1", customHeader }),
},
async function (request, reply) {
async function (request) {
const { name, age } = request.query;
return `Hello ${name}! You are ${age} years old!`;
},
);
app.get<{ Params: HelloParams }>(
"/hello/:id",
{ preValidation: requireApiKey({ scopes: "hello2", customHeader }) },
async function (request, reply) {
async function (request) {
const { id } = request.params;
return `Hello ${id}!`;
},
);
app.get(
"/error",
{ preValidation: requireApiKey({ customHeader }) },
async function (request, reply) {
async function () {
throw new Error("Error");
},
);
Expand Down
4 changes: 2 additions & 2 deletions tests/koa/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export const getAppWithKoaRouter = () => {
ctx.body = `Hello ${ctx.params.id}!`;
},
);
router.get("/error", requireApiKey(), async (ctx) => {
router.get("/error", requireApiKey(), async () => {
throw new Error("Error");
});

Expand Down Expand Up @@ -57,7 +57,7 @@ export const getAppWithKoaRoute = () => {
}),
);
app.use(
route.get("/error", async (ctx) => {
route.get("/error", async () => {
throw new Error("Error");
}),
);
Expand Down

0 comments on commit fb683fb

Please sign in to comment.