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

Commit

Permalink
[#iocit-378] fix based on comments
Browse files Browse the repository at this point in the history
  • Loading branch information
gquadrati committed Apr 17, 2023
1 parent d0251f2 commit a14d8d7
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 7 deletions.
25 changes: 22 additions & 3 deletions ReservePubKey/__tests__/handler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,9 @@ 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(
Expand All @@ -141,14 +144,23 @@ describe("reservePubKeys", () => {
mockedContainer.mock.create.mockImplementation(mockCreatePendingLollipop);

getAllAssertionsRef.mockImplementationOnce(() =>
TE.left(Error("an error"))
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: "Error - an error in getAllAssertionsRef"
},
tagOverrides: {
samplingEnabled: "false"
}
});

expect(result).toEqual(
expect.objectContaining({
Expand Down Expand Up @@ -224,7 +236,11 @@ describe("reservePubKeys", () => {
properties: {
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 Down Expand Up @@ -256,7 +272,10 @@ describe("reservePubKeys", () => {
properties: {
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
11 changes: 7 additions & 4 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 @@ -88,15 +88,18 @@ export const reservePubKeys = (
}
]),
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),
eventLog.taskEither.errorLeft(error => [
`Error reserving keys: ${error.detail}`,
{
name: FN_LOG_NAME
masterKey: assertions.master,
name: FN_LOG_NAME,
usedKey: assertions.used ?? assertions.master
}
])
)
Expand Down

0 comments on commit a14d8d7

Please sign in to comment.