import { FtrProviderContext } from '../../ftr_provider_context' ; export default function ( { getService } ) { const supertest = getService ( 'supertestWithoutAuth' ) ; const es = getService ( 'es' ) ; async function createToken ( ) { const { access_token : accessToken } = await es . security . getToken ( { body : { grant_type : 'password' , username : 'elastic' , password : 'changeme' , } , } ) ; return accessToken ; } describe ( 'header' , ( ) => { it ( 'accepts valid access token via authorization Bearer header' , async ( ) => { const token = await createToken ( ) ; await supertest . get ( '/internal/security/me' ) . set ( 'kbn-xsrf' , 'true' ) . set ( 'authorization' , `Bearer ${ token }` ) . expect ( 200 ) ; } ) ; it ( 'accepts multiple requests for a single valid access token' , async ( ) => { const token = await createToken ( ) ; await supertest . get ( '/internal/security/me' ) . set ( 'kbn-xsrf' , 'true' ) . set ( 'authorization' , `Bearer ${ token }` ) . expect ( 200 ) ; await supertest . get ( '/internal/security/me' ) . set ( 'kbn-xsrf' , 'true' ) . set ( 'authorization' , `Bearer ${ token }` ) . expect ( 200 ) ; } ) ; it ( 'rejects invalid access token via authorization Bearer header' , async ( ) => { await supertest . get ( '/internal/security/me' ) . set ( 'kbn-xsrf' , 'true' ) . set ( 'authorization' , 'Bearer notreal' ) . expect ( 401 ) ; } ) ; it ( 'rejects expired access token via authorization Bearer header' , async function ( ) { this . timeout ( 40000 ) ; const token = await createToken ( ) ; await new Promise ( ( resolve ) => setTimeout ( resolve , 20000 ) ) ; await supertest . get ( '/internal/security/me' ) . set ( 'kbn-xsrf' , 'true' ) . set ( 'authorization' , `Bearer ${ token }` ) . expect ( 401 ) ; } ) ; } ) ; }