import { AuthenticationClient } from "clients/authentication" ; import { ManagementClient } from "clients/management" ; import { chance , request } from "_/helpers" ; import { mockRegisterRequest , mockResetVerifyRequest } from "_/mocks/common" ; import { mockVerificationCode } from "_/mocks/verification" ; describe ( "POST /reset/:code" , ( ) => { const auth = new AuthenticationClient ( ) ; const mgmt = new ManagementClient ( ) ; it ( "should return error for malformed request" , ( ) => { const { id } = mockVerificationCode ( "reset" ) ; return request . post ( `/reset/${ id }` ) . set ( "Content-Type" , "application/json" ) . send ( `/${ chance . string ( ) }` ) . expect ( 400 , { type : "TypeError" , message : "Invalid request" } ) ; } ) ; it ( "should return error for invalid request" , ( ) => { const { id } = mockVerificationCode ( "reset" ) ; return request . post ( `/reset/${ id }` ) . set ( "Content-Type" , "application/json" ) . send ( `{ "${ chance . word ( ) }": "${ chance . string ( ) }" }` ) . expect ( 400 , { type : "TypeError" , message : "Invalid request" } ) ; } ) ; it ( "should return error for invalid verification code" , ( ) => { const { id } = mockVerificationCode ( "reset" ) ; return request . post ( `/reset/${ id }` ) . set ( "Content-Type" , "application/json" ) . send ( mockResetVerifyRequest ( ) ) . expect ( 403 , { type : "Error" , message : "Invalid verification code" } ) ; } ) ; describe ( "with confirmed user" , ( ) => { const { email , password } = mockRegisterRequest ( ) ; beforeAll ( async ( ) => { const { subject } = await auth . register ( email , password ) ; await mgmt . verifyUser ( subject ) ; } ) ; afterAll ( async ( ) => { } ) ; it ( "should return empty body" , async ( ) => { const { id } = await auth . forgotPassword ( email ) ; return request . post ( `/reset/${ id }` ) . set ( "Content-Type" , "application/json" ) . send ( mockResetVerifyRequest ( ) ) . expect ( 204 , "" ) ; } ) ; it ( "should set new password" , async ( ) => { const { id } = await auth . forgotPassword ( email ) ; const { password : value } = mockResetVerifyRequest ( ) ; await request . post ( `/reset/${ id }` ) . set ( "Content-Type" , "application/json" ) . send ( { password : value } ) . expect ( 204 , "" ) ; return auth . authenticateWithCredentials ( email , value ) ; } ) ; } ) ; } ) ;