From dbc5fe07437df5118aa7836750c4652a7b784981 Mon Sep 17 00:00:00 2001 From: frazarshad Date: Sun, 3 Mar 2024 22:50:43 -0800 Subject: [PATCH] Added Interaction to handle rejection of wallet connection (#8) * feat: added code to handle reject wallet access * feat: added test case for reject wallet access + modified test structure --- commands/keplr.js | 8 +++++- plugins/keplr-plugin.js | 17 ++++++----- support/commands.js | 10 +++++-- tests/e2e/specs/keplr/keplr-spec.js | 44 +++++++++++++++++++++-------- 4 files changed, 55 insertions(+), 24 deletions(-) diff --git a/commands/keplr.js b/commands/keplr.js index 148563db0..10c26612a 100644 --- a/commands/keplr.js +++ b/commands/keplr.js @@ -37,7 +37,7 @@ const keplr = { async disconnectWalletFromDapp() { await playwright.waitAndClickByText( 'Disconnect All', - playwright.keplrPermissionWindow() + playwright.keplrPermissionWindow(), ); return true; }, @@ -166,6 +166,12 @@ const keplr = { return true; }, + async rejectAccess() { + const notificationPage = await playwright.switchToKeplrNotification(); + await notificationPage.close(); + return true; + }, + async confirmTransaction() { const notificationPage = await playwright.switchToKeplrNotification(); await playwright.waitAndClick( diff --git a/plugins/keplr-plugin.js b/plugins/keplr-plugin.js index 1b9430d4a..f556721fd 100644 --- a/plugins/keplr-plugin.js +++ b/plugins/keplr-plugin.js @@ -32,7 +32,8 @@ module.exports = (on, config) => { if (!process.env.SKIP_KEPLR_INSTALL) { // NOTE: extensions cannot be loaded in headless Chrome const keplrPath = await helpers.prepareExtension( - process.env.KEPLR_VERSION || '0.12.68', process.env.EXTENSION + process.env.KEPLR_VERSION || '0.12.68', + process.env.EXTENSION, ); arguments_.extensions.push(keplrPath); } @@ -59,27 +60,25 @@ module.exports = (on, config) => { clearWindows: playwright.clearWindows, isCypressWindowActive: playwright.isCypressWindowActive, switchToExtensionWindow: playwright.switchToKeplrWindow, - switchToExtensionRegistrationWindow: playwright.switchToKeplrRegistrationWindow, + switchToExtensionRegistrationWindow: + playwright.switchToKeplrRegistrationWindow, switchToExtensionPermissionWindow: playwright.switchToKeplrPermissionWindow, // keplr commands importWallet: keplr.importWallet, acceptAccess: keplr.acceptAccess, + rejectAccess: keplr.rejectAccess, confirmTransaction: keplr.confirmTransaction, disconnectWalletFromDapp: keplr.disconnectWalletFromDapp, - setupWallet: async ({ - secretWordsOrPrivateKey, - password, - newAccount, - }) => { + setupWallet: async ({ secretWordsOrPrivateKey, password, newAccount }) => { await keplr.initialSetup(null, { secretWordsOrPrivateKey, password, - newAccount + newAccount, }); return true; }, }); return config; -}; \ No newline at end of file +}; diff --git a/support/commands.js b/support/commands.js index 1e8053867..8961b6d85 100644 --- a/support/commands.js +++ b/support/commands.js @@ -415,12 +415,12 @@ Cypress.Commands.add( ( secretWordsOrPrivateKey = 'orbit bench unit task food shock brand bracket domain regular warfare company announce wheel grape trust sphere boy doctor half guard ritual three ecology', password = 'Test1234', - newAccount = false + newAccount = false, ) => { return cy.task('setupWallet', { secretWordsOrPrivateKey, password, - newAccount + newAccount, }); }, ); @@ -429,6 +429,10 @@ Cypress.Commands.add('acceptAccess', () => { return cy.task('acceptAccess'); }); +Cypress.Commands.add('rejectAccess', () => { + return cy.task('rejectAccess'); +}); + Cypress.Commands.add('confirmTransaction', () => { return cy.task('confirmTransaction'); }); @@ -451,4 +455,4 @@ Cypress.Commands.add('switchToExtensionPermissionWindow', () => { Cypress.Commands.add('disconnectWalletFromDapp', () => { return cy.task('disconnectWalletFromDapp'); -}); \ No newline at end of file +}); diff --git a/tests/e2e/specs/keplr/keplr-spec.js b/tests/e2e/specs/keplr/keplr-spec.js index 808231d37..7afb6a698 100644 --- a/tests/e2e/specs/keplr/keplr-spec.js +++ b/tests/e2e/specs/keplr/keplr-spec.js @@ -1,23 +1,45 @@ /* eslint-disable ui-testing/no-disabled-tests */ describe('Keplr', () => { context('Test commands', () => { - it(`should complete Keplr connect with wallet, and confirm transaction after importing an existing wallet using 24 word phrase`, () => { + it(`should complete Keplr setup by importing an existing wallet using 24 word phrase`, () => { cy.setupWallet().then(setupFinished => { expect(setupFinished).to.be.true; + }); + cy.visit('/'); + }); + it(`should reject connect with wallet`, () => { + const alertShown = cy.stub().as('alertShown'); + cy.on('window:alert', alertShown); - cy.visit('/'); - cy.contains('Connect Wallet').click(); - cy.acceptAccess().then(taskCompleted => { - expect(taskCompleted).to.be.true; - - cy.contains('Make an Offer').click(); - cy.confirmTransaction().then(taskCompleted => { - expect(taskCompleted).to.be.true; - }); - }); + cy.contains('Connect Wallet').click(); + cy.rejectAccess().then(rejected => { + expect(rejected).to.be.true; + }); + cy.get('@alertShown').should( + 'have.been.calledOnceWith', + 'Request rejected', + ); + }); + it(`should accept connection with wallet`, () => { + cy.contains('Connect Wallet').click(); + cy.acceptAccess().then(taskCompleted => { + expect(taskCompleted).to.be.true; }); }); + it(`should confirm make an offer transaction`, () => { + const alertShown = cy.stub().as('alertShown'); + cy.on('window:alert', alertShown); + cy.contains('Make an Offer').click(); + cy.confirmTransaction().then(taskCompleted => { + expect(taskCompleted).to.be.true; + }); + + cy.get('@alertShown').should( + 'have.been.calledOnceWith', + 'Offer accepted', + ); + }); it(`should complete Keplr connect with wallet, and confirm transaction after creating a new wallet using 24 word phrase`, () => { cy.switchToExtensionRegistrationWindow().then(() => { cy.setupWallet(