Skip to content

Commit

Permalink
test: add e2e test for demo config workflow
Browse files Browse the repository at this point in the history
  • Loading branch information
MinHtet-O committed Jun 10, 2024
1 parent 10ac6f2 commit c2a562d
Show file tree
Hide file tree
Showing 9 changed files with 113 additions and 15 deletions.
2 changes: 1 addition & 1 deletion .testcaferc.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"appCommand": "npm run serve-static",
"src": ["integration/**/*.spec.ts"],
"src": ["integration/**/**.spec.ts"],
"cache": true,
"compilerOptions": {
"typescript": {
Expand Down
2 changes: 1 addition & 1 deletion integration/helper.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Selector, t } from "testcafe";

const PasswordField = Selector("[data-testid='password-field']");
const ButtonLogin = Selector("[data-testid='login-button']");
export const ButtonLogin = Selector("[data-testid='login-button']");
const ConfigFileDropZoneInput = Selector("[data-testid='config-file-dropzone'] input");
// fixtures

Expand Down
59 changes: 59 additions & 0 deletions integration/load-demo-config.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import { Selector } from "testcafe";

const LoadDemoConfigFileBtn = Selector("[data-testid='load-demo-config-button']");
const FillFormTitle = Selector("[data-testid='fill-form-title']");
const ProgressBar = Selector("[data-testid='progress-bar']");
const EblBeneficiaryField = Selector("[data-testid='transferable-record-beneficiary-input']");
const EblHolderField = Selector("[data-testid='transferable-record-holder-input']");
const SubmitButton = Selector("[data-testid='form-submit-button']");
const EblNumberField = Selector("input#root_blNumber");
const EblScacField = Selector("input#root_scac");
const ProcessTitle = Selector('[data-testid="process-title"]');
const IssuedFileName = Selector('[data-testid="file-name"]');
const DocumentDownloadBtn = Selector('[data-testid="download-file-button"]');
const ProcessAnotherDocBtn = Selector('[data-testid="process-another-document-button"]');

fixture("Demo Config File").page(`http://localhost:3000`);
const Button = Selector("button");

test("can able to load demo config file and issue example documents", async (t) => {
// ################ 1. Load Demo Config
// load demo config file
await t.click(LoadDemoConfigFileBtn);
await t.expect(ProgressBar.textContent).contains("1");

// ################ 2. Issue Bill of Lading Document
// select Bill of Laiding Document
await t.click(Button.withText("TradeTrust Bill of Lading v2 (Carrier)"));
await t.expect(FillFormTitle.textContent).contains("Fill and Preview Form");
await t.expect(ProgressBar.textContent).contains("2");

// fill the form
await t.typeText(EblBeneficiaryField, "0xF11e3850F0bb8C72925c329EC446a2026cd4bB94");
await t.typeText(EblHolderField, "0xF11e3850F0bb8C72925c329EC446a2026cd4bB94");
await t.typeText(EblNumberField, "MY-BL-NUMBER");
await t.typeText(EblScacField, "My-EBL-SCAC");
// submit form
await t.click(SubmitButton);

// check if bill of lading is created
await t.expect(ProcessTitle.innerText).eql("Document(s) issued successfully", { timeout: 20000 }); // cater for file issue delay
await t.expect(IssuedFileName.textContent).contains("TradeTrust-Bill-of-Lading-v2-(Carrier)-1-stability.tt");
await t.expect(DocumentDownloadBtn.exists).ok("Download button for document doesn't exist");

// ################ 3. Issue ChAFTA Certificate of Origin
await t.click(ProcessAnotherDocBtn);
await t.click(Button.withText("TradeTrust ChAFTA Certificate of Origin v2"));
await t.click(SubmitButton);
await t.expect(ProcessTitle.innerText).eql("Document(s) issued successfully", { timeout: 20000 }); // cater for file issuance delay
await t.expect(IssuedFileName.textContent).contains("TradeTrust-ChAFTA-Certificate-of-Origin-v2-1-stability.tt");
await t.expect(DocumentDownloadBtn.exists).ok("Download button for document doesn't exist");

// ################ 4. Issue TradeTrust Invoice DNS-DID
await t.click(ProcessAnotherDocBtn);
await t.click(Button.withText("TradeTrust Invoice v2 (DNS-DID)"));
await t.click(SubmitButton);
await t.expect(ProcessTitle.innerText).eql("Document(s) issued successfully", { timeout: 20000 }); // cater for file issuance delay
await t.expect(IssuedFileName.textContent).contains("TradeTrust-Invoice-v2-(DNS-DID)-1-stability.tt");
await t.expect(DocumentDownloadBtn.exists).ok("Download button for document doesn't exist");
});
1 change: 1 addition & 0 deletions src/common/config/decrypt.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export const decryptWalletOrSigner = async (
};

const provider = config.network === "local" ? new providers.JsonRpcProvider() : utils.generateProvider(opts);

if (isWalletOption(config.wallet)) {
// For backward compatibility when the wallet is still string
return decryptEncryptedJson(config.wallet, password, progressCallback, provider);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ export const ConfigFileDropZone: FunctionComponent<ConfigFileDropZone> = ({ onCo
</div>
<div className="border-2 border-gray-200 border-dashed h-10 hidden sm:block" />
<Button
data-testid="load-demo-config-button"
onClick={(e) => {
e.stopPropagation();
setFileErrors([]);
Expand Down
3 changes: 1 addition & 2 deletions src/components/Home/Home.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@ import { ConfigFileDropZoneContainer } from "./ConfigFileDropZone";
import { WalletDecryptionContainer } from "./WalletDecryption/WalletDecryptionContainer";

export const HomeContainer: FunctionComponent = () => {
const { config, isDemo } = useConfigContext();
const { config } = useConfigContext();
const { configFile } = usePersistedConfigFile();
console.log(`Isdemo from Home Container: ${isDemo}`);
// If wallet has been decrypted, redirect to forms
if (config) return <Redirect to="/forms-selection" />;

Expand Down
35 changes: 26 additions & 9 deletions src/components/Home/WalletDecryption/WalletDecryptionContainer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { useConfigContext } from "../../../common/context/config";
import { usePersistedConfigFile } from "../../../common/hook/usePersistedConfigFile";
import { WalletDecryption } from "./WalletDecryption";
import { getLogger } from "../../../utils/logger";
import { DEMO_PASSWD } from "../../../constants/demo-config";
import { loadDemoWallet } from "./loadDemoWallet";

const { stack } = getLogger("Wallet Decryption Container");

Expand Down Expand Up @@ -41,17 +41,34 @@ export const WalletDecryptionContainer: FunctionComponent = () => {
},
[configFile, setConfig, setDecryptProgress, setIsIncorrectPassword]
);

const loadDemo = useCallback(async (): Promise<void> => {
// use demo wallet and config
if (configFile) {
const walletOrSigner = await loadDemoWallet(configFile);
setConfig({
network: configFile.network,
wallet: walletOrSigner,
forms: configFile.forms,
documentStorage: configFile.documentStorage,
});
}
}, [configFile, setConfig]);

useEffect(() => {
if (isDemo && configFile) {
onDecryptConfigFile(DEMO_PASSWD);
loadDemo();
}
}, [isDemo, configFile, onDecryptConfigFile]);
}, [loadDemo, isDemo, configFile]);
return (
<WalletDecryption
decryptProgress={decryptProgress}
isIncorrectPassword={isIncorrectPassword}
onDecryptConfigFile={onDecryptConfigFile}
onResetConfigFile={onResetConfigFile}
/>
<>
<b>{decryptProgress}</b>
<WalletDecryption
decryptProgress={decryptProgress}
isIncorrectPassword={isIncorrectPassword}
onDecryptConfigFile={onDecryptConfigFile}
onResetConfigFile={onResetConfigFile}
/>
</>
);
};
21 changes: 21 additions & 0 deletions src/components/Home/WalletDecryption/loadDemoWallet.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { ProviderDetails, utils } from "@tradetrust-tt/tt-verify";
import { getChainInfoFromNetworkName } from "../../../common/utils";
import { ChainInfo } from "../../../constants/chainInfo";
import { ConfigFile, ConnectedSigner } from "../../../types";
import { Wallet, ethers, providers } from "ethers";
import { DEMO_PRIVATE_KEY } from "../../../constants/demo-config";

export const loadDemoWallet = async (config: ConfigFile): Promise<Wallet | ConnectedSigner> => {
const chainId = getChainInfoFromNetworkName(config.network).chainId;
const rpcUrl = ChainInfo[chainId].rpcUrl;
const opts: ProviderDetails = rpcUrl
? { url: rpcUrl }
: {
network: config.network,
providerType: "infura",
};

const provider = config.network === "local" ? new providers.JsonRpcProvider() : utils.generateProvider(opts);
const wallet = new ethers.Wallet(DEMO_PRIVATE_KEY);
return wallet.connect(provider);
};
4 changes: 2 additions & 2 deletions src/constants/demo-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ export const DEMO_CONFIG = {
wallet: {
type: "ENCRYPTED_JSON",
encryptedJson:
'{"address":"f11e3850f0bb8c72925c329ec446a2026cd4bb94","id":"d3993295-32ed-40e1-8e44-3ca8bd84fbeb","version":3,"crypto":{"cipher":"aes-128-ctr","cipherparams":{"iv":"91325d439195204264b16f5a8ea33e80"},"ciphertext":"6608eace3ffbf32e898919ac00babe014aab140ffd6eebd4c5b0e71577378e51","kdf":"scrypt","kdfparams":{"salt":"6f3ea4ca2f20b5e3a851738e719f576107462d0f48fd02efffe20e15bab3dbfa","n":131072,"dklen":32,"p":1,"r":8},"mac":"c7418015ab04f60c9cf58d9670799da0e8ea00bb893e61ca73fa4ca25756aecc"},"x-ethers":{"client":"ethers.js","gethFilename":"UTC--2024-06-06T07-32-36.0Z--f11e3850f0bb8c72925c329ec446a2026cd4bb94","mnemonicCounter":"690521b75992f909b116f7b75258bbe2","mnemonicCiphertext":"681a05475978f5498bc63add4a2f79a9","path":"m/44\'/60\'/0\'/0/0","locale":"en","version":"0.1"}}',
'{"address":"f11e3850f0bb8c72925c329ec446a2026cd4bb94","id":"d3993295-32ed-40e1-8e44-3ca8bd84fbeb","version":3,"crypto":{"cipher":"aes-128-ctr","cipherparams":{"iv":"91325d439195204264b16f5a8ea33e80"},"ciphertext":"6608eace3ffbf32e898919ac00babe014aab140ffd6eebd4c5b0e71577378e51","kdf":"scrypt","kdfparams":{"salt":"6f3ea4ca2f20b5e3a851738e719f576107462d0f48fd02efffe20e15bab3dbfa","n":131072,"dklen":32,"p":1,"r":8},"mac":"c7418015ab04f60c9cf58d9670799da0e8ea00bb893e61ca73fa4ca25756aecc"}}',
},
forms: [
{
Expand Down Expand Up @@ -849,4 +849,4 @@ export const DEMO_CONFIG = {
],
};

export const DEMO_PASSWD = "tradetrust-tt/tradetrust";
export const DEMO_PRIVATE_KEY = "2094fe88ad9d99fd3691af27adcf2fce39789e19f3d04119f1f8c258b5c678c3";

0 comments on commit c2a562d

Please sign in to comment.