-
Notifications
You must be signed in to change notification settings - Fork 107
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(Firma con IO): [SFEQS-1520] Update client with metadata (#4473)
This PR update FCI client with metadata request. A saga handler has been added. --------- Co-authored-by: Fabio Bombardi <[email protected]>
- Loading branch information
Showing
4 changed files
with
118 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
ts/features/fci/saga/networking/__tests__/handleGetMetadata.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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))); | ||
} | ||
} |