From 0fbb5265c642753a7b171ed2b199ee47ad107dbe Mon Sep 17 00:00:00 2001 From: Pete Watters <2938440+pete-watters@users.noreply.github.com> Date: Fri, 23 Aug 2024 12:10:25 +0100 Subject: [PATCH] chore: improve open API and add tests, ref leather-io/issues#5800 --- src/background/messaging/rpc-methods/open.ts | 27 +++------ src/shared/rpc/methods/open.ts | 11 +--- tests/specs/rpc-open/open.spec.ts | 62 ++++++++++++++++++++ 3 files changed, 73 insertions(+), 27 deletions(-) create mode 100644 tests/specs/rpc-open/open.spec.ts diff --git a/src/background/messaging/rpc-methods/open.ts b/src/background/messaging/rpc-methods/open.ts index fcd7cd3f864..0d8c914e303 100644 --- a/src/background/messaging/rpc-methods/open.ts +++ b/src/background/messaging/rpc-methods/open.ts @@ -1,30 +1,21 @@ -import { RpcErrorCode } from '@btckit/types'; - import { RouteUrls } from '@shared/route-urls'; import { OpenRequest } from '@shared/rpc/methods/open'; -import { makeRpcErrorResponse } from '@shared/rpc/rpc-methods'; +import { makeRpcSuccessResponse } from '@shared/rpc/rpc-methods'; -import { - listenForPopupClose, - makeSearchParamsWithDefaults, - triggerRequestWindowOpen, -} from '../messaging-utils'; +import { makeSearchParamsWithDefaults, triggerRequestWindowOpen } from '../messaging-utils'; import { trackRpcRequestSuccess } from '../rpc-message-handler'; export async function rpcOpen(message: OpenRequest, port: chrome.runtime.Port) { const { urlParams, tabId } = makeSearchParamsWithDefaults(port, [['requestId', message.id]]); - const { id } = await triggerRequestWindowOpen(RouteUrls.Home, urlParams); + + await triggerRequestWindowOpen(RouteUrls.Home, urlParams); void trackRpcRequestSuccess({ endpoint: message.method }); - listenForPopupClose({ + chrome.tabs.sendMessage( tabId, - id, - response: makeRpcErrorResponse('open', { + makeRpcSuccessResponse('open', { id: message.id, - error: { - code: RpcErrorCode.USER_REJECTION, - message: 'User rejected request to open wallet', - }, - }), - }); + result: { message: 'Success' }, + }) + ); } diff --git a/src/shared/rpc/methods/open.ts b/src/shared/rpc/methods/open.ts index 2ee6a16d6f4..673aa2f091a 100644 --- a/src/shared/rpc/methods/open.ts +++ b/src/shared/rpc/methods/open.ts @@ -1,14 +1,7 @@ import { DefineRpcMethod, RpcRequest, RpcResponse } from '@btckit/types'; -import * as yup from 'yup'; -const rpcOpenParamsSchema = yup.object().shape({ - url: yup.string(), -}); +export type OpenRequest = RpcRequest<'open'>; -type OpenRequestParams = yup.InferType; - -export type OpenRequest = RpcRequest<'open', OpenRequestParams>; - -type OpenResponse = RpcResponse; +type OpenResponse = RpcResponse<{ message: string }>; export type Open = DefineRpcMethod; diff --git a/tests/specs/rpc-open/open.spec.ts b/tests/specs/rpc-open/open.spec.ts new file mode 100644 index 00000000000..f5252fc51ac --- /dev/null +++ b/tests/specs/rpc-open/open.spec.ts @@ -0,0 +1,62 @@ +import type { BrowserContext, Page } from '@playwright/test'; +import { TEST_PASSWORD } from '@tests/mocks/constants'; +import { HomePageSelectors } from '@tests/selectors/home.selectors'; + +import { test } from '../../fixtures/fixtures'; + +const successResponse = { + jsonrpc: '2.0', + result: { + message: 'Success', + }, +}; + +async function interceptRequestPopup(context: BrowserContext) { + return context.waitForEvent('page'); +} + +async function assertWalletHomeOpens(popup: Page) { + await popup.getByTestId(HomePageSelectors.HomePageContainer).waitFor(); + const button = popup.getByTestId(HomePageSelectors.FundAccountBtn); + await test.expect(button).toBeVisible(); +} +async function initiateOpen(page: Page) { + return page.evaluate(async () => (window as any).LeatherProvider?.request('open')); +} + +test.describe('Rpc: Open', () => { + test.beforeEach(async ({ extensionId, globalPage, onboardingPage }) => { + await globalPage.setupAndUseApiCalls(extensionId); + + await onboardingPage.signInWithTestAccount(extensionId); + }); + + test('the wallet opens successfully', async ({ page, context }) => { + await page.goto('localhost:3000'); + const openPromise = await initiateOpen(page); + + const popup = await interceptRequestPopup(context); + await assertWalletHomeOpens(popup); + + const result = await openPromise; + if (!result) throw new Error('Expected result'); + const { id, ...payloadWithoutId } = result; + + await test.expect(payloadWithoutId).toEqual(successResponse); + }); + + test('it forces user to unlock wallet when wallet is locked', async ({ + homePage, + page, + context, + }) => { + await homePage.lock(); + await page.goto('localhost:3000'); + const openPromise = initiateOpen(page); + const popup = await interceptRequestPopup(context); + await popup.getByRole('textbox').fill(TEST_PASSWORD); + await popup.getByRole('button', { name: 'Continue' }).click(); + await assertWalletHomeOpens(popup); + await test.expect(openPromise).resolves.toMatchObject(successResponse); + }); +});