@@ -45,9 +45,11 @@ export default function agentConfigurationTests({ getService }: FtrProviderConte
4545 return supertestRead . get ( `/api/apm/settings/agent-configuration` ) . set ( 'kbn-xsrf' , 'foo' ) ;
4646 }
4747
48- async function createConfiguration ( config : AgentConfigurationIntake ) {
48+ async function createConfiguration ( config : AgentConfigurationIntake , { user = 'write' } = { } ) {
4949 log . debug ( 'creating configuration' , config . service ) ;
50- const res = await supertestWrite
50+ const supertestClient = user === 'read' ? supertestRead : supertestWrite ;
51+
52+ const res = await supertestClient
5153 . put ( `/api/apm/settings/agent-configuration` )
5254 . send ( config )
5355 . set ( 'kbn-xsrf' , 'foo' ) ;
@@ -57,9 +59,11 @@ export default function agentConfigurationTests({ getService }: FtrProviderConte
5759 return res ;
5860 }
5961
60- async function updateConfiguration ( config : AgentConfigurationIntake ) {
62+ async function updateConfiguration ( config : AgentConfigurationIntake , { user = 'write' } = { } ) {
6163 log . debug ( 'updating configuration' , config . service ) ;
62- const res = await supertestWrite
64+ const supertestClient = user === 'read' ? supertestRead : supertestWrite ;
65+
66+ const res = await supertestClient
6367 . put ( `/api/apm/settings/agent-configuration?overwrite=true` )
6468 . send ( config )
6569 . set ( 'kbn-xsrf' , 'foo' ) ;
@@ -69,9 +73,14 @@ export default function agentConfigurationTests({ getService }: FtrProviderConte
6973 return res ;
7074 }
7175
72- async function deleteConfiguration ( { service } : AgentConfigurationIntake ) {
76+ async function deleteConfiguration (
77+ { service } : AgentConfigurationIntake ,
78+ { user = 'write' } = { }
79+ ) {
7380 log . debug ( 'deleting configuration' , service ) ;
74- const res = await supertestWrite
81+ const supertestClient = user === 'read' ? supertestRead : supertestWrite ;
82+
83+ const res = await supertestClient
7584 . delete ( `/api/apm/settings/agent-configuration` )
7685 . send ( { service } )
7786 . set ( 'kbn-xsrf' , 'foo' ) ;
@@ -84,11 +93,16 @@ export default function agentConfigurationTests({ getService }: FtrProviderConte
8493 function throwOnError ( res : any ) {
8594 const { statusCode, req, body } = res ;
8695 if ( statusCode !== 200 ) {
87- throw new Error ( `
96+ const e = new Error ( `
8897 Endpoint: ${ req . method } ${ req . path }
8998 Service: ${ JSON . stringify ( res . request . _data . service ) }
9099 Status code: ${ statusCode }
91100 Response: ${ body . message } ` ) ;
101+
102+ // @ts -ignore
103+ e . res = res ;
104+
105+ throw e ;
92106 }
93107 }
94108
@@ -133,6 +147,47 @@ export default function agentConfigurationTests({ getService }: FtrProviderConte
133147 } ) ;
134148 } ) ;
135149
150+ describe ( 'as a read-only user' , ( ) => {
151+ const newConfig = { service : { } , settings : { transaction_sample_rate : '0.55' } } ;
152+ it ( 'throws when attempting to create config' , async ( ) => {
153+ try {
154+ await createConfiguration ( newConfig , { user : 'read' } ) ;
155+
156+ // ensure that `createConfiguration` throws
157+ expect ( true ) . to . be ( false ) ;
158+ } catch ( e ) {
159+ expect ( e . res . statusCode ) . to . be ( 404 ) ;
160+ }
161+ } ) ;
162+
163+ describe ( 'when a configuration already exists' , ( ) => {
164+ before ( async ( ) => createConfiguration ( newConfig ) ) ;
165+ after ( async ( ) => deleteConfiguration ( newConfig ) ) ;
166+
167+ it ( 'throws when attempting to update config' , async ( ) => {
168+ try {
169+ await updateConfiguration ( newConfig , { user : 'read' } ) ;
170+
171+ // ensure that `updateConfiguration` throws
172+ expect ( true ) . to . be ( false ) ;
173+ } catch ( e ) {
174+ expect ( e . res . statusCode ) . to . be ( 404 ) ;
175+ }
176+ } ) ;
177+
178+ it ( 'throws when attempting to delete config' , async ( ) => {
179+ try {
180+ await deleteConfiguration ( newConfig , { user : 'read' } ) ;
181+
182+ // ensure that `deleteConfiguration` throws
183+ expect ( true ) . to . be ( false ) ;
184+ } catch ( e ) {
185+ expect ( e . res . statusCode ) . to . be ( 404 ) ;
186+ }
187+ } ) ;
188+ } ) ;
189+ } ) ;
190+
136191 describe ( 'when creating one configuration' , ( ) => {
137192 const newConfig = {
138193 service : { } ,
@@ -144,59 +199,53 @@ export default function agentConfigurationTests({ getService }: FtrProviderConte
144199 etag : '7312bdcc34999629a3d39df24ed9b2a7553c0c39' ,
145200 } ;
146201
147- it ( 'can find the created config' , async ( ) => {
148- // setup
149- await createConfiguration ( newConfig ) ;
150-
151- const { status, body } = await searchConfigurations ( searchParams ) ;
152- expect ( status ) . to . equal ( 200 ) ;
153- expect ( body . _source . service ) . to . eql ( { } ) ;
154- expect ( body . _source . settings ) . to . eql ( { transaction_sample_rate : '0.55' } ) ;
202+ it ( 'can create and delete config' , async ( ) => {
203+ // assert that config does not exist
204+ const res1 = await searchConfigurations ( searchParams ) ;
205+ expect ( res1 . status ) . to . equal ( 404 ) ;
155206
156- // cleanup
157- await deleteConfiguration ( newConfig ) ;
158- } ) ;
159-
160- it ( 'can list one config' , async ( ) => {
161- // setup
207+ // assert that config was created
162208 await createConfiguration ( newConfig ) ;
209+ const res2 = await searchConfigurations ( searchParams ) ;
210+ expect ( res2 . status ) . to . equal ( 200 ) ;
163211
164- const { status, body } = await getAllConfigurations ( ) ;
165- expect ( status ) . to . equal ( 200 ) ;
166- expect ( omitTimestamp ( body ) ) . to . eql ( [
167- {
168- service : { } ,
169- settings : { transaction_sample_rate : '0.55' } ,
170- applied_by_agent : false ,
171- etag : 'eb88a8997666cc4b33745ef355a1bbd7c4782f2d' ,
172- } ,
173- ] ) ;
174-
175- // cleanup
212+ // assert that config was deleted
176213 await deleteConfiguration ( newConfig ) ;
214+ const res3 = await searchConfigurations ( searchParams ) ;
215+ expect ( res3 . status ) . to . equal ( 404 ) ;
177216 } ) ;
178217
179- it ( 'can update the created config' , async ( ) => {
180- // setup
181- await createConfiguration ( newConfig ) ;
182-
183- await updateConfiguration ( { service : { } , settings : { transaction_sample_rate : '0.85' } } ) ;
184- const { status, body } = await searchConfigurations ( searchParams ) ;
185- expect ( status ) . to . equal ( 200 ) ;
186- expect ( body . _source . service ) . to . eql ( { } ) ;
187- expect ( body . _source . settings ) . to . eql ( { transaction_sample_rate : '0.85' } ) ;
218+ describe ( 'when a configuration exists' , ( ) => {
219+ before ( async ( ) => createConfiguration ( newConfig ) ) ;
220+ after ( async ( ) => deleteConfiguration ( newConfig ) ) ;
188221
189- // cleanup
190- await deleteConfiguration ( newConfig ) ;
191- } ) ;
222+ it ( 'can find the config' , async ( ) => {
223+ const { status, body } = await searchConfigurations ( searchParams ) ;
224+ expect ( status ) . to . equal ( 200 ) ;
225+ expect ( body . _source . service ) . to . eql ( { } ) ;
226+ expect ( body . _source . settings ) . to . eql ( { transaction_sample_rate : '0.55' } ) ;
227+ } ) ;
192228
193- it ( 'can delete the created config' , async ( ) => {
194- // setup
195- await createConfiguration ( newConfig ) ;
229+ it ( 'can list the config' , async ( ) => {
230+ const { status, body } = await getAllConfigurations ( ) ;
231+ expect ( status ) . to . equal ( 200 ) ;
232+ expect ( omitTimestamp ( body ) ) . to . eql ( [
233+ {
234+ service : { } ,
235+ settings : { transaction_sample_rate : '0.55' } ,
236+ applied_by_agent : false ,
237+ etag : 'eb88a8997666cc4b33745ef355a1bbd7c4782f2d' ,
238+ } ,
239+ ] ) ;
240+ } ) ;
196241
197- await deleteConfiguration ( newConfig ) ;
198- const { status } = await searchConfigurations ( searchParams ) ;
199- expect ( status ) . to . equal ( 404 ) ;
242+ it ( 'can update the config' , async ( ) => {
243+ await updateConfiguration ( { service : { } , settings : { transaction_sample_rate : '0.85' } } ) ;
244+ const { status, body } = await searchConfigurations ( searchParams ) ;
245+ expect ( status ) . to . equal ( 200 ) ;
246+ expect ( body . _source . service ) . to . eql ( { } ) ;
247+ expect ( body . _source . settings ) . to . eql ( { transaction_sample_rate : '0.85' } ) ;
248+ } ) ;
200249 } ) ;
201250 } ) ;
202251
0 commit comments