CEC-2920 Aftersales certificates (#225)
* CEC-2920 aftersales certificates * smells * smells
This commit is contained in:
@@ -1,21 +1,25 @@
|
||||
import React, { useContext, useState } from "react";
|
||||
|
||||
import api from "../../services/certificatesAPI";
|
||||
import { CertTypes } from "../../utils/certificates";
|
||||
|
||||
const CertificateContext = React.createContext();
|
||||
|
||||
export const CertTypes = {
|
||||
TBOX: "TBOX",
|
||||
ICC: "ICC",
|
||||
Charging: "Charging",
|
||||
Aftersales: "Aftersales",
|
||||
};
|
||||
|
||||
const validateCreate = (data) => {
|
||||
if (!data.type) throw new Error("type is required");
|
||||
if (!data.common_name) throw new Error("common name is required");
|
||||
};
|
||||
|
||||
const callCreateCert = (data, token) => {
|
||||
if (data.type === CertTypes.Aftersales) {
|
||||
data.tool_id = data.common_name;
|
||||
delete data.common_name;
|
||||
return api.createAftersales(data, token);
|
||||
}
|
||||
|
||||
return api.create(data, token);
|
||||
};
|
||||
|
||||
export const CertificateProvider = ({ children }) => {
|
||||
const [busy, setBusy] = useState(false);
|
||||
|
||||
@@ -25,7 +29,8 @@ export const CertificateProvider = ({ children }) => {
|
||||
|
||||
validateCreate(data);
|
||||
|
||||
const result = await api.create(data, token);
|
||||
const result = await callCreateCert(data, token);
|
||||
|
||||
if (result.error) {
|
||||
throw new Error(`Create certificate error. ${result.message}`);
|
||||
}
|
||||
|
||||
105
src/components/Contexts/CertificateContext.test.jsx
Normal file
105
src/components/Contexts/CertificateContext.test.jsx
Normal file
@@ -0,0 +1,105 @@
|
||||
jest.mock("../../services/certificatesAPI");
|
||||
|
||||
import {
|
||||
act,
|
||||
cleanup,
|
||||
fireEvent,
|
||||
render,
|
||||
screen,
|
||||
waitFor,
|
||||
} from "@testing-library/react";
|
||||
import { useState } from "react";
|
||||
|
||||
import {
|
||||
CertificateProvider,
|
||||
useCertificateContext,
|
||||
} from "./CertificateContext";
|
||||
|
||||
describe("CertificateContext", () => {
|
||||
beforeEach(() => {
|
||||
const TestComp = () => {
|
||||
const { createCert } = useCertificateContext();
|
||||
const [result, setResult] = useState(null);
|
||||
const testCreate = async (data) => {
|
||||
const x = await createCert(data);
|
||||
setResult(JSON.stringify(x));
|
||||
};
|
||||
|
||||
return (
|
||||
<>
|
||||
<div data-testid="output">{result}</div>
|
||||
<button
|
||||
data-testid="createCertTBOX"
|
||||
onClick={async () => {
|
||||
await testCreate({
|
||||
common_name: "11111111111111111",
|
||||
type: "TBOX",
|
||||
});
|
||||
}}
|
||||
/>
|
||||
<button
|
||||
data-testid="createCertICC"
|
||||
onClick={async () => {
|
||||
await testCreate({
|
||||
common_name: "11111111111111111",
|
||||
type: "ICC",
|
||||
});
|
||||
}}
|
||||
/>
|
||||
<button
|
||||
data-testid="createCertAftersales"
|
||||
onClick={async () => {
|
||||
await testCreate({
|
||||
common_name: "11111111111111112",
|
||||
type: "Aftersales",
|
||||
});
|
||||
}}
|
||||
/>
|
||||
</>
|
||||
);
|
||||
};
|
||||
|
||||
render(
|
||||
<CertificateProvider>
|
||||
<TestComp />
|
||||
</CertificateProvider>
|
||||
);
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
cleanup();
|
||||
});
|
||||
|
||||
it("TBOX certificate", async () => {
|
||||
await act(async () => {
|
||||
fireEvent.click(screen.getByTestId("createCertTBOX"));
|
||||
await waitFor(() =>
|
||||
expect(screen.getByTestId("output").innerHTML).toBe(
|
||||
'{"private_key":"-----BEGIN RSA PRIVATE KEY-----DATA-----END RSA PRIVATE KEY-----","public_key":"-----BEGIN CERTIFICATE-----DATA-----END CERTIFICATE-----","serial_number":"00:76:d2:ca:86:35:8c:88:52:fd:94:8e:55:d3:b6:a8:2e:73:68:00","type":"TBOX"}'
|
||||
)
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
it("ICC certificate", async () => {
|
||||
await act(async () => {
|
||||
fireEvent.click(screen.getByTestId("createCertICC"));
|
||||
await waitFor(() =>
|
||||
expect(screen.getByTestId("output").innerHTML).toBe(
|
||||
'{"private_key":"-----BEGIN RSA PRIVATE KEY-----DATA-----END RSA PRIVATE KEY-----","public_key":"-----BEGIN CERTIFICATE-----DATA-----END CERTIFICATE-----","serial_number":"00:76:d2:ca:86:35:8c:88:52:fd:94:8e:55:d3:b6:a8:2e:73:68:00","type":"ICC"}'
|
||||
)
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
it("Aftersales certificate", async () => {
|
||||
await act(async () => {
|
||||
fireEvent.click(screen.getByTestId("createCertAftersales"));
|
||||
await waitFor(() =>
|
||||
expect(screen.getByTestId("output").innerHTML).toBe(
|
||||
'{"private_key":"-----BEGIN RSA PRIVATE KEY-----DATA-----END RSA PRIVATE KEY-----","public_key":"-----BEGIN CERTIFICATE-----DATA-----END CERTIFICATE-----","serial_number":"00:76:d2:ca:86:35:8c:88:52:fd:94:8e:55:d3:b6:a8:2e:73:68:00","type":"Aftersales"}'
|
||||
)
|
||||
);
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user