-
Notifications
You must be signed in to change notification settings - Fork 47
test(browser) : enhance test coverage for http and auth params utilities #223
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
KavishkaVenuka
wants to merge
10
commits into
asgardeo:main
Choose a base branch
from
KavishkaVenuka:test/asgardeo-browser#166
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
Show all changes
10 commits
Select commit
Hold shift + click to select a range
aa99c60
test(browser): enhance test coverage for http and auth params utilities
KavishkaVenuka 7956d08
Merge branch 'main' into test/asgardeo-browser#166
KavishkaVenuka 82eec4d
test(browser): enhance test coverage for theme utilities
KavishkaVenuka d00e06f
test(browser): enhance config type validation and test coverage
KavishkaVenuka 32ba9b9
chore(browser): add unit tests for AsgardeoBrowserClient to increase …
KavishkaVenuka 887d0cc
Merge branch 'asgardeo:main' into main
KavishkaVenuka f2b5ef9
Remove unnecessary Jest config as per review feedback
KavishkaVenuka 812c2a4
Merge branch 'asgardeo:main' into main
KavishkaVenuka 637495b
chore(browser): resolve eslint violations and apply prettier to tests
KavishkaVenuka 8154c4c
chore(browser): update test tooling and lockfile; fix typings in conf…
KavishkaVenuka 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
Empty file.
199 changes: 199 additions & 0 deletions
199
packages/browser/src/__tests__/AsgardeoBrowserClient.test.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,199 @@ | ||
| /** | ||
| * Copyright (c) 2025, WSO2 LLC. (https://www.wso2.com). | ||
| * | ||
| * WSO2 LLC. licenses this file to you under the Apache License, | ||
| * Version 2.0 (the "License"); you may not use this file except | ||
| * in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
|
|
||
| /* eslint-disable @typescript-eslint/typedef, class-methods-use-this, no-underscore-dangle, @typescript-eslint/no-unused-vars */ | ||
|
|
||
| import type { | ||
| AllOrganizationsApiResponse, | ||
| EmbeddedFlowExecuteRequestPayload, | ||
| EmbeddedFlowExecuteResponse, | ||
| EmbeddedSignInFlowHandleRequestPayload, | ||
| Organization, | ||
| SignInOptions, | ||
| SignOutOptions, | ||
| SignUpOptions, | ||
| Storage, | ||
| TokenExchangeRequestConfig, | ||
| TokenResponse, | ||
| User, | ||
| UserProfile, | ||
| } from '@asgardeo/javascript'; | ||
| import {AsgardeoJavaScriptClient} from '@asgardeo/javascript'; | ||
| import {describe, it, expect} from 'vitest'; | ||
| import AsgardeoBrowserClient from '../AsgardeoBrowserClient'; | ||
| import {AsgardeoBrowserConfig} from '../models/config'; | ||
|
|
||
| class TestBrowserClient extends AsgardeoBrowserClient<AsgardeoBrowserConfig> { | ||
| private _config!: AsgardeoBrowserConfig; | ||
|
|
||
| private _loading = false; | ||
|
|
||
| async switchOrganization(_organization: Organization, _sessionId?: string): Promise<TokenResponse | Response> { | ||
| return {accessToken: 'token'} as TokenResponse; | ||
| } | ||
|
|
||
| async initialize(config: AsgardeoBrowserConfig, _storage?: Storage): Promise<boolean> { | ||
| this._config = config; | ||
| this._loading = false; | ||
| return true; | ||
| } | ||
|
|
||
| async reInitialize(_config: Partial<AsgardeoBrowserConfig>): Promise<boolean> { | ||
| return true; | ||
| } | ||
|
|
||
| async getUser(_options?: any): Promise<User> { | ||
| return {id: 'u1'} as unknown as User; | ||
| } | ||
|
|
||
| async getAllOrganizations(_options?: any, _sessionId?: string): Promise<AllOrganizationsApiResponse> { | ||
| return {hasMore: false, organizations: []} as AllOrganizationsApiResponse; | ||
| } | ||
|
|
||
| async getMyOrganizations(_options?: any, _sessionId?: string): Promise<Organization[]> { | ||
| return [] as Organization[]; | ||
| } | ||
|
|
||
| async getCurrentOrganization(_sessionId?: string): Promise<Organization | null> { | ||
| return null; | ||
| } | ||
|
|
||
| async getUserProfile(_options?: any): Promise<UserProfile> { | ||
| return {id: 'u1'} as unknown as UserProfile; | ||
| } | ||
|
|
||
| isLoading(): boolean { | ||
| return this._loading; | ||
| } | ||
|
|
||
| async isSignedIn(): Promise<boolean> { | ||
| return true; | ||
| } | ||
|
|
||
| async updateUserProfile(_payload: any, _userId?: string): Promise<User> { | ||
| return {id: 'u1'} as unknown as User; | ||
| } | ||
|
|
||
| getConfiguration(): AsgardeoBrowserConfig { | ||
| return this._config; | ||
| } | ||
|
|
||
| async exchangeToken(_config: TokenExchangeRequestConfig, _sessionId?: string): Promise<TokenResponse | Response> { | ||
| return {accessToken: 'token'} as TokenResponse; | ||
| } | ||
|
|
||
| // signIn overloads | ||
| async signIn( | ||
| _options?: SignInOptions, | ||
| _sessionId?: string, | ||
| _onSignInSuccess?: (afterSignInUrl: string) => void, | ||
| ): Promise<User>; | ||
| async signIn( | ||
| _payload: EmbeddedSignInFlowHandleRequestPayload, | ||
| _request: Request, | ||
| _sessionId?: string, | ||
| _onSignInSuccess?: (afterSignInUrl: string) => void, | ||
| ): Promise<User>; | ||
| async signIn(): Promise<User> { | ||
| return {id: 'u1'} as unknown as User; | ||
| } | ||
|
|
||
| async signInSilently(_options?: SignInOptions): Promise<User | boolean> { | ||
| return false; | ||
| } | ||
|
|
||
| // signOut overloads | ||
| async signOut(_options?: SignOutOptions, _afterSignOut?: (afterSignOutUrl: string) => void): Promise<string>; | ||
| async signOut( | ||
| _options?: SignOutOptions, | ||
| _sessionId?: string, | ||
| _afterSignOut?: (afterSignOutUrl: string) => void, | ||
| ): Promise<string>; | ||
| async signOut(): Promise<string> { | ||
| return 'signed-out'; | ||
| } | ||
|
|
||
| // signUp overloads | ||
| async signUp(_options?: SignUpOptions): Promise<void>; | ||
| async signUp(_payload: EmbeddedFlowExecuteRequestPayload): Promise<EmbeddedFlowExecuteResponse>; | ||
| async signUp(): Promise<void | EmbeddedFlowExecuteResponse> { | ||
| return undefined; | ||
| } | ||
|
|
||
| async getAccessToken(_sessionId?: string): Promise<string> { | ||
| return 'token'; | ||
| } | ||
|
|
||
| clearSession(_sessionId?: string): void {} | ||
| } | ||
|
|
||
| describe('AsgardeoBrowserClient', () => { | ||
| it('should be a class export (default)', () => { | ||
| expect(typeof AsgardeoBrowserClient).toBe('function'); | ||
| }); | ||
|
|
||
| it('should allow creating a concrete subclass instance', async () => { | ||
| const client: TestBrowserClient = new TestBrowserClient(); | ||
| expect(client).toBeInstanceOf(TestBrowserClient); | ||
| // Inheritance check against the base JS client | ||
| expect(client).toBeInstanceOf(AsgardeoJavaScriptClient as unknown as Function); | ||
|
|
||
| const config: AsgardeoBrowserConfig = { | ||
| baseUrl: 'https://example.org/t/acme', | ||
| clientId: 'abc', | ||
| storage: 'browserMemory', | ||
| }; | ||
|
|
||
| const initialized: boolean = await client.initialize(config); | ||
| expect(initialized).toBe(true); | ||
| expect(client.getConfiguration()).toEqual(config); | ||
| }); | ||
|
|
||
| it('should return stubbed values for core methods', async () => { | ||
| const client: TestBrowserClient = new TestBrowserClient(); | ||
| await client.initialize({baseUrl: 'https://x', clientId: 'y', storage: 'browserMemory'}); | ||
|
|
||
| expect(client.isLoading()).toBe(false); | ||
| await expect(client.isSignedIn()).resolves.toBe(true); | ||
|
|
||
| await expect(client.getUser()).resolves.toBeTruthy(); | ||
| await expect(client.getUserProfile()).resolves.toBeTruthy(); | ||
|
|
||
| await expect(client.getMyOrganizations()).resolves.toEqual([]); | ||
| await expect(client.getAllOrganizations()).resolves.toMatchObject({hasMore: false}); | ||
| await expect(client.getCurrentOrganization()).resolves.toBeNull(); | ||
|
|
||
| await expect(client.getAccessToken()).resolves.toBe('token'); | ||
| await expect( | ||
| client.exchangeToken({ | ||
| attachToken: false, | ||
| data: {}, | ||
| id: 'req-1', | ||
| returnsSession: false, | ||
| signInRequired: false, | ||
| } as TokenExchangeRequestConfig), | ||
| ).resolves.toBeTruthy(); | ||
|
|
||
| await expect(client.signIn()).resolves.toBeTruthy(); | ||
| await expect(client.signInSilently()).resolves.toBe(false); | ||
| await expect(client.signOut()).resolves.toBe('signed-out'); | ||
|
|
||
| // signUp returns void in our stub | ||
| await expect(client.signUp()).resolves.toBeUndefined(); | ||
| }); | ||
| }); |
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 @@ | ||
| /** | ||
| * Copyright (c) 2025, WSO2 LLC. (https://www.wso2.com). | ||
| * | ||
| * WSO2 LLC. licenses this file to you under the Apache License, | ||
| * Version 2.0 (the "License"); you may not use this file except | ||
| * in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
|
|
||
| import { AsgardeoBrowserConfig } from '../config'; | ||
|
|
||
| describe('AsgardeoBrowserConfig', () => { | ||
| const validStorageTypes: readonly ['sessionStorage', 'localStorage', 'browserMemory', 'webWorker'] = [ | ||
| 'sessionStorage', | ||
| 'localStorage', | ||
| 'browserMemory', | ||
| 'webWorker', | ||
| ]; | ||
| const TEST_BASE_URL: string = 'https://localhost:9443'; | ||
|
|
||
| describe('Required Fields', () => { | ||
| it('should require baseUrl and clientId', () => { | ||
| const config: AsgardeoBrowserConfig = { | ||
| baseUrl: TEST_BASE_URL, | ||
| clientId: 'client123', | ||
| }; | ||
| expect(config.baseUrl).toBe(TEST_BASE_URL); | ||
| expect(config.clientId).toBe('client123'); | ||
| }); | ||
|
|
||
| it('should accept optional configurations', () => { | ||
| const config: AsgardeoBrowserConfig = { | ||
| baseUrl: TEST_BASE_URL, | ||
| clientId: 'client123', | ||
| signInRedirectURL: `${TEST_BASE_URL}/signin`, | ||
| signOutRedirectURL: `${TEST_BASE_URL}/signout`, | ||
| storage: 'sessionStorage', | ||
| }; | ||
|
|
||
| // Test actual values instead of just checking if defined | ||
| expect(config.signInRedirectURL).toBe(`${TEST_BASE_URL}/signin`); | ||
| expect(config.signOutRedirectURL).toBe(`${TEST_BASE_URL}/signout`); | ||
| expect(config.storage).toBe('sessionStorage'); | ||
| }); | ||
| }); | ||
|
|
||
| describe('Storage Type Validation', () => { | ||
| validStorageTypes.forEach((storageType: (typeof validStorageTypes)[number]) => { | ||
| it(`should accept ${storageType} as storage type`, () => { | ||
| const config: AsgardeoBrowserConfig = { | ||
| baseUrl: TEST_BASE_URL, | ||
| clientId: 'client123', | ||
| storage: storageType, | ||
| }; | ||
| expect(config.storage).toBe(storageType); | ||
| }); | ||
| }); | ||
|
|
||
| it('should enforce valid storage types at compile time', () => { | ||
| const configs: AsgardeoBrowserConfig[] = validStorageTypes.map( | ||
| (storage: (typeof validStorageTypes)[number]) => | ||
| ({ | ||
| baseUrl: TEST_BASE_URL, | ||
| clientId: 'client123', | ||
| storage, | ||
| }) as AsgardeoBrowserConfig, | ||
| ); | ||
|
|
||
| configs.forEach((config: AsgardeoBrowserConfig) => { | ||
| expect(validStorageTypes).toContain(config.storage); | ||
| }); | ||
| }); | ||
| }); | ||
|
|
||
| describe('Optional Configurations', () => { | ||
| it('should accept optional configurations', () => { | ||
| // Use type assertion to handle extended config properties | ||
| const config: Partial<AsgardeoBrowserConfig> = { | ||
| baseUrl: TEST_BASE_URL, | ||
| clientId: 'client123', | ||
| signInRedirectURL: `${TEST_BASE_URL}/signin`, | ||
| signOutRedirectURL: `${TEST_BASE_URL}/signout`, | ||
| storage: 'sessionStorage' as const, | ||
| } satisfies Partial<AsgardeoBrowserConfig>; | ||
|
|
||
| // Validate fields | ||
| expect(config.signInRedirectURL).toBe(`${TEST_BASE_URL}/signin`); | ||
| expect(config.signOutRedirectURL).toBe(`${TEST_BASE_URL}/signout`); | ||
| expect(config.storage).toBe('sessionStorage'); | ||
| expect(validStorageTypes).toContain(config.storage); | ||
| }); | ||
| }); | ||
|
|
||
| // Remove the incomplete invalid storage test | ||
| // Add runtime validation test instead | ||
| it('should not accept invalid storage type at runtime', () => { | ||
| const invalidStorage: string = 'invalid' as any; | ||
| expect((validStorageTypes as readonly string[]).includes(invalidStorage)).toBe(false); | ||
| }); | ||
| }); |
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.
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.
Add a new line.