Skip to content
This repository has been archived by the owner on Mar 23, 2023. It is now read-only.

Commit

Permalink
refactor: adjust tests checking for wrong mnemonics (#4030)
Browse files Browse the repository at this point in the history
  • Loading branch information
dated committed Jul 6, 2021
1 parent f17042a commit d363a47
Show file tree
Hide file tree
Showing 19 changed files with 612 additions and 762 deletions.
Expand Up @@ -256,49 +256,6 @@ describe("DelegateRegistrationForm", () => {
transactionMock.mockRestore();
});

it("should error if signing fails", async () => {
const form = {
clearErrors: jest.fn(),
getValues: () => ({
fee: "1",
mnemonic: MNEMONICS[0],
senderAddress: wallet.address(),
username: "test_delegate",
}),
setError: jest.fn(),
setValue: jest.fn(),
};
const consoleSpy = jest.spyOn(console, "error").mockImplementation(() => void 0);
const signMock = jest.spyOn(wallet.transaction(), "signDelegateRegistration").mockImplementation(() => {
throw new Error("Signing failed");
});
const broadcastMock = jest.spyOn(wallet.transaction(), "broadcast").mockResolvedValue({
accepted: [delegateRegistrationFixture.data.id],
errors: {},
rejected: [],
});
const transactionMock = createTransactionMock(wallet);

try {
await DelegateRegistrationForm.signTransaction({
env,
form,
profile,
});
// eslint-disable-next-line
} catch (error) {}

await waitFor(() => expect(signMock).toThrow());

expect(broadcastMock).not.toHaveBeenCalled();
expect(transactionMock).not.toHaveBeenCalled();

consoleSpy.mockRestore();
signMock.mockRestore();
broadcastMock.mockRestore();
transactionMock.mockRestore();
});

it("should output transaction details", () => {
const translations = jest.fn((translation) => translation);
const transaction = {
Expand Down
Expand Up @@ -257,52 +257,6 @@ describe("MultiSignature Registration Form", () => {
transactionMock.mockRestore();
});

it("should error if signing fails", async () => {
const form = {
clearErrors: jest.fn(),
getValues: () => ({
fee: "1",
minParticipants: 2,
mnemonic: MNEMONICS[0],
participants: [
{
address: wallet.address(),
publicKey: wallet.publicKey()!,
},
{
address: wallet2.address(),
publicKey: wallet2.publicKey()!,
},
],
senderAddress: wallet.address(),
}),
setError: jest.fn(),
setValue: jest.fn(),
};
const consoleSpy = jest.spyOn(console, "error").mockImplementation(() => void 0);
const signMock = jest.spyOn(wallet.transaction(), "signMultiSignature").mockImplementation(() => {
throw new Error("Signing failed");
});
const transactionMock = createTransactionMock(wallet);

try {
await MultiSignatureRegistrationForm.signTransaction({
env,
form,
profile,
});
// eslint-disable-next-line
} catch (error) {}

await waitFor(() => expect(signMock).toThrow());

expect(transactionMock).not.toHaveBeenCalled();

consoleSpy.mockRestore();
signMock.mockRestore();
transactionMock.mockRestore();
});

it("should sign transaction using encryption password", async () => {
const walletUsesWIFMock = jest.spyOn(wallet.wif(), "exists").mockReturnValue(true);
const walletWifMock = jest.spyOn(wallet.wif(), "get").mockImplementation(() => {
Expand Down
Expand Up @@ -354,49 +354,6 @@ describe("SecondSignatureRegistrationForm", () => {
transactionMock.mockRestore();
});

it("should error if signing fails", async () => {
const form = {
clearErrors: jest.fn(),
getValues: () => ({
fee: "1",
mnemonic: MNEMONICS[0],
secondMnemonic: MNEMONICS[1],
senderAddress: wallet.address(),
}),
setError: jest.fn(),
setValue: jest.fn(),
};

const consoleSpy = jest.spyOn(console, "error").mockImplementation(() => void 0);
const signMock = jest.spyOn(wallet.transaction(), "signIpfs").mockImplementation(() => {
throw new Error("Signing failed");
});
const broadcastMock = jest.spyOn(wallet.transaction(), "broadcast").mockResolvedValue({
accepted: [secondSignatureFixture.data.id],
errors: {},
rejected: [],
});
const transactionMock = createTransactionMock(wallet);

try {
await SecondSignatureRegistrationForm.signTransaction({
env,
form,
profile,
});
// eslint-disable-next-line
} catch (error) {}

await waitFor(() => expect(signMock).toThrow());

expect(broadcastMock).not.toHaveBeenCalled();
expect(transactionMock).not.toHaveBeenCalled();

consoleSpy.mockRestore();
signMock.mockRestore();
broadcastMock.mockRestore();
});

it("should sign transaction using encryption password", async () => {
const walletUsesWIFMock = jest.spyOn(wallet.wif(), "exists").mockReturnValue(true);
const walletWifMock = jest.spyOn(wallet.wif(), "get").mockImplementation(() => {
Expand Down
Expand Up @@ -206,10 +206,6 @@ describe("SendDelegateResignation", () => {
});

it("should show mnemonic authentication error", async () => {
const signMock = jest.spyOn(wallet.transaction(), "signDelegateResignation").mockImplementation(() => {
throw new Error("Signatory should be");
});

const secondPublicKeyMock = jest
.spyOn(wallet, "secondPublicKey")
.mockReturnValue((await wallet.coin().publicKey().fromMnemonic(MNEMONICS[1])).publicKey);
Expand All @@ -233,69 +229,22 @@ describe("SendDelegateResignation", () => {

fireEvent.input(getByTestId("AuthenticationStep__second-mnemonic"), {
target: {
value: MNEMONICS[1],
value: MNEMONICS[2],
},
});
await waitFor(() => expect(getByTestId("AuthenticationStep__second-mnemonic")).toHaveValue(MNEMONICS[1]));
await waitFor(() => expect(getByTestId("AuthenticationStep__second-mnemonic")).toHaveValue(MNEMONICS[2]));

fireEvent.click(getByTestId("StepNavigation__send-button"));

await waitFor(() => expect(getByTestId("AuthenticationStep__mnemonic")).toHaveAttribute("aria-invalid"));
await waitFor(() => expect(getByTestId("StepNavigation__send-button")).toBeDisabled());
expect(getByTestId("AuthenticationStep__second-mnemonic")).toHaveAttribute("aria-invalid");
expect(getByTestId("StepNavigation__send-button")).toBeDisabled();

expect(getByTestId("AuthenticationStep")).toBeTruthy();
expect(asFragment()).toMatchSnapshot();

secondPublicKeyMock.mockRestore();
signMock.mockRestore();
});

it("should show error step", async () => {
const signMock = jest.spyOn(wallet.transaction(), "signDelegateResignation").mockImplementation(() => {
throw new Error();
});

const secondPublicKeyMock = jest
.spyOn(wallet, "secondPublicKey")
.mockReturnValue((await wallet.coin().publicKey().fromMnemonic(MNEMONICS[1])).publicKey);

const { asFragment, getByTestId } = renderPage();

await waitFor(() => expect(getByTestId("SendDelegateResignation__form-step")).toBeTruthy());

fireEvent.click(getByTestId("StepNavigation__continue-button"));
await waitFor(() => expect(getByTestId("SendDelegateResignation__review-step")).toBeTruthy());

fireEvent.click(getByTestId("StepNavigation__continue-button"));
await waitFor(() => expect(getByTestId("AuthenticationStep")).toBeTruthy());

fireEvent.input(getByTestId("AuthenticationStep__mnemonic"), {
target: {
value: passphrase,
},
});
await waitFor(() => expect(getByTestId("AuthenticationStep__mnemonic")).toHaveValue(passphrase));

fireEvent.input(getByTestId("AuthenticationStep__second-mnemonic"), {
target: {
value: MNEMONICS[1],
},
});
await waitFor(() => expect(getByTestId("AuthenticationStep__second-mnemonic")).toHaveValue(MNEMONICS[1]));

fireEvent.click(getByTestId("StepNavigation__send-button"));

await waitFor(() => expect(getByTestId("ErrorStep")).toBeTruthy());

expect(asFragment()).toMatchSnapshot();

secondPublicKeyMock.mockRestore();
signMock.mockRestore();
});

it("should show error step and go back", async () => {
const signMock = jest.spyOn(wallet.transaction(), "signDelegateResignation").mockImplementation(() => {
throw new Error();
const broadcastMock = jest.spyOn(wallet.transaction(), "broadcast").mockImplementation(() => {
throw new Error("broadcast error");
});

const secondPublicKeyMock = jest
Expand Down Expand Up @@ -330,6 +279,7 @@ describe("SendDelegateResignation", () => {

await waitFor(() => expect(getByTestId("ErrorStep")).toBeTruthy());

expect(getByTestId("ErrorStep__errorMessage")).toHaveTextContent("broadcast error");
expect(asFragment()).toMatchSnapshot();

const historyMock = jest.spyOn(history, "push").mockReturnValue();
Expand All @@ -342,7 +292,7 @@ describe("SendDelegateResignation", () => {
historyMock.mockRestore();

secondPublicKeyMock.mockRestore();
signMock.mockRestore();
broadcastMock.mockRestore();
});

it("should successfully sign and submit resignation transaction", async () => {
Expand Down
Expand Up @@ -10,21 +10,19 @@ import { AuthenticationStep } from "domains/transaction/components/Authenticatio
import { ErrorStep } from "domains/transaction/components/ErrorStep";
import { FeeWarning } from "domains/transaction/components/FeeWarning";
import { useFeeConfirmation, useWalletSignatory } from "domains/transaction/hooks";
import { handleBroadcastError, isMnemonicError } from "domains/transaction/utils";
import { handleBroadcastError } from "domains/transaction/utils";
import React, { useEffect, useState } from "react";
import { useForm } from "react-hook-form";
import { useTranslation } from "react-i18next";
import { useHistory } from "react-router-dom";

import { FormStep, ReviewStep, SummaryStep } from ".";

export const SendDelegateResignation = () => {
const { t } = useTranslation();
const history = useHistory();

const form = useForm({ mode: "onChange" });

const { formState, getValues, register, setError, setValue, watch } = form;
const { formState, getValues, register, watch } = form;
const { isValid, isSubmitting } = formState;

const { fee, fees } = watch();
Expand Down Expand Up @@ -102,11 +100,6 @@ export const SendDelegateResignation = () => {

handleNext();
} catch (error) {
if (isMnemonicError(error)) {
setValue("mnemonic", "");
return setError("mnemonic", { message: t("TRANSACTION.INVALID_MNEMONIC"), type: "manual" });
}

setErrorMessage(JSON.stringify({ message: error.message, type: error.name }));
setActiveTab(5);
}
Expand Down

0 comments on commit d363a47

Please sign in to comment.