import { AuthenticationClient } from "clients/authentication" ; import { ManagementClient } from "clients/management" ; import { chance , request } from "_/helpers" ; import { mockRegisterRequest , mockResetRequest } from "_/mocks/common" ; describe ( "POST /reset" , ( ) => { const auth = new AuthenticationClient ( ) ; const mgmt = new ManagementClient ( ) ; it ( "should return error for empty request" , ( ) => { return request . post ( "/reset" ) . set ( "Content-Type" , "application/json" ) . expect ( 400 , { type : "TypeError" , message : "Invalid request" } ) ; } ) ; it ( "should return error for malformed request" , ( ) => { return request . post ( "/reset" ) . set ( "Content-Type" , "application/json" ) . send ( `/${ chance . string ( ) }` ) . expect ( 400 , { type : "TypeError" , message : "Invalid request" } ) ; } ) ; it ( "should return error for invalid request" , ( ) => { return request . post ( "/reset" ) . set ( "Content-Type" , "application/json" ) . send ( `{}` ) . expect ( 400 , { type : "TypeError" , message : "Invalid request" } ) ; } ) ; it ( "should return error for invalid user" , ( ) => { return request . post ( "/reset" ) . set ( "Content-Type" , "application/json" ) . send ( mockResetRequest ( ) ) . expect ( 400 , { type : "UserNotFoundException" , message : "User does not exist" } ) ; } ) ; describe ( "with unconfirmed user" , ( ) => { const { email , password } = mockRegisterRequest ( ) ; let id ; beforeAll ( async ( ) => { const { subject } = await auth . register ( email , password ) ; id = subject ; } ) ; afterAll ( async ( ) => { await mgmt . deleteUser ( id ) ; } ) ; it ( "should return error for unverified user" , ( ) => { return request . post ( "/reset" ) . set ( "Content-Type" , "application/json" ) . send ( { username : email } ) . expect ( 400 , { type : "UserNotFoundException" , message : "User does not exist" } ) ; } ) ; } ) ; describe ( "with confirmed user" , ( ) => { const { email , password } = mockRegisterRequest ( ) ; beforeAll ( async ( ) => { const { subject } = await auth . register ( email , password ) ; await mgmt . verifyUser ( subject ) ; } ) ; afterAll ( async ( ) => { await mgmt . deleteUser ( email ) ; } ) ; it ( "should return empty body" , ( ) => { return request . post ( "/reset" ) . set ( "Content-Type" , "application/json" ) . send ( { username : email } ) . expect ( 204 , "" ) ; } ) ; } ) ; } ) ;