Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: Adding e2e for SIWE and re-enabling redesign for SIWE #25831

Merged
merged 12 commits into from
Jul 22, 2024
1 change: 1 addition & 0 deletions .vscode/cspell.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
"quickstart",
"recompiles",
"shellcheck",
"SIWE",
"sourcemaps",
"sprintf",
"testcase",
Expand Down
161 changes: 161 additions & 0 deletions test/e2e/tests/confirmations/signatures/siwe.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
import { strict as assert } from 'assert';
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Info: Imported necessary modules and helper functions for the tests.

import { Suite } from 'mocha';
import {
scrollAndConfirmAndAssertConfirm,
withRedesignConfirmationFixtures,
} from '../helpers';
import {
DAPP_HOST_ADDRESS,
WINDOW_TITLES,
openDapp,
switchToNotificationWindow,
unlockWallet,
} from '../../../helpers';
import { Driver } from '../../../webdriver/driver';
import { Mockttp } from '../../../mock-e2e';
import {
assertHeaderInfoBalance,
assertPastedAddress,
clickHeaderInfoBtn,
copyAddressAndPasteWalletAddress,
assertSignatureMetrics,
assertAccountDetailsMetrics,
} from './signature-helpers';

describe('Confirmation Signature - SIWE @no-mmi', function (this: Suite) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Info: Test for initiating and confirming a SIWE request.

it('initiates and confirms', async function () {
await withRedesignConfirmationFixtures(
this.test?.fullTitle(),
async ({
driver,
mockedEndpoint: mockedEndpoints,
}: {
driver: Driver;
mockedEndpoint: Mockttp;
}) => {
await unlockWallet(driver);
await openDapp(driver);
await driver.clickElement('#siwe');
await switchToNotificationWindow(driver);

await clickHeaderInfoBtn(driver);
await assertHeaderInfoBalance(driver);

await copyAddressAndPasteWalletAddress(driver);
await assertPastedAddress(driver);
await assertAccountDetailsMetrics(
driver,
mockedEndpoints,
'personal_sign',
);
await switchToNotificationWindow(driver);
await assertInfoValues(driver);
await scrollAndConfirmAndAssertConfirm(driver);
await driver.delay(1000);

await assertVerifiedSiweMessage(
driver,
'0xef8674a92d62a1876624547bdccaef6c67014ae821de18fa910fbff56577a65830f68848585b33d1f4b9ea1c3da1c1b11553b6aabe8446717daf7cd1e38a68271c',
);
await assertSignatureMetrics(
driver,
mockedEndpoints,
'personal_sign',
'',
['redesigned_confirmation', 'sign_in_with_ethereum'],
);
},
);
});

it('initiates and rejects', async function () {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Info: Test for initiating and rejecting a SIWE request.

await withRedesignConfirmationFixtures(
this.test?.fullTitle(),
async ({
driver,
mockedEndpoint: mockedEndpoints,
}: {
driver: Driver;
mockedEndpoint: Mockttp;
}) => {
await unlockWallet(driver);
await openDapp(driver);
await driver.clickElement('#siwe');
await switchToNotificationWindow(driver);

await driver.clickElement(
'[data-testid="confirm-footer-cancel-button"]',
);

await driver.waitUntilXWindowHandles(2);
await driver.switchToWindowWithTitle(WINDOW_TITLES.TestDApp);

const rejectionResult = await driver.findElement('#siweResult');
assert.equal(
await rejectionResult.getText(),
'Error: User rejected the request.',
);
await assertSignatureMetrics(
driver,
mockedEndpoints,
'personal_sign',
'',
['redesigned_confirmation', 'sign_in_with_ethereum'],
);
},
);
});

it('displays alert for domain binding and confirms', async function () {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Info: Test for handling domain mismatch alerts and confirming a SIWE request.

await withRedesignConfirmationFixtures(
this.test?.fullTitle(),
async ({ driver }: { driver: Driver; mockedEndpoint: Mockttp }) => {
await unlockWallet(driver);
await openDapp(driver);
await driver.clickElement('#siweBadDomain');
await switchToNotificationWindow(driver);

const alert = await driver.findElement('[data-testid="inline-alert"]');
assert.equal(await alert.getText(), 'Alert');
await driver.clickElement('[data-testid="inline-alert"]');

await driver.clickElement(
'[data-testid="alert-modal-acknowledge-checkbox"]',
);
await driver.clickElement('[data-testid="alert-modal-button"]');

await scrollAndConfirmAndAssertConfirm(driver);

await driver.clickElement(
'[data-testid="alert-modal-acknowledge-checkbox"]',
);
await driver.clickElement(
'[data-testid="confirm-alert-modal-submit-button"]',
);

await assertVerifiedSiweMessage(
driver,
'0x24e559452c37827008633f9ae50c68cdb28e33f547f795af687839b520b022e4093c38bf1dfebda875ded715f2754d458ed62a19248e5a9bd2205bd1cb66f9b51b',
);
},
);
});
});

async function assertInfoValues(driver: Driver) {
const origin = driver.findElement({ text: DAPP_HOST_ADDRESS });
const message = driver.findElement({
text: 'I accept the MetaMask Terms of Service: https://community.metamask.io/tos',
});

assert.ok(await origin);
assert.ok(await message);
}
Comment on lines +145 to +153
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Info: Helper function to assert information values in the SIWE request.


async function assertVerifiedSiweMessage(driver: Driver, message: string) {
await driver.waitUntilXWindowHandles(2);
await driver.switchToWindowWithTitle(WINDOW_TITLES.TestDApp);

const verifySigUtil = await driver.findElement('#siweResult');
assert.equal(await verifySigUtil.getText(), message);
}
Comment on lines +155 to +161
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Info: Helper function to assert the verified SIWE message.

7 changes: 5 additions & 2 deletions ui/pages/confirmations/hooks/useCurrentConfirmation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ describe('useCurrentConfirmation', () => {
expect(currentConfirmation).toBeUndefined();
});

it('returns undefined if message is SIWE', () => {
it('returns if message is SIWE', () => {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Info: Updated test description to reflect the new expected behavior for SIWE messages.

const currentConfirmation = runHook({
message: {
...MESSAGE_MOCK,
Expand All @@ -214,7 +214,10 @@ describe('useCurrentConfirmation', () => {
redesignedConfirmationsEnabled: true,
});

expect(currentConfirmation).toBeUndefined();
expect(currentConfirmation).toStrictEqual({
id: APPROVAL_MOCK.id,
msgParams: { siwe: { isSIWEMessage: true } },
});
Comment on lines +217 to +220
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Info: Updated expectation to check for the correct structure of the returned SIWE message.

});

it('returns undefined if developer and user settings are enabled and transaction has incorrect type', () => {
Expand Down
8 changes: 1 addition & 7 deletions ui/pages/confirmations/hooks/useCurrentConfirmation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,15 +65,9 @@ const useCurrentConfirmation = () => {
pendingApproval?.type as ApprovalType,
);

const isSIWE =
pendingApproval?.type === TransactionType.personalSign &&
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(signatureMessage?.msgParams as any)?.siwe?.isSIWEMessage;

const shouldUseRedesign =
isRedesignedConfirmationsUserSettingEnabled &&
(isCorrectApprovalType || isCorrectTransactionType) &&
!isSIWE;
(isCorrectApprovalType || isCorrectTransactionType);
Comment on lines 68 to +70
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Info: Removed isSIWE check from shouldUseRedesign condition.


return useMemo(() => {
if (!shouldUseRedesign) {
Expand Down