Skip to content

Commit

Permalink
feat: rejection API with Cypress
Browse files Browse the repository at this point in the history
  • Loading branch information
matstyler committed Sep 9, 2024
1 parent 897be28 commit 2f4af7d
Show file tree
Hide file tree
Showing 8 changed files with 146 additions and 0 deletions.
30 changes: 30 additions & 0 deletions wallets/metamask/src/cypress/MetaMask.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,12 @@ export default class MetaMask {
})
}

async rejectTokenPermission() {
await this.metamaskPlaywright.rejectTokenPermission()

return true
}

// Network

async approveNewNetwork() {
Expand All @@ -206,6 +212,18 @@ export default class MetaMask {
return true
}

async rejectNewNetwork() {
await this.metamaskPlaywright.rejectNewNetwork()

return true
}

async rejectSwitchNetwork() {
await this.metamaskPlaywright.rejectSwitchNetwork()

return true
}

// Others

async providePublicEncryptionKey() {
Expand Down Expand Up @@ -241,6 +259,12 @@ export default class MetaMask {
})
}

async rejectSignature() {
await this.metamaskPlaywright.rejectSignature()

return true
}

async confirmTransaction(options?: { gasSetting?: GasSettings }) {
await waitFor(
() =>
Expand All @@ -254,6 +278,12 @@ export default class MetaMask {
return true
}

async rejectTransaction() {
await this.metamaskPlaywright.rejectTransaction()

return true
}

async confirmTransactionAndWaitForMining() {
return this.metamaskPlaywright
.confirmTransactionAndWaitForMining()
Expand Down
5 changes: 5 additions & 0 deletions wallets/metamask/src/cypress/configureSynpress.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ export default function configureSynpress(on: Cypress.PluginEvents, config: Cypr
addNetwork: (network: Network) => metamask?.addNetwork(network),
approveNewNetwork: () => metamask?.approveNewNetwork(),
approveSwitchNetwork: () => metamask?.approveSwitchNetwork(),
rejectNewNetwork: () => metamask?.rejectNewNetwork(),
rejectSwitchNetwork: () => metamask?.rejectSwitchNetwork(),

// Anvil
createAnvilNode: (options?: CreateAnvilOptions) => metamask?.createAnvilNode(options),
Expand All @@ -105,14 +107,17 @@ export default function configureSynpress(on: Cypress.PluginEvents, config: Cypr
spendLimit?: number | 'max'
gasSetting?: GasSettings
}) => metamask?.approveTokenPermission(options),
rejectTokenPermission: () => metamask?.rejectTokenPermission(),

// Encryption
providePublicEncryptionKey: () => metamask?.providePublicEncryptionKey(),
decrypt: () => metamask?.decrypt(),

// Transactions
confirmSignature: () => metamask?.confirmSignature(),
rejectSignature: () => metamask?.rejectSignature(),
confirmTransaction: (options?: { gasSetting?: GasSettings }) => metamask?.confirmTransaction(options),
rejectTransaction: () => metamask?.rejectTransaction(),
confirmTransactionAndWaitForMining: () => metamask?.confirmTransactionAndWaitForMining(),
openTransactionDetails: (txIndex: number) => metamask?.openTransactionDetails(txIndex),
closeTransactionDetails: () => metamask?.closeTransactionDetails()
Expand Down
20 changes: 20 additions & 0 deletions wallets/metamask/src/cypress/support/synpressCommands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,18 +37,23 @@ declare global {
addNetwork(network: Network): Chainable<void>
approveNewNetwork(): Chainable<void>
approveSwitchNetwork(): Chainable<void>
rejectNewNetwork(): Chainable<void>
rejectSwitchNetwork(): Chainable<void>

deployToken(): Chainable<void>
addNewToken(): Chainable<void>
approveTokenPermission(options?: {
spendLimit?: number | 'max'
gasSetting?: GasSettings
}): Chainable<void>
rejectTokenPermission(): Chainable<void>

providePublicEncryptionKey(): Chainable<void>
decrypt(): Chainable<void>
confirmSignature(): Chainable<void>
rejectSignature(): Chainable<void>
confirmTransaction(options?: { gasSetting?: GasSettings }): Chainable<void>
rejectTransaction(): Chainable<void>
confirmTransactionAndWaitForMining(): Chainable<void>
openTransactionDetails(txIndex: number): Chainable<void>
closeTransactionDetails(): Chainable<void>
Expand Down Expand Up @@ -122,6 +127,12 @@ export default function synpressCommands() {
Cypress.Commands.add('approveSwitchNetwork', () => {
return cy.task('approveSwitchNetwork')
})
Cypress.Commands.add('rejectNewNetwork', () => {
return cy.task('rejectNewNetwork')
})
Cypress.Commands.add('rejectSwitchNetwork', () => {
return cy.task('rejectSwitchNetwork')
})

// Token

Expand All @@ -140,6 +151,9 @@ export default function synpressCommands() {
return cy.task('approveTokenPermission', options)
}
)
Cypress.Commands.add('rejectTokenPermission', () => {
return cy.task('rejectTokenPermission')
})

// Others

Expand All @@ -152,9 +166,15 @@ export default function synpressCommands() {
Cypress.Commands.add('confirmSignature', () => {
return cy.task('confirmSignature')
})
Cypress.Commands.add('rejectSignature', () => {
return cy.task('rejectSignature')
})
Cypress.Commands.add('confirmTransaction', (options?: { gasSetting?: GasSettings }) => {
return cy.task('confirmTransaction', options)
})
Cypress.Commands.add('rejectTransaction', () => {
return cy.task('rejectTransaction')
})
Cypress.Commands.add('confirmTransactionAndWaitForMining', () => {
return cy.task('confirmTransactionAndWaitForMining')
})
Expand Down
18 changes: 18 additions & 0 deletions wallets/metamask/test/cypress/rejectAddNetwork.cy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
it('should reject new network request', () => {
cy.createAnvilNode({
chainId: 1338,
port: 8546
}).then(() => {
cy.get('#addEthereumChain').click()

cy.rejectNewNetwork().then(() => {
cy.get('#chainId').should('have.text', '0x1')

cy.emptyAnvilNode()
})
})
})

after(() => {
cy.switchNetwork('Ethereum Mainnet')
})
10 changes: 10 additions & 0 deletions wallets/metamask/test/cypress/rejectPermission.cy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
it('should reject approve request', () => {
cy.get('#tokenAddresses').should('be.empty')
cy.get('#createToken').click()

cy.confirmTransaction().then(() => {
cy.get('#approveTokens').click()

cy.rejectTokenPermission()
})
})
44 changes: 44 additions & 0 deletions wallets/metamask/test/cypress/rejectSignature.cy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
it('should reject `personal_sign`', () => {
cy.get('#personalSign').click()

cy.rejectSignature()

cy.get('#personalSign').should(
'have.text',
'Error: MetaMask Personal Message Signature: User denied message signature.'
)
cy.get('#personalSignResult').should('have.text', '')
})

it('should reject `eth_signTypedData`', () => {
cy.get('#signTypedData').click()

cy.rejectSignature()

cy.get('#signTypedDataResult').should(
'have.text',
'Error: MetaMask Typed Message Signature: User denied message signature.'
)
})

it('should reject `eth_signTypedData_v3`', () => {
cy.get('#signTypedDataV3').click()

cy.rejectSignature()

cy.get('#signTypedDataV3Result').should(
'have.text',
'Error: MetaMask Typed Message Signature: User denied message signature.'
)
})

it('should reject `eth_signTypedData_v4`', () => {
cy.get('#signTypedDataV4').click()

cy.rejectSignature()

cy.get('#signTypedDataV4Result').should(
'have.text',
'Error: MetaMask Typed Message Signature: User denied message signature.'
)
})
11 changes: 11 additions & 0 deletions wallets/metamask/test/cypress/rejectSwitchNetwork.cy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
it('should reject switch network request', () => {
cy.switchNetwork('Ethereum Mainnet').then(() => {
cy.get('#chainId').should('have.text', '0x1')

cy.get('#switchEthereumChain').click()

cy.rejectSwitchNetwork()

cy.get('#chainId').should('have.text', '0x1')
})
})
8 changes: 8 additions & 0 deletions wallets/metamask/test/cypress/rejectTransaction.cy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
it('should reject contract deployment', () => {
cy.get('#tokenAddresses').should('be.empty')
cy.get('#createToken').click()

cy.rejectTransaction().then(() => {
cy.get('#tokenAddresses').should('have.text', 'Creation Failed')
})
})

0 comments on commit 2f4af7d

Please sign in to comment.