Skip to content

Commit

Permalink
test: finish test cases and clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
ffmcgee725 committed Dec 19, 2024
1 parent 3f43004 commit a68081e
Show file tree
Hide file tree
Showing 2 changed files with 138 additions and 70 deletions.
139 changes: 71 additions & 68 deletions test/e2e/flask/multichain-api/create-session.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { strict as assert } from 'assert';
import { withFixtures } from '../../helpers';
import { WINDOW_TITLES, withFixtures } from '../../helpers';
import { Driver } from '../../webdriver/driver';
import FixtureBuilder from '../../fixture-builder';
import { DEFAULT_FIXTURE_ACCOUNT } from '../../constants';
Expand All @@ -12,9 +12,51 @@ import {
getExpectedSessionScope,
addRequestAccountsToCreateSession,
assertOnlyRequestedNetworksAreSelected,
addAndAuthorizeAccount,
uncheckNetworksExceptMainnet,
} from './testHelpers';

describe('Multichain API', function () {
describe('Connect wallet to the multichain dapp via `externally_connectable`, call `wallet_createSession` with requested EVM scope that does NOT match one of the user’s enabled networks', function () {
it("the specified EVM scopes that do not match the user's configured networks should be treated as if they were not requested", async function () {
await withFixtures(
{
title: this.test?.fullTitle(),
fixtures: new FixtureBuilder()
.withNetworkControllerOnMainnet()
.build(),
...DEFAULT_MULTICHAIN_TEST_DAPP_FIXTURE_OPTIONS,
},
async ({
driver,
extensionId,
}: {
driver: Driver;
extensionId: string;
}) => {
const scopesToIgnore = ['eip155:42161', 'eip155:10'];
await openMultichainDappAndConnectWalletWithExternallyConnectable(
driver,
extensionId,
);
await initCreateSessionScopes(driver, [
'eip155:1',
...scopesToIgnore,
]);
await confirmAndSwitchFocusToDapp(driver);
const getSessionScopesResult = await getSessionScopes(driver);

for (const scope of scopesToIgnore) {
assert.strictEqual(
getSessionScopesResult.sessionScopes[scope],
undefined,
);
}
},
);
});
});

describe('Connect wallet to the multichain dapp via `externally_connectable`, call `wallet_createSession` with requested EVM scope that match the user’s enabled networks', function () {
it('should only select the specified EVM scopes requested by the user, and session scope contain valid accounts only', async function () {
await withFixtures(
Expand Down Expand Up @@ -80,12 +122,15 @@ describe('Multichain API', function () {
});
});

describe.only('Connect wallet to the multichain dapp via `externally_connectable`, call `wallet_createSession` with requested EVM scope that match the user’s enabled networks, edit selection in wallet UI', function () {
describe('Connect wallet to the multichain dapp via `externally_connectable`, call `wallet_createSession` with requested EVM scope that match the user’s enabled networks, edit selection in wallet UI', function () {
it('should change result according to changed network & accounts', async function () {
await withFixtures(
{
title: this.test?.fullTitle(),
fixtures: new FixtureBuilder().withPopularNetworks().build(),
fixtures: new FixtureBuilder()
.withPopularNetworks()
.withPreferencesControllerAdditionalAccountIdentities()
.build(),
...DEFAULT_MULTICHAIN_TEST_DAPP_FIXTURE_OPTIONS,
},
async ({
Expand All @@ -95,15 +140,12 @@ describe('Multichain API', function () {
driver: Driver;
extensionId: string;
}) => {
const INVALID_ACCOUNT = '0x9999999999999999999999999999999999999999';
const requestScopesToNetworkMap = {
'eip155:1': 'Ethereum Mainnet',
'eip155:42161': 'Arbitrum One',
'eip155:10': 'OP Mainnet',
};

const requestScopes = Object.keys(requestScopesToNetworkMap);
// const networksToRequest = Object.values(requestScopesToNetworkMap);

await openMultichainDappAndConnectWalletWithExternallyConnectable(
driver,
Expand All @@ -112,75 +154,36 @@ describe('Multichain API', function () {

await addRequestAccountsToCreateSession(driver, [
DEFAULT_FIXTURE_ACCOUNT,
INVALID_ACCOUNT,
'',
]);

await initCreateSessionScopes(driver, requestScopes);
await driver.clickElement({ text: 'Update', tag: 'button' });

assert.equal(true, false, 'schmoney');
// await assertOnlyRequestedNetworksAreSelected(
// driver,
// networksToRequest,
// );

// await confirmAndSwitchFocusToDapp(driver);

// const getSessionScopesResult = await getSessionScopes(driver);
// for (const scope of requestScopes) {
// /**
// * Accounts in scope should not include invalid account {@link INVALID_ACCOUNT}, only the valid accounts.
// */
// const expectedSessionScope = getExpectedSessionScope(scope, [
// DEFAULT_FIXTURE_ACCOUNT,
// ]);
// const result = getSessionScopesResult.sessionScopes[scope].accounts;

// assert.deepEqual(
// expectedSessionScope.accounts,
// result,
// `${expectedSessionScope.accounts} does not match accounts in scope ${result}`,
// );
// }
},
);
});
});
await addAndAuthorizeAccount(driver);
await uncheckNetworksExceptMainnet(driver);

describe('Connect wallet to the multichain dapp via `externally_connectable`, call `wallet_createSession` with requested EVM scope that does NOT match one of the user’s enabled networks', function () {
it("the specified EVM scopes that do not match the user's configured networks should be treated as if they were not requested", async function () {
await withFixtures(
{
title: this.test?.fullTitle(),
fixtures: new FixtureBuilder()
.withNetworkControllerOnMainnet()
.build(),
...DEFAULT_MULTICHAIN_TEST_DAPP_FIXTURE_OPTIONS,
},
async ({
driver,
extensionId,
}: {
driver: Driver;
extensionId: string;
}) => {
const scopesToIgnore = ['eip155:42161', 'eip155:10'];
await openMultichainDappAndConnectWalletWithExternallyConnectable(
driver,
extensionId,
await driver.clickElement({ text: 'Connect', tag: 'button' });
await driver.switchToWindowWithTitle(
WINDOW_TITLES.MultichainTestDApp,
);
await initCreateSessionScopes(driver, [
'eip155:1',
...scopesToIgnore,
]);
await confirmAndSwitchFocusToDapp(driver);

const getSessionScopesResult = await getSessionScopes(driver);

for (const scope of scopesToIgnore) {
assert.strictEqual(
getSessionScopesResult.sessionScopes[scope],
undefined,
);
}
assert.strictEqual(
getSessionScopesResult.sessionScopes['eip155:10'],
undefined,
);

assert.ok(getSessionScopesResult.sessionScopes['eip155:1']);

assert.deepEqual(
getSessionScopesResult.sessionScopes['eip155:1'].accounts,
getExpectedSessionScope('eip155:1', [
DEFAULT_FIXTURE_ACCOUNT,
'0x09781764c08de8ca82e156bbf156a3ca217c7950',
]).accounts,
'Should add account 0x09781764c08de8ca82e156bbf156a3ca217c7950 to scope',
);
},
);
});
Expand Down
69 changes: 67 additions & 2 deletions test/e2e/flask/multichain-api/testHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
DAPP_URL,
largeDelayMs,
openDapp,
regularDelayMs,
unlockWallet,
WINDOW_TITLES,
} from '../../helpers';
Expand Down Expand Up @@ -108,6 +109,12 @@ export async function getSessionScopes(
return JSON.parse(await getSessionRawResult.getText());
}

/**
* Use dapp UI to send custom addresses to request scope for.
*
* @param driver - E2E test driver {@link Driver}, wrapping the Selenium WebDriver.
* @param accounts - The addresses to get session scope for.
*/
export async function addRequestAccountsToCreateSession(
driver: Driver,
accounts: [string, string],
Expand All @@ -121,7 +128,7 @@ export async function addRequestAccountsToCreateSession(
'input[type=text]',
);

// @ts-expect-error because this api is crazy
// @ts-expect-error Driver.findNestedElement injects `fill` method onto returned element, but typescript compiler will not let us access this method without a complaint, so we override it.
addressInput0.fill(accounts[0]);
await driver.clickElement({ text: '+', tag: 'button' });
await driver.delay(largeDelayMs);
Expand All @@ -136,9 +143,10 @@ export async function addRequestAccountsToCreateSession(
'input[type=text]',
);

// @ts-expect-error because this api is crazy
// @ts-expect-error refer above comment
addressInput1.fill(accounts[1]);
}

/**
* Retrieves the expected session scope for a given set of addresses.
*
Expand All @@ -152,6 +160,63 @@ export const getExpectedSessionScope = (scope: string, accounts: string[]) => ({
accounts: accounts.map((acc) => `${scope}:${acc.toLowerCase()}`),
});

export const addAndAuthorizeAccount = async (driver: Driver): Promise<void> => {
const editButtons = await driver.findElements('[data-testid="edit"]');
await editButtons[0].click();
await driver.clickElement({ text: 'New account', tag: 'button' });
await driver.clickElement({ text: 'Add account', tag: 'button' });
await driver.delay(regularDelayMs);

/**
* this needs to be called again, as previous element is stale and will not be found in current frame
*/
const freshEditButtons = await driver.findElements('[data-testid="edit"]');
await freshEditButtons[0].click();
await driver.delay(regularDelayMs);

const checkboxes = await driver.findElements('input[type="checkbox" i]');
await checkboxes[0].click(); // select all checkbox
await driver.delay(regularDelayMs);

await driver.clickElement({ text: 'Update', tag: 'button' });
};

/**
* Deselect all networks but Ethereum Mainnet through extension UI.
*
* @param driver - E2E test driver {@link Driver}, wrapping the Selenium WebDriver.
*/
export const uncheckNetworksExceptMainnet = async (
driver: Driver,
): Promise<void> => {
const editButtons = await driver.findElements('[data-testid="edit"]');
await editButtons[1].click();
await driver.delay(regularDelayMs);

const networkListItems = await driver.findElements(
'.multichain-network-list-item',
);

for (const item of networkListItems) {
const network = await item.getText();
const checkbox = await item.findElement(By.css('input[type="checkbox"]'));
const isChecked = await checkbox.isSelected();

// we make sure to uncheck every other previously selected network other than Ethereum Mainnet
if (isChecked && !network.includes('Ethereum Mainnet')) {
await checkbox.click();
await driver.delay(regularDelayMs);
}
}
await driver.clickElement({ text: 'Update', tag: 'button' });
};

/**
* Will assert only the requested networks are selected, and all other are not.
*
* @param driver - E2E test driver {@link Driver}, wrapping the Selenium WebDriver.
* @param networkList - list of networks to assert selection for.
*/
export const assertOnlyRequestedNetworksAreSelected = async (
driver: Driver,
networkList: string[],
Expand Down

0 comments on commit a68081e

Please sign in to comment.