Skip to content

Commit

Permalink
feat: adding test for verufy handler in embed
Browse files Browse the repository at this point in the history
  • Loading branch information
nutrina committed Jan 29, 2025
1 parent 244e569 commit 6e4ec35
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
9 changes: 7 additions & 2 deletions embed/__tests__/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@
import request from "supertest";
import { Response, Request } from "express";
import { apiKeyRateLimit, keyGenerator } from "../src/rate-limiter";
import { AutoVerificationResponseBodyType, AutoVerificationRequestBodyType } from "../src/handlers";
import {
AutoVerificationResponseBodyType,
AutoVerificationRequestBodyType,
autoVerificationHandler,
} from "../src/handlers";
import { ParamsDictionary } from "express-serve-static-core";
import { PassportScore } from "@gitcoin/passport-identity";

Expand Down Expand Up @@ -59,7 +63,7 @@ beforeEach(() => {
jest.clearAllMocks();
});

describe("POST /embed/verify", function () {
describe("autoVerificationHandler", function () {
it("handles valid verify requests", async () => {
// as each signature is unique, each request results in unique output
const payload = {
Expand All @@ -76,6 +80,7 @@ describe("POST /embed/verify", function () {

expect(apiKeyRateLimit as jest.Mock).toHaveBeenCalledTimes(1);
expect(keyGenerator as jest.Mock).toHaveBeenCalledTimes(1);
expect(autoVerificationHandler as jest.Mock).toHaveBeenCalledTimes(1);
expect(verifyRequest.status).toBe(200);
expect(verifyRequest.body).toStrictEqual(mockedScore);
});
Expand Down
9 changes: 7 additions & 2 deletions embed/src/handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export type PassportProviderPoints = {
export type AutoVerificationRequestBodyType = {
address: string;
scorerId: string;
credentialIds?: [];
};

type AutoVerificationFields = AutoVerificationRequestBodyType;
Expand Down Expand Up @@ -63,6 +64,7 @@ export const addStampsAndGetScore = async ({
scorerId,
stamps,
}: AutoVerificationFields & { stamps: VerifiableCredential[] }): Promise<PassportScore> => {
console.log("geri ---- addStampsAndGetScore");
try {
const scorerResponse: {
data?: {
Expand All @@ -81,8 +83,10 @@ export const addStampsAndGetScore = async ({
}
);

console.log("geri ---- scorerResponse", scorerResponse);
return scorerResponse.data?.score;
} catch (error) {
console.log("geri ---- scorerResponse error", error);
handleAxiosError(error, "Scorer Embed API", EmbedAxiosError, [apiKey]);
}
};
Expand All @@ -92,13 +96,13 @@ export const autoVerificationHandler = async (
res: Response
): Promise<void> => {
try {
const { address, scorerId } = req.body;
const { address, scorerId, credentialIds } = req.body;

if (!isAddress(address)) {
return void errorRes(res, "Invalid address", 400);
}

const stamps = await autoVerifyStamps({ address, scorerId });
const stamps = await autoVerifyStamps({ address, scorerId, credentialIds });

const score = await addStampsAndGetScore({ address, scorerId, stamps });

Expand All @@ -108,6 +112,7 @@ export const autoVerificationHandler = async (
if (error instanceof EmbedAxiosError) {
return void errorRes(res, error.message, error.code);
}

const message = addErrorDetailsToMessage("Unexpected error when processing request", error);
return void errorRes(res, message, 500);
}
Expand Down

0 comments on commit 6e4ec35

Please sign in to comment.