This repository has been archived by the owner on Jul 14, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: fill managed fields with username (#154)
* test: add authenticated endpoint for managed * test: add skip * feat: fill managed fields with username fix #153 * chore: remove unused @sap/cds * test: improve authorization, check created entity * chore: remove @sap/cds / add missing @sap/cds * chore: adjust descriptions
- Loading branch information
1 parent
5f972aa
commit 0640e13
Showing
12 changed files
with
435 additions
and
124 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
CREATE TABLE BeershopAdminService_UserScopes ( | ||
username VARCHAR(5000) NOT NULL, | ||
is_admin BOOLEAN, | ||
PRIMARY KEY(username) | ||
); | ||
|
||
CREATE TABLE csw_Beers ( | ||
ID VARCHAR(36) NOT NULL, | ||
createdAt TIMESTAMP, | ||
createdBy VARCHAR(255), | ||
modifiedAt TIMESTAMP, | ||
modifiedBy VARCHAR(255), | ||
name VARCHAR(100), | ||
abv DECIMAL(3, 1), | ||
ibu INTEGER, | ||
brewery_ID VARCHAR(36), | ||
PRIMARY KEY(ID) | ||
); | ||
|
||
CREATE TABLE csw_Brewery ( | ||
ID VARCHAR(36) NOT NULL, | ||
createdAt TIMESTAMP, | ||
createdBy VARCHAR(255), | ||
modifiedAt TIMESTAMP, | ||
modifiedBy VARCHAR(255), | ||
name VARCHAR(150), | ||
PRIMARY KEY(ID) | ||
); | ||
|
||
CREATE TABLE csw_TypeChecks ( | ||
ID VARCHAR(36) NOT NULL, | ||
type_Boolean BOOLEAN, | ||
type_Int32 INTEGER, | ||
type_Int64 BIGINT, | ||
type_Decimal DECIMAL(2, 1), | ||
type_Double NUMERIC(30, 15), | ||
type_Date DATE, | ||
type_Time TIME, | ||
type_DateTime TIMESTAMP, | ||
type_Timestamp TIMESTAMP, | ||
type_String VARCHAR(5000), | ||
type_Binary CHAR(100), | ||
type_LargeBinary BYTEA, | ||
type_LargeString TEXT, | ||
PRIMARY KEY(ID) | ||
); | ||
|
||
CREATE VIEW BeershopAdminService_Beers AS SELECT | ||
Beers_0.ID, | ||
Beers_0.createdAt, | ||
Beers_0.createdBy, | ||
Beers_0.modifiedAt, | ||
Beers_0.modifiedBy, | ||
Beers_0.name, | ||
Beers_0.abv, | ||
Beers_0.ibu, | ||
Beers_0.brewery_ID | ||
FROM csw_Beers AS Beers_0; | ||
|
||
CREATE VIEW BeershopAdminService_Breweries AS SELECT | ||
Brewery_0.ID, | ||
Brewery_0.createdAt, | ||
Brewery_0.createdBy, | ||
Brewery_0.modifiedAt, | ||
Brewery_0.modifiedBy, | ||
Brewery_0.name | ||
FROM csw_Brewery AS Brewery_0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
__tests__/__assets__/cap-proj/rest-client-test/beers-admin.http
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
### Read Entities | ||
GET https://localhost:4004/beershop-admin/ | ||
Authorization: Basic bob: | ||
### Read UserScopes | ||
GET https://localhost:4004/beershop-admin/UserScopes | ||
Authorization: Basic bob: | ||
### Read Beers | ||
GET https://localhost:4004/beershop-admin/Beers | ||
Authorization: Basic bob: | ||
### Create Beer with POST | ||
# @name create | ||
POST https://localhost:4004/beershop-admin/Beers | ||
Content-Type: application/json | ||
Authorization: Basic bob: | ||
|
||
{ | ||
"ID": "{{$guid}}", | ||
"name": "Testbier with POST" | ||
} | ||
### | ||
@id = {{create.response.body.ID}} | ||
### Read Beer | ||
GET https://localhost:4004/beershop/Beers({{id}}) | ||
### Delete Beer | ||
DELETE https://localhost:4004/beershop/Beers({{id}}) | ||
|
||
### Create Beer with PUT | ||
PUT https://localhost:4004/beershop/Beers({{id}}) | ||
Content-Type: application/json | ||
|
||
{ | ||
"name": "Testbier with PUT" | ||
} |
14 changes: 14 additions & 0 deletions
14
__tests__/__assets__/cap-proj/srv/beershop-admin-service.cds
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
using {csw} from '../db/schema'; | ||
|
||
@(requires : 'authenticated-user') | ||
service BeershopAdminService { | ||
|
||
entity Beers as projection on csw.Beers; | ||
entity Breweries as projection on csw.Brewery; | ||
|
||
@readonly | ||
entity UserScopes { | ||
key username : String; | ||
is_admin : Boolean; | ||
}; | ||
} |
11 changes: 11 additions & 0 deletions
11
__tests__/__assets__/cap-proj/srv/beershop-admin-service.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
module.exports = async function (srv) { | ||
srv.on('READ', 'UserScopes', async (req) => { | ||
const users = [ | ||
{ | ||
username: req.user.id, | ||
is_admin: req.user.is('admin'), | ||
}, | ||
] | ||
return users | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
const cds = require('@sap/cds') | ||
const deploy = require('@sap/cds/lib/deploy') | ||
|
||
cds.env.requires.db = { kind: 'postgres' } | ||
cds.env.requires.postgres = { | ||
impl: './cds-pg', // hint: not really sure as to why this is, but... | ||
} | ||
|
||
// default (single) test environment is local, | ||
// so running against a dockerized postgres with a local cap bootstrap service. | ||
// when there's a .env in /__tests__/__assets__/cap-proj/ | ||
// with a scpServiceURL (see .env.example in that dir) | ||
// tests are also run against a deployed service url (cf hyperscaler postgres) | ||
const { suiteEnvironments, app } = require('./_buildSuiteEnvironments') | ||
|
||
describe.each(suiteEnvironments)( | ||
'[%s] OData to Postgres dialect', | ||
(_suitename /* translates to %s via printf */, credentials, model, request) => { | ||
beforeAll(async () => { | ||
// mock console.* | ||
// in order not to pollute test logs | ||
global.console = { | ||
log: jest.fn(), | ||
info: jest.fn(), | ||
debug: jest.fn(), | ||
warn: jest.fn(), | ||
error: jest.fn(), | ||
} | ||
this._model = model | ||
this._dbProperties = { | ||
kind: 'postgres', | ||
model: this._model, | ||
credentials: credentials, | ||
} | ||
|
||
// only bootstrap in local mode as scp app is deployed and running | ||
if (_suitename.startsWith('local')) { | ||
await require('./_runLocal')(model, credentials, app, false) // don't deploy content initially | ||
} | ||
}) | ||
|
||
beforeEach(async () => { | ||
// "reset" aka re-deploy static content | ||
if (_suitename.startsWith('local')) { | ||
await deploy(this._model, {}).to(this._dbProperties) | ||
} else if (_suitename === 'scp') { | ||
await request.post(`/beershop/reset`).send({}).set('content-type', 'application/json') | ||
} | ||
}) | ||
|
||
afterAll(() => { | ||
delete global.console // avoid side effect | ||
}) | ||
|
||
test('OData: List of entities exposed by the admin service', async () => { | ||
const response = await request.get('/beershop-admin/').auth('bob', '') | ||
|
||
expect(response.status).toStrictEqual(200) | ||
expect(response.body.value.length).toStrictEqual(3) | ||
}) | ||
|
||
test('OData: List of entities exposed by the service', async () => { | ||
const response = await request.get('/beershop/') | ||
|
||
expect(response.status).toStrictEqual(200) | ||
expect(response.body.value.length).toStrictEqual(4) | ||
}) | ||
|
||
describe('OData admin: CREATE', () => { | ||
test('odata: entityset Beers -> sql: insert into beers', async () => { | ||
const response = await request | ||
.post('/beershop-admin/Beers') | ||
.send({ | ||
name: 'Schlappe Seppel', | ||
ibu: 10, | ||
abv: '16.2', | ||
}) | ||
.set('content-type', 'application/json;charset=UTF-8;IEEE754Compatible=true') | ||
.auth('bob', '') | ||
|
||
expect(response.body.createdAt).toBeTruthy() | ||
expect(response.body.modifiedAt).toBeTruthy() | ||
expect(response.body.createdBy).toStrictEqual('bob') | ||
expect(response.body.modifiedBy).toStrictEqual('bob') | ||
expect(response.status).toStrictEqual(201) | ||
|
||
const responseGet = await request.get(`/beershop-admin/Beers(${response.body.ID})`).auth('bob', '') | ||
|
||
expect(responseGet.status).toStrictEqual(200) | ||
expect(responseGet.body.createdBy).toStrictEqual('bob') | ||
expect(responseGet.body.modifiedBy).toStrictEqual('bob') | ||
}) | ||
}) | ||
} | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.