Skip to content

Commit

Permalink
fix: [SIW-1345] fixed createWalletAttestation returned object (#1140)
Browse files Browse the repository at this point in the history
* fixed createWalletAttestation returned object

* fixed lint
  • Loading branch information
silvicir authored Jul 15, 2024
1 parent 0676a95 commit d07463c
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 9 deletions.
9 changes: 9 additions & 0 deletions api_io_wallet.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ paths:
$ref: "#/components/responses/BadRequest"
"403":
$ref: "#/components/responses/Forbidden"
"404":
$ref: "#/components/responses/Forbidden"
"422":
$ref: "#/components/responses/UnprocessableContent"
"500":
Expand All @@ -85,6 +87,13 @@ components:
schema:
$ref: "#/components/schemas/ProblemDetail"

NotFound:
description: The specified resource was not found
content:
application/json:
schema:
$ref: "#/components/schemas/ProblemDetail"

UnprocessableContent:
description: Unprocessable Content
content:
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@
"generate:proxy:api-session": "rimraf generated/session && gen-api-models --api-spec api_session.yaml --out-dir generated/session",
"generate:lollipop-definitions": "rimraf generated/lollipop && gen-api-models --api-spec openapi/lollipop_definitions.yaml --out-dir generated/lollipop",
"generate:lollipop-first-sign": "rimraf generated/lollipop-first-consumer && gen-api-models --api-spec openapi/consumed/lollipop_first_consumer.yaml --out-dir generated/lollipop-first-consumer --request-types --response-decoders --client",
"generate:api:io-wallet": "rimraf generated/io-wallet-api && gen-api-models --api-spec https://raw.githubusercontent.com/pagopa/io-wallet/[email protected].4/apps/io-wallet-user-func/openapi.yaml --no-strict --out-dir generated/io-wallet-api --request-types --response-decoders --client",
"generate:api:io-wallet": "rimraf generated/io-wallet-api && gen-api-models --api-spec https://raw.githubusercontent.com/pagopa/io-wallet/[email protected].21/apps/io-wallet-user-func/openapi.yaml --no-strict --out-dir generated/io-wallet-api --request-types --response-decoders --client",
"generate:proxy:io-wallet-models": "rimraf generated/io-wallet && gen-api-models --api-spec api_io_wallet.yaml --out-dir generated/io-wallet",
"postversion": "git push && git push --tags",
"dist:modules": "modclean -r -n default:safe && yarn install --production",
Expand Down
14 changes: 9 additions & 5 deletions src/controllers/ioWalletController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@ import { sequenceS } from "fp-ts/lib/Apply";

import {
getResponseErrorForbiddenNotAuthorized,
IResponse,
IResponseErrorForbiddenNotAuthorized,
IResponseErrorGeneric,
IResponseErrorInternal,
IResponseErrorNotFound,
IResponseErrorValidation,
IResponseSuccessJson,
IResponseSuccessNoContent,
Expand All @@ -23,7 +24,9 @@ import { pipe } from "fp-ts/lib/function";
import { FiscalCode, NonEmptyString } from "@pagopa/ts-commons/lib/strings";
import { readableReport } from "@pagopa/ts-commons/lib/reporters";
import { UserDetailView } from "../../generated/io-wallet-api/UserDetailView";
import IoWalletService from "../services/ioWalletService";
import IoWalletService, {
IResponseSuccessJwt,
} from "../services/ioWalletService";

import { NonceDetailView } from "../../generated/io-wallet-api/NonceDetailView";
import { withUserFromRequest } from "../types/user";
Expand Down Expand Up @@ -55,7 +58,7 @@ export default class IoWalletController {
| IResponseErrorGeneric
| IResponseErrorValidation
| IResponseSuccessNoContent
| IResponse<"IResponseErrorForbiddenNotAuthorized">
| IResponseErrorForbiddenNotAuthorized
> =>
withUserFromRequest(req, async (user) =>
pipe(
Expand Down Expand Up @@ -97,8 +100,9 @@ export default class IoWalletController {
| IResponseErrorInternal
| IResponseErrorGeneric
| IResponseErrorValidation
| IResponse<"IResponseErrorForbiddenNotAuthorized">
| IResponseSuccessJson<string>
| IResponseErrorForbiddenNotAuthorized
| IResponseSuccessJwt
| IResponseErrorNotFound
> =>
withUserFromRequest(req, async (user) =>
pipe(
Expand Down
19 changes: 18 additions & 1 deletion src/services/__tests__/ioWalletService.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ const mockGetUserByFiscalCode = jest.fn();
const mockCreateWalletInstance = jest.fn();
const mockCreateWalletAttestation = jest.fn();
const mockHealthCheck = jest.fn();
const mockGetCurrentWalletInstanceStatus = jest.fn();
const mockSetWalletInstanceStatus = jest.fn();

mockGetUserByFiscalCode.mockImplementation(() =>
t.success({
Expand Down Expand Up @@ -45,13 +47,28 @@ mockCreateWalletAttestation.mockImplementation(() =>
})
);

mockGetCurrentWalletInstanceStatus.mockImplementation(() =>
t.success({
status: 200,
value: "value",
})
);

mockSetWalletInstanceStatus.mockImplementation(() =>
t.success({
status: 204,
})
);

const api = {
getEntityConfiguration: mockGetEntityConfiguration,
getNonce: mockGetNonce,
getUserByFiscalCode: mockGetUserByFiscalCode,
createWalletInstance: mockCreateWalletInstance,
createWalletAttestation: mockCreateWalletAttestation,
healthCheck: mockHealthCheck,
getCurrentWalletInstanceStatus: mockGetCurrentWalletInstanceStatus,
setWalletInstanceStatus: mockSetWalletInstanceStatus,
};

const mockCreateSubscription = jest.fn();
Expand Down Expand Up @@ -381,7 +398,7 @@ describe("IoWalletService#createWalletAttestation", () => {
);

expect(res).toMatchObject({
kind: "IResponseSuccessJson",
kind: "IResponseSuccessJwt",
});
});

Expand Down
33 changes: 31 additions & 2 deletions src/services/ioWalletService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
*/

import {
getResponseErrorForbiddenNotAuthorized,
HttpStatusCodeEnum,
IResponse,
IResponseErrorForbiddenNotAuthorized,
IResponseErrorGeneric,
IResponseErrorInternal,
IResponseErrorNotFound,
Expand Down Expand Up @@ -36,6 +40,20 @@ import { Subscription } from "../../generated/trial-system-api/Subscription";
const unprocessableContentError = "Unprocessable Content";
const invalidRequest = "Your request didn't validate";

const ResponseSuccessJwt = (o: string): IResponseSuccessJwt => ({
apply: (res) =>
res
.status(HttpStatusCodeEnum.HTTP_STATUS_200)
.set("Content-Type", "application/jwt")
.send(o),
kind: "IResponseSuccessJwt",
value: o,
});

export interface IResponseSuccessJwt extends IResponse<"IResponseSuccessJwt"> {
readonly value: string;
}

export default class IoWalletService {
constructor(
private readonly ioWalletApiClient: ReturnType<IoWalletAPIClient>,
Expand Down Expand Up @@ -150,7 +168,9 @@ export default class IoWalletService {
): Promise<
| IResponseErrorInternal
| IResponseErrorGeneric
| IResponseSuccessJson<string>
| IResponseErrorForbiddenNotAuthorized
| IResponseErrorNotFound
| IResponseSuccessJwt
> =>
withCatchAsInternalError(async () => {
const validated = await this.ioWalletApiClient.createWalletAttestation({
Expand All @@ -163,7 +183,16 @@ export default class IoWalletService {
return withValidatedOrInternalError(validated, (response) => {
switch (response.status) {
case 200:
return ResponseSuccessJson(response.value);
return ResponseSuccessJwt(response.value);
case 403:
return getResponseErrorForbiddenNotAuthorized(
"Wallet instance has been revoked"
);
case 404:
return ResponseErrorNotFound(
"Not Found",
"Wallet instance not found"
);
case 422:
return ResponseErrorGeneric(
response.status,
Expand Down

0 comments on commit d07463c

Please sign in to comment.