Implement GitHub authentication flows in CLI tests (#295386)

This commit is contained in:
Dmitriy Vasyura
2026-02-14 20:42:38 -08:00
committed by GitHub
parent d057f3f567
commit 8e56833278
2 changed files with 43 additions and 17 deletions
+35 -9
View File
@@ -3,15 +3,15 @@
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { Browser, Page } from 'playwright';
import { Page } from 'playwright';
import { TestContext } from './context.js';
/**
* Handles GitHub authentication flows in the browser.
*/
export class GitHubAuth {
// private readonly username = process.env.GITHUB_ACCOUNT;
// private readonly password = process.env.GITHUB_PASSWORD;
private readonly username = process.env.GITHUB_ACCOUNT;
private readonly password = process.env.GITHUB_PASSWORD;
public constructor(private readonly context: TestContext) { }
@@ -20,17 +20,43 @@ export class GitHubAuth {
* @param browser Browser to use.
* @param code Device authentication code to use.
*/
public async runDeviceCodeFlow(browser: Browser, code: string) {
public async runDeviceCodeFlow(page: Page, code: string) {
if (!this.username || !this.password) {
this.context.error('GITHUB_ACCOUNT and GITHUB_PASSWORD environment variables must be set');
}
this.context.log(`Running GitHub device flow with code ${code}`);
const page = await browser.newPage();
await page.goto('https://github.com/login/device');
this.context.log('Filling in GitHub credentials');
await page.getByLabel('Username or email address').fill(this.username);
await page.getByLabel('Password').fill(this.password);
await page.getByRole('button', { name: 'Sign in', exact: true }).click();
this.context.log('Confirming device activation');
await page.getByRole('button', { name: 'Continue' }).click();
this.context.log('Entering device code');
const codeChars = code.replace('-', '');
for (let i = 0; i < codeChars.length; i++) {
await page.getByRole('textbox').nth(i).fill(codeChars[i]);
}
await page.getByRole('button', { name: 'Continue' }).click();
this.context.log('Authorizing device');
await page.getByRole('button', { name: 'Authorize' }).click();
}
/**
* Runs GitHub user authentication flow in the browser.
* @param page Authentication page.
* Handles the GitHub "Authorize" dialog in a popup.
* Clicks "Continue" to authorize the app with the already signed-in account.
* @param page Main page that triggers the GitHub OAuth popup.
*/
public async runUserWebFlow(page: Page) {
this.context.log(`Running GitHub browser flow at ${page.url()}`);
public async runAuthorizeFlow(page: Page) {
this.context.log('Waiting for GitHub OAuth popup');
const popup = await page.waitForEvent('popup');
this.context.log(`Authorizing app at ${popup.url()}`);
await popup.getByRole('button', { name: 'Continue' }).click();
}
}