@@ -88,6 +88,7 @@ describe('Configurations Controller', () => {
8888 'getAll' ,
8989 'getLatest' ,
9090 'getByVersion' ,
91+ 'updateSandboxConfig' ,
9192 ] ;
9293
9394 let mockDataAccess ;
@@ -235,4 +236,140 @@ describe('Configurations Controller', () => {
235236 expect ( result . status ) . to . equal ( 400 ) ;
236237 expect ( error ) . to . have . property ( 'message' , 'Configuration version required to be an integer' ) ;
237238 } ) ;
239+
240+ describe ( 'Sandbox Configuration Methods' , ( ) => {
241+ let mockGlobalConfig ;
242+
243+ beforeEach ( ( ) => {
244+ mockGlobalConfig = {
245+ updateSandboxAuditConfigs : sandbox . stub ( ) ,
246+ } ;
247+
248+ mockDataAccess . Configuration . findLatest = sandbox . stub ( ) . resolves ( mockGlobalConfig ) ;
249+ mockDataAccess . Configuration . save = sandbox . stub ( ) . resolves ( ) ;
250+ } ) ;
251+
252+ describe ( 'updateSandboxConfig' , ( ) => {
253+ it ( 'should update sandbox configurations successfully' , async ( ) => {
254+ const requestContext = {
255+ data : {
256+ sandboxConfigs : {
257+ cwv : { expire : '10' } ,
258+ 'meta-tags' : { expire : '15' } ,
259+ } ,
260+ } ,
261+ } ;
262+
263+ const updatedConfig = { ...mockGlobalConfig } ;
264+ mockGlobalConfig . updateSandboxAuditConfigs . returns ( updatedConfig ) ;
265+
266+ const result = await configurationsController . updateSandboxConfig ( requestContext ) ;
267+ const response = await result . json ( ) ;
268+
269+ expect ( result . status ) . to . equal ( 200 ) ;
270+ expect ( response ) . to . have . property ( 'message' , 'Sandbox configurations updated successfully' ) ;
271+ expect ( response ) . to . have . property ( 'updatedConfigs' ) ;
272+ expect ( response . updatedConfigs ) . to . deep . equal ( {
273+ cwv : { expire : '10' } ,
274+ 'meta-tags' : { expire : '15' } ,
275+ } ) ;
276+ expect ( response ) . to . have . property ( 'totalUpdated' , 2 ) ;
277+ expect ( mockGlobalConfig . updateSandboxAuditConfigs ) . to . have . been . calledWith ( {
278+ cwv : { expire : '10' } ,
279+ 'meta-tags' : { expire : '15' } ,
280+ } ) ;
281+ expect ( mockDataAccess . Configuration . save ) . to . have . been . calledWith ( updatedConfig ) ;
282+ } ) ;
283+
284+ it ( 'should return bad request when sandboxConfigs is missing' , async ( ) => {
285+ const requestContext = {
286+ data : { } ,
287+ } ;
288+
289+ const result = await configurationsController . updateSandboxConfig ( requestContext ) ;
290+ const error = await result . json ( ) ;
291+
292+ expect ( result . status ) . to . equal ( 400 ) ;
293+ expect ( error . message ) . to . include ( 'sandboxConfigs object is required' ) ;
294+ } ) ;
295+
296+ it ( 'should return bad request when context.data is undefined' , async ( ) => {
297+ const requestContext = { } ;
298+
299+ const result = await configurationsController . updateSandboxConfig ( requestContext ) ;
300+ const error = await result . json ( ) ;
301+
302+ expect ( result . status ) . to . equal ( 400 ) ;
303+ expect ( error . message ) . to . include ( 'sandboxConfigs object is required' ) ;
304+ } ) ;
305+
306+ it ( 'should return bad request when sandboxConfigs is not an object' , async ( ) => {
307+ const requestContext = {
308+ data : {
309+ sandboxConfigs : 'invalid' ,
310+ } ,
311+ } ;
312+
313+ const result = await configurationsController . updateSandboxConfig ( requestContext ) ;
314+ const error = await result . json ( ) ;
315+
316+ expect ( result . status ) . to . equal ( 400 ) ;
317+ expect ( error . message ) . to . include ( 'sandboxConfigs object is required' ) ;
318+ } ) ;
319+
320+ it ( 'should return forbidden for non-admin users' , async ( ) => {
321+ context . attributes . authInfo . withProfile ( { is_admin : false } ) ;
322+
323+ const requestContext = {
324+ data : {
325+ sandboxConfigs : {
326+ cwv : { expire : '10' } ,
327+ } ,
328+ } ,
329+ } ;
330+
331+ const result = await configurationsController . updateSandboxConfig ( requestContext ) ;
332+ const error = await result . json ( ) ;
333+
334+ expect ( result . status ) . to . equal ( 403 ) ;
335+ expect ( error . message ) . to . include ( 'Only admins can update sandbox configurations' ) ;
336+ } ) ;
337+
338+ it ( 'should return not found when configuration does not exist' , async ( ) => {
339+ mockDataAccess . Configuration . findLatest . resolves ( null ) ;
340+
341+ const requestContext = {
342+ data : {
343+ sandboxConfigs : {
344+ cwv : { expire : '10' } ,
345+ } ,
346+ } ,
347+ } ;
348+
349+ const result = await configurationsController . updateSandboxConfig ( requestContext ) ;
350+ const error = await result . json ( ) ;
351+
352+ expect ( result . status ) . to . equal ( 404 ) ;
353+ expect ( error . message ) . to . include ( 'Configuration not found' ) ;
354+ } ) ;
355+
356+ it ( 'should return bad request when updateSandboxAuditConfigs throws an error' , async ( ) => {
357+ const requestContext = {
358+ data : {
359+ sandboxConfigs : {
360+ cwv : { expire : '10' } ,
361+ } ,
362+ } ,
363+ } ;
364+
365+ mockGlobalConfig . updateSandboxAuditConfigs . throws ( new Error ( 'Update failed' ) ) ;
366+
367+ const result = await configurationsController . updateSandboxConfig ( requestContext ) ;
368+ const error = await result . json ( ) ;
369+
370+ expect ( result . status ) . to . equal ( 400 ) ;
371+ expect ( error . message ) . to . include ( 'Error updating sandbox configuration: Update failed' ) ;
372+ } ) ;
373+ } ) ;
374+ } ) ;
238375} ) ;
0 commit comments