@@ -215,6 +215,16 @@ jest.mock('../src/cio', () => ({
215215 syncNotificationFlagsToCio : jest . fn ( ) ,
216216} ) ) ;
217217
218+ const mockSetPassword = jest . fn ( ) ;
219+ jest . mock ( '../src/betterAuth' , ( ) => ( {
220+ ...( jest . requireActual ( '../src/betterAuth' ) as Record < string , unknown > ) ,
221+ getBetterAuth : ( ) => ( {
222+ api : {
223+ setPassword : mockSetPassword ,
224+ } ,
225+ } ) ,
226+ } ) ) ;
227+
218228beforeAll ( async ( ) => {
219229 con = await createOrGetConnection ( ) ;
220230 state = await initializeGraphQLTesting (
@@ -8150,3 +8160,50 @@ describe('query userPostsAnalyticsHistory', () => {
81508160 } ) ;
81518161 } ) ;
81528162} ) ;
8163+
8164+ describe ( 'mutation setPassword' , ( ) => {
8165+ const MUTATION = `
8166+ mutation SetPassword($newPassword: String!) {
8167+ setPassword(newPassword: $newPassword) {
8168+ _
8169+ }
8170+ }
8171+ ` ;
8172+
8173+ it ( 'should not authorize when not logged in' , ( ) =>
8174+ testMutationErrorCode (
8175+ client ,
8176+ {
8177+ mutation : MUTATION ,
8178+ variables : { newPassword : 'newPassword123!' } ,
8179+ } ,
8180+ 'UNAUTHENTICATED' ,
8181+ ) ) ;
8182+
8183+ it ( 'should set password via better auth api' , async ( ) => {
8184+ loggedUser = '1' ;
8185+ mockSetPassword . mockResolvedValueOnce ( { status : true } ) ;
8186+
8187+ const res = await client . mutate ( MUTATION , {
8188+ variables : { newPassword : 'newPassword123!' } ,
8189+ } ) ;
8190+
8191+ expect ( res . errors ) . toBeFalsy ( ) ;
8192+ expect ( mockSetPassword ) . toHaveBeenCalledWith ( {
8193+ body : { newPassword : 'newPassword123!' } ,
8194+ headers : expect . any ( Headers ) ,
8195+ } ) ;
8196+ } ) ;
8197+
8198+ it ( 'should propagate error when better auth api fails' , async ( ) => {
8199+ loggedUser = '1' ;
8200+ mockSetPassword . mockRejectedValueOnce ( new Error ( 'Password too weak' ) ) ;
8201+
8202+ const res = await client . mutate ( MUTATION , {
8203+ variables : { newPassword : 'weak' } ,
8204+ } ) ;
8205+
8206+ expect ( res . errors ) . toBeTruthy ( ) ;
8207+ expect ( res . errors [ 0 ] . message ) . toBe ( 'Password too weak' ) ;
8208+ } ) ;
8209+ } ) ;
0 commit comments