From 950edb4c3f6e8c8a82a2e8f1b6ef4abd64a7b670 Mon Sep 17 00:00:00 2001 From: Subhankar Maiti Date: Thu, 28 Aug 2025 14:10:49 +0530 Subject: [PATCH] feat(auth): enhance resetPassword to include optional organization parameter and add tests --- .../services/AuthenticationOrchestrator.ts | 10 ++- .../AuthenticationOrchestrator.spec.ts | 65 +++++++++++++++++++ src/types/parameters.ts | 1 + 3 files changed, 75 insertions(+), 1 deletion(-) diff --git a/src/core/services/AuthenticationOrchestrator.ts b/src/core/services/AuthenticationOrchestrator.ts index 843911bf..ea5fcb29 100644 --- a/src/core/services/AuthenticationOrchestrator.ts +++ b/src/core/services/AuthenticationOrchestrator.ts @@ -358,11 +358,19 @@ export class AuthenticationOrchestrator implements IAuthenticationProvider { async resetPassword(parameters: ResetPasswordParameters): Promise { const { headers, ...payload } = parameters; - const body = { + const body: { + client_id: string; + email: string; + connection: string; + organization?: string; + } = { client_id: this.clientId, email: payload.email, connection: payload.connection, }; + if (payload.organization) { + body.organization = payload.organization; + } const { json, response } = await this.client.post( '/dbconnections/change_password', body, diff --git a/src/core/services/__tests__/AuthenticationOrchestrator.spec.ts b/src/core/services/__tests__/AuthenticationOrchestrator.spec.ts index 6f3a21c6..967e487d 100644 --- a/src/core/services/__tests__/AuthenticationOrchestrator.spec.ts +++ b/src/core/services/__tests__/AuthenticationOrchestrator.spec.ts @@ -447,6 +447,71 @@ describe('AuthenticationOrchestrator', () => { ); }); + it('resetPassword should include organization when provided', async () => { + mockHttpClientInstance.post.mockResolvedValueOnce({ + json: {}, + response: new Response(null, { status: 200 }), + }); + await orchestrator.resetPassword({ + email: 'info@auth0.com', + connection: 'Username-Password-Authentication', + organization: 'org_123', + }); + + expect(mockHttpClientInstance.post).toHaveBeenCalledWith( + '/dbconnections/change_password', + { + client_id: clientId, + email: 'info@auth0.com', + connection: 'Username-Password-Authentication', + organization: 'org_123', + }, + undefined + ); + }); + + it('resetPassword should handle custom headers', async () => { + mockHttpClientInstance.post.mockResolvedValueOnce({ + json: {}, + response: new Response(null, { status: 200 }), + }); + await orchestrator.resetPassword({ + email: 'info@auth0.com', + connection: 'Username-Password-Authentication', + organization: 'org_456', + headers: { 'X-Custom-Header': 'custom-value' }, + }); + + expect(mockHttpClientInstance.post).toHaveBeenCalledWith( + '/dbconnections/change_password', + { + client_id: clientId, + email: 'info@auth0.com', + connection: 'Username-Password-Authentication', + organization: 'org_456', + }, + { 'X-Custom-Header': 'custom-value' } + ); + }); + + it('resetPassword should handle error response', async () => { + const errorResponse = { + error: 'invalid_request', + error_description: 'Invalid connection', + }; + mockHttpClientInstance.post.mockResolvedValueOnce({ + json: errorResponse, + response: new Response(null, { status: 400 }), + }); + + await expect( + orchestrator.resetPassword({ + email: 'info@auth0.com', + connection: 'Invalid-Connection', + }) + ).rejects.toThrow(AuthError); + }); + it('createUser should send correct payload and return camelCased response', async () => { const userResponse = { id: 'user_id_123', diff --git a/src/types/parameters.ts b/src/types/parameters.ts index 6be6f3be..8ae10b7f 100644 --- a/src/types/parameters.ts +++ b/src/types/parameters.ts @@ -214,6 +214,7 @@ export interface UserInfoParameters extends RequestOptions { export interface ResetPasswordParameters extends RequestOptions { email: string; connection: string; + organization?: string; } /** Parameters for creating a new user in a database connection. */