Skip to content
This repository has been archived by the owner on Jan 15, 2025. It is now read-only.

Commit

Permalink
added tests
Browse files Browse the repository at this point in the history
  • Loading branch information
michaeldisaro authored and gquadrati committed Apr 12, 2023
1 parent 130a578 commit c36c9ec
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 0 deletions.
65 changes: 65 additions & 0 deletions ReservePubKey/__tests__/handler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ import {
} from "../../__mocks__/lollipopkeysMock";
import * as handler from "../handler";
import { AssertionRef } from "../../generated/definitions/external/AssertionRef";
import { contextMock } from "../../__mocks__/context.mock";
import { useWinstonFor } from "@pagopa/winston-ts";
import { LoggerId } from "@pagopa/winston-ts/dist/types/logging";
import { withApplicationInsight } from "@pagopa/io-functions-commons/dist/src/utils/transports/application_insight";
import { AzureContextTransport } from "@pagopa/io-functions-commons/dist/src/utils/logging";
import { TelemetryClient } from "applicationinsights";

const mockCreatePendingLollipop = (pendingLollipop: PendingLolliPopPubKeys) =>
Promise.resolve({
Expand All @@ -32,6 +38,33 @@ const mockCreatePendingLollipop = (pendingLollipop: PendingLolliPopPubKeys) =>
}
});

const FN_LOG_NAME = "reserve-pubkey";

const loggerMock = {
trackEvent: jest.fn(e => {
return void 0;
})
};

const azureContextTransport = new AzureContextTransport(
() => contextMock.log,
{}
);
useWinstonFor({
loggerId: LoggerId.event,
transports: [
withApplicationInsight(
(loggerMock as unknown) as TelemetryClient,
"lollipop"
),
azureContextTransport
]
});
useWinstonFor({
loggerId: LoggerId.default,
transports: [azureContextTransport]
});

describe("reserveSingleKey", () => {
test("GIVEN a working model WHEN reserve a pub_key THEN call the cosmos create and return the RetriveLollipop", async () => {
const mockedContainer = mockContainer();
Expand Down Expand Up @@ -74,6 +107,16 @@ describe("reserveSingleKey", () => {
pubKey.pub_key
)(assertionRef)();

expect(loggerMock.trackEvent).toHaveBeenCalledWith({
name: "lollipop.error.reserve-pubkey",
properties: {
message: `${FN_LOG_NAME} | COSMOS_ERROR_RESPONSE`,
},
tagOverrides: {
samplingEnabled: "false"
}
});

expect(result).toEqual(
expect.objectContaining({
left: expect.objectContaining({
Expand Down Expand Up @@ -146,6 +189,17 @@ describe("reservePubKeys", () => {
const model = new LolliPOPKeysModel(mockedContainer.container);
const pubKey = aSha512PubKey;
const result = await handler.reservePubKeys(model)(pubKey);

expect(loggerMock.trackEvent).toHaveBeenCalledWith({
name: "lollipop.error.reserve-pubkey",
properties: {
message: `${FN_LOG_NAME} | COSMOS_ERROR_RESPONSE`,
},
tagOverrides: {
samplingEnabled: "false"
}
});

expect(mockedContainer.mock.create).toHaveBeenCalledTimes(1);
expect(result).toEqual(
expect.objectContaining({
Expand All @@ -164,6 +218,17 @@ describe("reservePubKeys", () => {
algo: JwkPubKeyHashAlgorithmEnum.sha256
};
const result = await handler.reservePubKeys(model)(pubKey);

expect(loggerMock.trackEvent).toHaveBeenCalledWith({
name: "lollipop.error.reserve-pubkey",
properties: {
message: `${FN_LOG_NAME} | COSMOS_ERROR_RESPONSE`,
},
tagOverrides: {
samplingEnabled: "false"
}
});

expect(mockedContainer.mock.create).toHaveBeenCalledTimes(2);
expect(result).toEqual(
expect.objectContaining({
Expand Down
6 changes: 6 additions & 0 deletions ReservePubKey/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,12 @@ export const reserveSingleKey = (
pendingPubKey =>
pipe(
lollipopPubkeysModel.create(pendingPubKey),
eventLog.taskEither.errorLeft(error => [
`${FN_LOG_NAME} | ${error.kind}`,
{
name: FN_LOG_NAME
}
]),
TE.mapLeft(cosmosErrorsToResponse)
)
);
Expand Down

0 comments on commit c36c9ec

Please sign in to comment.