diff --git a/src/controllers/private/v1/ecosystems.private.controller.ts b/src/controllers/private/v1/ecosystems.private.controller.ts index bcd9243..db1b525 100644 --- a/src/controllers/private/v1/ecosystems.private.controller.ts +++ b/src/controllers/private/v1/ecosystems.private.controller.ts @@ -647,14 +647,12 @@ export const getEcosystemContract = async ( try { const { id } = req.params; const ecosystem = await Ecosystem.findById(id); - if (!ecosystem) return res.status(404).json({ code: 404, errorMsg: "Not found", message: "Ecosystem not found", }); - try { const contract = await getContractById(ecosystem.contract, "ecosystem"); return res.json(contract); diff --git a/src/controllers/private/v1/negotiation.private.controller.ts b/src/controllers/private/v1/negotiation.private.controller.ts index 6c3805d..dfd0502 100644 --- a/src/controllers/private/v1/negotiation.private.controller.ts +++ b/src/controllers/private/v1/negotiation.private.controller.ts @@ -118,9 +118,7 @@ export const authorizeExchangeConfiguration = async ( try { const { id } = req.params; const { policy } = req.body; - const exchangeConf = await ExchangeConfiguration.findById(id); - if (!exchangeConf) { return res.status(404).json({ code: 404, @@ -128,7 +126,6 @@ export const authorizeExchangeConfiguration = async ( message: "Exchange Configuration could not be found", }); } - if (exchangeConf.provider.toString() !== req.user.id.toString()) { return res.status(400).json({ code: 400, @@ -136,7 +133,6 @@ export const authorizeExchangeConfiguration = async ( message: "Exchange Configuration could not be authorized", }); } - if (exchangeConf.negotiationStatus === "Authorized") { return res.status(400).json({ code: 400, @@ -144,11 +140,9 @@ export const authorizeExchangeConfiguration = async ( message: "Exchange configuration has already been authorized", }); } - if (getDocumentId(exchangeConf.provider) !== req.user.id) { return res.status(401).json({ error: "Unauthorized operation" }); } - try { const contract = await generateBilateralContract({ dataConsumer: exchangeConf.consumer, @@ -163,13 +157,11 @@ export const authorizeExchangeConfiguration = async ( } catch (err) { return res .status(409) - .json({ error: "Failed to generate contract: " + err.message }); + .json({ errorMsg: "Failed to generate contract.", error: err.message }); } - exchangeConf.providerPolicies = policy; exchangeConf.negotiationStatus = "Authorized"; exchangeConf.latestNegotiator = req.user.id; - await exchangeConf.save(); return res.status(200).json(exchangeConf); } catch (err) { @@ -222,9 +214,7 @@ export const acceptNegotiation = async ( ) => { try { const { id } = req.params; - const exchangeConf = await ExchangeConfiguration.findById(id); - if (!exchangeConf) { return res.status(404).json({ code: 404, @@ -232,7 +222,6 @@ export const acceptNegotiation = async ( message: "Exchange Configuration could not be found", }); } - if (exchangeConf.negotiationStatus === "SignatureReady") { return res.status(400).json({ code: 400, @@ -243,9 +232,7 @@ export const acceptNegotiation = async ( } exchangeConf.negotiationStatus = "SignatureReady"; - await exchangeConf.save(); - return res.json(exchangeConf); } catch (err) { next(err); @@ -264,7 +251,6 @@ export const signExchangeConfiguration = async ( try { const { id } = req.params; const { signature } = req.body; - const exchangeConf = await ExchangeConfiguration.findById(id).populate<{ provider: IParticipant; consumer: IParticipant; @@ -290,7 +276,6 @@ export const signExchangeConfiguration = async ( populate: serviceOfferingPopulation, }, ]); - if (!exchangeConf) { return res.status(404).json({ code: 404, @@ -298,7 +283,6 @@ export const signExchangeConfiguration = async ( message: "Exchange Configuration could not be found", }); } - if (exchangeConf.negotiationStatus !== "SignatureReady") { return res.status(400).json({ code: 400, @@ -306,14 +290,12 @@ export const signExchangeConfiguration = async ( message: "Exchange configuration is not ready for signature", }); } - const signingParty = req.user.id === getDocumentId(exchangeConf.provider) ? "provider" : "consumer"; exchangeConf.signatures[signingParty] = signature; - try { // If both have applied signatures, we can inject the policies // this avoid injecting the same policies multiple times @@ -351,7 +333,6 @@ export const signExchangeConfiguration = async ( } await exchangeConf.save(); - // The client can handle UI to show depending on the contract status return res.json({ code: 200, diff --git a/src/controllers/private/v1/serviceOfferings.private.controller.ts b/src/controllers/private/v1/serviceOfferings.private.controller.ts index 1a4b7e8..f6460a6 100644 --- a/src/controllers/private/v1/serviceOfferings.private.controller.ts +++ b/src/controllers/private/v1/serviceOfferings.private.controller.ts @@ -80,8 +80,6 @@ export const updateServiceOffering = async ( if (!updatedServiceOffering) { return res.status(404).json({ - req, - res, code: 404, errorMsg: "Resource not found", message: "The service offering could not be found", @@ -105,8 +103,6 @@ export const deleteServiceOffering = async ( ); if (!serviceOffering) { return res.status(404).json({ - req, - res, code: 404, errorMsg: "Resource not found", message: "The service offering could not be found", diff --git a/src/controllers/public/v1/dataResources.public.controller.ts b/src/controllers/public/v1/dataResources.public.controller.ts index c70eacf..5d2b54d 100644 --- a/src/controllers/public/v1/dataResources.public.controller.ts +++ b/src/controllers/public/v1/dataResources.public.controller.ts @@ -81,9 +81,7 @@ export const getDataResourceById = async ( try { const dataResource = await DataResource.findById(req.params.id).populate([{ path: "representation", model: Representation }]).lean(); if (!dataResource) { - return res.json({ - req, - res, + return res.status(404).json({ code: 404, errorMsg: "Resource not found", message: "The data resource could not be found", @@ -110,9 +108,7 @@ export const getDCATDataResourceById = async ( try { const dataResource = await DataResource.findById(req.params.id).lean(); if (!dataResource) { - return res.json({ - req, - res, + return res.status(404).json({ code: 404, errorMsg: "Resource not found", message: "The data resource could not be found", diff --git a/tests/dataResources.spec.ts b/tests/dataResources.spec.ts index 412d22f..4376a62 100644 --- a/tests/dataResources.spec.ts +++ b/tests/dataResources.spec.ts @@ -20,7 +20,7 @@ let providerId = ""; let dataResourcesId: ""; let jwt = ""; -describe("Data Resources Routes Tests", () => { +describe("Data Resources Routes Tests", function () { let loadMongooseStub; before(async () => { loadMongooseStub = stub(loadMongoose, "loadMongoose").callsFake( @@ -69,7 +69,6 @@ describe("Data Resources Routes Tests", () => { .send(dataResourceData) .expect(201); dataResourcesId = response.body._id; - expect(response.body).to.be.an("object"); //assertions }); @@ -89,7 +88,6 @@ describe("Data Resources Routes Tests", () => { const response = await request(app) .get(`/v1/dataResources/${dataResourcesId}`) .expect(200); - //assertions //expect response id = dataresourceid }); @@ -98,7 +96,6 @@ describe("Data Resources Routes Tests", () => { .get(`/v1/dataResources/${dataResourcesId}`) .set("Authorization", `Bearer ${jwt}`) .expect(200); - //assertions //expect response id = dataresourceid }); @@ -107,8 +104,8 @@ describe("Data Resources Routes Tests", () => { .get("/v1/dataResources/me") .set("Authorization", `Bearer ${jwt}`) .expect(200); + expect(response.body).to.not.be.empty; //assertions - //expect response not empty }); it("Should get DCAT Data Resources", async () => { @@ -123,8 +120,7 @@ describe("Data Resources Routes Tests", () => { it("should get all dataResources", async () => { const response = await request(app).get("/v1/dataResources").expect(200); - //assertions - //expect response not empty + expect(response.body).to.not.be.empty; }); it("should delete DataResource by id", async () => { @@ -132,8 +128,5 @@ describe("Data Resources Routes Tests", () => { .delete(`/v1/dataResources/${dataResourcesId}`) .set("Authorization", `Bearer ${jwt}`) .expect(204); - //assertions - //expect - //error test get data resources deleted }); }); diff --git a/tests/ecosystemNegotiation.spec.ts b/tests/ecosystemNegotiation.spec.ts index 2a05e47..2b67d45 100644 --- a/tests/ecosystemNegotiation.spec.ts +++ b/tests/ecosystemNegotiation.spec.ts @@ -4,7 +4,7 @@ import { config } from "dotenv"; import { startServer } from "../src/server"; import { IncomingMessage, Server, ServerResponse } from "http"; import { Application } from "express"; -import { mockContract } from "./fixtures/fixture.contract"; +import { setupMocks } from "./fixtures/fixture.contract"; import { testProvider2, @@ -23,6 +23,7 @@ import { sampleUpdatedEcosystem, sampleOfferings, sampleJoinRequest, + sampleSignEcosystem, } from "./fixtures/sampleData"; import { stub } from "sinon"; import * as loadMongoose from "../src/config/database"; @@ -38,6 +39,7 @@ let provider2Id = ""; let orchestId = ""; let consumer1Id = ""; let consumer2Id = ""; +let organization1Id = ""; let dataResource1Id = ""; let dataResource2Id = ""; let softwareResource1Id: ""; @@ -55,7 +57,8 @@ let requestId1: ""; let requestId2: ""; let ecosystemId = ""; -describe("Ecosystem routes tests", () => { +describe("Ecosystem routes tests", function () { + this.timeout(30000) let loadMongooseStub; before(async () => { loadMongooseStub = stub(loadMongoose, "loadMongoose").callsFake( @@ -68,7 +71,7 @@ describe("Ecosystem routes tests", () => { await serverInstance.promise; app = serverInstance.app; server = serverInstance.server; - mockContract(); + setupMocks(); // Create provider1 const provider1Data = testProvider2; @@ -76,6 +79,8 @@ describe("Ecosystem routes tests", () => { .post("/v1/auth/signup") .send(provider1Data); provider1Id = provider1Response.body.participant._id; + organization1Id = provider1Response.body.admin.organization; + // Create provider2 const provider2Data = testProvider3; @@ -207,16 +212,23 @@ describe("Ecosystem routes tests", () => { closeMongoMemory(); server.close(); }); - it("should create a new ecosystem", async () => { const response = await request(app) .post("/v1/ecosystems") .set("Authorization", `Bearer ${orchestJwt}`) .send(sampleEcosystem) .expect(201); + expect(response.body.orchestrator).to.equal(orchestId); ecosystemId = response.body._id; }); + it("should get Ecosystem by ID", async () => { + const response = await request(app).get(`/v1/ecosystems/${ecosystemId}`); + expect(response.status).to.equal(200); + expect(response.body).to.not.be.empty; + expect(response.body._id).to.equal(ecosystemId); + }); + it("should getMyEcosystems", async () => { const response = await request(app) .get("/v1/ecosystems/me") @@ -233,13 +245,6 @@ describe("Ecosystem routes tests", () => { .expect(200); }); - it("should getMyEcosystems", async () => { - const response = await request(app) - .get("/v1/ecosystems/me") - .set("Authorization", `Bearer ${orchestJwt}`); - expect(response.status).to.equal(200); - expect(response.body).to.be.an("array").and.to.not.be.empty; - }); it("should create ecosystem contract", async () => { const response = await request(app) .post(`/v1/ecosystems/${ecosystemId}/contract`) @@ -251,16 +256,16 @@ describe("Ecosystem routes tests", () => { .get(`/v1/ecosystems/${ecosystemId}/contract`) .set("Authorization", `Bearer ${orchestJwt}`); expect(response.status).to.equal(200); + expect(response.body).to.not.be.empty; }); it("should apply Orchestrator Signature", async () => { const response = await request(app) .post(`/v1/ecosystems/${ecosystemId}/signature/orchestrator`) .set("Authorization", `Bearer ${orchestJwt}`) - .send({ - signature: "hasSigned", - }) + .send(sampleSignEcosystem) .expect(200); + expect(response.body.message).to.equal("successfully signed contract"); }); it("should create invitation to join ecosystem for a data provider", async () => { @@ -270,10 +275,11 @@ describe("Ecosystem routes tests", () => { .send({ ...sampleInvitation, participantId: provider1Id }) .expect(200); - // expect(response.body.participant).to.equal(provider1Id); - // expect(response.body.status).to.equal("Requested"); - // expect(response.body.latestNegotiator).to.equal(orchestId); + expect(response.body.invitations[0].participant).to.equal(provider1Id); + expect(response.body.invitations[0].status).to.equal("Pending"); + expect(response.body.participants[0].participant).to.equal(orchestId); }); + it("should create invitation to join ecosystem for a service provider", async () => { const response = await request(app) .post(`/v1/ecosystems/${ecosystemId}/invites`) @@ -281,9 +287,9 @@ describe("Ecosystem routes tests", () => { .send({ ...sampleInvitation, participantId: consumer1Id }) .expect(200); - // expect(response.body.participant).to.equal(consumer1Id); - // expect(response.body.status).to.equal("Requested"); - // expect(response.body.latestNegotiator).to.equal(orchestId); + expect(response.body.invitations[1].participant).to.equal(consumer1Id); + expect(response.body.invitations[1].status).to.equal("Pending"); + expect(response.body.participants[0].participant).to.equal(orchestId); }); it("should get All Invitations for a participant", async () => { const response = await request(app) @@ -305,12 +311,15 @@ describe("Ecosystem routes tests", () => { .post(`/v1/ecosystems/${ecosystemId}/invites/accept`) .set("Authorization", `Bearer ${provider1Jwt}`) .expect(200); + expect(response.body.invitations[0].status).to.equal("Authorized"); }); + it("should deny invitation to join ecosystem", async () => { const response = await request(app) .post(`/v1/ecosystems/${ecosystemId}/invites/deny`) .set("Authorization", `Bearer ${consumer1Jwt}`) .expect(200); + // expect(response.body.invitations[1].status).to.equal("Rejected"); }); it("should configure Participant Ecosystem Offerings", async () => { @@ -328,12 +337,31 @@ describe("Ecosystem routes tests", () => { const response = await request(app) .post(`/v1/ecosystems/${ecosystemId}/signature/participant`) .set("Authorization", `Bearer ${provider1Jwt}`) - .send({ - signature: "hasSigned", - }) + .send(sampleSignEcosystem) .expect(200); + expect(response.body.message).to.equal("successfully signed contract"); }); + //TO FIX : STATUS 500 + // it("should get Ecosystems for a single orchestrator", async () => { + // const response = await request(app).get( + // `/v1/ecosystems/participant/${orchestId}` + // ); + // expect(response.status).to.equal(200); + // expect(response.body).to.not.be.empty; + // expect(response.body.orchestrator).to.equal(orchestId); + // }); + + //TO FIX : STATUS 500 + // it("should get Ecosystems for a single participant", async () => { + // const response = await request(app).get( + // `/v1/ecosystems/participant/${organization1Id}` + // ); + // expect(response.status).to.equal(200); + // expect(response.body).to.not.be.empty; + // expect(response.body.orchestrator).to.equal(orchestId); + // }); + it("should create join request by a data provider", async () => { const modifiedSampleJoinRequest = { ...sampleJoinRequest }; modifiedSampleJoinRequest.offerings[0].serviceOffering = @@ -380,11 +408,25 @@ describe("Ecosystem routes tests", () => { }); it("should reject join request", async () => { const response = await request(app) - .put(`/v1/ecosystems/${ecosystemId}/requests/${requestId1}/reject`) + .put(`/v1/ecosystems/${ecosystemId}/requests/${requestId2}/reject`) .set("Authorization", `Bearer ${orchestJwt}`) .expect(200); }); + it("should get all Ecosystems", async () => { + const response = await request(app).get(`/v1/ecosystems`); + expect(response.status).to.equal(200); + expect(response.body).to.be.an("array").and.to.not.be.empty; + }); + + it("should get Circulating Resources In Ecosystem", async () => { + const response = await request(app).get( + `/v1/ecosystems/${ecosystemId}/circulating` + ); + expect(response.status).to.equal(200); + expect(response.body).to.not.be.empty; + }); + it("should delete the ecosystem by Id", async () => { const response = await request(app) .delete("/v1/ecosystems/" + ecosystemId) diff --git a/tests/errors.spec.ts b/tests/errors.spec.ts new file mode 100644 index 0000000..c0f6e6e --- /dev/null +++ b/tests/errors.spec.ts @@ -0,0 +1,1008 @@ +import { expect } from "chai"; +import request from "supertest"; +import { config } from "dotenv"; +import { startServer } from "../src/server"; +import { IncomingMessage, Server, ServerResponse } from "http"; +import { Application } from "express"; +import axios from "axios"; +import MockAdapter from "axios-mock-adapter"; +import { + setBilateralAvailability, + setContractAvailability, + setupMocks, +} from "./fixtures/fixture.contract"; + +import { + testErrorProvider1, + testErrorProvider2, + testErrorOrchest1, + testErrorOrchest2, + testErrorOrchest3, + testErrorConsumer, +} from "./fixtures/testAccount"; +import { + sampleDataResource, + sampleUpdatedDataResource, + sampleSoftwareResource, + sampleUpdatedSoftwareResource, + sampleProviderServiceOffering, + sampleConsumerServiceOffering, + sampleBilateralNegotiation, + sampleAuthorizeNegotiation, + sampleNegotiatePolicies, + sampleSignNegotiation, + sampleEcosystem1, + sampleInvitation, + sampleUpdatedEcosystem, + sampleOfferings, + sampleJoinRequest, + sampleSignEcosystem, +} from "./fixtures/sampleData"; +import { stub } from "sinon"; +import * as loadMongoose from "../src/config/database"; +import { closeMongoMemory, openMongoMemory } from "./utils.ts/mongoMemory"; + +config(); + +export let app: Application; +export let server: Server; + +let provider1Id = ""; +let provider2Id = ""; +let orchest1Id = ""; +let orchest2Id = ""; +let orchest3Id = ""; +let consumerId = ""; +let dataResource1Id = ""; +let dataResource2Id = ""; +let softwareResource1Id: ""; +let orchest1Jwt = ""; +let orchest2Jwt = ""; +let orchest3Jwt = ""; +let provider1Jwt = ""; +let provider2Jwt = ""; +let consumerJwt = ""; +let providerServiceOffering1Id = ""; +let providerServiceOffering2Id = ""; +let consumerServiceOffering1Id = ""; +let consumerServiceOffering2Id = ""; +let negotiation1Id = ""; +let negotiation2Id = ""; +let negotiation1bId = ""; +let requestId1 = ""; +let ecosystem1Id= ""; +let ecosystem2Id = ""; +let ecosystem3Id = ""; +const mock = new MockAdapter(axios); +const nonExistentDataResourcesId = "000000000000000000000000"; +const nonExistentSoftwareResourcesId = "000000000000000000000000"; +const nonExistentnegotiation1Id = "000000000000000000000000"; +const nonExistentServiceOfferingId = "000000000000000000000000"; +const nonExistentRequestId = "6633bf5cebbb53c52859179b"; +const nonExistentEcosystemId = "6633bf5cebbb53c52859179b"; + +describe("Error Management catalog_api Routes Tests", function () { + this.timeout(10000); + let loadMongooseStub; + before(async () => { + loadMongooseStub = stub(loadMongoose, "loadMongoose").callsFake( + async () => { + await openMongoMemory(); + } + ); + // Start the server and obtain the app and server instances + const serverInstance = await startServer(3001); + await serverInstance.promise; + app = serverInstance.app; + server = serverInstance.server; + setupMocks(); + + // Create provider1 + const provider1Data = testErrorProvider1; + const provider1Response = await request(app) + .post("/v1/auth/signup") + .send(provider1Data); + provider1Id = provider1Response.body.participant._id; + + // Create provider2 + const provider2Data = testErrorProvider2; + const provider2Response = await request(app) + .post("/v1/auth/signup") + .send(provider2Data); + provider2Id = provider2Response.body.participant._id; + + // Create consumer 1 + const consumer1Data = testErrorConsumer; + const consumer1Response = await request(app) + .post("/v1/auth/signup") + .send(consumer1Data); + consumerId = consumer1Response.body.participant._id; + + // Create orchestrator 1 + const orchest1Data = testErrorOrchest1; + const orchest1Response = await request(app) + .post("/v1/auth/signup") + .send(orchest1Data); + orchest1Id = orchest1Response.body.participant._id; + + // Create orchestrator 2 + const orchest2Data = testErrorOrchest2; + const orchest2Response = await request(app) + .post("/v1/auth/signup") + .send(orchest2Data); + orchest2Id = orchest2Response.body.participant._id; + + // Create orchestrator 3 + const orchest3Data = testErrorOrchest3; + const orchest3Response = await request(app) + .post("/v1/auth/signup") + .send(orchest3Data); + orchest3Id = orchest3Response.body.participant._id; + + // Login orchestrator 1 + const orchest1AuthResponse = await request(app).post("/v1/auth/login").send({ + email: testErrorOrchest1.email, + password: testErrorOrchest1.password, + }); + orchest1Jwt = orchest1AuthResponse.body.token; + + // Login orchestrator 2 + const orchest2AuthResponse = await request(app).post("/v1/auth/login").send({ + email: testErrorOrchest2.email, + password: testErrorOrchest2.password, + }); + orchest2Jwt = orchest2AuthResponse.body.token; + + // Login orchestrator 3 + const orchest3AuthResponse = await request(app).post("/v1/auth/login").send({ + email: testErrorOrchest3.email, + password: testErrorOrchest3.password, + }); + orchest3Jwt = orchest3AuthResponse.body.token; + + // Login consumer 1 + const consumer1AuthResponse = await request(app) + .post("/v1/auth/login") + .send({ + email: testErrorConsumer.email, + password: testErrorConsumer.password, + }); + consumerJwt = consumer1AuthResponse.body.token; + + // Login provider1 + const provider1AuthResponse = await request(app) + .post("/v1/auth/login") + .send({ + email: testErrorProvider1.email, + password: testErrorProvider1.password, + }); + provider1Jwt = provider1AuthResponse.body.token; + + // Login provider2 + const provider2AuthResponse = await request(app) + .post("/v1/auth/login") + .send({ + email: testErrorProvider2.email, + password: testErrorProvider2.password, + }); + provider2Jwt = provider2AuthResponse.body.token; + + // Create data resource 1 + const dataResource1Data = sampleDataResource; + const dataResponse1 = await request(app) + .post("/v1/dataResources") + .set("Authorization", `Bearer ${provider1Jwt}`) + .send(dataResource1Data); + dataResource1Id = dataResponse1.body._id; + + // Create service offerings 1 + const resProvider1 = await request(app) + .post("/v1/serviceofferings") + .set("Authorization", `Bearer ${provider1Jwt}`) + .send({ ...sampleProviderServiceOffering, providedBy: provider1Id }); + providerServiceOffering1Id = resProvider1.body._id; + + // Create data resource 2 + const dataResource2Data = sampleDataResource; + const dataResponse2 = await request(app) + .post("/v1/dataResources") + .set("Authorization", `Bearer ${provider2Jwt}`) + .send(dataResource2Data); + dataResource2Id = dataResponse2.body._id; + + // Create service offerings 2 + const resProvider2 = await request(app) + .post("/v1/serviceofferings") + .set("Authorization", `Bearer ${provider2Jwt}`) + .send({ ...sampleProviderServiceOffering, providedBy: provider2Id }); + providerServiceOffering2Id = resProvider2.body._id; + + //create software resource 1 + const softwareResource1Data = sampleSoftwareResource; + const softwareResponse1 = await request(app) + .post("/v1/softwareresources") + .set("Authorization", `Bearer ${consumerJwt}`) + .send(softwareResource1Data); + softwareResource1Id = softwareResponse1.body.id; + + // Create service offerings 1 + const resConsumer1 = await request(app) + .post("/v1/serviceofferings") + .set("Authorization", `Bearer ${consumerJwt}`) + .send({ ...sampleConsumerServiceOffering, providedBy: consumerId }); + consumerServiceOffering1Id = resConsumer1.body._id; + + // Create service offerings 1 + const resConsumer2 = await request(app) + .post("/v1/serviceofferings") + .set("Authorization", `Bearer ${consumerJwt}`) + .send({ ...sampleConsumerServiceOffering, providedBy: consumerId }); + consumerServiceOffering2Id = resConsumer2.body._id; + + //create bilateral negotiation 1 (authorized & accepted in tests below) + const negotiationData = sampleBilateralNegotiation; + const negotiationResponse = await request(app) + .post("/v1/negotiation") + .set("Authorization", `Bearer ${provider1Jwt}`) + .send({ + ...negotiationData, + provider: provider1Id, + consumer: consumerId, + providerServiceOffering: providerServiceOffering1Id, + consumerServiceOffering: consumerServiceOffering1Id, + }); + negotiation1Id = negotiationResponse.body._id; + + //create bilateral negotiation 1b + const negotiation1bData = sampleBilateralNegotiation; + const negotiation1bResponse = await request(app) + .post("/v1/negotiation") + .set("Authorization", `Bearer ${provider1Jwt}`) + .send({ + ...negotiation1bData, + provider: provider1Id, + consumer: consumerId, + providerServiceOffering: providerServiceOffering1Id, + consumerServiceOffering: consumerServiceOffering2Id, + }); + negotiation1bId = negotiation1bResponse.body._id; + //create bilateral negotiation 2 (authorized but not accepted in tests below) + const negotiation2Data = sampleBilateralNegotiation; + const negotiation2Response = await request(app) + .post("/v1/negotiation") + .set("Authorization", `Bearer ${provider2Jwt}`) + .send({ + ...negotiation2Data, + provider: provider2Id, + consumer: consumerId, + providerServiceOffering: providerServiceOffering2Id, + consumerServiceOffering: consumerServiceOffering1Id, + }); + negotiation2Id = negotiation2Response.body._id; + + //authorize negotiation 2 + await request(app) + .put(`/v1/negotiation/${negotiation2Id}`) + .set("Authorization", `Bearer ${provider2Jwt}`) + .send(sampleAuthorizeNegotiation); + + //create ecosystem 1 : no participants invited + const ecosystem1response = await request(app) + .post("/v1/ecosystems") + .set("Authorization", `Bearer ${orchest1Jwt}`) + .send(sampleEcosystem1); + ecosystem1Id = ecosystem1response.body._id; + + //create join request to ecosystem 1 + const modifiedSampleJoinRequest = { ...sampleJoinRequest }; + modifiedSampleJoinRequest.offerings[0].serviceOffering = + providerServiceOffering1Id; + await request(app) + .post(`/v1/ecosystems/${ecosystem1Id}/requests`) + .set("Authorization", `Bearer ${provider1Jwt}`) + .send(modifiedSampleJoinRequest) + // get join request + const joinRequestResponse = await request(app) + .get(`/v1/ecosystems/${ecosystem1Id}/requests`) + .set("Authorization", `Bearer ${orchest1Jwt}`) + const matchingResponse = joinRequestResponse.body.find( + (item) => item.participant === provider1Id + ); + if (matchingResponse) { + requestId1 = matchingResponse._id; + } + + //create ecosystem 2 : participant invited and contract signed + const ecosystem2response = await request(app) + .post("/v1/ecosystems") + .set("Authorization", `Bearer ${orchest2Jwt}`) + .send(sampleEcosystem1); + ecosystem2Id = ecosystem2response.body._id; + // orchest create ecosystem contract + await request(app) + .post(`/v1/ecosystems/${ecosystem2Id}/contract`) + .set("Authorization", `Bearer ${orchest2Jwt}`); + //orchest sign + await request(app) + .post(`/v1/ecosystems/${ecosystem2Id}/signature/orchestrator`) + .set("Authorization", `Bearer ${orchest2Jwt}`) + .send(sampleSignEcosystem); + //invite consumer to join ecosystem 2 + await request(app) + .post(`/v1/ecosystems/${ecosystem2Id}/invites`) + .set("Authorization", `Bearer ${orchest2Jwt}`) + .send({ ...sampleInvitation, participantId: consumerId }); + //consumer accept invitation + await request(app) + .post(`/v1/ecosystems/${ecosystem2Id}/invites/accept`) + .set("Authorization", `Bearer ${consumerJwt}`); + //consumer configure offerings + const modifiedSampleOfferings = { ...sampleOfferings }; + modifiedSampleOfferings.offerings[0].serviceOffering = + consumerServiceOffering1Id; + await request(app) + .put(`/v1/ecosystems/${ecosystem2Id}/offerings`) + .set("Authorization", `Bearer ${consumerJwt}`) + .send(modifiedSampleOfferings); + //consumer sign + await request(app) + .post(`/v1/ecosystems/${ecosystem2Id}/signature/participant`) + .set("Authorization", `Bearer ${consumerJwt}`) + .send(sampleSignEcosystem); + //create ecosystem 3 + const ecosystem3response = await request(app) + .post("/v1/ecosystems") + .set("Authorization", `Bearer ${orchest3Jwt}`) + .send(sampleEcosystem1); + ecosystem3Id = ecosystem3response.body._id; + //invite provider to join ecosystem 3 + await request(app) + .post(`/v1/ecosystems/${ecosystem3Id}/invites`) + .set("Authorization", `Bearer ${orchest3Jwt}`) + .send({ ...sampleInvitation, participantId: provider2Id }); + //orchest sign + await request(app) + .post(`/v1/ecosystems/${ecosystem3Id}/signature/orchestrator`) + .set("Authorization", `Bearer ${orchest3Jwt}`) + .send(sampleSignEcosystem); + //provider accept invitation + await request(app) + .post(`/v1/ecosystems/${ecosystem3Id}/invites/accept`) + .set("Authorization", `Bearer ${provider2Jwt}`); + //provider configure offerings + const modifiedSampleOfferings2 = { ...sampleOfferings }; + modifiedSampleOfferings2.offerings[0].serviceOffering = + providerServiceOffering2Id; + await request(app) + .put(`/v1/ecosystems/${ecosystem3Id}/offerings`) + .set("Authorization", `Bearer ${provider2Jwt}`) + .send(modifiedSampleOfferings2); + }); + + after(() => { + // Close the server after all tests are completed + loadMongooseStub.restore(); + closeMongoMemory(); + server.close(); + }); + + //Error Management for Data Resources Routes Tests + describe("Error Management for Data Resources Routes Tests", function () { + it("should not get a non-existent data resource", async () => { + const response = await request(app) + .get(`/v1/dataResources/${nonExistentDataResourcesId}`) + .set("Authorization", `Bearer ${provider1Jwt}`) + .expect(404); + expect(response.body.errorMsg).to.equal("Resource not found"); + expect(response.body.message).to.equal( + "The data resource could not be found" + ); + }); + + it("should not get DCAT for a non-existent data resource", async () => { + const response = await request(app) + .get(`/v1/dataResources/dcat/${nonExistentDataResourcesId}`) + .expect(404); + expect(response.body.errorMsg).to.equal("Resource not found"); + expect(response.body.message).to.equal( + "The data resource could not be found" + ); + }); + + it("should not update a non-existent data resource", async () => { + const updatedDataResourceData = sampleUpdatedDataResource; + const response = await request(app) + .put(`/v1/dataResources/${nonExistentDataResourcesId}`) + .set("Authorization", `Bearer ${provider1Jwt}`) + .send(updatedDataResourceData) + .expect(404); + expect(response.body.errorMsg).to.equal("Resource not found"); + expect(response.body.message).to.equal( + "The data resource could not be found" + ); + }); + + it("should not delete a non-existent DataResource", async () => { + const response = await request(app) + .delete(`/v1/dataResources/${nonExistentDataResourcesId}`) + .set("Authorization", `Bearer ${provider1Jwt}`) + .expect(404); + expect(response.body.errorMsg).to.equal("Resource not found"); + expect(response.body.message).to.equal( + "The data resource could not be found" + ); + }); + }); + + //Error Management for Software Resources Routes Tests + describe("Error Management for Software Resources Routes Tests", () => { + it("should not get a non-existent software resource", async () => { + const response = await request(app) + .get(`/v1/softwareresources/${nonExistentSoftwareResourcesId}`) + .set("Authorization", `Bearer ${consumerJwt}`) + .expect(404); + expect(response.body.errorMsg).to.equal("Resource not found"); + expect(response.body.message).to.equal( + "The software resource could not be found" + ); + }); + + it("should not get DCAT for a non-existent software resource", async () => { + const response = await request(app) + .get(`/v1/softwareresources/dcat/${nonExistentSoftwareResourcesId}`) + .set("Authorization", `Bearer ${consumerJwt}`) + .expect(404); + expect(response.body.errorMsg).to.equal("Resource not found"); + expect(response.body.message).to.equal( + "The software resource could not be found" + ); + }); + + it("should not update a non-existent software resource", async () => { + const updatedSoftwareResourceData = sampleUpdatedSoftwareResource; + const response = await request(app) + .put(`/v1/softwareresources/${nonExistentSoftwareResourcesId}`) + .set("Authorization", `Bearer ${consumerJwt}`) + .send(updatedSoftwareResourceData) + .expect(404); + expect(response.body.errorMsg).to.equal("Resource not found"); + expect(response.body.message).to.equal( + "The software resource could not be found" + ); + }); + + it("should not delete a non-existent software Resource", async () => { + const response = await request(app) + .delete(`/v1/softwareresources/${nonExistentSoftwareResourcesId}`) + .set("Authorization", `Bearer ${consumerJwt}`) + .expect(404); + expect(response.body.errorMsg).to.equal("Resource not found"); + expect(response.body.message).to.equal( + "The software resource could not be found" + ); + }); + }); + + //Error Management for Service Offerings Tests + describe("Error Management for Service Offerings Routes Tests", () => { + it("Should not update non-existent service offerings", async () => { + const response = await request(app) + .put(`/v1/serviceofferings/${nonExistentServiceOfferingId}`) + .set("Authorization", `Bearer ${provider1Jwt}`) + .send({ ...sampleProviderServiceOffering, providedBy: provider1Id }) + + .expect(404); + expect(response.body.errorMsg).to.equal("Resource not found"); + expect(response.body.message).to.equal( + "The service offering could not be found" + ); + }); + + it("Should not get the service offering by non-existent id", async () => { + const response = await request(app) + .get("/v1/serviceofferings/" + nonExistentServiceOfferingId) + .set("Authorization", `Bearer ${provider1Jwt}`) + .expect(404); + expect(response.body.errorMsg).to.equal("Resource not found"); + expect(response.body.message).to.equal( + "The service offering could not be found" + ); + }); + + it("Should not get DCAT ServiceOffering by non-existent id", async () => { + const response = await request(app) + .get(`/v1/serviceofferings/dcat/${nonExistentServiceOfferingId}`) + .expect(404); + expect(response.body.errorMsg).to.equal("Resource not found"); + expect(response.body.message).to.equal( + "The service offering could not be found" + ); + }); + + it("Should not delete non-existent service offering", async () => { + const response = await request(app) + .delete("/v1/serviceofferings/" + nonExistentServiceOfferingId) + .set("Authorization", `Bearer ${provider1Jwt}`) + .expect(404); + expect(response.body.errorMsg).to.equal("Resource not found"); + expect(response.body.message).to.equal( + "The service offering could not be found" + ); + }); + }); + + //Error Management for Negotiation Routes Tests + describe("Error Management for Bilateral Negotiation Routes Tests", () => { + it("should not get by ID a non-existent exchange configuration ", async () => { + const response = await request(app) + .get(`/v1/negotiation/${nonExistentnegotiation1Id}`) + .set("Authorization", `Bearer ${provider1Jwt}`) + .expect(404); + expect(response.body.errorMsg).to.equal("Resource not found"); + expect(response.body.message).to.equal( + "Exchange Configuration not found" + ); + }); + it("should not Create an already existing access request", async () => { + const negotiationData = sampleBilateralNegotiation; + const existingnegotiation1Id = negotiation1Id; + const response = await request(app) + .post("/v1/negotiation") + .set("Authorization", `Bearer ${provider1Jwt}`) + .send({ + ...negotiationData, + provider: provider1Id, + consumer: consumerId, + providerServiceOffering: providerServiceOffering1Id, + consumerServiceOffering: consumerServiceOffering1Id, + }) + .expect(409); + expect(response.body.errorMsg).to.equal("conflicting resource"); + expect(response.body.message).to.equal( + "An access request for this configuration already exists with id: " + + existingnegotiation1Id + ); + }); + it("should not authorize a non-existent negotiation", async () => { + const response = await request(app) + .put(`/v1/negotiation/${nonExistentnegotiation1Id}`) + .set("Authorization", `Bearer ${provider1Jwt}`) + .send(sampleAuthorizeNegotiation) + .expect(404); + expect(response.body.errorMsg).to.equal("Resource not found"); + expect(response.body.message).to.equal( + "Exchange Configuration could not be found" + ); + }); + it("should not authorize an already authorized Exchange configuration", async () => { + //authorize negotiation + await request(app) + .put(`/v1/negotiation/${negotiation1bId}`) + .set("Authorization", `Bearer ${provider1Jwt}`) + .send(sampleAuthorizeNegotiation); + //authorize negotiation again + const alreadyAuthorizedNegotitaionId = negotiation1bId; + const response = await request(app) + .put(`/v1/negotiation/${alreadyAuthorizedNegotitaionId}`) + .set("Authorization", `Bearer ${provider1Jwt}`) + .send(sampleAuthorizeNegotiation) + .expect(400); + expect(response.body.errorMsg).to.equal("Invalid operation"); + expect(response.body.message).to.equal( + "Exchange configuration has already been authorized" + ); + }); + + it("should not authorize exchange configuration if user is not the owner", async () => { + const response = await request(app) + .put(`/v1/negotiation/${negotiation1Id}`) + .set("Authorization", `Bearer ${provider2Jwt}`) + .send(sampleAuthorizeNegotiation) + .expect(400); + expect(response.body.errorMsg).to.equal("Resource error"); + expect(response.body.message).to.equal( + "Exchange Configuration could not be authorized" + ); + }); + + it("should not accept a non-existent negotiation", async () => { + const response = await request(app) + .put(`/v1/negotiation/${nonExistentnegotiation1Id}/accept`) + .set("Authorization", `Bearer ${consumerJwt}`) + .expect(404); + expect(response.body.errorMsg).to.equal("Resource not found"); + expect(response.body.message).to.equal( + "Exchange Configuration could not be found" + ); + }); + + it("should not accept an already accepted Negotiation", async () => { + //accept negotiation + await request(app) + .put(`/v1/negotiation/${negotiation1Id}/accept`) + .set("Authorization", `Bearer ${consumerJwt}`) + .expect(200); + //accept negotiation again + const response = await request(app) + .put(`/v1/negotiation/${negotiation1Id}/accept`) + .set("Authorization", `Bearer ${consumerJwt}`) + .expect(400); + expect(response.body.errorMsg).to.equal("Invalid operation"); + expect(response.body.message).to.equal( + "Exchange configuration has already been validated and is pending signatures" + ); + }); + + it("should not negotiate policies for non-existent exchange configuration ", async () => { + const response = await request(app) + .put(`/v1/negotiation/${nonExistentnegotiation1Id}/negotiate`) + .set("Authorization", `Bearer ${consumerJwt}`) + .send(sampleNegotiatePolicies) + .expect(404); + expect(response.body.errorMsg).to.equal("Resource not found"); + expect(response.body.message).to.equal( + "Exchange Configuration could not be found" + ); + }); + + it("should not sign non-existent exchange configuration", async () => { + const response = await request(app) + .put(`/v1/negotiation/${nonExistentnegotiation1Id}/sign`) + .set("Authorization", `Bearer ${provider1Jwt}`) + .send(sampleSignNegotiation) + .expect(404); + expect(response.body.errorMsg).to.equal("Resource not found"); + expect(response.body.message).to.equal( + "Exchange Configuration could not be found" + ); + }); + + it("should not sign an exchange configuration not ready for signature", async () => { + const response = await request(app) + .put(`/v1/negotiation/${negotiation2Id}/sign`) + .set("Authorization", `Bearer ${consumerJwt}`) + .send(sampleSignNegotiation) + .expect(400); + expect(response.body.errorMsg).to.equal("Invalid operation"); + expect(response.body.message).to.equal( + "Exchange configuration is not ready for signature" + ); + }); + }); + + describe("Check errors when bilateral contract component is unavalaible", () => { + before(() => { + setBilateralAvailability(false); + }); + after(() => { + setBilateralAvailability(true); + }); + it("should not authorize exchange configuration when contract generation fails", async () => { + const response = await request(app) + .put(`/v1/negotiation/${negotiation1Id}`) + .set("Authorization", `Bearer ${provider1Jwt}`) + .send(sampleAuthorizeNegotiation) + .expect(409); + expect(response.body.errorMsg).to.equal("Failed to generate contract."); + }); + + // next tests + // it("should fail to inject policies in bilateral contract", async () => { + // const response = await request(app) + // .put(`/v1/negotiation/${negotiation1Id}/sign`) + // .set("Authorization", `Bearer ${provider1Jwt}`) + // .send(sampleSignNegotiation); + // await request(app) + // .put(`/v1/negotiation/${negotiation1Id}/sign`) + // .set("Authorization", `Bearer ${consumerJwt}`) + // .send(sampleSignNegotiation) + // .expect(409); + // expect(response.body.error).to.equal( + // "Failed to inject policies in bilateral contract" + // ); + // }); + }); + + //Error Management for Ecosystem Routes Tests + describe("Error Management for Ecosystem Routes Tests", () => { + it("should not get a non-existent ecosystem", async () => { + const response = await request(app) + .get(`/v1/ecosystems/${nonExistentEcosystemId}`) + .expect(404); + expect(response.body.message).to.equal("Ecosystem not found"); + }); + + it("should not update a non-existent ecosystem", async () => { + const response = await request(app) + .put(`/v1/ecosystems/${nonExistentEcosystemId}`) + .set("Authorization", `Bearer ${orchest1Jwt}`) + .send(sampleUpdatedEcosystem) + .expect(404); + expect(response.body.message).to.equal("Ecosystem not found"); + }); + + // TO FIX : STATUS 200 + // it("should not get an ecosystem contract not yet created", async () => { + // const response = await request(app) + // .get(`/v1/ecosystems/${ecosystem1Id}/contract`) + // .set("Authorization", `Bearer ${orchest1Jwt}`) + // .expect(404); + // expect(response.body.message).to.equal("Contract not found"); + // }); + + it("should not get contract for a non existant ecosystem", async () => { + const response = await request(app) + .get(`/v1/ecosystems/${nonExistentEcosystemId}/contract`) + .set("Authorization", `Bearer ${orchest1Jwt}`) + .expect(404); + expect(response.body.message).to.equal("Ecosystem not found"); + }); + + it("should not create contract for a non existant ecosystem", async () => { + const response = await request(app) + .post(`/v1/ecosystems/${nonExistentEcosystemId}/contract`) + .set("Authorization", `Bearer ${orchest1Jwt}`) + .expect(404); + expect(response.body.errorMsg).to.equal("Not found"); + expect(response.body.message).to.equal("Ecosystem not found"); + }); + + it("should not apply Orchestrator Signature for a non existant ecosystem", async () => { + const response = await request(app) + .post(`/v1/ecosystems/${nonExistentEcosystemId}/signature/orchestrator`) + .set("Authorization", `Bearer ${orchest1Jwt}`) + .send(sampleSignEcosystem) + .expect(404); + expect(response.body.errorMsg).to.equal("Not found"); + expect(response.body.message).to.equal("Ecosystem not found"); + }); + + // TO FIX : STATUS 200 + // it("should not apply Orchestrator Signature before creating ecosystem contract", async () => { + // const response = await request(app) + // .post(`/v1/ecosystems/${ecosystem1Id}/signature/orchestrator`) + // .set("Authorization", `Bearer ${orchest1Jwt}`) + // .send(sampleSignEcosystem) + // .expect(400); + // expect(response.body.errorMsg).to.equal("Contract does not exist"); + // expect(response.body.message).to.equal( + // "The ecosystem contract was not properly generated" + // ); + // }); + + it("should not create invitation to join a non-existent ecosystem ", async () => { + const response = await request(app) + .post(`/v1/ecosystems/${nonExistentEcosystemId}/invites`) + .set("Authorization", `Bearer ${orchest1Jwt}`) + .send({ ...sampleInvitation, participantId: provider1Id }) + .expect(500); + // expect(response.body.errorMsg).to.equal("Not found"); + // expect(response.body.message).to.equal("Ecosystem not found"); + }); + + it("should not get Invitations for a participant if token is invalid", async () => { + const invalidJwt = "invalidJwt"; + const response = await request(app) + .get("/v1/ecosystems/me/invites") + .set("Authorization", `Bearer ${invalidJwt}`) + .expect(401); + expect(response.body.message).to.equal("Invalid or expired token"); + }); + + it("should not get pending Invitations of not existing ecosystem", async () => { + const response = await request(app) + .get(`/v1/ecosystems/${nonExistentEcosystemId}/invites`) + .set("Authorization", `Bearer ${orchest1Jwt}`) + .expect(404); + expect(response.body.errorMsg).to.equal("resource not found"); + + expect(response.body.message).to.equal("Ecosystem not found"); + }); + + it("should not accept invitation to join an non-existent ecosystem", async () => { + const response = await request(app) + .post(`/v1/ecosystems/${nonExistentEcosystemId}/invites/accept`) + .set("Authorization", `Bearer ${provider1Jwt}`) + .expect(404); + expect(response.body.errorMsg).to.equal("resource not found"); + expect(response.body.message).to.equal("Ecosystem not found"); + }); + + it("should not accept an invitation from participant not invited to join", async () => { + const response = await request(app) + .post(`/v1/ecosystems/${ecosystem1Id}/invites/accept`) + .set("Authorization", `Bearer ${provider1Jwt}`) + .expect(400); + expect(response.body.errorMsg).to.equal( + "ecosystem invitation accept error" + ); + expect(response.body.message).to.equal( + "An error occured when trying to accept the invitation" + ); + }); + + it("should not deny invitation to join a not existing ecosystem", async () => { + const response = await request(app) + .post(`/v1/ecosystems/${nonExistentEcosystemId}/invites/deny`) + .set("Authorization", `Bearer ${provider1Jwt}`) + .expect(500); + // expect(response.body.errorMsg).to.equal("Not found"); + // expect(response.body.message).to.equal("Ecosystem not found"); + }); + + it("should not configure Offerings for a not existant ecosystem", async () => { + const modifiedSampleOfferings = { ...sampleOfferings }; + modifiedSampleOfferings.offerings[0].serviceOffering = + providerServiceOffering1Id; + const response = await request(app) + .put(`/v1/ecosystems/${nonExistentEcosystemId}/offerings`) + .set("Authorization", `Bearer ${provider1Jwt}`) + .send(modifiedSampleOfferings) + .expect(500); + // expect(response.body.errorMsg).to.equal("Not found"); + // expect(response.body.message).to.equal("Ecosystem not found"); + }); + + it("should not apply Participant Signature for a not existent exosystem", async () => { + const response = await request(app) + .post(`/v1/ecosystems/${nonExistentEcosystemId}/signature/participant`) + .set("Authorization", `Bearer ${provider1Jwt}`) + .send(sampleSignEcosystem) + .expect(404); + expect(response.body.errorMsg).to.equal("Not found"); + expect(response.body.message).to.equal("Ecosystem not found"); + }); + + //TO FIX : STATUS 200 + // it("should not apply Participant Signature before creating ecosystem contract", async () => { + // const response = await request(app) + // .post(`/v1/ecosystems/${ecosystem3Id}/signature/participant`) + // .set("Authorization", `Bearer ${provider2Jwt}`) + // .send(sampleSignEcosystem) + // .expect(400); + // expect(response.body.errorMsg).to.equal("Contract does not exist"); + // expect(response.body.message).to.equal( + // "The ecosystem contract was not properly generated" + // ); + // }); + + describe("Check errors when ecosystem contract component is unavalaible", () => { + before(() => { + setContractAvailability(false); + }); + after(() => { + setContractAvailability(true); + }); + it("should not create a new ecosystem when contract generation fails", async () => { + const response = await request(app) + .post("/v1/ecosystems") + .set("Authorization", `Bearer ${orchest1Jwt}`) + .send(sampleEcosystem1) + .expect(424); + expect(response.body.errorMsg).to.equal("third party api failure"); + expect(response.body.message).to.equal( + "Failed to generate ecosystem contract" + ); + }); + + it("should not create ecosystem contract when it fail to generate contract", async () => { + const response = await request(app) + .post(`/v1/ecosystems/${ecosystem1Id}/contract`) + .set("Authorization", `Bearer ${orchest1Jwt}`) + .expect(424); + }); + + it("should not apply Orchestrator Signature when it fail to generate contact", async () => { + const response = await request(app) + .post(`/v1/ecosystems/${ecosystem1Id}/signature/orchestrator`) + .set("Authorization", `Bearer ${orchest1Jwt}`) + .send(sampleSignEcosystem) + .expect(424); + expect(response.body.errorMsg).to.equal("third party api failure"); + expect(response.body.message).to.equal( + "Failed to sign ecosystem contract" + ); + }); + + it("should not apply Participant Signature when it fail to generate contact", async () => { + const response = await request(app) + .post(`/v1/ecosystems/${ecosystem1Id}/signature/participant`) + .set("Authorization", `Bearer ${provider1Jwt}`) + .send(sampleSignEcosystem) + .expect(424); + expect(response.body.errorMsg).to.equal('third party api failure'); + expect(response.body.message).to.equal( + "Failed to sign ecosystem contract" + ); + }); + }); + + // TO FIX : STATUS 424 + it("should not apply Signature for a participant not invited or asked to join ecosystem ", async () => { + const response = await request(app) + .post(`/v1/ecosystems/${ecosystem1Id}/signature/participant`) + .set("Authorization", `Bearer ${provider1Jwt}`) + .send(sampleSignEcosystem) + .expect(424); + console.log(response.body) + // expect(response.body.errorMsg).to.equal( + // "unauthorized participant in ecosystem" + // ); + }); + + //TO FIX : ERROR MSG STATUS 500 + it("should not create join request by participant already participant in the ecosystem", async () => { + const alreadyParticipantJwt = consumerJwt; + const modifiedSampleJoinRequest = { ...sampleJoinRequest }; + modifiedSampleJoinRequest.offerings[0].serviceOffering = + consumerServiceOffering1Id; + const response = await request(app) + .post(`/v1/ecosystems/${ecosystem2Id}/requests`) + .set("Authorization", `Bearer ${alreadyParticipantJwt}`) + .send(modifiedSampleJoinRequest) + .expect(500); + // expect(response.body.errorMsg).to.equal("existing participant"); + // expect(response.body.message).to.equal( + // "Service is already a participant in this ecosystem" + // ); + }); + it("should not create join request for an not exsiting ecosystem", async () => { + const modifiedSampleJoinRequest = { ...sampleJoinRequest }; + modifiedSampleJoinRequest.offerings[0].serviceOffering = + providerServiceOffering1Id; + const response = await request(app) + .post(`/v1/ecosystems/${nonExistentEcosystemId}/requests`) + .set("Authorization", `Bearer ${provider1Jwt}`) + .send(modifiedSampleJoinRequest) + .expect(404); + expect(response.body.message).to.equal("Ecosystem not found"); + }); + it("should not get join requests for a not existing ecosystem", async () => { + const response = await request(app) + .get(`/v1/ecosystems/${nonExistentEcosystemId}/requests`) + .set("Authorization", `Bearer ${orchest1Jwt}`) + .expect(404); + expect(response.body.message).to.equal("Ecosystem not found"); + }); + + //TO FIX : STATUS 200 + // it("should not authorize not existing join request", async () => { + // const response = await request(app) + // .put( + // `/v1/ecosystems/${ecosystem1Id}/requests/${nonExistentRequestId}/authorize` + // ) + // .set("Authorization", `Bearer ${orchest1Jwt}`) + // .expect(400); + // }); + + it("should not authorize join request to a not existent ecosystem", async () => { + const response = await request(app) + .put( + `/v1/ecosystems/${nonExistentEcosystemId}/requests/${requestId1}/authorize` + ) + .set("Authorization", `Bearer ${orchest1Jwt}`) + .expect(404); + expect(response.body.message).to.equal( + "Ecosystem not found or unauthorized" + ); + }); + + // TO FIX : STATUS 200 + // it("should not reject not existing join request", async () => { + // const response = await request(app) + // .put( + // `/v1/ecosystems/${ecosystem1Id}/requests/${nonExistentRequestId}/reject` + // ) + // .set("Authorization", `Bearer ${orchest1Jwt}`) + // .expect(400); + // }); + + it("should not delete an non-existent ecosystem", async () => { + const response = await request(app) + .delete("/v1/ecosystems/" + nonExistentEcosystemId) + .set("Authorization", `Bearer ${orchest1Jwt}`) + .expect(404); + expect(response.body.message).to.equal("Ecosystem not found"); + }); + }); +}); diff --git a/tests/fixtures/fixture.contract.ts b/tests/fixtures/fixture.contract.ts index ec211d2..a88b22c 100644 --- a/tests/fixtures/fixture.contract.ts +++ b/tests/fixtures/fixture.contract.ts @@ -1,8 +1,15 @@ import axios from "axios"; import MockAdapter from "axios-mock-adapter"; -export const mockBilateralContract = () => { - const mock = new MockAdapter(axios); +let bilateralMockUnavailable = false; +export const setBilateralAvailability = (availability) => { + bilateralMockUnavailable = !availability; +}; +let contractMockUnavailable = false; +export const setContractAvailability = (availability) => { + contractMockUnavailable = !availability; +}; +const mockBilateralContract = (mock: MockAdapter) => { const date = new Date().toISOString(); const contractBase = { status: "pending", @@ -17,6 +24,9 @@ export const mockBilateralContract = () => { mock.onPost(`${contractUrl}`).reply((config) => { try { + if (bilateralMockUnavailable) { + return [500, { error: "Internal Server Error" }]; + } const data = JSON.parse(config.data); const { ...rest } = data.contract; contract = { @@ -33,6 +43,9 @@ export const mockBilateralContract = () => { mock.onPut(`${contractUrl}/policies/${contractId}`).reply((config) => { try { + if (bilateralMockUnavailable) { + return [500, { error: "Internal Server Error" }]; + } const injections = JSON.parse(config.data); for (const injection of injections) { const { ruleId } = injection; @@ -52,6 +65,9 @@ export const mockBilateralContract = () => { mock.onPut(`${contractUrl}/sign/${contractId}`).reply((config) => { try { + if (bilateralMockUnavailable) { + return [500, { error: "Internal Server Error" }]; + } const inputSignature = JSON.parse(config.data); if (!contract.signatures) { contract.signatures = []; @@ -74,18 +90,22 @@ export const mockBilateralContract = () => { } }); - mock.onGet(`${contractUrl}/${contractId}`).reply(200, { - ...contract, - _id: contractId, + mock.onGet(`${contractUrl}/${contractId}`).reply((config) => { + if (bilateralMockUnavailable) { + return [500, { error: "Service indisponible" }]; + } + return [200, { ...contract, _id: contractId }]; }); - mock - .onDelete(`${contractUrl}/${contractId}`) - .reply(200, { message: "Contract deleted successfully." }); + mock.onDelete(`${contractUrl}/${contractId}`).reply((config) => { + if (bilateralMockUnavailable) { + return [500, { error: "Internal Server Error" }]; + } + return [200, { message: "Contract deleted successfully." }]; + }); }; -export const mockContract = () => { - const mock = new MockAdapter(axios); +const mockContract = (mock: MockAdapter) => { const date = new Date().toISOString(); const contractBase = { rolesAndObligations: [], @@ -104,6 +124,9 @@ export const mockContract = () => { mock.onPost(`${contractUrl}`).reply((config) => { try { + if (contractMockUnavailable) { + return [500, { error: "Internal Server Error" }]; + } const data = JSON.parse(config.data); const { permission = [], prohibition = [], ...rest } = data.contract; const rolesAndObligations = data.role @@ -134,6 +157,9 @@ export const mockContract = () => { mock.onPut(`${contractUrl}/policies/${contractId}`).reply((config) => { try { + if (contractMockUnavailable) { + return [500, { error: "Internal Server Error" }]; + } const injections = JSON.parse(config.data); for (const injection of injections) { const { role } = injection; @@ -163,6 +189,9 @@ export const mockContract = () => { mock.onPut(`${contractUrl}/sign/${contractId}`).reply((config) => { try { + if (contractMockUnavailable) { + return [500, { error: "Internal Server Error" }]; + } const injections = JSON.parse(config.data); const { participant, signature } = injections; const existingMember = contract.members.find( @@ -193,12 +222,23 @@ export const mockContract = () => { } }); - mock.onGet(`${contractUrl}/${contractId}`).reply(200, { - ...contract, - _id: contractId, + mock.onGet(`${contractUrl}/${contractId}`).reply((config) => { + if (contractMockUnavailable) { + return [500, { error: "Internal Server Error" }]; + } + return [200, { ...contract, _id: contractId }]; }); - mock - .onDelete(`${contractUrl}/${contractId}`) - .reply(200, { message: "Contract deleted successfully." }); + mock.onDelete(`${contractUrl}/${contractId}`).reply((config) => { + if (contractMockUnavailable) { + return [500, { error: "Internal Server Error" }]; + } + return [200, { message: "Contract deleted successfully." }]; + }); +}; + +export const setupMocks = () => { + const mock = new MockAdapter(axios); + mockContract(mock); + mockBilateralContract(mock); }; diff --git a/tests/fixtures/sampleData.ts b/tests/fixtures/sampleData.ts index bc98ab8..547f338 100644 --- a/tests/fixtures/sampleData.ts +++ b/tests/fixtures/sampleData.ts @@ -62,18 +62,13 @@ export const sampleEcosystem = { } }, ], - - buildingBlocks: [ - { - buildingBlock: "Block 1", - implementation: "Implementation 1", - }, - ], }; + + export const sampleEcosystem1 = { - name: "Sample Ecosystem", - description: "This is a sample ecosystem description.", + name: "Sample Ecosystem1", + description: "This is a sample ecosystem1 description.", provides: ["Data", "Users"], searchedData: ["Data 1", "Data 2"], searchedServices: ["Service 1"], @@ -333,6 +328,37 @@ export const sampleBilateralNegotiation = { consumerServiceOffering:"", }; +export const sampleAuthorizeNegotiation = { + policy: [ + { + ruleId: "rule-access-5", + values: { + dateBegin: "2024-01-01", + dateEnd: "2026-01-01", + }, + }, + ], +}; + +export const sampleNegotiatePolicies = { + policy: [ + { + ruleId: "rule-access-5", + values: { + dateBegin: "2022-02-02", + dateEnd: "2023-03-03", + }, + }, + ], +}; + +export const sampleSignNegotiation = { + signature: "hasSigned", +} +export const sampleSignEcosystem = { + signature: "hasSigned", +} + export const sampleSoftwareResource = { aggregationOf: ["Resource 1", "Resource 2"], name: "Sample software Resource", diff --git a/tests/fixtures/testAccount.ts b/tests/fixtures/testAccount.ts index 54a2fe7..00f2399 100644 --- a/tests/fixtures/testAccount.ts +++ b/tests/fixtures/testAccount.ts @@ -8,7 +8,7 @@ export const testParticipantOrganizationAdmin = { export const testOrchestrator = { email: "orchestrator@example.com", password: "Password@123!", - participantName: "orchestOrganization1", + participantName: "orchestOrganization", firstName: "test", lastName: "orchest", }; @@ -16,62 +16,105 @@ export const testProvider1 = { email: "provider1@example.com", password: "Password@123!", participantName: "providerOrganization1", - firstName: "test", + firstName: "test1", lastName: "provider1", }; export const testProvider2 = { email: "provider2@example.com", password: "Password@123!", participantName: "providerOrganization2", - firstName: "test", + firstName: "test2", lastName: "provider2", }; export const testProvider3 = { email: "provider3@example.com", password: "Password@123!", participantName: "providerOrganization3", - firstName: "test", + firstName: "test3", lastName: "provider3", }; export const testProvider4 = { email: "provider4@example.com", password: "Password@123!", participantName: "providerOrganization4", - firstName: "test", + firstName: "test4", lastName: "provider4", }; export const testProvider5 = { email: "provider5@example.com", password: "Password@123!", participantName: "providerOrganization5", - firstName: "test", + firstName: "test5", lastName: "provider5", }; export const testConsumer1 = { email: "consumer1@example.com", password: "Password@123!", participantName: "consumerOrganization1", - firstName: "test", + firstName: "test1", lastName: "consumer1", -} +}; export const testConsumer2 = { email: "consumer2@example.com", password: "Password@123!", participantName: "consumerOrganization2", - firstName: "test", + firstName: "test2", lastName: "consumer2", -} +}; export const testConsumer3 = { email: "consumer3@example.com", password: "Password@123!", participantName: "consumerOrganization3", - firstName: "test", + firstName: "test3", lastName: "consumer3", -} +}; export const testConsumer4 = { email: "consumer4@example.com", password: "Password@123!", participantName: "consumerOrganization4", - firstName: "test", + firstName: "test4", lastName: "consumer4", -} \ No newline at end of file +}; + +export const testErrorOrchest1 = { + email: "orchest1@example.com", + password: "Password@123!", + participantName: "orchest_Organization1", + firstName: "test1", + lastName: "orchesterror1", +}; +export const testErrorOrchest2 = { + email: "orchest2@example.com", + password: "Password@123!", + participantName: "orchest_Organization2", + firstName: "test2", + lastName: "orchesterror2", +}; +export const testErrorOrchest3 = { + email: "orchest3@example.com", + password: "Password@123!", + participantName: "orchest_Organization3", + firstName: "test3", + lastName: "orchesterror3", +}; +export const testErrorProvider1 = { + email: "provider_1@example.com", + password: "Password@123!", + participantName: "provider_Organization1", + firstName: "test1", + lastName: "provider1", +}; +export const testErrorProvider2 = { + email: "provider_2@example.com", + password: "Password@123!", + participantName: "provider_Organization2", + firstName: "test2", + lastName: "provider2", +}; +export const testErrorConsumer = { + email: "consumer_1@example.com", + password: "Password@123!", + participantName: "consumer_Organization", + firstName: "test", + lastName: "consumer1", +}; diff --git a/tests/index.spec.ts b/tests/index.spec.ts index 3a2601d..7e74a24 100644 --- a/tests/index.spec.ts +++ b/tests/index.spec.ts @@ -14,7 +14,7 @@ import { sampleServiceOffering, } from "./fixtures/sampleData"; import { Application } from "express"; -import { mockContract } from "./fixtures/fixture.contract"; +import { setupMocks } from "./fixtures/fixture.contract"; import { closeMongoose } from "../src/config/database"; let app: Application; @@ -33,7 +33,7 @@ describe("Main Catalog Api Test Cases", () => { await serverInstance.promise; app = serverInstance.app; server = serverInstance.server; - mockContract(); + setupMocks(); }); after(async () => { diff --git a/tests/negotiation.spec.ts b/tests/negotiation.spec.ts index b9a8798..803ed4b 100644 --- a/tests/negotiation.spec.ts +++ b/tests/negotiation.spec.ts @@ -5,7 +5,7 @@ config(); import { startServer } from "../src/server"; import { IncomingMessage, Server, ServerResponse } from "http"; import { Application } from "express"; -import { mockBilateralContract } from "./fixtures/fixture.contract"; +import { setupMocks } from "./fixtures/fixture.contract"; import { testProvider4, testConsumer3 } from "./fixtures/testAccount"; import { sampleDataResource, @@ -13,6 +13,9 @@ import { sampleProviderServiceOffering, sampleConsumerServiceOffering, sampleBilateralNegotiation, + sampleAuthorizeNegotiation, + sampleNegotiatePolicies, + sampleSignNegotiation, } from "./fixtures/sampleData"; import { stub } from "sinon"; import * as loadMongoose from "../src/config/database"; @@ -44,7 +47,7 @@ describe("Bilateral Negotiation Routes Tests", () => { await serverInstance.promise; app = serverInstance.app; server = serverInstance.server; - mockBilateralContract(); + setupMocks(); //create provider const providerData = testProvider4; @@ -138,17 +141,7 @@ describe("Bilateral Negotiation Routes Tests", () => { const response = await request(app) .put(`/v1/negotiation/${negotiationId}`) .set("Authorization", `Bearer ${providerJwt}`) - .send({ - policy: [ - { - ruleId: "rule-access-5", - values: { - dateBegin: "2024-01-01", - dateEnd: "2026-01-01", - }, - }, - ], - }) + .send(sampleAuthorizeNegotiation) .expect(200); expect(response.body).to.be.an("object"); expect(response.body).to.have.property("negotiationStatus", "Authorized"); @@ -180,19 +173,15 @@ describe("Bilateral Negotiation Routes Tests", () => { it("should sign exchange configuration by data provider", async () => { const response = await request(app) .put(`/v1/negotiation/${negotiationId}/sign`) - .set("Authorization", `Bearer ${consumerJwt}`) - .send({ - signature: "hasSigned", - }) + .set("Authorization", `Bearer ${providerJwt}`) + .send(sampleSignNegotiation) .expect(200); }); it("should sign exchange configuration by service provider", async () => { const response = await request(app) .put(`/v1/negotiation/${negotiationId}/sign`) .set("Authorization", `Bearer ${consumerJwt}`) - .send({ - signature: "hasSigned", - }) + .send(sampleSignNegotiation) .expect(200); }); @@ -216,21 +205,11 @@ describe("Bilateral Negotiation Routes Tests", () => { .set("Authorization", `Bearer ${consumerJwt}`) .expect(200); }); - it("should negociate exchange configuration policies", async () => { + it("should negotiate exchange configuration policies", async () => { const response = await request(app) .put(`/v1/negotiation/${negotiationId}/negotiate`) .set("Authorization", `Bearer ${consumerJwt}`) - .send({ - policy: [ - { - ruleId: "rule-access-5", - values: { - dateBegin: "2022-02-02", - dateEnd: "2023-03-03", - }, - }, - ], - }) + .send(sampleNegotiatePolicies) .expect(200); expect(response.body).to.be.an("object"); expect(response.body).to.have.property("negotiationStatus", "Negotiation"); diff --git a/tests/softwareResources.spec.ts b/tests/softwareResources.spec.ts index 23a656b..2fad1ae 100644 --- a/tests/softwareResources.spec.ts +++ b/tests/softwareResources.spec.ts @@ -22,7 +22,7 @@ let consumerId = ""; let softwareResourceId: ""; let jwt = ""; -describe("Software Resources Routes Tests", () => { +describe("Software Resources Routes Tests", function () { let loadMongooseStub; before(async () => { loadMongooseStub = stub(loadMongoose, "loadMongoose").callsFake( @@ -71,8 +71,6 @@ describe("Software Resources Routes Tests", () => { softwareResourceId = response.body._id; expect(response.body).to.be.an("object"); expect(response.body.providedBy).to.equal(consumerId); - - //assertions }); it("should update software resource", async () => { @@ -89,8 +87,7 @@ describe("Software Resources Routes Tests", () => { const response = await request(app) .get(`/v1/softwareresources/${softwareResourceId}`) .expect(200); - //assertions - //expect response id = softwareResourceId + expect(response.body._id).to.equal(softwareResourceId); }); it("should get software resource by ID-protected", async () => { @@ -98,8 +95,7 @@ describe("Software Resources Routes Tests", () => { .get(`/v1/softwareresources/${softwareResourceId}`) .set("Authorization", `Bearer ${jwt}`) .expect(200); - //assertions - //expect response id = softwareResourceId + expect(response.body._id).to.equal(softwareResourceId); }); it("should get Participant softwareResources", async () => { @@ -107,25 +103,21 @@ describe("Software Resources Routes Tests", () => { .get("/v1/softwareresources/me") .set("Authorization", `Bearer ${jwt}`) .expect(200); - //assertions - //expect response not empty - }); + expect(response.body).to.not.be.empty; + }); it("should get all softwareResources", async () => { const response = await request(app) .get("/v1/softwareresources") .expect(200); - //assertions - //expect response not empty - }); + expect(response.body).to.not.be.empty; + }); it("should delete softwareResource", async () => { const response = await request(app) .delete(`/v1/softwareresources/${softwareResourceId}`) .set("Authorization", `Bearer ${jwt}`) .expect(204); - //assertions - //expect + expect(response.body).to.be.empty; }); - // test error get software resources deleted });