Skip to content

Commit

Permalink
fix: disable the confirm button when there is a blocking alert (#27347)
Browse files Browse the repository at this point in the history
<!--
Please submit this PR as a draft initially.
Do not mark it as "Ready for review" until the template has been
completely filled out, and PR status checks have passed at least once.
-->

## **Description**
This PR aims to disable the confirm button when there is a blocking
alert.
<!--
Write a short description of the changes included in this pull request,
also include relevant motivation and context. Have in mind the following
questions:
1. What is the reason for the change?
2. What is the improvement/solution?
-->

[![Open in GitHub
Codespaces](https://github.com/codespaces/badge.svg)](https://codespaces.new/MetaMask/metamask-extension/pull/27347?quickstart=1)

## **Related issues**

Fixes: #27147

## **Manual testing steps**

1. Go to test dapp
2. Don't have funds
3. Trigger malicious mint erc20 on Sepolia
4. Click on Review alerts
or
1. Go to test dapp
2. Click Sign In With Ethereum Bad Domain
3. Click Confirm
4. See the friction modal 

## **Screenshots/Recordings**

<!-- If applicable, add screenshots and/or recordings to visualize the
before and after of your change. -->

[Screencast from 23-09-2024
17:57:22.webm](https://github.com/user-attachments/assets/4c00284c-93d3-4b1b-9553-39eda67b7924)

### **Before**

<!-- [screenshots/recordings] -->

### **After**

<!-- [screenshots/recordings] -->

## **Pre-merge author checklist**

- [x] I've followed [MetaMask Contributor
Docs](https://github.com/MetaMask/contributor-docs) and [MetaMask
Extension Coding
Standards](https://github.com/MetaMask/metamask-extension/blob/develop/.github/guidelines/CODING_GUIDELINES.md).
- [x] I've completed the PR template to the best of my ability
- [x] I’ve included tests if applicable
- [x] I’ve documented my code using [JSDoc](https://jsdoc.app/) format
if applicable
- [x] I’ve applied the right labels on the PR (see [labeling
guidelines](https://github.com/MetaMask/metamask-extension/blob/develop/.github/guidelines/LABELING_GUIDELINES.md)).
Not required for external contributors.

## **Pre-merge reviewer checklist**

- [ ] I've manually tested the PR (e.g. pull and build branch, run the
app, test code being changed).
- [ ] I confirm that this PR addresses all acceptance criteria described
in the ticket it closes and includes the necessary testing evidence such
as recordings and or screenshots.
  • Loading branch information
vinistevam authored Sep 24, 2024
1 parent 24811bb commit d78a181
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import {
WINDOW_TITLES,
} from '../../../helpers';
import { SMART_CONTRACTS } from '../../../seeder/smart-contracts';
import { scrollAndConfirmAndAssertConfirm } from '../helpers';
import {
TestSuiteArguments,
openDAppWithContract,
Expand Down Expand Up @@ -48,8 +47,6 @@ describe('Alert for insufficient funds @no-mmi', function () {

await verifyAlertForInsufficientBalance(driver);

await scrollAndConfirmAndAssertConfirm(driver);

await verifyConfirmationIsDisabled(driver);
},
);
Expand All @@ -58,7 +55,7 @@ describe('Alert for insufficient funds @no-mmi', function () {

async function verifyConfirmationIsDisabled(driver: Driver) {
const confirmButton = await driver.findElement(
'[data-testid="confirm-alert-modal-submit-button"]',
'[data-testid="confirm-footer-button"]',
);
assert.equal(await confirmButton.isEnabled(), false);
}
Expand Down
84 changes: 61 additions & 23 deletions ui/pages/confirmations/components/confirm/footer/footer.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import { Severity } from '../../../../../helpers/constants/design-system';
import { SignatureRequestType } from '../../../types/confirm';
import * as confirmContext from '../../../context/confirm';

import { Alert } from '../../../../../ducks/confirm-alerts/confirm-alerts';
import Footer from './footer';

jest.mock('react-redux', () => ({
Expand Down Expand Up @@ -247,7 +248,8 @@ describe('ConfirmFooter', () => {
const OWNER_ID_MOCK = '123';
const KEY_ALERT_KEY_MOCK = 'Key';
const ALERT_MESSAGE_MOCK = 'Alert 1';
const alertsMock = [

const alertsMock: Alert[] = [
{
key: KEY_ALERT_KEY_MOCK,
field: KEY_ALERT_KEY_MOCK,
Expand All @@ -257,38 +259,74 @@ describe('ConfirmFooter', () => {
alertDetails: ['Detail 1', 'Detail 2'],
},
];
const stateWithAlertsMock = getMockPersonalSignConfirmStateForRequest(
{
...unapprovedPersonalSignMsg,
id: OWNER_ID_MOCK,
msgParams: {
from: '0xc42edfcc21ed14dda456aa0756c153f7985d8813',
},
} as SignatureRequestType,
{
confirmAlerts: {
alerts: { [OWNER_ID_MOCK]: alertsMock },
confirmed: {
[OWNER_ID_MOCK]: { [KEY_ALERT_KEY_MOCK]: false },

const createStateWithAlerts = (
alerts: Alert[],
confirmed: Record<string, boolean>,
) => {
return getMockPersonalSignConfirmStateForRequest(
{
...unapprovedPersonalSignMsg,
id: OWNER_ID_MOCK,
msgParams: {
from: '0xc42edfcc21ed14dda456aa0756c153f7985d8813',
},
} as SignatureRequestType,
{
confirmAlerts: {
alerts: { [OWNER_ID_MOCK]: alerts },
confirmed: { [OWNER_ID_MOCK]: confirmed },
},
metamask: {},
},
metamask: {},
},
);
it('renders the review alerts button when there are unconfirmed alerts', () => {
const { getByText } = render(stateWithAlertsMock);
expect(getByText('Confirm')).toBeInTheDocument();
);
};

const stateWithAlertsMock = createStateWithAlerts(alertsMock, {
[KEY_ALERT_KEY_MOCK]: false,
});

it('renders the confirm button when there are no unconfirmed alerts', () => {
const { getByText } = render();
expect(getByText('Confirm')).toBeInTheDocument();
it('renders the "review alerts" button when there are unconfirmed alerts', () => {
const stateWithMultipleDangerAlerts = createStateWithAlerts(
[
alertsMock[0],
{
...alertsMock[0],
key: 'From',
},
],
{ [KEY_ALERT_KEY_MOCK]: false },
);
const { getByText } = render(stateWithMultipleDangerAlerts);
expect(getByText('Review alerts')).toBeInTheDocument();
});

it('renders the "review alerts" button disabled when there are blocking alerts', () => {
const stateWithMultipleDangerAlerts = createStateWithAlerts(
[
alertsMock[0],
{
...alertsMock[0],
key: 'From',
isBlocking: true,
},
],
{ [KEY_ALERT_KEY_MOCK]: false },
);
const { getByText } = render(stateWithMultipleDangerAlerts);
expect(getByText('Review alerts')).toBeInTheDocument();
expect(getByText('Review alerts')).toBeDisabled();
});

it('sets the alert modal visible when the review alerts button is clicked', () => {
const { getByTestId } = render(stateWithAlertsMock);
fireEvent.click(getByTestId('confirm-footer-button'));
expect(getByTestId('confirm-alert-modal-submit-button')).toBeDefined();
});

it('renders the "confirm" button when there are no alerts', () => {
const { getByText } = render();
expect(getByText('Confirm')).toBeInTheDocument();
});
});
});
29 changes: 27 additions & 2 deletions ui/pages/confirmations/components/confirm/footer/footer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,30 @@ import {
import { useConfirmContext } from '../../../context/confirm';
import { getConfirmationSender } from '../utils';
import { MetaMetricsEventLocation } from '../../../../../../shared/constants/metametrics';
import { Severity } from '../../../../../helpers/constants/design-system';

export type OnCancelHandler = ({
location,
}: {
location: MetaMetricsEventLocation;
}) => void;

function getButtonDisabledState(
hasUnconfirmedDangerAlerts: boolean,
hasBlockingAlerts: boolean,
disabled: boolean,
) {
if (hasBlockingAlerts) {
return true;
}

if (hasUnconfirmedDangerAlerts) {
return false;
}

return disabled;
}

const ConfirmButton = ({
alertOwnerId = '',
disabled,
Expand All @@ -63,6 +80,10 @@ const ConfirmButton = ({
const { dangerAlerts, hasDangerAlerts, hasUnconfirmedDangerAlerts } =
useAlerts(alertOwnerId);

const hasDangerBlockingAlerts = dangerAlerts.some(
(alert) => alert.severity === Severity.Danger && alert.isBlocking,
);

const handleCloseConfirmModal = useCallback(() => {
setConfirmModalVisible(false);
}, []);
Expand All @@ -86,12 +107,16 @@ const ConfirmButton = ({
block
danger
data-testid="confirm-footer-button"
disabled={hasUnconfirmedDangerAlerts ? false : disabled}
disabled={getButtonDisabledState(
hasUnconfirmedDangerAlerts,
hasDangerBlockingAlerts,
disabled,
)}
onClick={handleOpenConfirmModal}
size={ButtonSize.Lg}
startIconName={IconName.Danger}
>
{dangerAlerts?.length > 1 ? t('reviewAlerts') : t('confirm')}
{dangerAlerts?.length > 0 ? t('reviewAlerts') : t('confirm')}
</Button>
) : (
<Button
Expand Down

0 comments on commit d78a181

Please sign in to comment.