-
Notifications
You must be signed in to change notification settings - Fork 5k
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
Multichain API E2E Test: wallet_revokeSession #29639
Open
ffmcgee725
wants to merge
9
commits into
jl/caip-multichain-migrate-core
Choose a base branch
from
jc/wallet_revokeSession-test-dapp-e2e
base: jl/caip-multichain-migrate-core
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+170
−0
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
c7087f4
test: multichain test dapp wallet_revokeSession e2e tests
ffmcgee725 2296b30
Merge branch 'jl/caip-multichain-migrate-core' into jc/wallet_revokeS…
ffmcgee725 4039417
refactor: code review improvements
ffmcgee725 28712d7
refactor: code review improvements
ffmcgee725 9034264
fix: address typo
ffmcgee725 9fd065e
fix: address typo
ffmcgee725 6301012
refactor: minor message adjustment
ffmcgee725 635cb04
refactor: minor message adjustment
ffmcgee725 da2a498
Merge branch 'jl/caip-multichain-migrate-core' into jc/wallet_revokeS…
adonesky1 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
170 changes: 170 additions & 0 deletions
170
test/e2e/flask/multichain-api/wallet_revokeSession.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,170 @@ | ||
import { strict as assert } from 'assert'; | ||
import { | ||
ACCOUNT_1, | ||
ACCOUNT_2, | ||
largeDelayMs, | ||
WINDOW_TITLES, | ||
withFixtures, | ||
} from '../../helpers'; | ||
import { Driver } from '../../webdriver/driver'; | ||
import FixtureBuilder from '../../fixture-builder'; | ||
import { | ||
initCreateSessionScopes, | ||
DEFAULT_MULTICHAIN_TEST_DAPP_FIXTURE_OPTIONS, | ||
openMultichainDappAndConnectWalletWithExternallyConnectable, | ||
addAccountInWalletAndAuthorize, | ||
getSessionScopes, | ||
} from './testHelpers'; | ||
|
||
describe('Initializing a session w/ several scopes and accounts, then calling `wallet_revokeSession`', function () { | ||
const GANACHE_SCOPES = ['eip155:1337', 'eip155:1338', 'eip155:1000']; | ||
const ACCOUNTS = [ACCOUNT_1, ACCOUNT_2]; | ||
it('Should return empty object from `wallet_getSession` call', async function () { | ||
await withFixtures( | ||
{ | ||
title: this.test?.fullTitle(), | ||
fixtures: new FixtureBuilder() | ||
.withNetworkControllerTripleGanache() | ||
.build(), | ||
...DEFAULT_MULTICHAIN_TEST_DAPP_FIXTURE_OPTIONS, | ||
}, | ||
async ({ | ||
driver, | ||
extensionId, | ||
}: { | ||
driver: Driver; | ||
extensionId: string; | ||
}) => { | ||
await openMultichainDappAndConnectWalletWithExternallyConnectable( | ||
driver, | ||
extensionId, | ||
); | ||
await initCreateSessionScopes(driver, GANACHE_SCOPES, ACCOUNTS); | ||
await addAccountInWalletAndAuthorize(driver); | ||
await driver.clickElement({ text: 'Connect', tag: 'button' }); | ||
await driver.delay(largeDelayMs); | ||
await driver.switchToWindowWithTitle(WINDOW_TITLES.MultichainTestDApp); | ||
|
||
/** | ||
* We verify that scopes are not empty before calling `wallet_revokeSession` | ||
*/ | ||
const { sessionScopes } = await getSessionScopes(driver); | ||
for (const scope of GANACHE_SCOPES) { | ||
assert.notStrictEqual( | ||
sessionScopes[scope], | ||
undefined, | ||
`scope ${scope} should not be empty.`, | ||
); | ||
} | ||
|
||
await driver.clickElement({ | ||
text: 'wallet_revokeSession', | ||
tag: 'span', | ||
}); | ||
|
||
const parsedResult = await getSessionScopes(driver); | ||
const resultSessionScopes = parsedResult.sessionScopes; | ||
assert.deepStrictEqual( | ||
resultSessionScopes, | ||
{}, | ||
'Should receive an empty session scope after calling `wallet_getSession`', | ||
); | ||
}, | ||
); | ||
}); | ||
|
||
it('Should throw an error if `wallet_invokeMethod` is called afterwards', async function () { | ||
await withFixtures( | ||
{ | ||
title: this.test?.fullTitle(), | ||
fixtures: new FixtureBuilder() | ||
.withNetworkControllerTripleGanache() | ||
.build(), | ||
...DEFAULT_MULTICHAIN_TEST_DAPP_FIXTURE_OPTIONS, | ||
}, | ||
async ({ | ||
driver, | ||
extensionId, | ||
}: { | ||
driver: Driver; | ||
extensionId: string; | ||
}) => { | ||
await openMultichainDappAndConnectWalletWithExternallyConnectable( | ||
driver, | ||
extensionId, | ||
); | ||
const expectedError = { | ||
code: 4100, | ||
message: | ||
'The requested account and/or method has not been authorized by the user.', | ||
}; | ||
|
||
await initCreateSessionScopes(driver, GANACHE_SCOPES, ACCOUNTS); | ||
await addAccountInWalletAndAuthorize(driver); | ||
await driver.clickElement({ text: 'Connect', tag: 'button' }); | ||
await driver.delay(largeDelayMs); | ||
await driver.switchToWindowWithTitle(WINDOW_TITLES.MultichainTestDApp); | ||
|
||
await driver.clickElement({ | ||
text: 'wallet_revokeSession', | ||
tag: 'span', | ||
}); | ||
|
||
for (const scope of GANACHE_SCOPES) { | ||
const id = 1999133338649204; | ||
const data = JSON.stringify({ | ||
id, | ||
jsonrpc: '2.0', | ||
method: 'wallet_invokeMethod', | ||
params: { | ||
scope, | ||
request: { | ||
method: 'eth_getBalance', | ||
params: [ACCOUNT_1, 'latest'], | ||
}, | ||
}, | ||
}); | ||
|
||
const script = ` | ||
const port = chrome.runtime.connect('${extensionId}'); | ||
const data = ${data}; | ||
const result = new Promise((resolve) => { | ||
port.onMessage.addListener((msg) => { | ||
if (msg.type !== 'caip-x') { | ||
return; | ||
} | ||
if (msg.data?.id !== ${id}) { | ||
return; | ||
} | ||
|
||
if (msg.data.id || msg.data.error) { | ||
resolve(msg) | ||
} | ||
}) | ||
}) | ||
port.postMessage({ type: 'caip-x', data }); | ||
return result;`; | ||
|
||
/** | ||
* We call `executeScript` to attempt JSON rpc call directly through the injected provider object since when session is revoked, | ||
* webapp does not provide UI to make call. | ||
*/ | ||
const actualError = await driver | ||
.executeScript(script) | ||
.then((res) => res.data?.error); | ||
|
||
/** | ||
* We make sure it's the expected error by comparing expected error code and message (we ignore `stack` property) | ||
*/ | ||
Object.entries(expectedError).forEach(([key, value]) => { | ||
assert.deepEqual( | ||
value, | ||
actualError[key], | ||
`calling wallet_invokeMethod should throw an error with ${key} ${value} for scope ${scope}`, | ||
); | ||
}); | ||
} | ||
}, | ||
); | ||
}); | ||
}); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ideally in this section I'd prefer something along the lines of
Which jest's API allows me to do, but node's strict doesn't seem to have a similar feature. Open to suggestion on a cleaner way of asserting here.