2024-08-28 05:40:40 +00:00
|
|
|
// Copyright 2024 The Forgejo Authors. All rights reserved.
|
|
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
|
2024-09-11 20:34:33 +00:00
|
|
|
// @watch start
|
|
|
|
// templates/user/auth/**
|
|
|
|
// templates/user/settings/**
|
|
|
|
// web_src/js/features/user-**
|
|
|
|
// @watch end
|
|
|
|
|
2024-08-28 05:40:40 +00:00
|
|
|
import {expect} from '@playwright/test';
|
2024-11-12 20:07:09 +00:00
|
|
|
import {test, create_temp_user, login_user} from './utils_e2e.ts';
|
2024-08-28 05:40:40 +00:00
|
|
|
|
2024-10-19 20:40:08 +00:00
|
|
|
test('WebAuthn register & login flow', async ({browser, request}, workerInfo) => {
|
2024-08-28 05:40:40 +00:00
|
|
|
test.skip(workerInfo.project.name !== 'chromium', 'Uses Chrome protocol');
|
2024-10-19 20:40:08 +00:00
|
|
|
const {context, username} = await create_temp_user(browser, workerInfo, request);
|
2024-08-28 05:40:40 +00:00
|
|
|
const page = await context.newPage();
|
|
|
|
|
|
|
|
// Register a security key.
|
|
|
|
let response = await page.goto('/user/settings/security');
|
2024-10-23 14:22:25 +00:00
|
|
|
expect(response?.status()).toBe(200);
|
2024-08-28 05:40:40 +00:00
|
|
|
|
|
|
|
// https://github.com/microsoft/playwright/issues/7276#issuecomment-1516768428
|
|
|
|
const cdpSession = await page.context().newCDPSession(page);
|
|
|
|
await cdpSession.send('WebAuthn.enable');
|
|
|
|
await cdpSession.send('WebAuthn.addVirtualAuthenticator', {
|
|
|
|
options: {
|
|
|
|
protocol: 'ctap2',
|
|
|
|
ctap2Version: 'ctap2_1',
|
|
|
|
hasUserVerification: true,
|
|
|
|
transport: 'usb',
|
|
|
|
automaticPresenceSimulation: true,
|
|
|
|
isUserVerified: true,
|
2024-11-08 08:55:54 +00:00
|
|
|
backupEligibility: true, // TODO: this doesn't seem to be available?!
|
2024-08-28 05:40:40 +00:00
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
await page.locator('input#nickname').fill('Testing Security Key');
|
|
|
|
await page.getByText('Add security key').click();
|
|
|
|
|
|
|
|
// Logout.
|
2024-11-12 20:07:09 +00:00
|
|
|
await expect(async () => {
|
|
|
|
await page.locator('div[aria-label="Profile and settings…"]').click();
|
|
|
|
await page.getByText('Sign Out').click();
|
|
|
|
}).toPass();
|
2024-08-28 05:40:40 +00:00
|
|
|
await page.waitForURL(`${workerInfo.project.use.baseURL}/`);
|
|
|
|
|
|
|
|
// Login.
|
|
|
|
response = await page.goto('/user/login');
|
2024-10-23 14:22:25 +00:00
|
|
|
expect(response?.status()).toBe(200);
|
2024-08-28 05:40:40 +00:00
|
|
|
|
2024-10-19 20:40:08 +00:00
|
|
|
await page.getByLabel('Username or email address').fill(username);
|
2024-08-28 05:40:40 +00:00
|
|
|
await page.getByLabel('Password').fill('password');
|
|
|
|
await page.getByRole('button', {name: 'Sign in'}).click();
|
|
|
|
await page.waitForURL(`${workerInfo.project.use.baseURL}/user/webauthn`);
|
|
|
|
await page.waitForURL(`${workerInfo.project.use.baseURL}/`);
|
|
|
|
|
|
|
|
// Cleanup.
|
|
|
|
response = await page.goto('/user/settings/security');
|
2024-10-23 14:22:25 +00:00
|
|
|
expect(response?.status()).toBe(200);
|
2024-08-28 05:40:40 +00:00
|
|
|
await page.getByRole('button', {name: 'Remove'}).click();
|
|
|
|
await page.getByRole('button', {name: 'Yes'}).click();
|
2024-11-12 20:07:09 +00:00
|
|
|
await page.waitForLoadState();
|
|
|
|
|
|
|
|
// verify the user can login without a key
|
|
|
|
await login_user(browser, workerInfo, username);
|
2024-08-28 05:40:40 +00:00
|
|
|
});
|