import { getServices , chance } from './lib' ; export function docExistsSuite ( ) { async function setup ( options = { } ) { const { initialSettings } = options ; const { osdServer , uiSettings , callCluster } = getServices ( ) ; await callCluster ( 'deleteByQuery' , { index : osdServer . config . get ( 'opensearchDashboards.index' ) , body : { conflicts : 'proceed' , query : { match_all : { } } , } , } ) ; if ( initialSettings ) { await uiSettings . setMany ( initialSettings ) ; } return { osdServer , uiSettings } ; } describe ( 'get route' , ( ) => { it ( 'returns a 200 and includes userValues' , async ( ) => { const defaultIndex = chance . word ( { length : 10 } ) ; const { osdServer } = await setup ( { initialSettings : { defaultIndex , } , } ) ; const { statusCode , result } = await osdServer . inject ( { method : 'GET' , url : '/api/opensearch-dashboards/settings' , } ) ; expect ( statusCode ) . toBe ( 200 ) ; expect ( result ) . toMatchObject ( { settings : { buildNum : { userValue : expect . any ( Number ) , } , defaultIndex : { userValue : defaultIndex , } , foo : { userValue : 'bar' , isOverridden : true , } , } , } ) ; } ) ; } ) ; describe ( 'set route' , ( ) => { it ( 'returns a 200 and all values including update' , async ( ) => { const { osdServer } = await setup ( ) ; const defaultIndex = chance . word ( ) ; const { statusCode , result } = await osdServer . inject ( { method : 'POST' , url : '/api/opensearch-dashboards/settings/defaultIndex' , payload : { value : defaultIndex , } , } ) ; expect ( statusCode ) . toBe ( 200 ) ; expect ( result ) . toMatchObject ( { settings : { buildNum : { userValue : expect . any ( Number ) , } , defaultIndex : { userValue : defaultIndex , } , foo : { userValue : 'bar' , isOverridden : true , } , } , } ) ; } ) ; it ( 'returns a 400 if trying to set overridden value' , async ( ) => { const { osdServer } = await setup ( ) ; const { statusCode , result } = await osdServer . inject ( { method : 'POST' , url : '/api/opensearch-dashboards/settings/foo' , payload : { value : 'baz' , } , } ) ; expect ( statusCode ) . toBe ( 400 ) ; expect ( result ) . toEqual ( { error : 'Bad Request' , message : 'Unable to update "foo" because it is overridden' , statusCode : 400 , } ) ; } ) ; } ) ; describe ( 'setMany route' , ( ) => { it ( 'returns a 200 and all values including updates' , async ( ) => { const { osdServer } = await setup ( ) ; const defaultIndex = chance . word ( ) ; const { statusCode , result } = await osdServer . inject ( { method : 'POST' , url : '/api/opensearch-dashboards/settings' , payload : { changes : { defaultIndex , } , } , } ) ; expect ( statusCode ) . toBe ( 200 ) ; expect ( result ) . toMatchObject ( { settings : { buildNum : { userValue : expect . any ( Number ) , } , defaultIndex : { userValue : defaultIndex , } , foo : { userValue : 'bar' , isOverridden : true , } , } , } ) ; } ) ; it ( 'returns a 400 if trying to set overridden value' , async ( ) => { const { osdServer } = await setup ( ) ; const { statusCode , result } = await osdServer . inject ( { method : 'POST' , url : '/api/opensearch-dashboards/settings' , payload : { changes : { foo : 'baz' , } , } , } ) ; expect ( statusCode ) . toBe ( 400 ) ; expect ( result ) . toEqual ( { error : 'Bad Request' , message : 'Unable to update "foo" because it is overridden' , statusCode : 400 , } ) ; } ) ; } ) ; describe ( 'delete route' , ( ) => { it ( 'returns a 200 and deletes the setting' , async ( ) => { const defaultIndex = chance . word ( { length : 10 } ) ; const { osdServer , uiSettings } = await setup ( { initialSettings : { defaultIndex } , } ) ; expect ( await uiSettings . get ( 'defaultIndex' ) ) . toBe ( defaultIndex ) ; const { statusCode , result } = await osdServer . inject ( { method : 'DELETE' , url : '/api/opensearch-dashboards/settings/defaultIndex' , } ) ; expect ( statusCode ) . toBe ( 200 ) ; expect ( result ) . toMatchObject ( { settings : { buildNum : { userValue : expect . any ( Number ) , } , foo : { userValue : 'bar' , isOverridden : true , } , } , } ) ; } ) ; it ( 'returns a 400 if deleting overridden value' , async ( ) => { const { osdServer } = await setup ( ) ; const { statusCode , result } = await osdServer . inject ( { method : 'DELETE' , url : '/api/opensearch-dashboards/settings/foo' , } ) ; expect ( statusCode ) . toBe ( 400 ) ; expect ( result ) . toEqual ( { error : 'Bad Request' , message : 'Unable to update "foo" because it is overridden' , statusCode : 400 , } ) ; } ) ; } ) ; }