Skip to content

Commit

Permalink
Merge branch 'main' into rename-ci-env-var-names
Browse files Browse the repository at this point in the history
  • Loading branch information
sidvishnoi authored Oct 10, 2024
2 parents 37ff655 + dd25b24 commit c438a38
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 28 deletions.
7 changes: 2 additions & 5 deletions src/background/services/keyAutoAdd.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {
ErrorWithKey,
ensureEnd,
errorWithKeyToJSON,
getJWKS,
isErrorWithKey,
withResolvers,
type ErrorWithKeyLike,
Expand Down Expand Up @@ -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');
}
Expand Down
9 changes: 8 additions & 1 deletion src/shared/helpers.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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`
*/
Expand Down
13 changes: 5 additions & 8 deletions tests/e2e/connectAutoKeyTestWallet.spec.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -22,8 +21,6 @@ test('Connect to test wallet with automatic key addition when not logged-in to w
const password = process.env.TEST_WALLET_PASSWORD;
const walletAddressUrl = process.env.TEST_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 () => {
Expand Down Expand Up @@ -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 });
Expand Down Expand Up @@ -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();
});

Expand Down
13 changes: 8 additions & 5 deletions tests/e2e/helpers/common.ts
Original file line number Diff line number Diff line change
@@ -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<JWKS>);
}, 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',
);
}
10 changes: 1 addition & 9 deletions tests/e2e/helpers/testWallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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: {
Expand Down

0 comments on commit c438a38

Please sign in to comment.