From dd25b2447338dd5f6aacfb2bbb5c151ebe0c8c1f Mon Sep 17 00:00:00 2001 From: Sid Vishnoi <8426945+sidvishnoi@users.noreply.github.com> Date: Thu, 10 Oct 2024 14:55:38 +0530 Subject: [PATCH] refactor: extract some common helpers (#651) --- src/background/services/keyAutoAdd.ts | 7 ++----- src/shared/helpers.ts | 9 ++++++++- tests/e2e/connectAutoKeyTestWallet.spec.ts | 13 +++++-------- tests/e2e/helpers/common.ts | 13 ++++++++----- tests/e2e/helpers/testWallet.ts | 10 +--------- 5 files changed, 24 insertions(+), 28 deletions(-) diff --git a/src/background/services/keyAutoAdd.ts b/src/background/services/keyAutoAdd.ts index 302f764b..cfc489ce 100644 --- a/src/background/services/keyAutoAdd.ts +++ b/src/background/services/keyAutoAdd.ts @@ -1,7 +1,7 @@ import { ErrorWithKey, - ensureEnd, errorWithKeyToJSON, + getJWKS, isErrorWithKey, withResolvers, type ErrorWithKeyLike, @@ -151,10 +151,7 @@ export class KeyAutoAddService { } private async validate(walletAddressUrl: string, keyId: string) { - type JWKS = { keys: { kid: string }[] }; - const jwksUrl = new URL('jwks.json', ensureEnd(walletAddressUrl, '/')); - const res = await fetch(jwksUrl.toString()); - const jwks: JWKS = await res.json(); + const jwks = await getJWKS(walletAddressUrl); if (!jwks.keys.find((key) => key.kid === keyId)) { throw new Error('Key not found in jwks'); } diff --git a/src/shared/helpers.ts b/src/shared/helpers.ts index eb5882c1..2a4ea470 100644 --- a/src/shared/helpers.ts +++ b/src/shared/helpers.ts @@ -1,5 +1,5 @@ import type { SuccessResponse } from '@/shared/messages'; -import type { WalletAddress } from '@interledger/open-payments/dist/types'; +import type { WalletAddress, JWKS } from '@interledger/open-payments'; import { cx, CxOptions } from 'class-variance-authority'; import { twMerge } from 'tailwind-merge'; import { addSeconds } from 'date-fns/addSeconds'; @@ -67,6 +67,13 @@ export const getWalletInformation = async ( return json; }; +export const getJWKS = async (walletAddressUrl: string) => { + const jwksUrl = new URL('jwks.json', ensureEnd(walletAddressUrl, '/')); + const res = await fetch(jwksUrl.href); + const json = await res.json(); + return json as JWKS; +}; + /** * Error object with key and substitutions based on `_locales/[lang]/messages.json` */ diff --git a/tests/e2e/connectAutoKeyTestWallet.spec.ts b/tests/e2e/connectAutoKeyTestWallet.spec.ts index 9e7fc5c3..37da2713 100644 --- a/tests/e2e/connectAutoKeyTestWallet.spec.ts +++ b/tests/e2e/connectAutoKeyTestWallet.spec.ts @@ -1,15 +1,14 @@ import { test, expect } from './fixtures/base'; -import { ensureEnd, withResolvers } from '@/shared/helpers'; +import { withResolvers, getJWKS } from '@/shared/helpers'; import { disconnectWallet, fillPopup } from './pages/popup'; +import { waitForWelcomePage } from './helpers/common'; import { acceptGrant, KEYS_PAGE_URL, getContinueWaitTime, revokeKey, waitForGrantConsentPage, - waitForWelcomePage, } from './helpers/testWallet'; -import { getJWKS } from './helpers/common'; test('Connect to test wallet with automatic key addition when not logged-in to wallet', async ({ page, @@ -22,8 +21,6 @@ test('Connect to test wallet with automatic key addition when not logged-in to w const password = process.env.WALLET_PASSWORD!; const walletAddressUrl = process.env.CONNECT_WALLET_ADDRESS_URL!; - const jwksUrl = new URL('jwks.json', ensureEnd(walletAddressUrl, '/')).href; - const loginPageUrl = `https://rafiki.money/auth/login?callbackUrl=%2Fsettings%2Fdeveloper-keys`; const connectButton = await test.step('fill popup', async () => { @@ -111,14 +108,14 @@ test('Connect to test wallet with automatic key addition when not logged-in to w return chrome.storage.local.get<{ keyId: string }>(['keyId']); }); - const jwksBefore = await getJWKS(page, jwksUrl); + const jwksBefore = await getJWKS(walletAddressUrl); expect(jwksBefore.keys.length).toBeGreaterThanOrEqual(0); expect(jwksBefore.keys.find((key) => key.kid === keyId)).toBeUndefined(); pause.resolve(); const { accountId, walletId } = await promise; - const jwks = await getJWKS(page, jwksUrl); + const jwks = await getJWKS(walletAddressUrl); expect(jwks.keys.length).toBeGreaterThan(0); const key = jwks.keys.find((key) => key.kid === keyId); expect(key).toMatchObject({ kid: keyId }); @@ -146,7 +143,7 @@ test('Connect to test wallet with automatic key addition when not logged-in to w await test.step('revoke key', async () => { await revokeKey(page, revokeInfo); - const { keys } = await getJWKS(page, jwksUrl); + const { keys } = await getJWKS(walletAddressUrl); expect(keys.find((key) => key.kid === revokeInfo.keyId)).toBeUndefined(); }); diff --git a/tests/e2e/helpers/common.ts b/tests/e2e/helpers/common.ts index 6e6fb7c5..08164ba0 100644 --- a/tests/e2e/helpers/common.ts +++ b/tests/e2e/helpers/common.ts @@ -1,8 +1,11 @@ import type { Page } from '@playwright/test'; -export async function getJWKS(page: Page, jwksUrl: string) { - type JWKS = { keys: { kid: string }[] }; - return await page.evaluate(async (jwksUrl) => { - return await fetch(jwksUrl).then((r) => r.json() as Promise); - }, jwksUrl); +const OPEN_PAYMENTS_REDIRECT_URL = `https://webmonetization.org/welcome`; + +export async function waitForWelcomePage(page: Page) { + await page.waitForURL( + (url) => + url.href.startsWith(OPEN_PAYMENTS_REDIRECT_URL) && + url.searchParams.get('result') === 'grant_success', + ); } diff --git a/tests/e2e/helpers/testWallet.ts b/tests/e2e/helpers/testWallet.ts index c4c6a414..96d323a4 100644 --- a/tests/e2e/helpers/testWallet.ts +++ b/tests/e2e/helpers/testWallet.ts @@ -7,9 +7,9 @@ import { } from '../fixtures/helpers'; import { fillPopup, type Popup, type ConnectDetails } from '../pages/popup'; import { getWalletInformation } from '@/shared/helpers'; +import { waitForWelcomePage } from './common'; export const KEYS_PAGE_URL = `https://rafiki.money/settings/developer-keys`; -const CONFIG_OPEN_PAYMENTS_REDIRECT_URL = `https://webmonetization.org/welcome`; export async function connectWallet( context: BrowserContext, @@ -84,14 +84,6 @@ export async function acceptGrant(page: Page, continueWaitMs: number) { await page.getByRole('button', { name: 'Accept' }).click(); } -export async function waitForWelcomePage(page: Page) { - await page.waitForURL( - (url) => - url.href.startsWith(CONFIG_OPEN_PAYMENTS_REDIRECT_URL) && - url.searchParams.get('result') === 'grant_success', - ); -} - export async function revokeKey( page: Page, info: {