1515//
1616// 1. ⭐ an upsert naming a row the caller cannot read answers the SAME body as
1717// a batch create naming that id (the row's `id` echo aside), whose code and
18- // status are the plain `POST /data/:object` create's `409
19- // UNIQUE_VIOLATION`; the stored row is byte-identical afterwards and the
20- // answer names no other organization;
21- // 2. an upsert naming an id that exists nowhere is created, as the create is;
18+ // status are the plain `POST /data/:object` create's — `409
19+ // UNIQUE_VIOLATION` for a caller holding the create grant, the create's
20+ // `403 PERMISSION_DENIED` for one without it; the stored row is
21+ // byte-identical afterwards and the answer names no other organization;
22+ // 2. an upsert naming an id that exists nowhere answers as the create does:
23+ // created with the create grant, the create's refusal without it (and
24+ // then the same refusal as for a held id);
2225// 3. a row the caller CAN read keeps the update arm: written with the edit
2326// grant, the engine's refusal without it.
2427//
3235// reads: the update arm's refusal, unchanged;
3336// - a create-only principal on `showcase_contact` (no read at all) — every
3437// row is one it cannot read;
38+ // - the edit-without-read class on `showcase_semantic_zoo`: a principal
39+ // holding edit and no read, once without and once with create. The object
40+ // declares `public_read_write`, so the platform's `update` ownership floor
41+ // does not bind it, and no baseline set grants anything on it; an edit
42+ // grant makes no row readable, so every row is one the principal cannot
43+ // read;
3544// - control: member, `showcase_account` (read only) — one refusal for a row
3645// that exists and for one that does not, unchanged.
3746//
@@ -100,13 +109,22 @@ describe('upsert by id: a row the caller cannot read answers as a create naming
100109 let adminTok : string ;
101110 let memberTok : string ;
102111 let creatorTok : string ;
112+ let editorTok : string ;
113+ let editCreatorTok : string ;
103114 const d = doors ( ( ) => stack ) ;
104115 const rows : Record < string , string > = { } ;
105116 let projectId = '' ;
106117
107118 const CREATE_ONLY = 'qa_upsert_read_contact_create_only' ;
108119 const CREATOR = 'upsert-read-creator@verify.test' ;
109120 const MEMBER = 'upsert-read-member@verify.test' ;
121+ // The edit-without-read class, on an object whose OWD keeps the platform's
122+ // `update` ownership floor out of play and that no baseline set grants.
123+ const OPEN_WRITE = 'showcase_semantic_zoo' ;
124+ const EDIT_ONLY = 'qa_upsert_read_zoo_edit_only' ;
125+ const EDIT_CREATE = 'qa_upsert_read_zoo_edit_create' ;
126+ const EDITOR = 'upsert-read-editor@verify.test' ;
127+ const EDIT_CREATOR = 'upsert-read-edit-creator@verify.test' ;
110128
111129 const stored = async ( object : string , id : string ) =>
112130 JSON . stringify ( await ql . findOne ( object , { where : { id } , context : SYS } ) ) ;
@@ -117,6 +135,27 @@ describe('upsert by id: a row the caller cannot read answers as a create naming
117135 showcase_task : ( ) => ( { title : 'upsert probe' , project : projectId , status : 'todo' } ) ,
118136 showcase_contact : ( ) => ( { name : 'Upsert Probe' , email : 'upsert-probe@verify.test' } ) ,
119137 showcase_account : ( ) => ( { name : 'Upsert Probe' , status : 'prospect' } ) ,
138+ [ OPEN_WRITE ] : ( ) => ( { name : 'Upsert Probe' } ) ,
139+ } ;
140+
141+ /** Save a permission set granting `grant` on one object, and bind it to a fresh user. */
142+ const principalWith = async ( set : string , object : string , grant : Record < string , boolean > , email : string ) : Promise < string > => {
143+ const saved = await stack . apiAs ( adminTok , 'PUT' , `/meta/permission/${ set } ` , {
144+ name : set , label : set , systemPermissions : [ ] , objects : { [ object ] : grant } ,
145+ } ) ;
146+ expect ( saved . status , await saved . clone ( ) . text ( ) ) . toBe ( 200 ) ;
147+ const token = await stack . signUp ( email , PASS ) ;
148+ const userId = ( await ql . findOne ( 'sys_user' , { where : { email } , context : SYS } ) ) ?. id ;
149+ expect ( userId , `${ email } provisioned` ) . toBeTruthy ( ) ;
150+ await ql . insert ( 'sys_user_permission_set' , { user_id : userId , permission_set : set } , { context : SYS } ) ;
151+ return token ;
152+ } ;
153+
154+ /** The `object_crud` layer's verdict `/security/explain` gives the caller for one operation. */
155+ const crudVerdict = async ( token : string , object : string , operation : string ) : Promise < string > => {
156+ const res = await d . call ( token , 'GET' , `/security/explain?object=${ object } &operation=${ operation } ` ) ;
157+ expect ( res . status , JSON . stringify ( res . body ) ) . toBe ( 200 ) ;
158+ return String ( ( res . body ?. layers ?? [ ] ) . find ( ( l : any ) => l ?. layer === 'object_crud' ) ?. verdict ) ;
120159 } ;
121160
122161 beforeAll ( async ( ) => {
@@ -137,12 +176,16 @@ describe('upsert by id: a row the caller cannot read answers as a create naming
137176 expect ( creatorId , 'creator provisioned' ) . toBeTruthy ( ) ;
138177 await ql . insert ( 'sys_user_permission_set' , { user_id : creatorId , permission_set : CREATE_ONLY } , { context : SYS } ) ;
139178
179+ // The edit-without-read class: edit and no read, without and with create.
180+ editorTok = await principalWith ( EDIT_ONLY , OPEN_WRITE , { allowRead : false , allowCreate : false , allowEdit : true , allowDelete : false } , EDITOR ) ;
181+ editCreatorTok = await principalWith ( EDIT_CREATE , OPEN_WRITE , { allowRead : false , allowCreate : true , allowEdit : true , allowDelete : false } , EDIT_CREATOR ) ;
182+
140183 projectId = String ( ( await ql . find ( 'showcase_project' , { limit : 1 , context : SYS } ) ) ?. [ 0 ] ?. id ?? '' ) ;
141184 expect ( projectId , 'the showcase seed provides a project' ) . toBeTruthy ( ) ;
142185
143186 // Rows the admin owns: hidden from the member by the private OWD, and from
144- // the creator by having no read at all.
145- for ( const object of [ 'showcase_inquiry' , 'showcase_private_note' , 'showcase_contact' ] ) {
187+ // the creator and the edit-without-read principals by having no read at all.
188+ for ( const object of [ 'showcase_inquiry' , 'showcase_private_note' , 'showcase_contact' , OPEN_WRITE ] ) {
146189 const made = await d . create ( adminTok , object , unknownId ( ) , PAYLOAD [ object ] ( ) ) ;
147190 expect ( made . status , `admin ${ object } created — ${ JSON . stringify ( made . body ) } ` ) . toBeLessThan ( 300 ) ;
148191 rows [ `hidden:${ object } ` ] = createdId ( made ) ;
@@ -171,15 +214,32 @@ describe('upsert by id: a row the caller cannot read answers as a create naming
171214 expect ( ( await d . create ( memberTok , 'showcase_account' , unknownId ( ) , PAYLOAD . showcase_account ( ) ) ) . status , 'the member may not create accounts' ) . toBe ( 403 ) ;
172215 expect ( ( await d . call ( creatorTok , 'GET' , `/data/showcase_contact/${ rows [ 'hidden:showcase_contact' ] } ` ) ) . status , 'the creator reads no contact' ) . toBe ( 403 ) ;
173216 expect ( ( await d . create ( creatorTok , 'showcase_contact' , unknownId ( ) , PAYLOAD . showcase_contact ( ) ) ) . status , 'the creator creates contacts' ) . toBe ( 201 ) ;
217+
218+ // The edit-without-read class: the edit grant is real, the read grant is
219+ // absent, and the create grant is exactly where the table says.
220+ const zooRow = rows [ `hidden:${ OPEN_WRITE } ` ] ;
221+ expect ( ( await d . call ( adminTok , 'GET' , `/data/${ OPEN_WRITE } /${ zooRow } ` ) ) . status ) . toBe ( 200 ) ;
222+ for ( const [ who , tok , mayCreate ] of [ [ 'edit only' , editorTok , false ] , [ 'edit+create' , editCreatorTok , true ] ] as const ) {
223+ expect ( await crudVerdict ( tok , OPEN_WRITE , 'update' ) , `${ who } : holds edit` ) . toBe ( 'grants' ) ;
224+ expect ( await crudVerdict ( tok , OPEN_WRITE , 'read' ) , `${ who } : holds no read` ) . toBe ( 'denies' ) ;
225+ expect ( ( await d . call ( tok , 'GET' , `/data/${ OPEN_WRITE } /${ zooRow } ` ) ) . status , `${ who } : reads no row` ) . toBe ( 403 ) ;
226+ expect ( ( await d . create ( tok , OPEN_WRITE , unknownId ( ) , PAYLOAD [ OPEN_WRITE ] ( ) ) ) . status , `${ who } : the create grant` ) . toBe ( mayCreate ? 201 : 403 ) ;
227+ }
174228 } ) ;
175229
176- const HIDDEN : Array < [ string , string , ( ) => string ] > = [
177- [ 'member, read+create without edit, private row' , 'showcase_inquiry' , ( ) => memberTok ] ,
178- [ 'member, read+create+edit, private row' , 'showcase_private_note' , ( ) => memberTok ] ,
179- [ 'create-only principal, no read at all' , 'showcase_contact' , ( ) => creatorTok ] ,
230+ /** The plain create's answer for an id already held, by whether the caller holds the create grant. */
231+ const COLLISION = { status : 409 , code : 'UNIQUE_VIOLATION' , mayCreate : true } as const ;
232+ const CREATE_REFUSED = { status : 403 , code : 'PERMISSION_DENIED' , mayCreate : false } as const ;
233+
234+ const HIDDEN : Array < [ string , string , ( ) => string , typeof COLLISION | typeof CREATE_REFUSED ] > = [
235+ [ 'member, read+create without edit, private row' , 'showcase_inquiry' , ( ) => memberTok , COLLISION ] ,
236+ [ 'member, read+create+edit, private row' , 'showcase_private_note' , ( ) => memberTok , COLLISION ] ,
237+ [ 'create-only principal, no read at all' , 'showcase_contact' , ( ) => creatorTok , COLLISION ] ,
238+ [ 'edit without read or create, open-write OWD' , OPEN_WRITE , ( ) => editorTok , CREATE_REFUSED ] ,
239+ [ 'edit+create without read, open-write OWD' , OPEN_WRITE , ( ) => editCreatorTok , COLLISION ] ,
180240 ] ;
181241
182- for ( const [ label , object , tok ] of HIDDEN ) {
242+ for ( const [ label , object , tok , answer ] of HIDDEN ) {
183243 for ( const atomic of [ false , true ] ) {
184244 it ( `${ label } (${ object } , ${ atomic ? 'atomic' : 'non-atomic' } ): the unreadable row answers as a create naming it` , async ( ) => {
185245 const id = rows [ `hidden:${ object } ` ] ;
@@ -192,25 +252,34 @@ describe('upsert by id: a row the caller cannot read answers as a create naming
192252 // ⭐ The create's answer, whole: same status, same envelope, same row
193253 // refusal — nothing of the upsert's own.
194254 expect ( comparable ( ups ) , JSON . stringify ( ups . body ) ) . toEqual ( comparable ( batchCreate ) ) ;
195- expect ( create . status , JSON . stringify ( create . body ) ) . toBe ( 409 ) ;
196- expect ( create . body ?. code ) . toBe ( 'UNIQUE_VIOLATION' ) ;
255+ expect ( create . status , JSON . stringify ( create . body ) ) . toBe ( answer . status ) ;
256+ expect ( create . body ?. code ) . toBe ( answer . code ) ;
197257 expect ( ups . body ?. results ?. [ 0 ] ?. errors ?. [ 0 ] ) . toMatchObject ( { code : create . body . code , httpStatus : create . status } ) ;
198258 expect ( ups . body ?. results ?. [ 0 ] ?. id ) . toBe ( id ) ;
199259 // Nothing moved.
200260 expect ( await stored ( object , id ) ) . toBe ( before ) ;
201261 } ) ;
202262
203- it ( `${ label } (${ object } , ${ atomic ? 'atomic' : 'non-atomic' } ): an id that names no row is created, as the create is ` , async ( ) => {
263+ it ( `${ label } (${ object } , ${ atomic ? 'atomic' : 'non-atomic' } ): an id that names no row answers as the create does ` , async ( ) => {
204264 const id = unknownId ( ) ;
205265 const ups = await d . upsert ( tok ( ) , object , id , PAYLOAD [ object ] ( ) , atomic ) ;
206- expect ( ups . status , JSON . stringify ( ups . body ) ) . toBe ( 200 ) ;
207- expect ( ups . body ?. success , JSON . stringify ( ups . body ) ) . toBe ( true ) ;
208- expect ( ups . body ?. results ?. [ 0 ] ) . toMatchObject ( { id, success : true } ) ;
209- expect ( JSON . parse ( await stored ( object , id ) ) ?. id ) . toBe ( id ) ;
210-
211266 const other = unknownId ( ) ;
212267 const batchCreate = await d . batchCreate ( tok ( ) , object , other , PAYLOAD [ object ] ( ) , atomic ) ;
213- expect ( batchCreate . body ?. success , JSON . stringify ( batchCreate . body ) ) . toBe ( true ) ;
268+ if ( answer . mayCreate ) {
269+ expect ( ups . status , JSON . stringify ( ups . body ) ) . toBe ( 200 ) ;
270+ expect ( ups . body ?. success , JSON . stringify ( ups . body ) ) . toBe ( true ) ;
271+ expect ( ups . body ?. results ?. [ 0 ] ) . toMatchObject ( { id, success : true } ) ;
272+ expect ( JSON . parse ( await stored ( object , id ) ) ?. id ) . toBe ( id ) ;
273+ expect ( batchCreate . body ?. success , JSON . stringify ( batchCreate . body ) ) . toBe ( true ) ;
274+ return ;
275+ }
276+ // Without the create grant: the create's refusal, nothing stored, and
277+ // the same answer an upsert naming a held id gets.
278+ expect ( comparable ( ups ) , JSON . stringify ( ups . body ) ) . toEqual ( comparable ( batchCreate ) ) ;
279+ expect ( ups . body ?. results ?. [ 0 ] ?. errors ?. [ 0 ] , JSON . stringify ( ups . body ) ) . toMatchObject ( { code : answer . code , httpStatus : answer . status } ) ;
280+ expect ( await ql . findOne ( object , { where : { id } , context : SYS } ) ) . toBeFalsy ( ) ;
281+ const held = await d . upsert ( tok ( ) , object , rows [ `hidden:${ object } ` ] , PAYLOAD [ object ] ( ) , atomic ) ;
282+ expect ( comparable ( held ) , JSON . stringify ( held . body ) ) . toEqual ( comparable ( ups ) ) ;
214283 } ) ;
215284 }
216285 }
0 commit comments