-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add api spec test infrastructure (#9356)
<!-- 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 adds tests using [api-specs](https://github.com/MetaMask/api-specs) via the [@open-rpc/test-coverage](https://github.com/open-rpc/test-coverage) tool. <!-- 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? --> ## **Related issues** Fixes: https://github.com/MetaMask/MetaMask-planning/issues/2238 ## **Manual testing steps** 1. `yarn setup` 2. `yarn test:e2e:ios:debug:build` 3. `yarn test:api-specs` ## **Screenshots/Recordings** ![Screenshot 2024-06-18 at 3 25 26 PM](https://github.com/MetaMask/metamask-mobile/assets/364566/ecad8a8a-60ed-4f89-b85e-3e4ba34ef692) ![image](https://github.com/MetaMask/metamask-mobile/assets/364566/82e7bfc1-933b-4a14-80ca-ca7baf34904f) <!-- If applicable, add screenshots and/or recordings to visualize the before and after of your change. --> ## **Pre-merge author checklist** - [x] I’ve followed [MetaMask Coding Standards](https://github.com/MetaMask/metamask-mobile/blob/main/.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-mobile/blob/main/.github/guidelines/LABELING_GUIDELINES.md)). Not required for external contributors. ## **Pre-merge reviewer checklist** - [x] I've manually tested the PR (e.g. pull and build branch, run the app, test code being changed). - [x] 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. --------- Co-authored-by: Shane Jonas <jonas.shane@gmail.com> Co-authored-by: Curtis <Curtis.David7@gmail.com>
- Loading branch information
1 parent
12c15ce
commit 94d146e
Showing
15 changed files
with
1,141 additions
and
79 deletions.
There are no files selected for viewing
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
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 |
---|---|---|
|
@@ -113,3 +113,6 @@ app/lib/ppom/ppom.html.js | |
|
||
# ccache | ||
ccache | ||
|
||
# open-rpc/test-coverage | ||
html-report/ |
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
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
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,187 @@ | ||
import { device } from 'detox'; | ||
import { addToQueue } from './helpers'; | ||
import paramsToObj from '@open-rpc/test-coverage/build/utils/params-to-obj'; | ||
import TestHelpers from '../helpers'; | ||
import Matchers from '../utils/Matchers'; | ||
import Gestures from '../utils/Gestures'; | ||
import ConnectModal from '../pages/modals/ConnectModal'; | ||
import AssetWatchModal from '../pages/modals/AssetWatchModal'; | ||
|
||
// eslint-disable-next-line import/no-nodejs-modules | ||
import fs from 'fs'; | ||
|
||
import Assertions from '../utils/Assertions'; | ||
|
||
const getBase64FromPath = async (path) => { | ||
const data = await fs.promises.readFile(path); | ||
return data.toString('base64'); | ||
}; | ||
|
||
export default class ConfirmationsRejectRule { | ||
constructor(options) { | ||
this.driver = options.driver; // Pass element for detox instead of all the driver | ||
this.only = options.only; | ||
this.allCapsCancel = ['wallet_watchAsset']; | ||
this.requiresEthAccountsPermission = [ | ||
'personal_sign', | ||
'eth_signTypedData_v4', | ||
'eth_getEncryptionPublicKey', | ||
]; | ||
} | ||
|
||
getTitle() { | ||
return 'Confirmations Rejection Rule'; | ||
} | ||
|
||
async beforeRequest(_, call) { | ||
await new Promise((resolve, reject) => { | ||
addToQueue({ | ||
name: 'beforeRequest', | ||
resolve, | ||
reject, | ||
task: async () => { | ||
if (this.requiresEthAccountsPermission.includes(call.methodName)) { | ||
const requestPermissionsRequest = JSON.stringify({ | ||
jsonrpc: '2.0', | ||
method: 'wallet_requestPermissions', | ||
params: [{ eth_accounts: {} }], | ||
}); | ||
|
||
await this.driver.runScript(`(el) => { | ||
window.ethereum.request(${requestPermissionsRequest}) | ||
}`); | ||
|
||
/** | ||
* | ||
* Screenshot Code Section | ||
* | ||
*/ | ||
|
||
// Connect accounts modal | ||
await Assertions.checkIfVisible(ConnectModal.container); | ||
await ConnectModal.tapConnectButton(); | ||
await Assertions.checkIfNotVisible(ConnectModal.container); | ||
await TestHelpers.delay(3000); | ||
} | ||
|
||
// we need this because mobile doesnt support just raw json signTypedData, it requires a stringified version | ||
// it was fixed and should get it in @metamask/message-manager@7.0.1 | ||
if (call.methodName === 'eth_signTypedData_v4') { | ||
call.params[1] = JSON.stringify(call.params[1]); | ||
} | ||
}, | ||
}); | ||
}); | ||
} | ||
|
||
// get all the confirmation calls to make and expect to pass | ||
// Need this now? | ||
getCalls(_, method) { | ||
const calls = []; | ||
const isMethodAllowed = this.only ? this.only.includes(method.name) : true; | ||
if (isMethodAllowed) { | ||
if (method.examples) { | ||
// pull the first example | ||
const e = method.examples[0]; | ||
const ex = e; | ||
|
||
if (!ex.result) { | ||
return calls; | ||
} | ||
const p = ex.params.map((e) => e.value); | ||
const params = | ||
method.paramStructure === 'by-name' | ||
? paramsToObj(p, method.params) | ||
: p; | ||
calls.push({ | ||
title: `${this.getTitle()} - with example ${ex.name}`, | ||
methodName: method.name, | ||
params, | ||
url: '', | ||
resultSchema: method.result.schema, | ||
expectedResult: ex.result.value, | ||
}); | ||
} else { | ||
// naively call the method with no params | ||
calls.push({ | ||
title: `${method.name} > confirmation rejection`, | ||
methodName: method.name, | ||
params: [], | ||
url: '', | ||
resultSchema: method.result.schema, | ||
}); | ||
} | ||
} | ||
return calls; | ||
} | ||
|
||
async afterRequest(_, call) { | ||
await new Promise((resolve, reject) => { | ||
addToQueue({ | ||
name: 'afterRequest', | ||
resolve, | ||
reject, | ||
task: async () => { | ||
await TestHelpers.delay(3000); | ||
const imagePath = await device.takeScreenshot( | ||
`afterRequest-${this.getTitle()}`, | ||
); | ||
const image = await getBase64FromPath(imagePath); | ||
call.attachments = call.attachments || []; | ||
call.attachments.push({ | ||
data: `data:image/png;base64,${image}`, | ||
image, | ||
type: 'image', | ||
}); | ||
let cancelButton; | ||
await TestHelpers.delay(3000); | ||
if (this.allCapsCancel.includes(call.methodName)) { | ||
await AssetWatchModal.tapCancelButton(); | ||
} else { | ||
cancelButton = await Matchers.getElementByText('Cancel'); | ||
await Gestures.waitAndTap(cancelButton); | ||
} | ||
}, | ||
}); | ||
}); | ||
|
||
/** | ||
* | ||
* Screen shot code section | ||
*/ | ||
} | ||
|
||
async afterResponse(_, call) { | ||
await new Promise((resolve, reject) => { | ||
addToQueue({ | ||
name: 'afterResponse', | ||
resolve, | ||
reject, | ||
task: async () => { | ||
// Revoke Permissions | ||
if (this.requiresEthAccountsPermission.includes(call.methodName)) { | ||
const revokePermissionRequest = JSON.stringify({ | ||
jsonrpc: '2.0', | ||
method: 'wallet_revokePermissions', | ||
params: [{ eth_accounts: {} }], | ||
}); | ||
|
||
await this.driver.runScript(`(el) => { | ||
window.ethereum.request(${revokePermissionRequest}) | ||
}`); | ||
} | ||
}, | ||
}); | ||
}); | ||
} | ||
|
||
validateCall(call) { | ||
if (call.error) { | ||
call.valid = call.error.code === 4001; | ||
if (!call.valid) { | ||
call.reason = `Expected error code 4001, got ${call.error.code}`; | ||
} | ||
} | ||
return call; | ||
} | ||
} |
Oops, something went wrong.