-
Notifications
You must be signed in to change notification settings - Fork 70
chore(e2e): migrate gitlab auth provider tests #3207
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
JessicaJHee
wants to merge
1
commit into
redhat-developer:main
Choose a base branch
from
JessicaJHee:migrate-gitlab-auth
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
109 changes: 109 additions & 0 deletions
109
workspaces/backstage/e2e-tests/support/api/gitlab-oauth-helper.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,109 @@ | ||
| export interface GitLabOAuthApp { | ||
| id: number; | ||
| applicationId: string; | ||
| applicationName: string; | ||
| secret: string; | ||
| callbackUrl: string; | ||
| scopes: string[]; | ||
| } | ||
|
|
||
| /** | ||
| * GitLab OAuth application helper for auth-provider e2e. | ||
| * Ported from RHDH core (gitlab-helper) — create/delete OAuth apps only. | ||
| */ | ||
| export class GitLabOAuthHelper { | ||
| private readonly personalAccessToken: string; | ||
| private readonly apiBaseUrl: string; | ||
|
|
||
| constructor(host: string, personalAccessToken: string) { | ||
| const cleanHost = host.replace(/^https?:\/\//, ""); | ||
| this.apiBaseUrl = `https://${cleanHost}/api/v4`; | ||
| this.personalAccessToken = personalAccessToken; | ||
| } | ||
|
|
||
| async createOAuthApplication( | ||
| name: string, | ||
| redirectUri: string, | ||
| scopes = "api read_user write_repository sudo", | ||
| trusted = true, | ||
| ): Promise<GitLabOAuthApp> { | ||
| const response = await fetch(`${this.apiBaseUrl}/applications`, { | ||
| method: "POST", | ||
| headers: { | ||
| // GitLab API header name | ||
| // eslint-disable-next-line @typescript-eslint/naming-convention | ||
| "PRIVATE-TOKEN": this.personalAccessToken, | ||
| "Content-Type": "application/json", | ||
| }, | ||
| body: JSON.stringify({ | ||
| name, | ||
| // eslint-disable-next-line @typescript-eslint/naming-convention | ||
| redirect_uri: redirectUri, | ||
| scopes, | ||
| trusted, | ||
| }), | ||
| }); | ||
|
|
||
| if (!response.ok) { | ||
| const errorText = await response.text(); | ||
| throw new Error( | ||
| `Failed to create OAuth application: ${response.status} ${response.statusText} - ${errorText}`, | ||
| ); | ||
| } | ||
|
|
||
| const app = (await response.json()) as Record<string, unknown>; | ||
| const id = app.id; | ||
| const applicationId = app.application_id; | ||
| const secret = app.secret; | ||
| if ( | ||
| typeof id !== "number" || | ||
| typeof applicationId !== "string" || | ||
| typeof secret !== "string" | ||
| ) { | ||
| throw new TypeError( | ||
| "GitLab API response missing required fields (id, application_id, or secret)", | ||
| ); | ||
| } | ||
|
|
||
| const applicationName = | ||
| (typeof app.application_name === "string" && app.application_name) || | ||
| (typeof app.name === "string" && app.name) || | ||
| name; | ||
| const callbackUrl = | ||
| (typeof app.callback_url === "string" && app.callback_url) || | ||
| (typeof app.redirect_uri === "string" && app.redirect_uri) || | ||
| redirectUri; | ||
| const responseScopes = Array.isArray(app.scopes) | ||
| ? app.scopes.filter((s): s is string => typeof s === "string") | ||
| : scopes.split(" "); | ||
|
|
||
| return { | ||
| id, | ||
| applicationId, | ||
| applicationName, | ||
| secret, | ||
| callbackUrl, | ||
| scopes: responseScopes, | ||
| }; | ||
| } | ||
|
|
||
| async deleteOAuthApplication(applicationId: number): Promise<void> { | ||
| const response = await fetch( | ||
| `${this.apiBaseUrl}/applications/${applicationId}`, | ||
| { | ||
| method: "DELETE", | ||
| headers: { | ||
| // eslint-disable-next-line @typescript-eslint/naming-convention | ||
| "PRIVATE-TOKEN": this.personalAccessToken, | ||
| }, | ||
| }, | ||
| ); | ||
|
|
||
| if (!response.ok && response.status !== 404) { | ||
| const errorText = await response.text(); | ||
| throw new Error( | ||
| `Failed to delete OAuth application: ${response.status} ${response.statusText} - ${errorText}`, | ||
| ); | ||
| } | ||
| } | ||
| } |
20 changes: 20 additions & 0 deletions
20
workspaces/backstage/e2e-tests/support/constants/gitlab-auth.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| /** Static catalog token from tests/config/gitlab-auth/value-file.yaml */ | ||
| export const GITLAB_AUTH_CATALOG_TOKEN = "gitlab-auth-e2e-token"; | ||
|
|
||
| /** Display names expected after GitLab org ingestion (core auth-providers suite). */ | ||
| export const GITLAB_INGESTED_USERS = [ | ||
| "user1", | ||
| "user2", | ||
| "user3", | ||
| "Administrator", | ||
| ] as const; | ||
|
|
||
| export const GITLAB_INGESTED_GROUPS = [ | ||
| "my-org", | ||
| "group1", | ||
| "all", | ||
| "nested", | ||
| "nested_2", | ||
| ] as const; | ||
|
|
||
| export const GITLAB_LOGIN_USER = "user1"; |
107 changes: 107 additions & 0 deletions
107
workspaces/backstage/e2e-tests/support/gitlab/gitlab-login.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,107 @@ | ||
| import { expect, type Locator, type Page } from "@playwright/test"; | ||
| import type { UIhelper } from "@red-hat-developer-hub/e2e-test-utils/helpers"; | ||
|
|
||
| /** | ||
| * GitLab OAuth popup login (ported from RHDH core Common.gitlabLogin). | ||
| * Not yet on LoginHelper in e2e-test-utils. | ||
| */ | ||
| export async function gitlabLogin( | ||
| page: Page, | ||
| uiHelper: UIhelper, | ||
| username: string, | ||
| password: string, | ||
| ): Promise<string> { | ||
| await page.goto("/"); | ||
| await page.waitForSelector('p:has-text("Sign in using GitLab")'); | ||
|
|
||
| const [popup] = await Promise.all([ | ||
| page.waitForEvent("popup"), | ||
| uiHelper.clickButton("Sign In"), | ||
| ]); | ||
|
|
||
| await expect(async () => { | ||
| await popup.waitForLoadState("domcontentloaded"); | ||
| expect(popup).toBeTruthy(); | ||
| }).toPass({ | ||
| intervals: [5_000, 10_000], | ||
| timeout: 20_000, | ||
| }); | ||
|
|
||
| try { | ||
| await popup.waitForEvent("close", { timeout: 5000 }); | ||
| return "Already logged in"; | ||
| } catch { | ||
| // Popup stayed open — continue with credentials. | ||
| } | ||
|
|
||
| await popup.locator("#user_login").click({ timeout: 5000 }); | ||
| await popup.locator("#user_login").fill(username, { timeout: 5000 }); | ||
| await popup.locator("#user_password").click({ timeout: 5000 }); | ||
| await popup.locator("#user_password").fill(password, { timeout: 5000 }); | ||
| await popup.getByTestId("sign-in-button").click({ timeout: 5000 }); | ||
|
|
||
| await popup | ||
| .waitForLoadState("domcontentloaded", { timeout: 10_000 }) | ||
| .catch(() => undefined); | ||
|
|
||
| const twoFactorInput = popup.locator("#user_otp_attempt"); | ||
| if (await twoFactorInput.isVisible({ timeout: 5000 }).catch(() => false)) { | ||
| await popup.waitForEvent("close", { timeout: 20_000 }); | ||
| return "Login successful"; | ||
| } | ||
|
|
||
| // GitLab EE button text is "Authorize <app-name>"; prefer role + testid. | ||
| const authorizeCandidates: Locator[] = [ | ||
| popup.getByRole("button", { name: /Authorize/ }), | ||
| popup.getByTestId("authorize-button"), | ||
| popup.locator('button:has-text("Authorize")'), | ||
| ]; | ||
|
|
||
| await expect(async () => { | ||
| for (const candidate of authorizeCandidates) { | ||
| if (await candidate.isVisible({ timeout: 2000 }).catch(() => false)) { | ||
| return; | ||
| } | ||
| } | ||
| throw new Error("Authorization button not found"); | ||
| }).toPass({ | ||
| intervals: [1000, 2000], | ||
| timeout: 15_000, | ||
| }); | ||
|
|
||
| let buttonToClick: Locator | undefined; | ||
| for (const candidate of authorizeCandidates) { | ||
| if (await candidate.isVisible().catch(() => false)) { | ||
| buttonToClick = candidate; | ||
| break; | ||
| } | ||
| } | ||
| if (!buttonToClick) { | ||
| throw new Error("Failed to find authorization button"); | ||
| } | ||
|
|
||
| await popup | ||
| .getByRole("document") | ||
| .click({ timeout: 1000 }) | ||
| .catch(() => undefined); | ||
|
|
||
| await buttonToClick.waitFor({ state: "visible", timeout: 5000 }); | ||
| await expect(buttonToClick).toBeEnabled({ timeout: 10_000 }); | ||
| await buttonToClick.scrollIntoViewIfNeeded({ timeout: 5000 }); | ||
| await popup.waitForTimeout(1000); | ||
|
|
||
| try { | ||
| await buttonToClick.click({ timeout: 5000 }); | ||
| } catch { | ||
| await buttonToClick.click({ force: true, timeout: 5000 }); | ||
| } | ||
|
|
||
| try { | ||
| await popup.waitForEvent("close", { timeout: 20_000 }); | ||
| } catch { | ||
| if (!popup.isClosed()) { | ||
| throw new Error("GitLab login popup did not close after sign-in"); | ||
| } | ||
| } | ||
| return "Login successful"; | ||
| } |
56 changes: 56 additions & 0 deletions
56
workspaces/backstage/e2e-tests/tests/config/gitlab-auth/app-config-rhdh.yaml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| app: | ||
| baseUrl: ${RHDH_BASE_URL} | ||
| title: RHDH GitLab Auth E2E | ||
|
|
||
| backend: | ||
| baseUrl: ${RHDH_BASE_URL} | ||
| cors: | ||
| origin: ${RHDH_BASE_URL} | ||
|
|
||
| permission: | ||
| enabled: false | ||
|
|
||
| signInPage: gitlab | ||
|
|
||
| auth: | ||
| environment: production | ||
| providers: | ||
| guest: | ||
| dangerouslyAllowOutsideDevelopment: true | ||
| gitlab: | ||
| production: | ||
| audience: https://${AUTH_PROVIDERS_GITLAB_HOST} | ||
| clientId: ${AUTH_PROVIDERS_GITLAB_CLIENT_ID} | ||
| clientSecret: ${AUTH_PROVIDERS_GITLAB_CLIENT_SECRET} | ||
| callbackUrl: ${RHDH_BASE_URL}/api/auth/gitlab/handler/frame | ||
| # Dynamic gitlab auth provider has no implicit default (unlike core's | ||
| # static authProvidersModule). Match upstream default: | ||
| # usernameMatchingUserEntityName. | ||
| signIn: | ||
| resolvers: | ||
| - resolver: usernameMatchingUserEntityName | ||
|
|
||
| catalog: | ||
| rules: | ||
| - allow: [API, Component, Group, Location, Resource, System, Template, User] | ||
| providers: | ||
| gitlab: | ||
| orgProvider: | ||
| host: ${AUTH_PROVIDERS_GITLAB_HOST} | ||
| orgEnabled: true | ||
| group: ${AUTH_PROVIDERS_GITLAB_PARENT_ORG} | ||
| restrictUsersToGroup: true | ||
| includeUsersWithoutSeat: true | ||
| schedule: | ||
| initialDelay: | ||
| seconds: 0 | ||
| frequency: | ||
| minutes: 1 | ||
| timeout: | ||
| minutes: 1 | ||
|
|
||
| integrations: | ||
| gitlab: | ||
| - host: ${AUTH_PROVIDERS_GITLAB_HOST} | ||
| token: ${AUTH_PROVIDERS_GITLAB_TOKEN} | ||
| apiBaseUrl: https://${AUTH_PROVIDERS_GITLAB_HOST}/api/v4 |
24 changes: 24 additions & 0 deletions
24
workspaces/backstage/e2e-tests/tests/config/gitlab-auth/dynamic-plugins.yaml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| plugins: | ||
| - package: oci://quay.io/rhdh/red-hat-developer-hub-backstage-plugin-homepage:2.0.0--1.17.1!red-hat-developer-hub-backstage-plugin-homepage | ||
| disabled: true | ||
| - package: ./dynamic-plugins/dist/backstage-plugin-auth-backend-module-gitlab-provider-dynamic | ||
| disabled: false | ||
| - package: ./dynamic-plugins/dist/backstage-plugin-catalog-backend-module-gitlab-org-dynamic | ||
| disabled: false | ||
| pluginConfig: | ||
| catalog: | ||
| providers: | ||
| gitlab: | ||
| orgProvider: | ||
| host: ${AUTH_PROVIDERS_GITLAB_HOST} | ||
| orgEnabled: true | ||
| group: ${AUTH_PROVIDERS_GITLAB_PARENT_ORG} | ||
| restrictUsersToGroup: true | ||
| includeUsersWithoutSeat: true | ||
| schedule: | ||
| initialDelay: | ||
| seconds: 0 | ||
| frequency: | ||
| minutes: 1 | ||
| timeout: | ||
| minutes: 1 | ||
15 changes: 15 additions & 0 deletions
15
workspaces/backstage/e2e-tests/tests/config/gitlab-auth/rhdh-secrets.yaml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| apiVersion: v1 | ||
| kind: Secret | ||
| metadata: | ||
| name: rhdh-secrets | ||
| type: Opaque | ||
| stringData: | ||
| # Vault-synced (must be VAULT_* in CI / vault UI) | ||
| AUTH_PROVIDERS_GITLAB_HOST: $VAULT_AUTH_PROVIDERS_GITLAB_HOST | ||
| AUTH_PROVIDERS_GITLAB_TOKEN: $VAULT_AUTH_PROVIDERS_GITLAB_TOKEN | ||
| AUTH_PROVIDERS_GITLAB_PARENT_ORG: $VAULT_AUTH_PROVIDERS_GITLAB_PARENT_ORG | ||
| DEFAULT_USER_PASSWORD: $VAULT_DEFAULT_USER_PASSWORD | ||
| # Set dynamically in beforeAll after creating the ephemeral OAuth app | ||
| AUTH_PROVIDERS_GITLAB_CLIENT_ID: $AUTH_PROVIDERS_GITLAB_CLIENT_ID | ||
| AUTH_PROVIDERS_GITLAB_CLIENT_SECRET: $AUTH_PROVIDERS_GITLAB_CLIENT_SECRET | ||
| RHDH_BASE_URL: $RHDH_BASE_URL |
15 changes: 15 additions & 0 deletions
15
workspaces/backstage/e2e-tests/tests/config/gitlab-auth/value-file.yaml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| # Static external-access token for catalog API calls (Helm-injected appConfig). | ||
| global: | ||
| lightspeed: | ||
| enabled: false | ||
|
|
||
| upstream: | ||
| backstage: | ||
| appConfig: | ||
| backend: | ||
| auth: | ||
| externalAccess: | ||
| - type: static | ||
| options: | ||
| token: gitlab-auth-e2e-token | ||
| subject: gitlab-auth-e2e |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[low] style/conventions
The catalog.providers.gitlab.orgProvider configuration block is duplicated identically between app-config-rhdh.yaml (lines 34-47) and dynamic-plugins.yaml pluginConfig (lines 8-24). This duplication increases maintenance burden without functional benefit since deep merge produces identical results.
Suggested fix: Keep the orgProvider config in one location only — either in dynamic-plugins.yaml pluginConfig (where the gitlab-org module reads it) or in app-config-rhdh.yaml, but not both.