Skip to content

Commit

Permalink
feat(template): policyhub restrict or operand to access policies (#107)
Browse files Browse the repository at this point in the history
Reviewed-By: Phil Schneider <info@philschneider.de>
Refs: #43
  • Loading branch information
AnuragNagpure authored Apr 16, 2024
1 parent 4364f46 commit 6a4cacd
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,11 @@ private static PolicyFileContent CreateFileContent(PolicyTypeId type, OperatorId

public async Task<PolicyResponse> 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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<ControllerArgumentException>(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()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<ErrorResponse>(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<ErrorResponse>(JsonOptions).ConfigureAwait(false);
// error!.Errors.Should().ContainSingle().And.Satisfy(
// x => x.Value.Single() == "Keys FrameworkAgreement.traceability have been defined multiple times");
// }

#endregion

Expand Down

0 comments on commit 6a4cacd

Please sign in to comment.