+
Skip to content

Refactor createRegisterUrl() and add tests #80

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

Merged
merged 1 commit into from
Apr 25, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions lib/keycloak.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// @ts-check
/**
* @import {KeycloakLoginOptions, KeycloakLogoutOptions} from "./keycloak.d.ts"
* @import {KeycloakLoginOptions, KeycloakLogoutOptions, KeycloakRegisterOptions} from "./keycloak.d.ts"
*/
/*
* Copyright 2016 Red Hat, Inc. and/or its affiliates
Expand Down Expand Up @@ -510,12 +510,12 @@ function Keycloak (config) {
return adapter.register(options);
}

/**
* @param {KeycloakRegisterOptions} [options]
* @returns {Promise<string>}
*/
kc.createRegisterUrl = async function(options) {
if (!options) {
options = {};
}
options.action = 'register';
return await kc.createLoginUrl(options);
return await kc.createLoginUrl({ ...options, action: 'register' });
}

kc.createAccountUrl = function(options) {
Expand Down
9 changes: 8 additions & 1 deletion test/support/test-executor.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { ConsoleMessage, Page } from 'playwright'
import type Keycloak from '../../lib/keycloak.d.ts'
import type { KeycloakConfig, KeycloakInitOptions, KeycloakLoginOptions, KeycloakLogoutOptions, KeycloakProfile, KeycloakTokenParsed } from '../../lib/keycloak.d.ts'
import type { KeycloakConfig, KeycloakInitOptions, KeycloakLoginOptions, KeycloakLogoutOptions, KeycloakProfile, KeycloakRegisterOptions, KeycloakTokenParsed } from '../../lib/keycloak.d.ts'
import { AUTHORIZED_PASSWORD, AUTHORIZED_USERNAME, CLIENT_ID } from './common.ts'
import type { TestOptions } from './testbed.ts'

Expand Down Expand Up @@ -134,6 +134,13 @@ export class TestExecutor {
}, options)
}

async createRegisterUrl (options?: KeycloakRegisterOptions): Promise<string> {
await this.#assertInstantiated()
return await this.#page.evaluate(async (options) => {
return await ((globalThis as any).keycloak as Keycloak).createRegisterUrl(options)
}, options)
}

async login (options?: KeycloakLoginOptions): Promise<void> {
await this.#assertInstantiated()

Expand Down
67 changes: 67 additions & 0 deletions test/tests/register-url.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import { expect } from '@playwright/test'
import { AUTHORIZED_USERNAME, CLIENT_ID } from '../support/common.ts'
import { createTestBed, test } from '../support/testbed.ts'

// Since `createRegisterUrl()` calls `createLoginUrl()` internally, only a small subset of the behavior of `createLoginUrl()` is tested here. All other tests are in the `login-url.spec.ts` file.

test('creates a registration URL with all options', async ({ page, appUrl, authServerUrl }) => {
const { executor, realm } = await createTestBed(page, { appUrl, authServerUrl })
await executor.initializeAdapter(executor.defaultInitOptions())
const redirectUri = new URL('/foo/bar', appUrl)
const registerUrl = new URL(await executor.createRegisterUrl({
scope: 'openid profile email',
redirectUri: redirectUri.toString(),
prompt: 'none',
maxAge: 3600,
loginHint: AUTHORIZED_USERNAME,
idpHint: 'facebook',
locale: 'nl-NL nl',
acr: {
values: ['foo', 'bar'],
essential: false
},
acrValues: '2fa'
}))
expect(registerUrl.pathname).toBe(`/realms/${realm}/protocol/openid-connect/registrations`)
expect(registerUrl.searchParams.get('client_id')).toBe(CLIENT_ID)
expect(registerUrl.searchParams.get('redirect_uri')).toBe(redirectUri.toString())
expect(registerUrl.searchParams.get('state')).toEqual(expect.any(String))
expect(registerUrl.searchParams.get('response_mode')).toBe('fragment')
expect(registerUrl.searchParams.get('response_type')).toBe('code')
expect(registerUrl.searchParams.get('scope')).toBe('openid profile email')
expect(registerUrl.searchParams.get('nonce')).toEqual(expect.any(String))
expect(registerUrl.searchParams.get('prompt')).toBe('none')
expect(registerUrl.searchParams.get('max_age')).toBe('3600')
expect(registerUrl.searchParams.get('login_hint')).toBe(AUTHORIZED_USERNAME)
expect(registerUrl.searchParams.get('kc_idp_hint')).toBe('facebook')
expect(registerUrl.searchParams.get('kc_action')).toBeNull()
expect(registerUrl.searchParams.get('ui_locales')).toBe('nl-NL nl')
expect(registerUrl.searchParams.get('claims')).toBe('{"id_token":{"acr":{"values":["foo","bar"],"essential":false}}}')
expect(registerUrl.searchParams.get('acr_values')).toBe('2fa')
expect(registerUrl.searchParams.get('code_challenge')).toEqual(expect.any(String))
expect(registerUrl.searchParams.get('code_challenge_method')).toBe('S256')
})

test('creates a registration URL with default options', async ({ page, appUrl, authServerUrl }) => {
const { executor, realm } = await createTestBed(page, { appUrl, authServerUrl })
await executor.initializeAdapter(executor.defaultInitOptions())
const registerUrl = new URL(await executor.createRegisterUrl())
expect(registerUrl.pathname).toBe(`/realms/${realm}/protocol/openid-connect/registrations`)
expect(registerUrl.searchParams.get('client_id')).toBe(CLIENT_ID)
expect(registerUrl.searchParams.get('redirect_uri')).toBe(appUrl.toString())
expect(registerUrl.searchParams.get('state')).toEqual(expect.any(String))
expect(registerUrl.searchParams.get('response_mode')).toBe('fragment')
expect(registerUrl.searchParams.get('response_type')).toBe('code')
expect(registerUrl.searchParams.get('scope')).toBe('openid')
expect(registerUrl.searchParams.get('nonce')).toEqual(expect.any(String))
expect(registerUrl.searchParams.get('prompt')).toBeNull()
expect(registerUrl.searchParams.get('max_age')).toBeNull()
expect(registerUrl.searchParams.get('login_hint')).toBeNull()
expect(registerUrl.searchParams.get('kc_idp_hint')).toBeNull()
expect(registerUrl.searchParams.get('kc_action')).toBeNull()
expect(registerUrl.searchParams.get('ui_locales')).toBeNull()
expect(registerUrl.searchParams.get('claims')).toBeNull()
expect(registerUrl.searchParams.get('acr_values')).toBeNull()
expect(registerUrl.searchParams.get('code_challenge')).toEqual(expect.any(String))
expect(registerUrl.searchParams.get('code_challenge_method')).toBe('S256')
})
点击 这是indexloc提供的php浏览器服务,不要输入任何密码和下载