From 6a4cacd607325fecf08ce567c13b2d3bacd2e636 Mon Sep 17 00:00:00 2001 From: AnuragNagpure <145100366+AnuragNagpure@users.noreply.github.com> Date: Tue, 16 Apr 2024 11:16:24 +0530 Subject: [PATCH] feat(template): policyhub restrict or operand to access policies (#107) Reviewed-By: Phil Schneider Refs: #43 --- .../BusinessLogic/PolicyHubBusinessLogic.cs | 5 + .../PolicyHubBusinessLogicTests.cs | 19 ++++ .../Controllers/PolicyHubControllerTests.cs | 94 +++++++++---------- 3 files changed, 71 insertions(+), 47 deletions(-) diff --git a/src/hub/PolicyHub.Service/BusinessLogic/PolicyHubBusinessLogic.cs b/src/hub/PolicyHub.Service/BusinessLogic/PolicyHubBusinessLogic.cs index 3b0ef5c..413a25b 100644 --- a/src/hub/PolicyHub.Service/BusinessLogic/PolicyHubBusinessLogic.cs +++ b/src/hub/PolicyHub.Service/BusinessLogic/PolicyHubBusinessLogic.cs @@ -112,6 +112,11 @@ private static PolicyFileContent CreateFileContent(PolicyTypeId type, OperatorId public async Task GetPolicyContentAsync(PolicyContentRequest requestData) { + if (requestData.PolicyType == PolicyTypeId.Usage && requestData.ConstraintOperand == ConstraintOperandId.Or) + { + throw new ControllerArgumentException($"The support of OR constraintOperand for Usage constraints are not supported for now"); + } + var keyCounts = requestData.Constraints .GroupBy(pair => pair.Key) .ToDictionary(group => group.Key, group => group.Count()); diff --git a/tests/hub/PolicyHub.Service.Tests/BusinessLogic/PolicyHubBusinessLogicTests.cs b/tests/hub/PolicyHub.Service.Tests/BusinessLogic/PolicyHubBusinessLogicTests.cs index bc72911..71de559 100644 --- a/tests/hub/PolicyHub.Service.Tests/BusinessLogic/PolicyHubBusinessLogicTests.cs +++ b/tests/hub/PolicyHub.Service.Tests/BusinessLogic/PolicyHubBusinessLogicTests.cs @@ -275,6 +275,25 @@ public async Task GetPolicyContentAsync_WithRegexWithoutMatchingRegexPattern_Thr ex.Message.Should().Be(@"The provided value testRegValue does not match the regex pattern ^BPNL[\w|\d]{12}$ (Parameter 'value')"); } + [Fact] + public async Task GetPolicyContentAsync_WithUsageConstraintNotAllowedWithOR_ThrowsControllerArgumentException() + { + // Arrange + var data = new PolicyContentRequest(PolicyTypeId.Usage, ConstraintOperandId.Or, + new[] + { + new Constraints("test", OperatorId.Equals, "testRegValue"), + }); + + async Task Act() => await _sut.GetPolicyContentAsync(data); + + // Act + var ex = await Assert.ThrowsAsync(Act); + + // Assert + ex.Message.Should().Be(@"The support of OR constraintOperand for Usage constraints are not supported for now"); + } + [Fact] public async Task GetPolicyContentAsync_WithMultipleDefinedKeys_ThrowsNotFoundException() { diff --git a/tests/hub/PolicyHub.Service.Tests/Controllers/PolicyHubControllerTests.cs b/tests/hub/PolicyHub.Service.Tests/Controllers/PolicyHubControllerTests.cs index 4d8dcc5..9f8be3e 100644 --- a/tests/hub/PolicyHub.Service.Tests/Controllers/PolicyHubControllerTests.cs +++ b/tests/hub/PolicyHub.Service.Tests/Controllers/PolicyHubControllerTests.cs @@ -269,53 +269,53 @@ public async Task GetPolicyContentWithFiltersAsync_MultipleConstraintsEqualsAndO .Be("{\"content\":{\"@context\":[\"https://www.w3.org/ns/odrl.jsonld\",{\"cx\":\"https://w3id.org/catenax/v0.0.1/ns/\"}],\"@type\":\"Offer\",\"@id\":\"....\",\"permission\":{\"action\":\"use\",\"constraint\":{\"odrl:and\":[{\"leftOperand\":\"BusinessPartnerNumber\",\"operator\":\"eq\",\"rightOperand\":\"BPNL00000003CRHK\"},{\"leftOperand\":\"FrameworkAgreement.traceability\",\"operator\":\"eq\",\"rightOperand\":\"@FrameworkAgreement.traceability-Version\"},{\"leftOperand\":\"Dismantler.activityType\",\"operator\":\"in\",\"rightOperand\":[\"Audi\",\"BMW\",\"VW\"]}]}}},\"attributes\":[{\"key\":\"@FrameworkAgreement.traceability-Version\",\"possibleValues\":[\"active:1.0\",\"active:1.1\",\"active:1.2\"]}]}"); } - [Fact] - public async Task GetPolicyContentWithFiltersAsync_MultipleConstraintsEqualsOrOperand_ReturnsExpected() - { - // Arrange - var data = new PolicyContentRequest( - PolicyTypeId.Usage, - ConstraintOperandId.Or, - new[] - { - new Constraints("FrameworkAgreement.traceability", OperatorId.Equals, null), - new Constraints("companyRole.dismantler", OperatorId.In, null), - }); - - // Act - var response = await _client.PostAsJsonAsync($"{BaseUrl}/policy-content", data, JsonOptions); - - // Assert - response.Should().NotBeNull(); - response.StatusCode.Should().Be(HttpStatusCode.OK); - (await response.Content.ReadAsStringAsync()) - .Should() - .Be("{\"content\":{\"@context\":[\"https://www.w3.org/ns/odrl.jsonld\",{\"cx\":\"https://w3id.org/catenax/v0.0.1/ns/\"}],\"@type\":\"Offer\",\"@id\":\"....\",\"permission\":{\"action\":\"use\",\"constraint\":{\"odrl:or\":[{\"leftOperand\":\"FrameworkAgreement.traceability\",\"operator\":\"eq\",\"rightOperand\":\"@FrameworkAgreement.traceability-Version\"},{\"leftOperand\":\"Dismantler.activityType\",\"operator\":\"in\",\"rightOperand\":[\"Audi\",\"BMW\",\"VW\"]}]}}},\"attributes\":[{\"key\":\"@FrameworkAgreement.traceability-Version\",\"possibleValues\":[\"active:1.0\",\"active:1.1\",\"active:1.2\"]}]}"); - } - - [Fact] - public async Task GetPolicyContentWithFiltersAsync_WithSameConstraintKeys_ReturnsError() - { - // Arrange - var data = new PolicyContentRequest( - PolicyTypeId.Usage, - ConstraintOperandId.Or, - new[] - { - new Constraints("FrameworkAgreement.traceability", OperatorId.Equals, null), - new Constraints("FrameworkAgreement.traceability", OperatorId.Equals, null), - }); - - // Act - var response = await _client.PostAsJsonAsync($"{BaseUrl}/policy-content", data, JsonOptions); - - // Assert - response.Should().NotBeNull(); - response.StatusCode.Should().Be(HttpStatusCode.BadRequest); - var error = await response.Content.ReadFromJsonAsync(JsonOptions); - error!.Errors.Should().ContainSingle().And.Satisfy( - x => x.Value.Single() == "Keys FrameworkAgreement.traceability have been defined multiple times"); - } + // [Fact] + // public async Task GetPolicyContentWithFiltersAsync_MultipleConstraintsEqualsOrOperand_ReturnsExpected() + // { + // // Arrange + // var data = new PolicyContentRequest( + // PolicyTypeId.Usage, + // ConstraintOperandId.Or, + // new[] + // { + // new Constraints("FrameworkAgreement.traceability", OperatorId.Equals, null), + // new Constraints("companyRole.dismantler", OperatorId.In, null), + // }); + + // // Act + // var response = await _client.PostAsJsonAsync($"{BaseUrl}/policy-content", data, JsonOptions).ConfigureAwait(false); + + // // Assert + // response.Should().NotBeNull(); + // response.StatusCode.Should().Be(HttpStatusCode.OK); + // (await response.Content.ReadAsStringAsync().ConfigureAwait(false)) + // .Should() + // .Be("{\"content\":{\"@context\":[\"https://www.w3.org/ns/odrl.jsonld\",{\"cx\":\"https://w3id.org/catenax/v0.0.1/ns/\"}],\"@type\":\"Offer\",\"@id\":\"....\",\"permission\":{\"action\":\"use\",\"constraint\":{\"odrl:or\":[{\"leftOperand\":\"FrameworkAgreement.traceability\",\"operator\":\"eq\",\"rightOperand\":\"@FrameworkAgreement.traceability-Version\"},{\"leftOperand\":\"Dismantler.activityType\",\"operator\":\"in\",\"rightOperand\":[\"Audi\",\"BMW\",\"VW\"]}]}}},\"attributes\":[{\"key\":\"@FrameworkAgreement.traceability-Version\",\"possibleValues\":[\"active:1.0\",\"active:1.1\",\"active:1.2\"]}]}"); + // } + + // [Fact] + // public async Task GetPolicyContentWithFiltersAsync_WithSameConstraintKeys_ReturnsError() + // { + // // Arrange + // var data = new PolicyContentRequest( + // PolicyTypeId.Usage, + // ConstraintOperandId.Or, + // new[] + // { + // new Constraints("FrameworkAgreement.traceability", OperatorId.Equals, null), + // new Constraints("FrameworkAgreement.traceability", OperatorId.Equals, null), + // }); + + // // Act + // var response = await _client.PostAsJsonAsync($"{BaseUrl}/policy-content", data, JsonOptions).ConfigureAwait(false); + + // // Assert + // response.Should().NotBeNull(); + // response.StatusCode.Should().Be(HttpStatusCode.BadRequest); + // var error = await response.Content.ReadFromJsonAsync(JsonOptions).ConfigureAwait(false); + // error!.Errors.Should().ContainSingle().And.Satisfy( + // x => x.Value.Single() == "Keys FrameworkAgreement.traceability have been defined multiple times"); + // } #endregion