@@ -174,18 +174,18 @@ describe('/tables', async () => {
174174 const nameColumn = datum . columns . find ( ( x ) => x . name === 'name' )
175175 const statusColumn = memes . columns . find ( ( x ) => x . name === 'status' )
176176 assert . equal ( tables . status , STATUS . SUCCESS )
177- assert . equal ( true , ! ! datum )
178- assert . equal ( true , ! notIncluded )
179- assert . equal ( datum [ 'rls_enabled' ] , false )
180- assert . equal ( datum [ 'rls_forced' ] , false )
181- assert . equal ( datum . columns . length > 0 , true )
182- assert . equal ( datum . primary_keys . length > 0 , true )
183- assert . equal ( idColumn . is_updatable , true )
184- assert . equal ( idColumn . is_identity , true )
185- assert . equal ( nameColumn . is_identity , false )
186- assert . equal ( datum . grants . length > 0 , true )
187- assert . equal ( datum . policies . length == 0 , true )
188- assert . equal ( statusColumn . enums [ 0 ] , 'new' )
177+ assert . equal ( true , ! ! datum , 'Table included' )
178+ assert . equal ( true , ! notIncluded , 'System table not included' )
179+ assert . equal ( datum [ 'rls_enabled' ] , false , 'RLS false' )
180+ assert . equal ( datum [ 'rls_forced' ] , false , 'RLS Forced' )
181+ assert . equal ( datum . columns . length > 0 , true , 'Has columns' )
182+ assert . equal ( datum . primary_keys . length > 0 , true , 'Has PK' )
183+ assert . equal ( idColumn . is_updatable , true , 'Is updatable' )
184+ assert . equal ( idColumn . is_identity , true , 'ID is Identity' )
185+ assert . equal ( nameColumn . is_identity , false , 'Name is not identity' )
186+ assert . equal ( datum . grants . length > 0 , true , 'Has grants' )
187+ assert . equal ( datum . policies . length == 0 , true , 'Has no policies' )
188+ assert . equal ( statusColumn . enums [ 0 ] , 'new' , 'Has enums' )
189189 } )
190190 it ( '/tables should return the relationships' , async ( ) => {
191191 const tables = await axios . get ( `${ URL } /tables` )
@@ -443,3 +443,62 @@ describe('/roles', () => {
443443 assert . equal ( newRoleExists , false )
444444 } )
445445} )
446+ describe ( '/policies' , ( ) => {
447+ var policy = {
448+ id : null ,
449+ name : 'test policy' ,
450+ schema : 'public' ,
451+ table : 'memes' ,
452+ action : 'RESTRICTIVE'
453+ }
454+ before ( async ( ) => {
455+ await axios . post ( `${ URL } /query` , {
456+ query : `DROP POLICY IF EXISTS "${ policy . name } " on "${ policy . schema } "."${ policy . table } " ` ,
457+ } )
458+ } )
459+ it ( 'GET' , async ( ) => {
460+ const res = await axios . get ( `${ URL } /policies` )
461+ // console.log('res', res)
462+ const policy = res . data [ 0 ]
463+ assert . equal ( 'id' in policy , true , 'Has ID' )
464+ assert . equal ( 'name' in policy , true , 'Has name' )
465+ assert . equal ( 'action' in policy , true , 'Has action' )
466+ assert . equal ( 'table' in policy , true , 'Has table' )
467+ assert . equal ( 'table_id' in policy , true , 'Has table_id' )
468+ assert . equal ( 'roles' in policy , true , 'Has roles' )
469+ assert . equal ( 'command' in policy , true , 'Has command' )
470+ assert . equal ( 'definition' in policy , true , 'Has definition' )
471+ assert . equal ( 'check' in policy , true , 'Has check' )
472+ } )
473+ it ( 'POST' , async ( ) => {
474+ const { data : newPolicy } = await axios . post ( `${ URL } /policies` , policy )
475+ assert . equal ( newPolicy . name , 'test policy' )
476+ assert . equal ( newPolicy . schema , 'public' )
477+ assert . equal ( newPolicy . table , 'memes' )
478+ assert . equal ( newPolicy . action , 'RESTRICTIVE' )
479+ assert . equal ( newPolicy . roles [ 0 ] , 'public' )
480+ assert . equal ( newPolicy . command , 'ALL' )
481+ assert . equal ( newPolicy . definition , null )
482+ assert . equal ( newPolicy . check , null )
483+ policy . id = newPolicy . id
484+ } )
485+ it ( 'PATCH' , async ( ) => {
486+ const updates = {
487+ name : 'policy updated' ,
488+ definition : `current_setting('my.username') IN (name)` ,
489+ check : `current_setting('my.username') IN (name)` ,
490+ }
491+ let { data : updated } = await axios . patch ( `${ URL } /policies/${ policy . id } ` , updates )
492+ // console.log('updated', updated)
493+ assert . equal ( updated . id , policy . id )
494+ assert . equal ( updated . name , 'policy updated' , 'name updated' )
495+ assert . notEqual ( updated . definition , null , 'definition updated' )
496+ assert . notEqual ( updated . check , null , 'check updated' )
497+ } )
498+ it ( 'DELETE' , async ( ) => {
499+ await axios . delete ( `${ URL } /policies/${ policy . id } ` )
500+ const { data : policies } = await axios . get ( `${ URL } /policies` )
501+ const stillExists = policies . some ( ( x ) => policy . id === x . id )
502+ assert . equal ( stillExists , false , 'Policy is deleted' )
503+ } )
504+ } )
0 commit comments