mirror of
https://github.com/SableClient/Sable.git
synced 2026-11-21 17:41:25 -08:00
test(e2e): cover the device verification reset UIA path (#1375)
<!-- Please read https://github.com/SableClient/Sable/blob/dev/CONTRIBUTING.md before submitting your pull request --> ### Description <!-- Please include a summary of the change. Please also include relevant motivation and context. List any dependencies that are required for this change. --> Fixes # #### Type of change - [ ] Bug fix (non-breaking change which fixes an issue) - [ ] New feature (non-breaking change which adds functionality) - [ ] Breaking change (fix or feature that would cause existing functionality to not work as expected) - [ ] This change requires a documentation update ### Checklist: - [ ] My code follows the style guidelines of this project - [ ] I have performed a self-review of my own code - [ ] I have commented my code, particularly in hard-to-understand areas - [ ] I have made corresponding changes to the documentation - [ ] My changes generate no new warnings ### AI disclosure: - [ ] Partially AI assisted (clarify which code was AI assisted and briefly explain what it does). - [ ] Fully AI generated (explain what all the generated code does in moderate detail). <!-- Write any explanation required here, but do not generate the explanation using AI!! You must prove you understand what the code in this PR does. -->
This commit is contained in:
1 file changed
+173
@@ -0,0 +1,173 @@
|
||||
import { readFile } from 'node:fs/promises';
|
||||
import { test, expect, type Page } from '@playwright/test';
|
||||
import { registerUser } from './fixtures/continuwuity';
|
||||
|
||||
const PASSWORD = 'test-passw0rd';
|
||||
const UPLOAD_ROUTE = '**/_matrix/client/*/keys/device_signing/upload';
|
||||
const TICKET_PATH = '/e2e-oauth-ticket';
|
||||
const SESSION = 'e2e-reset-oauth-session';
|
||||
|
||||
type AuthDict = { type?: string; session?: string };
|
||||
|
||||
type InjectedSession = {
|
||||
baseUrl: string;
|
||||
userId: string;
|
||||
deviceId: string;
|
||||
accessToken: string;
|
||||
slidingSyncOptIn?: boolean;
|
||||
};
|
||||
|
||||
async function homeserverBaseUrl(storageStatePath: string): Promise<string> {
|
||||
const state = JSON.parse(await readFile(storageStatePath, 'utf8')) as {
|
||||
origins: { localStorage: { name: string; value: string }[] }[];
|
||||
};
|
||||
const entry = state.origins[0]!.localStorage.find((item) => item.name === 'matrixSessions')!;
|
||||
return (JSON.parse(entry.value) as InjectedSession[])[0]!.baseUrl;
|
||||
}
|
||||
|
||||
async function loginAsFreshUser(
|
||||
page: Page,
|
||||
baseUrl: string,
|
||||
name: string,
|
||||
slidingSync = false
|
||||
): Promise<void> {
|
||||
const user = await registerUser(baseUrl, name, PASSWORD);
|
||||
const session: InjectedSession = {
|
||||
baseUrl,
|
||||
userId: user.userId,
|
||||
deviceId: user.deviceId,
|
||||
accessToken: user.accessToken,
|
||||
...(slidingSync ? { slidingSyncOptIn: true } : {}),
|
||||
};
|
||||
await page.addInitScript((injected: InjectedSession) => {
|
||||
localStorage.setItem('matrixSessions', JSON.stringify([injected]));
|
||||
localStorage.setItem('matrixActiveSession', JSON.stringify(injected.userId));
|
||||
localStorage.setItem('dismissNotice', 'true');
|
||||
}, session);
|
||||
}
|
||||
|
||||
const recoveryKeyShown = (page: Page) => page.getByRole('button', { name: 'Copy', exact: true });
|
||||
|
||||
test.describe('device verification reset', () => {
|
||||
test('completes the UIA stage and reveals a new recovery key', async ({ page }, testInfo) => {
|
||||
test.setTimeout(180_000);
|
||||
const storageStatePath = testInfo.project.use.storageState as string;
|
||||
const hsBaseUrl = await homeserverBaseUrl(storageStatePath);
|
||||
await loginAsFreshUser(page, hsBaseUrl, `reset-uia-${testInfo.project.name}-${process.pid}`);
|
||||
|
||||
await page.goto('/settings/devices');
|
||||
|
||||
await page.getByRole('button', { name: 'Enable' }).click({ timeout: 180_000 });
|
||||
await expect(page.getByText('Setup Device Verification')).toBeVisible();
|
||||
await page.locator('form').getByRole('button', { name: 'Continue' }).click();
|
||||
await expect(recoveryKeyShown(page)).toBeVisible({ timeout: 120_000 });
|
||||
|
||||
await page.reload();
|
||||
await page
|
||||
.locator('#device-verification')
|
||||
.locator('button[aria-pressed]')
|
||||
.click({ timeout: 180_000 });
|
||||
await page.getByRole('button', { name: 'Reset', exact: true }).click();
|
||||
|
||||
await expect(page.getByText('Reset Device Verification')).toBeVisible();
|
||||
await page.getByRole('button', { name: 'Reset', exact: true }).last().click();
|
||||
await page.locator('input[name="passphraseInput"]').fill('a memorable passphrase');
|
||||
await page.locator('form').getByRole('button', { name: 'Continue' }).click();
|
||||
|
||||
await expect(page.getByText('Account Password', { exact: true })).toBeVisible({
|
||||
timeout: 60_000,
|
||||
});
|
||||
const password = page.locator('input[name="passwordInput"]');
|
||||
await password.fill(PASSWORD);
|
||||
await password.press('Enter');
|
||||
|
||||
await expect(recoveryKeyShown(page)).toBeVisible({ timeout: 120_000 });
|
||||
await expect(page.getByText('Account Password', { exact: true })).toBeHidden();
|
||||
});
|
||||
|
||||
test('completes the m.oauth UIA stage and reveals a new recovery key', async ({
|
||||
page,
|
||||
baseURL,
|
||||
}, testInfo) => {
|
||||
test.setTimeout(180_000);
|
||||
const storageStatePath = testInfo.project.use.storageState as string;
|
||||
const hsBaseUrl = await homeserverBaseUrl(storageStatePath);
|
||||
await loginAsFreshUser(page, hsBaseUrl, `reset-oauth-${testInfo.project.name}-${process.pid}`);
|
||||
|
||||
await page.goto('/settings/devices');
|
||||
|
||||
await page.getByRole('button', { name: 'Enable' }).click({ timeout: 180_000 });
|
||||
await expect(page.getByText('Setup Device Verification')).toBeVisible();
|
||||
await page.locator('form').getByRole('button', { name: 'Continue' }).click();
|
||||
await expect(recoveryKeyShown(page)).toBeVisible({ timeout: 120_000 });
|
||||
|
||||
const ticketUrl = `${baseURL}${TICKET_PATH}`;
|
||||
const authDicts: (AuthDict | undefined)[] = [];
|
||||
await page.route(`**${TICKET_PATH}`, (route) =>
|
||||
route.fulfill({ contentType: 'text/html', body: '<!doctype html><title>ticket</title>' })
|
||||
);
|
||||
await page.route(UPLOAD_ROUTE, async (route) => {
|
||||
const { auth } = route.request().postDataJSON() as { auth?: AuthDict };
|
||||
authDicts.push(auth);
|
||||
|
||||
if (auth?.type === 'm.oauth' && auth.session === SESSION) {
|
||||
await route.fulfill({ status: 200, contentType: 'application/json', body: '{}' });
|
||||
return;
|
||||
}
|
||||
|
||||
await route.fulfill({
|
||||
status: 401,
|
||||
contentType: 'application/json',
|
||||
body: JSON.stringify({
|
||||
flows: [{ stages: ['m.oauth'] }],
|
||||
params: { 'm.oauth': { url: ticketUrl } },
|
||||
session: SESSION,
|
||||
}),
|
||||
});
|
||||
});
|
||||
|
||||
await page.reload();
|
||||
await page
|
||||
.locator('#device-verification')
|
||||
.locator('button[aria-pressed]')
|
||||
.click({ timeout: 180_000 });
|
||||
await page.getByRole('button', { name: 'Reset', exact: true }).click();
|
||||
|
||||
await expect(page.getByText('Reset Device Verification')).toBeVisible();
|
||||
await page.getByRole('button', { name: 'Reset', exact: true }).last().click();
|
||||
await page.locator('form').getByRole('button', { name: 'Continue' }).click();
|
||||
|
||||
await expect(page.getByText('Account Authorization')).toBeVisible({ timeout: 120_000 });
|
||||
|
||||
const popup = page.waitForEvent('popup');
|
||||
await page.getByRole('button', { name: 'Continue in Browser' }).click();
|
||||
await (await popup).close();
|
||||
await page.getByRole('button', { name: 'Continue', exact: true, disabled: false }).click();
|
||||
|
||||
await expect(recoveryKeyShown(page)).toBeVisible({ timeout: 120_000 });
|
||||
await expect(page.getByText('Account Authorization')).toBeHidden();
|
||||
expect(authDicts[authDicts.length - 1]).toEqual({ type: 'm.oauth', session: SESSION });
|
||||
});
|
||||
|
||||
test('sets up verification when the session syncs over sliding sync', async ({
|
||||
page,
|
||||
}, testInfo) => {
|
||||
test.setTimeout(180_000);
|
||||
const storageStatePath = testInfo.project.use.storageState as string;
|
||||
const hsBaseUrl = await homeserverBaseUrl(storageStatePath);
|
||||
await loginAsFreshUser(
|
||||
page,
|
||||
hsBaseUrl,
|
||||
`setup-sliding-${testInfo.project.name}-${process.pid}`,
|
||||
true
|
||||
);
|
||||
|
||||
await page.goto('/settings/devices');
|
||||
|
||||
await page.getByRole('button', { name: 'Enable' }).click({ timeout: 180_000 });
|
||||
await expect(page.getByText('Setup Device Verification')).toBeVisible();
|
||||
await page.locator('form').getByRole('button', { name: 'Continue' }).click();
|
||||
|
||||
await expect(recoveryKeyShown(page)).toBeVisible({ timeout: 120_000 });
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user