|
| 1 | +/** |
| 2 | + * Copyright (c) 2025, WSO2 LLC. (https://www.wso2.com). |
| 3 | + * |
| 4 | + * WSO2 LLC. licenses this file to you under the Apache License, |
| 5 | + * Version 2.0 (the "License"); you may not use this file except |
| 6 | + * in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, |
| 12 | + * software distributed under the License is distributed on an |
| 13 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | + * KIND, either express or implied. See the License for the |
| 15 | + * specific language governing permissions and limitations |
| 16 | + * under the License. |
| 17 | + */ |
| 18 | + |
| 19 | +import {describe, it, expect, vi, afterEach} from 'vitest'; |
| 20 | +import {Config} from '../../models/config'; |
| 21 | +import isRecognizedBaseUrlPattern from '../isRecognizedBaseUrlPattern'; |
| 22 | +import getRedirectBasedSignUpUrl from '../getRedirectBasedSignUpUrl'; |
| 23 | + |
| 24 | +vi.mock('../isRecognizedBaseUrlPattern', () => ({default: vi.fn()})); |
| 25 | + |
| 26 | +describe('getRedirectBasedSignUpUrl', () => { |
| 27 | + const baseUrl: string = 'https://api.asgardeo.io/t/org'; |
| 28 | + const expectedBaseUrl: string = 'https://accounts.asgardeo.io/t/org'; |
| 29 | + const clientId: string = 'client123'; |
| 30 | + const applicationId: string = 'app456'; |
| 31 | + |
| 32 | + afterEach(() => { |
| 33 | + vi.clearAllMocks(); |
| 34 | + }); |
| 35 | + |
| 36 | + it('returns the correct sign-up URL if baseUrl is recognized and both params are present', () => { |
| 37 | + (isRecognizedBaseUrlPattern as unknown as ReturnType<typeof vi.fn>).mockReturnValue(true); |
| 38 | + const config: Config = {baseUrl, clientId, applicationId}; |
| 39 | + const url: URL = new URL(expectedBaseUrl + '/accountrecoveryendpoint/register.do'); |
| 40 | + url.searchParams.set('client_id', clientId); |
| 41 | + url.searchParams.set('spId', applicationId); |
| 42 | + expect(getRedirectBasedSignUpUrl(config)).toBe(url.toString()); |
| 43 | + }); |
| 44 | + |
| 45 | + it('returns the correct sign-up URL if only clientId is present', () => { |
| 46 | + (isRecognizedBaseUrlPattern as unknown as ReturnType<typeof vi.fn>).mockReturnValue(true); |
| 47 | + const config: Config = {baseUrl, clientId}; |
| 48 | + const url: URL = new URL(expectedBaseUrl + '/accountrecoveryendpoint/register.do'); |
| 49 | + url.searchParams.set('client_id', clientId); |
| 50 | + expect(getRedirectBasedSignUpUrl(config)).toBe(url.toString()); |
| 51 | + }); |
| 52 | + |
| 53 | + it('returns the correct sign-up URL if only applicationId is present', () => { |
| 54 | + (isRecognizedBaseUrlPattern as unknown as ReturnType<typeof vi.fn>).mockReturnValue(true); |
| 55 | + const config: Config = {baseUrl, applicationId, clientId: ''}; |
| 56 | + const url: URL = new URL(expectedBaseUrl + '/accountrecoveryendpoint/register.do'); |
| 57 | + url.searchParams.set('spId', applicationId); |
| 58 | + expect(getRedirectBasedSignUpUrl(config)).toBe(url.toString()); |
| 59 | + }); |
| 60 | + |
| 61 | + it('returns the correct sign-up URL if neither param is present', () => { |
| 62 | + (isRecognizedBaseUrlPattern as unknown as ReturnType<typeof vi.fn>).mockReturnValue(true); |
| 63 | + const config: Config = {baseUrl, clientId: ''}; |
| 64 | + const url: URL = new URL(expectedBaseUrl + '/accountrecoveryendpoint/register.do'); |
| 65 | + expect(getRedirectBasedSignUpUrl(config)).toBe(url.toString()); |
| 66 | + }); |
| 67 | + |
| 68 | + it('returns empty string if baseUrl is not recognized', () => { |
| 69 | + (isRecognizedBaseUrlPattern as unknown as ReturnType<typeof vi.fn>).mockReturnValue(false); |
| 70 | + const config: Config = {baseUrl, clientId, applicationId}; |
| 71 | + expect(getRedirectBasedSignUpUrl(config)).toBe(''); |
| 72 | + }); |
| 73 | +}); |
0 commit comments