From 96160907942b5183a94915fafcff648abbda4ca8 Mon Sep 17 00:00:00 2001 From: Albin Ramovic Date: Tue, 12 Nov 2024 15:32:29 +0100 Subject: [PATCH] add tests for protected resources --- .../internalResourcesCsn.json} | 4 +- .../privateResourcesCsn.json} | 2 +- __tests__/protectedResources.test.js | 46 +++++++++++++++++++ lib/ord.js | 7 +-- 4 files changed, 50 insertions(+), 9 deletions(-) rename __tests__/{mockups/noPublicResourcesCsn.json => __mocks__/internalResourcesCsn.json} (99%) rename __tests__/{mockups/emptyCdsrcCsn.json => __mocks__/privateResourcesCsn.json} (99%) create mode 100644 __tests__/protectedResources.test.js diff --git a/__tests__/mockups/noPublicResourcesCsn.json b/__tests__/__mocks__/internalResourcesCsn.json similarity index 99% rename from __tests__/mockups/noPublicResourcesCsn.json rename to __tests__/__mocks__/internalResourcesCsn.json index df14f41..57df60a 100644 --- a/__tests__/mockups/noPublicResourcesCsn.json +++ b/__tests__/__mocks__/internalResourcesCsn.json @@ -5,7 +5,7 @@ "@requires": "authenticated-user", "@ORD.Extensions.title": "This is test AdminService title", "@ORD.Extensions.shortDescription": "short description for test AdminService", - "@ORD.Extensions.visibility": "private", + "@ORD.Extensions.visibility": "internal", "@ORD.Extensions.version": "2.0.0", "@ORD.Extensions.extensible.supported": "yes", "@ORD.Extensions.entityTypeMappings.entityTypeTargets": [ @@ -316,7 +316,7 @@ "@path": "/browse", "@ORD.Extensions.title": "This is test Catalog Service title", "@ORD.Extensions.shortDescription": "short description for test CatalogService", - "@ORD.Extensions.visibility": "private", + "@ORD.Extensions.visibility": "internal", "@ORD.Extensions.version": "2.0.0", "@ORD.Extensions.extensible.supported": "yes" }, diff --git a/__tests__/mockups/emptyCdsrcCsn.json b/__tests__/__mocks__/privateResourcesCsn.json similarity index 99% rename from __tests__/mockups/emptyCdsrcCsn.json rename to __tests__/__mocks__/privateResourcesCsn.json index df14f41..bee362e 100644 --- a/__tests__/mockups/emptyCdsrcCsn.json +++ b/__tests__/__mocks__/privateResourcesCsn.json @@ -499,7 +499,7 @@ "@path": "/browse", "@ORD.Extensions.title": "This is test Cinema Service title", "@ORD.Extensions.shortDescription": "short description for test CinemaService", - "@ORD.Extensions.visibility": "internal", + "@ORD.Extensions.visibility": "private", "@ORD.Extensions.version": "1.0.0", "@ORD.Extensions.extensible.supported": "yes" }, diff --git a/__tests__/protectedResources.test.js b/__tests__/protectedResources.test.js new file mode 100644 index 0000000..686c1f7 --- /dev/null +++ b/__tests__/protectedResources.test.js @@ -0,0 +1,46 @@ +const ord = require("../lib/ord"); +const private_csn = require("./__mocks__/privateResourcesCsn.json"); +const internal_csn = require("./__mocks__/internalResourcesCsn.json"); + + +// Mock the @sap/cds module +jest.mock("@sap/cds", () => { + const path = require("path"); + let cds = jest.requireActual("@sap/cds"); + cds.root = path.join(__dirname, "bookshop"); + + return cds; +}); + +jest.mock("../lib/date", () => ({ + getRFC3339Date: jest.fn(() => "2024-11-04T14:33:25+01:00"), +})); + +jest.mock("../lib/extendOrdWithCustom", () => ({ + extendCustomORDContentIfExists: jest.fn( + (_appConfig, ordContent, _lazyLogger) => { + console.log( + "The custom.ord.json file is not considered for the purpose of this test." + ); + return ordContent; + } + ), +})); + +describe("Tests for ORD document when there is no public service", () => { + test("Private services: Successfully create ORD Documents without packages, empty apiResources and eventResources lists", () => { + const document = ord(private_csn); + + expect(document.packages).not.toBeDefined(); + expect(document.apiResources).toHaveLength(0); + expect(document.eventResources).toHaveLength(0); + }); + + test("Internal services: Successfully create ORD Documents without packages, empty apiResources and eventResources lists", () => { + const document = ord(internal_csn); + + expect(document.packages).not.toBeDefined(); + expect(document.apiResources).toHaveLength(0); + expect(document.eventResources).toHaveLength(0); + }); +}); diff --git a/lib/ord.js b/lib/ord.js index 9fe8f0d..760c63b 100644 --- a/lib/ord.js +++ b/lib/ord.js @@ -160,13 +160,8 @@ module.exports = (csn) => { let ordDocument = createDefaultORDDocument(linkedCsn, appConfig); const packageIds = extractPackageIds(ordDocument); - // TODO: add testcase without apiResources or event, no empty package ordDocument.apiResources = _getAPIResources(linkedCsn, appConfig, packageIds); - const eventResources = _getEventResources(linkedCsn, appConfig, packageIds); - if (eventResources.length) { - ordDocument.eventResources = eventResources; - } - + ordDocument.eventResources = _getEventResources(linkedCsn, appConfig, packageIds); ordDocument = extendCustomORDContentIfExists(appConfig, ordDocument, logger); return ordDocument;