Skip to content

Commit

Permalink
refactor: Use generic helper function to initiate signatures (#26584)
Browse files Browse the repository at this point in the history
## **Description**

Refactor signature tests to use a helper function in order to load the
test state.
Also fixes the flaky SIWE tests by adding an extra scroll to bottom

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

## **Related issues**

Fixes:
[#24468](#24468)
[#26445](#26445)

## **Manual testing steps**

1. Go to this page...
2.
3.

## **Screenshots/Recordings**

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

### **Before**

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

### **After**

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

## **Pre-merge author checklist**

- [ ] 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).
- [ ] I've completed the PR template to the best of my ability
- [ ] I’ve included tests if applicable
- [ ] I’ve documented my code using [JSDoc](https://jsdoc.app/) format
if applicable
- [ ] 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
pnarayanaswamy authored Aug 22, 2024
1 parent 41736f0 commit ed44d49
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 69 deletions.
7 changes: 3 additions & 4 deletions test/e2e/tests/confirmations/signatures/permit.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import {
assertSignatureMetrics,
clickHeaderInfoBtn,
copyAddressAndPasteWalletAddress,
openDappAndTriggerSignature,
SignatureType,
} from './signature-helpers';

describe('Confirmation Signature - Permit @no-mmi', function (this: Suite) {
Expand All @@ -35,10 +37,7 @@ describe('Confirmation Signature - Permit @no-mmi', function (this: Suite) {
const addresses = await (ganacheServer as Ganache).getAccounts();
const publicAddress = addresses?.[0] as string;

await unlockWallet(driver);
await openDapp(driver);
await driver.clickElement('#signPermit');
await driver.switchToWindowWithTitle(WINDOW_TITLES.Dialog);
await openDappAndTriggerSignature(driver, SignatureType.Permit);

await clickHeaderInfoBtn(driver);
await assertHeaderInfoBalance(driver);
Expand Down
14 changes: 4 additions & 10 deletions test/e2e/tests/confirmations/signatures/personal-sign.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@ import { MockedEndpoint } from 'mockttp';
import {
DAPP_HOST_ADDRESS,
WINDOW_TITLES,
openDapp,
switchToNotificationWindow,
unlockWallet,
} from '../../../helpers';
import { Ganache } from '../../../seeder/ganache';
import { Driver } from '../../../webdriver/driver';
Expand All @@ -19,6 +17,8 @@ import {
assertSignatureMetrics,
clickHeaderInfoBtn,
copyAddressAndPasteWalletAddress,
openDappAndTriggerSignature,
SignatureType,
} from './signature-helpers';

describe('Confirmation Signature - Personal Sign @no-mmi', function (this: Suite) {
Expand All @@ -33,10 +33,7 @@ describe('Confirmation Signature - Personal Sign @no-mmi', function (this: Suite
const addresses = await (ganacheServer as Ganache).getAccounts();
const publicAddress = addresses?.[0] as string;

await unlockWallet(driver);
await openDapp(driver);
await driver.clickElement('#personalSign');
await switchToNotificationWindow(driver);
await openDappAndTriggerSignature(driver, SignatureType.PersonalSign);

await clickHeaderInfoBtn(driver);
await assertHeaderInfoBalance(driver);
Expand Down Expand Up @@ -70,10 +67,7 @@ describe('Confirmation Signature - Personal Sign @no-mmi', function (this: Suite
driver,
mockedEndpoint: mockedEndpoints,
}: TestSuiteArguments) => {
await unlockWallet(driver);
await openDapp(driver);
await driver.clickElement('#personalSign');
await switchToNotificationWindow(driver);
await openDappAndTriggerSignature(driver, SignatureType.PersonalSign);

await driver.clickElement(
'[data-testid="confirm-footer-cancel-button"]',
Expand Down
20 changes: 10 additions & 10 deletions test/e2e/tests/confirmations/signatures/sign-typed-data-v3.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@ import { MockedEndpoint } from 'mockttp';
import {
DAPP_HOST_ADDRESS,
WINDOW_TITLES,
openDapp,
switchToNotificationWindow,
unlockWallet,
} from '../../../helpers';
import { Ganache } from '../../../seeder/ganache';
import { Driver } from '../../../webdriver/driver';
Expand All @@ -22,6 +20,8 @@ import {
assertSignatureMetrics,
clickHeaderInfoBtn,
copyAddressAndPasteWalletAddress,
openDappAndTriggerSignature,
SignatureType,
} from './signature-helpers';

describe('Confirmation Signature - Sign Typed Data V3 @no-mmi', function (this: Suite) {
Expand All @@ -36,10 +36,10 @@ describe('Confirmation Signature - Sign Typed Data V3 @no-mmi', function (this:
const addresses = await (ganacheServer as Ganache).getAccounts();
const publicAddress = addresses?.[0] as string;

await unlockWallet(driver);
await openDapp(driver);
await driver.clickElement('#signTypedDataV3');
await switchToNotificationWindow(driver);
await openDappAndTriggerSignature(
driver,
SignatureType.SignTypedDataV3,
);

await clickHeaderInfoBtn(driver);
await assertHeaderInfoBalance(driver);
Expand Down Expand Up @@ -73,10 +73,10 @@ describe('Confirmation Signature - Sign Typed Data V3 @no-mmi', function (this:
driver,
mockedEndpoint: mockedEndpoints,
}: TestSuiteArguments) => {
await unlockWallet(driver);
await openDapp(driver);
await driver.clickElement('#signTypedDataV3');
await switchToNotificationWindow(driver);
await openDappAndTriggerSignature(
driver,
SignatureType.SignTypedDataV3,
);

await driver.clickElement(
'[data-testid="confirm-footer-cancel-button"]',
Expand Down
26 changes: 11 additions & 15 deletions test/e2e/tests/confirmations/signatures/sign-typed-data-v4.spec.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
import { strict as assert } from 'assert';
import { Suite } from 'mocha';
import { MockedEndpoint } from 'mockttp';
import {
DAPP_HOST_ADDRESS,
WINDOW_TITLES,
openDapp,
switchToNotificationWindow,
unlockWallet,
} from '../../../helpers';
import { DAPP_HOST_ADDRESS, WINDOW_TITLES } from '../../../helpers';
import { Ganache } from '../../../seeder/ganache';
import { Driver } from '../../../webdriver/driver';
import {
Expand All @@ -22,6 +16,8 @@ import {
assertSignatureMetrics,
clickHeaderInfoBtn,
copyAddressAndPasteWalletAddress,
openDappAndTriggerSignature,
SignatureType,
} from './signature-helpers';

describe('Confirmation Signature - Sign Typed Data V4 @no-mmi', function (this: Suite) {
Expand All @@ -36,10 +32,10 @@ describe('Confirmation Signature - Sign Typed Data V4 @no-mmi', function (this:
const addresses = await (ganacheServer as Ganache).getAccounts();
const publicAddress = addresses?.[0] as string;

await unlockWallet(driver);
await openDapp(driver);
await driver.clickElement('#signTypedDataV4');
await switchToNotificationWindow(driver);
await openDappAndTriggerSignature(
driver,
SignatureType.SignTypedDataV4,
);

await clickHeaderInfoBtn(driver);
await assertHeaderInfoBalance(driver);
Expand Down Expand Up @@ -74,10 +70,10 @@ describe('Confirmation Signature - Sign Typed Data V4 @no-mmi', function (this:
driver,
mockedEndpoint: mockedEndpoints,
}: TestSuiteArguments) => {
await unlockWallet(driver);
await openDapp(driver);
await driver.clickElement('#signTypedDataV4');
await switchToNotificationWindow(driver);
await openDappAndTriggerSignature(
driver,
SignatureType.SignTypedDataV4,
);

await driver.clickElement(
'[data-testid="confirm-footer-cancel-button"]',
Expand Down
20 changes: 5 additions & 15 deletions test/e2e/tests/confirmations/signatures/sign-typed-data.spec.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
import { strict as assert } from 'assert';
import { Suite } from 'mocha';
import { MockedEndpoint } from 'mockttp';
import {
DAPP_HOST_ADDRESS,
WINDOW_TITLES,
openDapp,
switchToNotificationWindow,
unlockWallet,
} from '../../../helpers';
import { DAPP_HOST_ADDRESS, WINDOW_TITLES } from '../../../helpers';
import { Ganache } from '../../../seeder/ganache';
import { Driver } from '../../../webdriver/driver';
import { withRedesignConfirmationFixtures } from '../helpers';
Expand All @@ -19,6 +13,8 @@ import {
assertSignatureMetrics,
clickHeaderInfoBtn,
copyAddressAndPasteWalletAddress,
openDappAndTriggerSignature,
SignatureType,
} from './signature-helpers';

describe('Confirmation Signature - Sign Typed Data @no-mmi', function (this: Suite) {
Expand All @@ -33,10 +29,7 @@ describe('Confirmation Signature - Sign Typed Data @no-mmi', function (this: Sui
const addresses = await (ganacheServer as Ganache).getAccounts();
const publicAddress = addresses?.[0] as string;

await unlockWallet(driver);
await openDapp(driver);
await driver.clickElement('#signTypedData');
await switchToNotificationWindow(driver);
await openDappAndTriggerSignature(driver, SignatureType.SignTypedData);

await clickHeaderInfoBtn(driver);
await assertHeaderInfoBalance(driver);
Expand Down Expand Up @@ -72,10 +65,7 @@ describe('Confirmation Signature - Sign Typed Data @no-mmi', function (this: Sui
driver,
mockedEndpoint: mockedEndpoints,
}: TestSuiteArguments) => {
await unlockWallet(driver);
await openDapp(driver);
await driver.clickElement('#signTypedData');
await switchToNotificationWindow(driver);
await openDappAndTriggerSignature(driver, SignatureType.SignTypedData);

await driver.clickElement(
'[data-testid="confirm-footer-cancel-button"]',
Expand Down
26 changes: 25 additions & 1 deletion test/e2e/tests/confirmations/signatures/signature-helpers.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,24 @@
import { strict as assert } from 'assert';
import { MockedEndpoint } from 'mockttp';
import { WINDOW_TITLES, getEventPayloads } from '../../../helpers';
import {
WINDOW_TITLES,
getEventPayloads,
openDapp,
unlockWallet,
} from '../../../helpers';
import { Driver } from '../../../webdriver/driver';

export const WALLET_ADDRESS = '0x5CfE73b6021E818B776b421B1c4Db2474086a7e1';
export const WALLET_ETH_BALANCE = '25';
export enum SignatureType {
PersonalSign = '#personalSign',
Permit = '#signPermit',
SignTypedDataV3 = '#signTypedDataV3',
SignTypedDataV4 = '#signTypedDataV4',
SignTypedData = '#signTypedData',
SIWE = '#siwe',
SIWE_BadDomain = '#siweBadDomain',
}

export async function assertSignatureMetrics(
driver: Driver,
Expand Down Expand Up @@ -99,3 +113,13 @@ export async function assertPastedAddress(driver: Driver) {
const formFieldEl = await driver.findElement('#eip747ContractAddress');
assert.equal(await formFieldEl.getAttribute('value'), WALLET_ADDRESS);
}

export async function openDappAndTriggerSignature(
driver: Driver,
type: string,
) {
await unlockWallet(driver);
await openDapp(driver);
await driver.clickElement(type);
await driver.switchToWindowWithTitle(WINDOW_TITLES.Dialog);
}
20 changes: 6 additions & 14 deletions test/e2e/tests/confirmations/signatures/siwe.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@ import { MockedEndpoint } from 'mockttp';
import {
DAPP_HOST_ADDRESS,
WINDOW_TITLES,
openDapp,
switchToNotificationWindow,
unlockWallet,
} from '../../../helpers';
import { Driver } from '../../../webdriver/driver';
import {
Expand All @@ -21,6 +19,8 @@ import {
assertSignatureMetrics,
clickHeaderInfoBtn,
copyAddressAndPasteWalletAddress,
openDappAndTriggerSignature,
SignatureType,
} from './signature-helpers';

describe('Confirmation Signature - SIWE @no-mmi', function (this: Suite) {
Expand All @@ -31,10 +31,7 @@ describe('Confirmation Signature - SIWE @no-mmi', function (this: Suite) {
driver,
mockedEndpoint: mockedEndpoints,
}: TestSuiteArguments) => {
await unlockWallet(driver);
await openDapp(driver);
await driver.clickElement('#siwe');
await switchToNotificationWindow(driver);
await openDappAndTriggerSignature(driver, SignatureType.SIWE);

await clickHeaderInfoBtn(driver);
await assertHeaderInfoBalance(driver);
Expand Down Expand Up @@ -73,10 +70,7 @@ describe('Confirmation Signature - SIWE @no-mmi', function (this: Suite) {
driver,
mockedEndpoint: mockedEndpoints,
}: TestSuiteArguments) => {
await unlockWallet(driver);
await openDapp(driver);
await driver.clickElement('#siwe');
await switchToNotificationWindow(driver);
await openDappAndTriggerSignature(driver, SignatureType.SIWE);

await driver.clickElement(
'[data-testid="confirm-footer-cancel-button"]',
Expand Down Expand Up @@ -105,11 +99,9 @@ describe('Confirmation Signature - SIWE @no-mmi', function (this: Suite) {
await withRedesignConfirmationFixtures(
this.test?.fullTitle(),
async ({ driver }: TestSuiteArguments) => {
await unlockWallet(driver);
await openDapp(driver);
await driver.clickElement('#siweBadDomain');
await switchToNotificationWindow(driver);
await openDappAndTriggerSignature(driver, SignatureType.SIWE_BadDomain);

await driver.clickElementSafe('.confirm-scroll-to-bottom__button');
const alert = await driver.findElement('[data-testid="inline-alert"]');
assert.equal(await alert.getText(), 'Alert');
await driver.clickElement('[data-testid="inline-alert"]');
Expand Down

0 comments on commit ed44d49

Please sign in to comment.