Skip to content

Commit

Permalink
Merge pull request #957 from dlcs/fix/disableAdditionalChannelsToDefault
Browse files Browse the repository at this point in the history
Add additional validation rule to provide a bad request when more than just the default channel
  • Loading branch information
JackLewis-digirati authored Feb 10, 2025
2 parents 4cd4bf2 + 93b1285 commit 5cbd022
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,25 @@ public void DeliveryChannel_ValidationError_WhenNoneAndMoreDeliveryChannels()
result.ShouldHaveValidationErrorFor(a => a.DeliveryChannels);
}

[Fact]
public void DeliveryChannel_ValidationError_WhenDefaultAndMoreDeliveryChannels()
{
var sut = GetSut();
var model = new Image { DeliveryChannels = new[]
{
new DeliveryChannel()
{
Channel = "default"
},
new DeliveryChannel()
{
Channel = "file"
}
} };
var result = sut.TestValidate(model);
result.ShouldHaveValidationErrorFor(a => a.DeliveryChannels);
}

[Fact]
public void DeliveryChannel_NoValidationError_WhenDeliveryChannelsWithNoNone()
{
Expand Down
56 changes: 56 additions & 0 deletions src/protagonist/API.Tests/Integration/ModifyAssetTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,62 @@ public async Task Put_NewImageAsset_Creates_Asset_WithDeliveryChannelsSetToDefau
.Contain(i => i.Channel == AssetDeliveryChannels.Thumbnails);
}

[Fact]
public async Task Put_NewImageAsset_BadRequest_WithDefaultDeliveryChannelPlusOther()
{
var customerAndSpace = await CreateCustomerAndSpace();

var assetId = new AssetId(customerAndSpace.customer, customerAndSpace.space, nameof(Put_NewImageAsset_Creates_Asset_WithDeliveryChannelsSetToDefault));
var hydraImageBody = $@"{{
""@type"": ""Image"",
""origin"": ""https://example.org/{assetId.Asset}.tiff"",
""family"": ""I"",
""mediaType"": ""image/tiff"",
""deliveryChannels"": [
{{
""channel"": ""default""
}},
{{
""channel"": ""iiif-img""
}}]
}}";

// act
var content = new StringContent(hydraImageBody, Encoding.UTF8, "application/json");
var response = await httpClient.AsCustomer(customerAndSpace.customer).PutAsync(assetId.ToApiResourcePath(), content);

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

[Fact]
public async Task Put_NewImageAsset_BadRequest_WithMultipleSingleOnlyDeliveryChannel()
{
var customerAndSpace = await CreateCustomerAndSpace();

var assetId = new AssetId(customerAndSpace.customer, customerAndSpace.space, nameof(Put_NewImageAsset_Creates_Asset_WithDeliveryChannelsSetToDefault));
var hydraImageBody = $@"{{
""@type"": ""Image"",
""origin"": ""https://example.org/{assetId.Asset}.tiff"",
""family"": ""I"",
""mediaType"": ""image/tiff"",
""deliveryChannels"": [
{{
""channel"": ""default""
}},
{{
""channel"": ""none""
}}]
}}";

// act
var content = new StringContent(hydraImageBody, Encoding.UTF8, "application/json");
var response = await httpClient.AsCustomer(customerAndSpace.customer).PutAsync(assetId.ToApiResourcePath(), content);

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

[Fact]
public async Task Put_NewImageAsset_Creates_Asset_WithCustomDefaultDeliveryChannel()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,11 @@ private void ImageDeliveryChannelDependantValidation()
RuleForEach(a => a.DeliveryChannels)
.Must(dc => AssetDeliveryChannels.IsValidChannel(dc.Channel))
.WithMessage($"DeliveryChannel must be one of {AssetDeliveryChannels.AllString}");

RuleFor(a => a.DeliveryChannels)
.Must(d => d.All(d => d.Channel != AssetDeliveryChannels.None))
.Must(dl => dl.All(d => !AssetDeliveryChannels.SingleOnly.Contains(d.Channel)))
.When(a => a.DeliveryChannels!.Length > 1)
.WithMessage("If 'none' is the specified channel, then no other delivery channels are allowed");
.WithMessage(d => $"If one of '{AssetDeliveryChannels.SingleOnlyString}' is a specified channel, then no other delivery channels are allowed");

RuleForEach(a => a.DeliveryChannels)
.Must(c => !string.IsNullOrEmpty(c.Channel))
Expand Down
10 changes: 10 additions & 0 deletions src/protagonist/DLCS.Model/Assets/AssetDeliveryChannels.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,22 @@ public static class AssetDeliveryChannels
/// All possible delivery channels
/// </summary>
public static string[] All { get; } = { File, Timebased, Image, Thumbnails, None, Default };

/// <summary>
/// All possible delivery channels that can only be used by themselves
/// </summary>
public static string[] SingleOnly { get; } = { Default, None };

/// <summary>
/// All possible delivery channels as a comma-delimited string
/// </summary>
public static readonly string AllString = string.Join(',', All);

/// <summary>
/// All single only delivery channels as a comma-delimited string
/// </summary>
public static readonly string SingleOnlyString = string.Join(',', SingleOnly);

/// <summary>
/// Checks if an asset has any delivery channel specified in a list
/// </summary>
Expand Down

0 comments on commit 5cbd022

Please sign in to comment.