Skip to content

Commit

Permalink
feat(Firma con IO): [SFEQS-1520] Update client with metadata (#4473)
Browse files Browse the repository at this point in the history
This PR update FCI client with metadata request. A saga handler has been
added.
---------
Co-authored-by: Fabio Bombardi <[email protected]>
  • Loading branch information
hevelius authored Mar 27, 2023
1 parent 5fe897f commit 3ffb5db
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 3 deletions.
15 changes: 13 additions & 2 deletions ts/features/fci/api/backendFci.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ import {
GetQtspClausesMetadataT,
getQtspClausesMetadataDefaultDecoder,
CreateSignatureT,
createSignatureDefaultDecoder
createSignatureDefaultDecoder,
GetMetadataT,
getMetadataDefaultDecoder
} from "../../../../definitions/fci/requestTypes";
import { SessionToken } from "../../../types/SessionToken";
import {
Expand Down Expand Up @@ -57,6 +59,14 @@ const postSignature: CreateSignatureT = {
response_decoder: createSignatureDefaultDecoder()
};

const getMetadata: GetMetadataT = {
method: "get",
url: () => `/api/v1/sign/metadata`,
headers: composeHeaderProducers(tokenHeaderProducer, ApiHeaderJson),
query: _ => ({}),
response_decoder: getMetadataDefaultDecoder()
};

// client for FCI to handle API communications
export const BackendFciClient = (
baseUrl: string,
Expand Down Expand Up @@ -87,6 +97,7 @@ export const BackendFciClient = (
fetchApi: lpFetch
})
);
}
},
getMetadata: withBearerToken(createFetchRequestForApi(getMetadata, options))
};
};
10 changes: 9 additions & 1 deletion ts/features/fci/saga/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ import {
fciSigningRequest,
fciEndRequest,
fciShowSignedDocumentsStartRequest,
fciShowSignedDocumentsEndRequest
fciShowSignedDocumentsEndRequest,
fciMetadataRequest
} from "../store/actions";
import {
fciQtspClausesMetadataSelector,
Expand All @@ -48,6 +49,7 @@ import { handleGetQtspMetadata } from "./networking/handleGetQtspMetadata";
import { handleCreateFilledDocument } from "./networking/handleCreateFilledDocument";
import { handleDownloadDocument } from "./networking/handleDownloadDocument";
import { handleCreateSignature } from "./networking/handleCreateSignature";
import { handleGetMetadata } from "./networking/handleGetMetadata";

/**
* Handle the FCI Signature requests
Expand Down Expand Up @@ -118,6 +120,12 @@ export function* watchFciSaga(
);

yield* takeLatest(getType(fciEndRequest), watchFciEndSaga);

yield* takeLatest(
getType(fciMetadataRequest.request),
handleGetMetadata,
fciClient.getMetadata
);
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import { testSaga } from "redux-saga-test-plan";
import { left, right } from "fp-ts/lib/Either";
import { getNetworkError } from "../../../../../utils/errors";
import { mockFciMetadata } from "../../../types/__mocks__/Metadata.mock";
import { Metadata } from "../../../../../../definitions/fci/Metadata";
import { handleGetMetadata } from "../handleGetMetadata";
import { fciMetadataRequest } from "../../../store/actions";

const successResponse = {
status: 200,
value: mockFciMetadata as Metadata
};

const failureResponse = {
status: 401
};

describe("handleGetMetadata", () => {
const mockBackendFciClient = jest.fn();
it("it should dispatch fciMetadataRequest.success with the response payload if the response is right and the status code is 200", () => {
testSaga(handleGetMetadata, mockBackendFciClient)
.next()
.call(mockBackendFciClient, {})
.next(right(successResponse))
.put(fciMetadataRequest.success(successResponse.value))
.next()
.isDone();
});
it("it should dispatch fciMetadataRequest.failure with the response status code as payload if the response is right and the status code is different from 200", () => {
testSaga(handleGetMetadata, mockBackendFciClient)
.next()
.call(mockBackendFciClient, {})
.next(right(failureResponse))
.next(
fciMetadataRequest.failure(
getNetworkError(new Error(failureResponse.status.toString()))
)
)
.next()
.isDone();
});
it("it should dispatch fciMetadataRequest.failure with a fixed message as payload if the response left", () => {
testSaga(handleGetMetadata, mockBackendFciClient)
.next()
.call(mockBackendFciClient, {})
.next(left(new Error()))
.next(
fciMetadataRequest.failure(
getNetworkError(new Error("Invalid payload from fciMetadataRequest"))
)
)
.next()
.isDone();
});
it("it should dispatch fciMetadataRequest.failure with the error message as payload if an exception is raised", () => {
const mockedError = new Error("mockedErrorMessage");
testSaga(handleGetMetadata, mockBackendFciClient)
.next()
.call(mockBackendFciClient, {})
.throw(mockedError)
.next(fciMetadataRequest.failure(getNetworkError(mockedError)))
.next()
.isDone();
});
});
31 changes: 31 additions & 0 deletions ts/features/fci/saga/networking/handleGetMetadata.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { SagaIterator } from "redux-saga";
import { call, put } from "typed-redux-saga/macro";
import * as E from "fp-ts/lib/Either";
import { readablePrivacyReport } from "../../../../utils/reporters";
import { BackendFciClient } from "../../api/backendFci";
import { fciMetadataRequest } from "../../store/actions";
import { getNetworkError } from "../../../../utils/errors";

/*
* A saga to load a QTSP metadata.
*/
export function* handleGetMetadata(
getMetadata: ReturnType<typeof BackendFciClient>["getMetadata"]
): SagaIterator {
try {
const getMetadataResponse = yield* call(getMetadata, {});

if (E.isLeft(getMetadataResponse)) {
throw Error(readablePrivacyReport(getMetadataResponse.left));
}

if (getMetadataResponse.right.status === 200) {
yield* put(fciMetadataRequest.success(getMetadataResponse.right.value));
return;
}

throw Error(`response status ${getMetadataResponse.right.status}`);
} catch (e) {
yield* put(fciMetadataRequest.failure(getNetworkError(e)));
}
}

0 comments on commit 3ffb5db

Please sign in to comment.