Skip to content

Commit

Permalink
FIX: start to introduce draft mode handling in the unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
lajosf committed Jan 30, 2025
1 parent 02ee908 commit a6df66c
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 32 deletions.
1 change: 0 additions & 1 deletion srv/spacefarer-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ class SpacefarerService extends cds.ApplicationService {
const app = cds.app;
setupSecurityMiddleware(app);

// Password hashing before CREATE
this.before('CREATE', 'GalacticSpacefarers', async (req) => {
if (req.data.password) {
req.data.password = passwordService.hashPassword(req.data.password);
Expand Down
6 changes: 4 additions & 2 deletions test/spacefarer-service-auth.http
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
# POSITIVE TEST CASES #
############################################################################

# Disable draft mode on GalacticSpacefarers before manual test

### ------------------------------------------------------------------------
# Admin retrieves all Spacefarers
GET {{baseUrl}}/odata/v4/spacefarer/GalacticSpacefarers
Expand All @@ -25,8 +27,8 @@ Content-Type: application/json
"name": "Test Spacefarer",
"email": "[email protected]",
"password": "password",
"stardustCollection": 5,
"wormholeNavigationSkill": 3,
"stardustCollection": 1,
"wormholeNavigationSkill": 1,
"originPlanet": "Earth",
"spacesuitColor": "Red",
"department_ID": "7dacdb28-0601-40f0-8c64-cd86732db1f0",
Expand Down
2 changes: 2 additions & 0 deletions test/spacefarer-service.http
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# Disable draft mode on GalacticSpacefarers before manual test

# Base URL setting
@baseUrl = http://localhost:4004

Expand Down
68 changes: 39 additions & 29 deletions test/spacefarer-service.test.js
Original file line number Diff line number Diff line change
@@ -1,32 +1,50 @@
const cds = require('@sap/cds');
const { POST, expect } = cds.test('.').in(__dirname, '..');
const cds = require('@sap/cds/lib');
const { default: axios } = require('axios');
const { POST, PATCH, GET, expect } = cds.test(__dirname + '../../');

describe('SpacefarerService', () => {
const SERVICE_PATH = '/odata/v4/spacefarer';

const createSpacefarerData = () => ({
name: 'Friedrich Schiller',
email: '[email protected]',
password: 'password',
stardustCollection: 1,
wormholeNavigationSkill: 1,
originPlanet: 'Earth',
spacesuitColor: 'White',
department_ID: '7dacdb28-0601-40f0-8c64-cd86732db1f0',
position_ID: '40493cf4-e1af-4ca7-bbc1-941bd80bd97e'
});

beforeAll(async () => {
await cds.deploy(__dirname + '/../db/schema.cds').to('sqlite::memory:');
axios.defaults.auth = { username: '[email protected]', password: 'password' };
});

test('should allow admin to create spacefarer', async () => {
const data = createSpacefarerData();
const response = await POST(`${SERVICE_PATH}/GalacticSpacefarers`, data, {
auth: { username: '[email protected]', password: 'password' }
describe('SpaceFarer Draft Choreography on updating his/her data', () => {
const spacefarerId = '70416e33-224b-4970-a624-168662918af5'; // Junior Spacefarer ID

test('should create a draft for modification', async () => {
axios.defaults.auth = { username: '[email protected]', password: 'password' };
const response = await POST(
`${SERVICE_PATH}/GalacticSpacefarers(ID='${spacefarerId}',IsActiveEntity=true)/SpacefarerService.draftEdit`
);
expect(response.status).to.equal(201);
});

test('should modify the draft', async () => {
const response = await PATCH(
`${SERVICE_PATH}/GalacticSpacefarers(ID='${spacefarerId}',IsActiveEntity=false)`,
{ stardustCollection: 20 }
);
expect(response.status).to.equal(200);
});
expect(response.status).to.equal(201);
expect(response.data).to.have.property('ID');

it('should activate the modified draft', async () => {
const response = await POST(
`${SERVICE_PATH}/GalacticSpacefarers(ID='${spacefarerId}',IsActiveEntity=false)/SpacefarerService.draftActivate`
);
expect(response.status).to.equal(200);
expect(response.data.stardustCollection).to.equal(20);
});

test('should get modified spacefarer', async () => {
const response = await GET(
`${SERVICE_PATH}/GalacticSpacefarers(ID='${spacefarerId}',IsActiveEntity=true)`
);
expect(response.status).to.equal(200);
expect(response.data.stardustCollection).to.equal(20);
});

});

/* TODO: introduced dradt mode make fail the below tests. We should handle draft mode in tests!
Expand Down Expand Up @@ -75,14 +93,6 @@ describe('SpacefarerService', () => {
}
});
test('should enhance wormhole navigation and stardust collection skills', async () => {
const data = createSpacefarerData();
const response = await POST(`${SERVICE_PATH}/GalacticSpacefarers`, data, {
auth: { username: 'admin@space.com', password: 'password' }
});
expect(response.status).to.equal(201);
expect(response.data.wormholeNavigationSkill).to.equal(3);
expect(response.data.stardustCollection).to.equal(10);
}); */
*/

});

0 comments on commit a6df66c

Please sign in to comment.