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

Commit

Permalink
Merge pull request #36 from pagopa/iocit-378-fix
Browse files Browse the repository at this point in the history
[#IOPID-146] Remove duplicated events
  • Loading branch information
gquadrati authored Apr 17, 2023
2 parents a2740ce + a14d8d7 commit a8aa549
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 52 deletions.
123 changes: 88 additions & 35 deletions ReservePubKey/__tests__/handler.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import * as TE from "fp-ts/TaskEither";
import { NonNegativeInteger } from "@pagopa/ts-commons/lib/numbers";
import { NonEmptyString } from "@pagopa/ts-commons/lib/strings";
import { JwkPubKeyHashAlgorithmEnum } from "../../generated/definitions/internal/JwkPubKeyHashAlgorithm";
Expand Down Expand Up @@ -38,34 +39,44 @@ 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]
});
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]
});

const lollipopKeysUtils = require("../../utils/lollipopKeys");
const getAllAssertionsRef = jest.spyOn(
lollipopKeysUtils,
"getAllAssertionsRef"
);

describe("reserveSingleKey", () => {
beforeEach(async () => {
jest.clearAllMocks();
});

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

// By raising an event within reserveSingleKey,
// it will be duplicated most of the times (by default masterAlgo != usedAlgo).
// We will raise just one event from the caller.
expect(loggerMock.trackEvent).toHaveBeenCalledTimes(0);

expect(result).toEqual(
expect.objectContaining({
left: expect.objectContaining({
kind: "IResponseErrorInternal",
detail: expect.stringContaining("COSMOS_ERROR_RESPONSE")
})
})
);
});
});

describe("reservePubKeys", () => {
beforeEach(async () => {
jest.clearAllMocks();
});

test("GIVEN a working model WHEN getAllAssertionsRef fails THEN an internal error is returned", async () => {
const mockedContainer = mockContainer();
mockedContainer.mock.create.mockImplementation(mockCreatePendingLollipop);

getAllAssertionsRef.mockImplementationOnce(() =>
TE.left(Error("an error in getAllAssertionsRef"))
);

const model = new LolliPOPKeysModel(mockedContainer.container);
const pubKey = aSha512PubKey;
const result = await handler.reservePubKeys(model)(pubKey);

expect(loggerMock.trackEvent).toHaveBeenCalledTimes(1);
expect(loggerMock.trackEvent).toHaveBeenCalledWith({
name: "lollipop.error.reserve-pubkey",
properties: {
message: `${FN_LOG_NAME} | COSMOS_ERROR_RESPONSE`,
message: "Error - an error in getAllAssertionsRef"
},
tagOverrides: {
samplingEnabled: "false"
Expand All @@ -119,16 +164,11 @@ describe("reserveSingleKey", () => {

expect(result).toEqual(
expect.objectContaining({
left: expect.objectContaining({
kind: "IResponseErrorInternal",
detail: expect.stringContaining("COSMOS_ERROR_RESPONSE")
})
kind: "IResponseErrorInternal"
})
);
});
});

describe("reservePubKeys", () => {
test("GIVEN a working model WHEN reserve a master pub_key THEN store it and return a redirect containing the assertion ref ", async () => {
const mockedContainer = mockContainer();
mockedContainer.mock.create.mockImplementation(mockCreatePendingLollipop);
Expand Down Expand Up @@ -190,10 +230,17 @@ describe("reservePubKeys", () => {
const pubKey = aSha512PubKey;
const result = await handler.reservePubKeys(model)(pubKey);

expect(loggerMock.trackEvent).toHaveBeenCalledTimes(1);
expect(loggerMock.trackEvent).toHaveBeenCalledWith({
name: "lollipop.error.reserve-pubkey",
properties: {
message: `${FN_LOG_NAME} | COSMOS_ERROR_RESPONSE`,
message: `Error reserving keys: Internal server error: ${JSON.stringify(
{ error: {}, kind: "COSMOS_ERROR_RESPONSE" }
)}`,
masterKey:
"sha512-WbgQ6E5Rzdj1HSBkRQbZ_CMI2O9IDReGkb-CcJIuv7dS8GKWrC4EPxy4rWXfQ9F-JWz-67VYfKRjS3m7uc8wBQ",
usedKey:
"sha512-WbgQ6E5Rzdj1HSBkRQbZ_CMI2O9IDReGkb-CcJIuv7dS8GKWrC4EPxy4rWXfQ9F-JWz-67VYfKRjS3m7uc8wBQ"
},
tagOverrides: {
samplingEnabled: "false"
Expand All @@ -219,10 +266,16 @@ describe("reservePubKeys", () => {
};
const result = await handler.reservePubKeys(model)(pubKey);

expect(loggerMock.trackEvent).toHaveBeenCalledTimes(1);
expect(loggerMock.trackEvent).toHaveBeenCalledWith({
name: "lollipop.error.reserve-pubkey",
properties: {
message: `${FN_LOG_NAME} | COSMOS_ERROR_RESPONSE`,
message: `Error reserving keys: Internal server error: ${JSON.stringify(
{ error: {}, kind: "COSMOS_ERROR_RESPONSE" }
)}`,
masterKey:
"sha512-WbgQ6E5Rzdj1HSBkRQbZ_CMI2O9IDReGkb-CcJIuv7dS8GKWrC4EPxy4rWXfQ9F-JWz-67VYfKRjS3m7uc8wBQ",
usedKey: "sha256-LWmgzxnrIhywpNW0mctCFWfh2CptjGJJN_H2_FLN2fg"
},
tagOverrides: {
samplingEnabled: "false"
Expand Down
31 changes: 14 additions & 17 deletions ReservePubKey/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
ResponseErrorInternal,
ResponseSuccessRedirectToResource
} from "@pagopa/ts-commons/lib/responses";
import { flow, pipe } from "fp-ts/lib/function";
import { pipe } from "fp-ts/lib/function";
import * as TE from "fp-ts/TaskEither";
import { NonEmptyString } from "@pagopa/ts-commons/lib/strings";
import { CosmosErrors } from "@pagopa/io-functions-commons/dist/src/utils/cosmosdb_model";
Expand Down Expand Up @@ -68,12 +68,6 @@ 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 All @@ -88,17 +82,26 @@ export const reservePubKeys = (
inputPubkeys.pub_key
),
eventLog.taskEither.errorLeft(error => [
`${FN_LOG_NAME} | ${error.name} - ${error.message}`,
`${error.name} - ${error.message}`,
{
name: FN_LOG_NAME
}
]),
TE.mapLeft(err => ResponseErrorInternal(err.message)),
TE.chain(
flow(
TE.chain(assertions =>
pipe(
assertions,
R.filter(isDefined),
R.map(reserveSingleKey(lollipopPubkeysModel, inputPubkeys.pub_key)),
A.sequenceS(TE.ApplicativePar)
A.sequenceS(TE.ApplicativePar),
eventLog.taskEither.errorLeft(error => [
`Error reserving keys: ${error.detail}`,
{
masterKey: assertions.master,
name: FN_LOG_NAME,
usedKey: assertions.used ?? assertions.master
}
])
)
),
TE.map(reservedKeys =>
Expand All @@ -118,12 +121,6 @@ export const reservePubKeys = (
newPubKey
)
),
eventLog.taskEither.errorLeft(error => [
`${FN_LOG_NAME} | ${error.kind} ${error.detail}`,
{
name: FN_LOG_NAME
}
]),
TE.toUnion
)();

Expand Down

0 comments on commit a8aa549

Please sign in to comment.