2021-02-04 19:15:23 +01:00
|
|
|
import React from 'react';
|
|
|
|
import sinon from 'sinon';
|
2023-06-20 15:37:09 +02:00
|
|
|
import { fireEvent, screen, act } from '@testing-library/react';
|
2021-02-04 19:15:23 +01:00
|
|
|
import configureMockStore from 'redux-mock-store';
|
2022-08-08 22:28:49 +02:00
|
|
|
import { renderWithProvider } from '../../../../test/lib/render-helpers';
|
2021-03-16 22:00:08 +01:00
|
|
|
import SignatureRequest from './signature-request.container';
|
2020-01-30 20:34:45 +01:00
|
|
|
|
2023-03-08 17:05:55 +01:00
|
|
|
const mockStoreWithEth = {
|
|
|
|
metamask: {
|
|
|
|
tokenList: {
|
|
|
|
'0x514910771af9ca656af840dff83e8264ecf986ca': {
|
|
|
|
address: '0x514910771af9ca656af840dff83e8264ecf986ca',
|
|
|
|
symbol: 'LINK',
|
|
|
|
decimals: 18,
|
|
|
|
name: 'ChainLink Token',
|
|
|
|
iconUrl: 'https://crypto.com/price/coin-data/icon/LINK/color_icon.png',
|
|
|
|
aggregators: [
|
|
|
|
'Aave',
|
|
|
|
'Bancor',
|
|
|
|
'CMC',
|
|
|
|
'Crypto.com',
|
|
|
|
'CoinGecko',
|
|
|
|
'1inch',
|
|
|
|
'Paraswap',
|
|
|
|
'PMM',
|
|
|
|
'Zapper',
|
|
|
|
'Zerion',
|
|
|
|
'0x',
|
|
|
|
],
|
|
|
|
occurrences: 12,
|
|
|
|
unlisted: false,
|
2022-11-10 11:28:34 +01:00
|
|
|
},
|
2023-03-08 17:05:55 +01:00
|
|
|
},
|
|
|
|
identities: {
|
|
|
|
'0xb19ac54efa18cc3a14a5b821bfec73d284bf0c5e': {
|
|
|
|
name: 'Account 2',
|
|
|
|
address: '0xb19ac54efa18cc3a14a5b821bfec73d284bf0c5e',
|
2022-11-10 11:28:34 +01:00
|
|
|
},
|
2023-03-08 17:05:55 +01:00
|
|
|
},
|
|
|
|
addressBook: {
|
|
|
|
undefined: {
|
|
|
|
0: {
|
|
|
|
address: '0x39a4e4Af7cCB654dB9500F258c64781c8FbD39F0',
|
|
|
|
name: '',
|
|
|
|
isEns: false,
|
2022-11-10 11:28:34 +01:00
|
|
|
},
|
|
|
|
},
|
2023-03-08 17:05:55 +01:00
|
|
|
},
|
2023-05-02 17:53:20 +02:00
|
|
|
providerConfig: {
|
2023-03-08 17:05:55 +01:00
|
|
|
type: 'rpc',
|
|
|
|
},
|
|
|
|
preferences: {
|
|
|
|
useNativeCurrencyAsPrimaryCurrency: true,
|
|
|
|
},
|
|
|
|
accounts: {
|
|
|
|
'0xd8f6a2ffb0fc5952d16c9768b71cfd35b6399aa5': {
|
|
|
|
address: '0xd8f6a2ffb0fc5952d16c9768b71cfd35b6399aa5',
|
|
|
|
balance: '0x03',
|
2020-05-21 22:21:34 +02:00
|
|
|
},
|
2020-01-30 20:34:45 +01:00
|
|
|
},
|
2023-03-08 17:05:55 +01:00
|
|
|
cachedBalances: {},
|
|
|
|
unapprovedDecryptMsgs: {},
|
|
|
|
unapprovedEncryptionPublicKeyMsgs: {},
|
2023-06-08 13:02:08 +02:00
|
|
|
unconfirmedTransactions: {},
|
2023-03-08 17:05:55 +01:00
|
|
|
selectedAddress: '0xd8f6a2ffb0fc5952d16c9768b71cfd35b6399aa5',
|
|
|
|
nativeCurrency: 'ETH',
|
|
|
|
currentCurrency: 'usd',
|
|
|
|
conversionRate: 231.06,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
const mockStoreWithFiat = {
|
|
|
|
...mockStoreWithEth,
|
|
|
|
preferences: {
|
|
|
|
useNativeCurrencyAsPrimaryCurrency: false,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
describe('Signature Request', () => {
|
|
|
|
const propsWithEth = {
|
2020-03-06 22:34:56 +01:00
|
|
|
fromAccount: {
|
|
|
|
address: '0xd8f6a2ffb0fc5952d16c9768b71cfd35b6399aa5',
|
2023-03-08 17:05:55 +01:00
|
|
|
balance: '0x346ba7725f412cbfdb',
|
|
|
|
name: 'John Doe',
|
2020-03-06 22:34:56 +01:00
|
|
|
},
|
2020-01-30 20:34:45 +01:00
|
|
|
history: {
|
|
|
|
push: sinon.spy(),
|
|
|
|
},
|
2022-06-07 04:30:07 +02:00
|
|
|
hardwareWalletRequiresConnection: false,
|
2023-01-31 16:29:23 +01:00
|
|
|
mostRecentOverviewPage: '/',
|
2020-01-30 20:34:45 +01:00
|
|
|
clearConfirmTransaction: sinon.spy(),
|
|
|
|
cancelMessage: sinon.spy(),
|
|
|
|
cancel: sinon.stub().resolves(),
|
2023-06-20 15:37:09 +02:00
|
|
|
rejectPendingApproval: sinon.stub().resolves(),
|
2023-01-31 16:29:23 +01:00
|
|
|
showRejectTransactionsConfirmationModal: sinon.stub().resolves(),
|
|
|
|
cancelAll: sinon.stub().resolves(),
|
2023-05-02 17:53:20 +02:00
|
|
|
providerConfig: {
|
2022-11-30 19:11:36 +01:00
|
|
|
type: 'rpc',
|
|
|
|
},
|
2023-01-31 16:29:23 +01:00
|
|
|
unapprovedMessagesCount: 2,
|
2020-01-30 20:34:45 +01:00
|
|
|
sign: sinon.stub().resolves(),
|
2023-06-20 15:37:09 +02:00
|
|
|
cancelAllApprovals: sinon.stub().resolves(),
|
|
|
|
resolvePendingApproval: sinon.stub().resolves(),
|
|
|
|
completedTx: sinon.stub().resolves(),
|
2020-01-30 20:34:45 +01:00
|
|
|
txData: {
|
|
|
|
msgParams: {
|
|
|
|
id: 1,
|
2022-07-31 20:26:40 +02:00
|
|
|
data: '{"types":{"EIP712Domain":[{"name":"name","type":"string"},{"name":"version","type":"string"},{"name":"chainId","type":"uint256"},{"name":"verifyingContract","type":"address"}],"Person":[{"name":"name","type":"string"},{"name":"wallet","type":"address"}],"Mail":[{"name":"from","type":"Person"},{"name":"to","type":"Person"},{"name":"contents","type":"string"}]},"primaryType":"Mail","domain":{"name":"Ether Mail","version":"1","chainId":"4","verifyingContract":"0xCcCCccccCCCCcCCCCCCcCcCccCcCCCcCcccccccC"},"message":{"from":{"name":"Cow","wallet":"0xCD2a3d9F938E13CD947Ec05AbC7FE734Df8DD826"},"to":{"name":"Bob","wallet":"0xbBbBBBBbbBBBbbbBbbBbbbbBBbBbbbbBbBbbBBbB"},"contents":"Hello, Bob!"}}',
|
2020-01-30 20:34:45 +01:00
|
|
|
from: '0xd8f6a2ffb0fc5952d16c9768b71cfd35b6399aa5',
|
|
|
|
origin: 'test.domain',
|
|
|
|
},
|
|
|
|
status: 'unapproved',
|
|
|
|
time: 1,
|
|
|
|
type: 'eth_sign',
|
|
|
|
},
|
2023-03-08 17:05:55 +01:00
|
|
|
nativeCurrency: 'ETH',
|
|
|
|
currentCurrency: 'usd',
|
|
|
|
conversionRate: null,
|
2023-04-25 12:47:12 +02:00
|
|
|
selectedAccount: {
|
|
|
|
address: '0x123456789abcdef',
|
|
|
|
},
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2020-01-30 20:34:45 +01:00
|
|
|
|
2023-03-08 17:05:55 +01:00
|
|
|
const propsWithFiat = {
|
|
|
|
...propsWithEth,
|
|
|
|
conversionRate: 156.72,
|
|
|
|
};
|
2022-12-02 15:12:43 +01:00
|
|
|
|
2023-03-08 17:05:55 +01:00
|
|
|
describe('Render with different currencies', () => {
|
|
|
|
it('should render balance with ETH when conversionRate is not provided', () => {
|
|
|
|
const store = configureMockStore()(mockStoreWithEth);
|
|
|
|
renderWithProvider(
|
|
|
|
<SignatureRequest.WrappedComponent {...propsWithEth} />,
|
|
|
|
store,
|
|
|
|
);
|
|
|
|
expect(
|
|
|
|
screen.getByTestId('request-signature-account').textContent,
|
|
|
|
).toMatchInlineSnapshot(
|
|
|
|
`"UUnknown private networkJohn DoeBalance966.987986 ETH"`,
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should render balance with fiat when conversionRate not provided', () => {
|
|
|
|
const store = configureMockStore()(mockStoreWithFiat);
|
|
|
|
renderWithProvider(
|
|
|
|
<SignatureRequest.WrappedComponent {...propsWithFiat} />,
|
|
|
|
store,
|
|
|
|
);
|
|
|
|
expect(
|
|
|
|
screen.getByTestId('request-signature-account').textContent,
|
|
|
|
).toMatchInlineSnapshot(
|
|
|
|
`"UUnknown private networkJohn DoeBalance$151,546.36 USD"`,
|
|
|
|
);
|
|
|
|
});
|
2022-12-02 15:12:43 +01:00
|
|
|
});
|
2023-01-13 20:28:52 +01:00
|
|
|
|
2023-03-08 17:05:55 +01:00
|
|
|
describe('functionality check', () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
const store = configureMockStore()(mockStoreWithFiat);
|
|
|
|
renderWithProvider(
|
|
|
|
<SignatureRequest.WrappedComponent {...propsWithFiat} />,
|
|
|
|
store,
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
propsWithFiat.clearConfirmTransaction.resetHistory();
|
|
|
|
});
|
|
|
|
|
2023-06-20 15:37:09 +02:00
|
|
|
it('cancel', async () => {
|
2023-03-08 17:05:55 +01:00
|
|
|
const cancelButton = screen.getByTestId('page-container-footer-cancel');
|
2023-06-20 15:37:09 +02:00
|
|
|
await act(() => {
|
|
|
|
fireEvent.click(cancelButton);
|
|
|
|
});
|
|
|
|
expect(propsWithFiat.rejectPendingApproval.calledOnce).toStrictEqual(
|
|
|
|
true,
|
|
|
|
);
|
2023-03-08 17:05:55 +01:00
|
|
|
});
|
|
|
|
|
2023-06-20 15:37:09 +02:00
|
|
|
it('sign', async () => {
|
2023-03-08 17:05:55 +01:00
|
|
|
const signButton = screen.getByTestId('page-container-footer-next');
|
2023-06-20 15:37:09 +02:00
|
|
|
await act(() => {
|
|
|
|
fireEvent.click(signButton);
|
|
|
|
});
|
|
|
|
expect(propsWithFiat.resolvePendingApproval.calledOnce).toStrictEqual(
|
|
|
|
true,
|
|
|
|
);
|
2023-03-08 17:05:55 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
it('have user warning', () => {
|
|
|
|
const warningText = screen.getByText(
|
|
|
|
'Only sign this message if you fully understand the content and trust the requesting site.',
|
|
|
|
);
|
|
|
|
|
|
|
|
expect(warningText).toBeInTheDocument();
|
|
|
|
});
|
2023-01-13 20:28:52 +01:00
|
|
|
});
|
|
|
|
|
2023-03-08 17:05:55 +01:00
|
|
|
describe('contract details', () => {
|
|
|
|
let store;
|
|
|
|
beforeEach(() => {
|
|
|
|
store = configureMockStore()(mockStoreWithFiat);
|
|
|
|
});
|
|
|
|
it('shows verify contract details link when verifyingContract is set', () => {
|
|
|
|
renderWithProvider(
|
|
|
|
<SignatureRequest.WrappedComponent {...propsWithFiat} />,
|
|
|
|
store,
|
|
|
|
);
|
|
|
|
const verifyingContractLink = screen.getByTestId(
|
|
|
|
'verify-contract-details',
|
|
|
|
);
|
|
|
|
expect(verifyingContractLink).toBeInTheDocument();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should not show verify contract details link when verifyingContract is not set', () => {
|
|
|
|
const newData = JSON.parse(propsWithFiat.txData.msgParams.data);
|
|
|
|
delete newData.domain.verifyingContract;
|
|
|
|
|
|
|
|
const newProps = {
|
|
|
|
...propsWithFiat,
|
|
|
|
txData: {
|
|
|
|
...propsWithFiat.txData,
|
|
|
|
msgParams: {
|
|
|
|
...propsWithFiat.txData.msgParams,
|
|
|
|
data: JSON.stringify(newData),
|
|
|
|
},
|
2023-01-13 20:28:52 +01:00
|
|
|
},
|
2023-03-08 17:05:55 +01:00
|
|
|
};
|
2023-01-13 20:28:52 +01:00
|
|
|
|
2023-03-08 17:05:55 +01:00
|
|
|
renderWithProvider(
|
|
|
|
<SignatureRequest.WrappedComponent {...newProps} />,
|
|
|
|
store,
|
|
|
|
);
|
2023-01-13 20:28:52 +01:00
|
|
|
|
2023-03-08 17:05:55 +01:00
|
|
|
expect(screen.queryByTestId('verify-contract-details')).toBeNull();
|
|
|
|
});
|
2023-01-13 20:28:52 +01:00
|
|
|
});
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|