import { parse as parseCookie , Cookie } from 'tough-cookie' ; import { FtrProviderContext } from '../../ftr_provider_context' ; export default function ( { getService } ) { const supertest = getService ( 'supertestWithoutAuth' ) ; const security = getService ( 'security' ) ; const mockUserName = 'test-user' ; const mockUserPassword = 'test-password' ; describe ( 'Change password' , ( ) => { let sessionCookie ; beforeEach ( async ( ) => { await security . user . create ( mockUserName , { password : mockUserPassword , roles : [ ] } ) ; const loginResponse = await supertest . post ( '/internal/security/login' ) . set ( 'kbn-xsrf' , 'xxx' ) . send ( { providerType : 'basic' , providerName : 'basic' , currentURL : '/' , params : { username : mockUserName , password : mockUserPassword } , } ) . expect ( 200 ) ; sessionCookie = parseCookie ( loginResponse . headers [ 'set-cookie' ] [ 0 ] ) ! ; } ) ; afterEach ( async ( ) => await security . user . delete ( mockUserName ) ) ; it ( 'should reject password change if current password is wrong' , async ( ) => { const wrongPassword = `wrong-${ mockUserPassword }` ; const newPassword = `xxx-${ mockUserPassword }-xxx` ; await supertest . post ( `/internal/security/users/${ mockUserName }/password` ) . set ( 'kbn-xsrf' , 'xxx' ) . set ( 'Cookie' , sessionCookie . cookieString ( ) ) . send ( { password : wrongPassword , newPassword } ) . expect ( 403 ) ; await supertest . post ( '/internal/security/login' ) . set ( 'kbn-xsrf' , 'xxx' ) . send ( { providerType : 'basic' , providerName : 'basic' , currentURL : '/' , params : { username : mockUserName , password : wrongPassword } , } ) . expect ( 401 ) ; await supertest . post ( '/internal/security/login' ) . set ( 'kbn-xsrf' , 'xxx' ) . send ( { providerType : 'basic' , providerName : 'basic' , currentURL : '/' , params : { username : mockUserName , password : newPassword } , } ) . expect ( 401 ) ; await supertest . post ( '/internal/security/login' ) . set ( 'kbn-xsrf' , 'xxx' ) . send ( { providerType : 'basic' , providerName : 'basic' , currentURL : '/' , params : { username : mockUserName , password : mockUserPassword } , } ) . expect ( 200 ) ; } ) ; it ( 'should allow password change if current password is correct' , async ( ) => { const newPassword = `xxx-${ mockUserPassword }-xxx` ; const passwordChangeResponse = await supertest . post ( `/internal/security/users/${ mockUserName }/password` ) . set ( 'kbn-xsrf' , 'xxx' ) . set ( 'Cookie' , sessionCookie . cookieString ( ) ) . send ( { password : mockUserPassword , newPassword } ) . expect ( 204 ) ; const newSessionCookie = parseCookie ( passwordChangeResponse . headers [ 'set-cookie' ] [ 0 ] ) ! ; await supertest . get ( '/internal/security/me' ) . set ( 'kbn-xsrf' , 'xxx' ) . set ( 'Cookie' , sessionCookie . cookieString ( ) ) . expect ( 200 ) ; await supertest . post ( '/internal/security/login' ) . set ( 'kbn-xsrf' , 'xxx' ) . send ( { providerType : 'basic' , providerName : 'basic' , currentURL : '/' , params : { username : mockUserName , password : mockUserPassword } , } ) . expect ( 401 ) ; await supertest . get ( '/internal/security/me' ) . set ( 'kbn-xsrf' , 'xxx' ) . set ( 'Cookie' , newSessionCookie . cookieString ( ) ) . expect ( 200 ) ; await supertest . post ( '/internal/security/login' ) . set ( 'kbn-xsrf' , 'xxx' ) . send ( { providerType : 'basic' , providerName : 'basic' , currentURL : '/' , params : { username : mockUserName , password : newPassword } , } ) . expect ( 200 ) ; } ) ; } ) ; }