Skip to content

Commit

Permalink
Ensure that delivery channel policy names don't contain whitespace or…
Browse files Browse the repository at this point in the history
… capital letters, add tests that ensure this rule is enforced
  • Loading branch information
griffri committed Feb 22, 2024
1 parent 82d970b commit 8fc766a
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 4 deletions.
43 changes: 42 additions & 1 deletion src/protagonist/API.Tests/Integration/DeliveryChannelTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,27 @@ public async Task Post_DeliveryChannelPolicy_400_IfChannelInvalid()
response.StatusCode.Should().Be(HttpStatusCode.BadRequest);
}

[Fact]
public async Task Post_DeliveryChannelPolicy_400_IfNameInvalid()
{
// Arrange
const int customerId = 88;
const string newDeliveryChannelPolicyJson = @"{
""name"": ""foo bar"",
""displayName"": ""Invalid Policy"",
""policyData"": ""[\""audio-mp3-128\""]""
}";

var path = $"customers/{customerId}/deliveryChannelPolicies/iiif-av";

// Act
var content = new StringContent(newDeliveryChannelPolicyJson, Encoding.UTF8, "application/json");
var response = await httpClient.AsCustomer(customerId).PostAsync(path, content);

// Assert
response.StatusCode.Should().Be(HttpStatusCode.BadRequest);
}

[Theory]
[InlineData("")] // No PolicyData specified
[InlineData("[]")] // Empty array
Expand Down Expand Up @@ -218,6 +239,26 @@ public async Task Put_DeliveryChannelPolicy_400_IfChannelInvalid()
response.StatusCode.Should().Be(HttpStatusCode.BadRequest);
}

[Fact]
public async Task Put_DeliveryChannelPolicy_400_IfNameInvalid()
{
// Arrange
const int customerId = 88;
const string newDeliveryChannelPolicyJson = @"{
""displayName"": ""Invalid Policy"",
""policyData"": ""[\""audio-mp3-128\""]""r
}";

var path = $"customers/{customerId}/deliveryChannelPolicies/iiif-av/FooBar";

// Act
var content = new StringContent(newDeliveryChannelPolicyJson, Encoding.UTF8, "application/json");
var response = await httpClient.AsCustomer(customerId).PutAsync(path, content);

// Assert
response.StatusCode.Should().Be(HttpStatusCode.BadRequest);
}

[Theory]
[InlineData("")] // No PolicyData specified
[InlineData("[]")] // Empty array
Expand Down Expand Up @@ -303,7 +344,7 @@ public async Task Patch_DeliveryChannelPolicy_201()
var policy = new DLCS.Model.Policies.DeliveryChannelPolicy()
{
Customer = customerId,
Name = "put-av-policy-3",
Name = "put-av-policy",
DisplayName = "My IIIF-AV Policy 3",
Channel = "iiif-av",
PolicyData = "[\"audio-mp3-128\"]"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System.Text.RegularExpressions;
using API.Features.DeliveryChannels.Converters;
using API.Features.DeliveryChannels.Requests;
using API.Features.DeliveryChannels.Validation;
Expand Down Expand Up @@ -115,6 +116,12 @@ public async Task<IActionResult> PostDeliveryChannelPolicy(
400, "Invalid delivery channel policy");
}

if (!IsValidName(hydraDeliveryChannelPolicy.Name))
{
return this.HydraProblem($"'The name specified for this delivery channel policy is invalid", null,
400, "Invalid delivery channel policy");
}

var validationResult = await validator.ValidateAsync(hydraDeliveryChannelPolicy,
policy => policy.IncludeRuleSets("default", "post"), cancellationToken);
if (!validationResult.IsValid)
Expand Down Expand Up @@ -170,14 +177,20 @@ public async Task<IActionResult> PutDeliveryChannelPolicy(
return this.HydraProblem($"'{deliveryChannelName}' is not a valid/permitted delivery channel", null,
400, "Invalid delivery channel policy");
}


if (!IsValidName(deliveryChannelPolicyName))
{
return this.HydraProblem($"'The name specified for this delivery channel policy is invalid", null,
400, "Invalid delivery channel policy");
}

var validationResult = await validator.ValidateAsync(hydraDeliveryChannelPolicy,
policy => policy.IncludeRuleSets("default", "put-patch"), cancellationToken);
if (!validationResult.IsValid)
{
return this.ValidationFailed(validationResult);
}

hydraDeliveryChannelPolicy.CustomerId = customerId;
hydraDeliveryChannelPolicy.Name = deliveryChannelPolicyName;
hydraDeliveryChannelPolicy.Channel = deliveryChannelName;
Expand Down Expand Up @@ -210,6 +223,12 @@ public async Task<IActionResult> PatchDeliveryChannelPolicy(
400, "Invalid delivery channel policy");
}

if (!IsValidName(deliveryChannelPolicyName))
{
return this.HydraProblem($"The name specified for this delivery channel policy is invalid", null,
400, "Invalid delivery channel policy");
}

var validationResult = await validator.ValidateAsync(hydraDeliveryChannelPolicy,
policy => policy.IncludeRuleSets("default", "put-patch"), cancellationToken);
if (!validationResult.IsValid)
Expand Down Expand Up @@ -263,4 +282,10 @@ private bool IsPermittedDeliveryChannel(string deliveryChannelPolicyName)
{
return allowedDeliveryChannels.Contains(deliveryChannelPolicyName);
}

private bool IsValidName(string? inputName)
{
const string regex = "[\\sA-Z]"; // Delivery channel policy names should not contain capital letters or spaces
return !(string.IsNullOrEmpty(inputName) || Regex.IsMatch(inputName, regex));
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using FluentValidation;
using System.Text.RegularExpressions;
using FluentValidation;

namespace API.Features.DeliveryChannels.Validation;

Expand Down

0 comments on commit 8fc766a

Please sign in to comment.