Skip to content
This repository has been archived by the owner on Jul 14, 2023. It is now read-only.

Commit

Permalink
fix: delete with grant (#334)
Browse files Browse the repository at this point in the history
closes #262 
* Fix Update/Delete issue when using grant
* chore: align formatting
* test: add authenticated deletion
* test: restrict deletion to createdBy = $user

Co-authored-by: Vasil Shmidt <[email protected]>
  • Loading branch information
gregorwolf and vshmidty committed Aug 3, 2022
1 parent 83bc2db commit d72ec82
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 6 deletions.
10 changes: 9 additions & 1 deletion __tests__/__assets__/cap-proj/srv/beershop-admin-service.cds
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,16 @@ using {csw} from '../db/schema';

@(requires : 'authenticated-user')
service BeershopAdminService {

@restrict : [{
grant : [
'READ',
'WRITE',
'DELETE'
],
where : 'createdBy = $user'
}]
entity Beers as projection on csw.Beers;

entity Breweries as projection on csw.Brewery;

@readonly
Expand Down
11 changes: 7 additions & 4 deletions __tests__/lib/pg/service-admin.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const deploy = require('@sap/cds/lib/deploy')
cds.env.requires.db = { kind: 'postgres' }
cds.env.requires.postgres = {
dialect: 'plain',
impl: './cds-pg', // hint: not really sure as to why this is, but...
impl: './cds-pg' // hint: not really sure as to why this is, but...
}

// default (single) test environment is local,
Expand All @@ -25,13 +25,13 @@ describe.each(suiteEnvironments)(
info: jest.fn(),
debug: jest.fn(),
warn: jest.fn(),
error: jest.fn(),
error: jest.fn()
}
this._model = model
this._dbProperties = {
kind: 'postgres',
model: this._model,
credentials: credentials,
credentials: credentials
}

// only bootstrap in local mode as scp app is deployed and running
Expand Down Expand Up @@ -74,7 +74,7 @@ describe.each(suiteEnvironments)(
.send({
name: 'Schlappe Seppel',
ibu: 10,
abv: '16.2',
abv: '16.2'
})
.set('content-type', 'application/json;charset=UTF-8;IEEE754Compatible=true')
.auth('bob', '')
Expand All @@ -90,6 +90,9 @@ describe.each(suiteEnvironments)(
expect(responseGet.status).toStrictEqual(200)
expect(responseGet.body.createdBy).toStrictEqual('bob')
expect(responseGet.body.modifiedBy).toStrictEqual('bob')

const responseDelete = await request.delete(`/beershop-admin/Beers(${response.body.ID})`).auth('bob', '')
expect(responseDelete.status).toStrictEqual(204)
})
})
}
Expand Down
2 changes: 1 addition & 1 deletion lib/pg/sql-builder/SelectBuilder.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ class PGSelectBuilder extends SelectBuilder {
// could not determine data type of parameter $1
// described in issue #223
if (this._obj.SELECT.columns.length === 1 && this._obj.SELECT.columns[0].func === 'count') {
this._obj.SELECT.columns[0].args[0].val = 1
this._obj.SELECT.columns[0].args[0] = '1'
} else {
for (let index = 0; index < this._obj.SELECT.columns.length; index++) {
const element = this._obj.SELECT.columns[index]
Expand Down

0 comments on commit d72ec82

Please sign in to comment.