Skip to content

Commit fab0cc9

Browse files
authored
Merge pull request #1086 from dlcs/bugfix/adjunct_missing_id
Adjunct bugfixes
2 parents 893fb7e + f663934 commit fab0cc9

26 files changed

+1995
-199
lines changed

src/protagonist/API.Tests/Features/Adjuncts/Validation/HydraAdjunctValidatorTests.cs

Lines changed: 65 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,18 @@
11
using API.Features.Adjuncts.Validation;
2+
using API.Settings;
23
using DLCS.HydraModel;
34
using FluentValidation.TestHelper;
5+
using Microsoft.Extensions.Options;
46

57
namespace API.Tests.Features.Adjuncts.Validation;
68

79
public class HydraAdjunctValidatorTests
810
{
9-
private readonly HydraAdjunctValidator sut = new();
11+
private readonly HydraAdjunctValidator sut = new(Options.Create(
12+
new ApiSettings
13+
{
14+
RestrictedResourceIdCharacterString = "\\ /"
15+
}));
1016

1117
[Fact]
1218
public void Valid_WhenAllValidatorsUsed()
@@ -17,7 +23,7 @@ public void Valid_WhenAllValidatorsUsed()
1723
MediaType = "mediaType",
1824
IIIFLink = "seeAlso",
1925
Type = "Image",
20-
Id = "https://localhost/customers/1/spaces/1/images/assetId/adjuncts/adjunctId",
26+
Language = ["fra", "en"]
2127
};
2228
var result = sut.TestValidate(adjunct);
2329
result.ShouldNotHaveAnyValidationErrors();
@@ -41,7 +47,7 @@ public void ExternalId_NotValidUri()
4147
[Fact]
4248
public void ExternalId_Null()
4349
{
44-
var adjunct = new Adjunct("https://localhost", 1, 1, "assetId", "adjunctId")
50+
var adjunct = new Adjunct
4551
{
4652
ExternalId = null,
4753
MediaType = "mediaType",
@@ -58,7 +64,7 @@ public void ExternalId_Null()
5864
[InlineData("Invalid")]
5965
public void IIIFLink_NotValid(string iiifLink)
6066
{
61-
var adjunct = new Adjunct("https://localhost", 1, 1, "assetId", "adjunctId")
67+
var adjunct = new Adjunct
6268
{
6369
MediaType = "mediaType",
6470
IIIFLink = iiifLink,
@@ -71,68 +77,87 @@ public void IIIFLink_NotValid(string iiifLink)
7177
}
7278

7379
[Fact]
74-
public void Id_NotMatched()
80+
public void IIIFLink_Null()
7581
{
76-
var adjunct = new Adjunct("https://localhost", 1, 1, "assetId", "adjunctId")
82+
var adjunct = new Adjunct
7783
{
7884
MediaType = "mediaType",
79-
IIIFLink = "seeAlso",
8085
Type = "Image",
81-
Id = "https://localhost/customers/1/spaces/1/images/assetId/adjuncts/different",
8286
ExternalId = "https://localhost/customers/1/spaces/1/images/assetId/valid"
8387
};
8488
var result = sut.TestValidate(adjunct);
85-
result.ShouldHaveValidationErrorFor(r => r)
86-
.WithErrorMessage("'id' and '@id' must have a matching adjunct identifier");
89+
result.ShouldHaveValidationErrorFor(r => r.IIIFLink)
90+
.WithErrorMessage("'iiifLink' is required");
8791
}
8892

8993
[Fact]
90-
public void IIIFLink_Null()
94+
public void MediaType_Null()
9195
{
92-
var adjunct = new Adjunct("https://localhost", 1, 1, "assetId", "adjunctId")
96+
var adjunct = new Adjunct
9397
{
94-
MediaType = "mediaType",
98+
IIIFLink = "seeAlso",
9599
Type = "Image",
96100
ExternalId = "https://localhost/customers/1/spaces/1/images/assetId/valid"
97101
};
98102
var result = sut.TestValidate(adjunct);
99-
result.ShouldHaveValidationErrorFor(r => r.IIIFLink)
100-
.WithErrorMessage("'iiifLink' is required");
103+
result.ShouldHaveValidationErrorFor(r => r.MediaType)
104+
.WithErrorMessage("'mediaType' is required");
101105
}
102106

103107
[Fact]
104-
public void MediaType_Null()
108+
public void ModelId_Null()
105109
{
106-
var adjunct = new Adjunct("https://localhost", 1, 1, "assetId", "adjunctId")
110+
var adjunct = new Adjunct
107111
{
112+
MediaType = "mediaType",
108113
IIIFLink = "seeAlso",
109114
Type = "Image",
110115
ExternalId = "https://localhost/customers/1/spaces/1/images/assetId/valid"
111116
};
112117
var result = sut.TestValidate(adjunct);
113-
result.ShouldHaveValidationErrorFor(r => r.MediaType)
114-
.WithErrorMessage("'mediaType' is required");
118+
result.ShouldHaveValidationErrorFor(r => r.ModelId)
119+
.WithErrorMessage("Adjunct identifier could not be found");
120+
}
121+
122+
[Theory]
123+
[InlineData(" space")]
124+
[InlineData("slash\\")]
125+
[InlineData("other/slash")]
126+
public void ModelId_InvalidCharacters(string modelId)
127+
{
128+
var adjunct = new Adjunct
129+
{
130+
MediaType = "mediaType",
131+
IIIFLink = "seeAlso",
132+
Type = "Image",
133+
ExternalId = "https://localhost/customers/1/spaces/1/images/assetId/valid",
134+
ModelId = modelId
135+
};
136+
var result = sut.TestValidate(adjunct);
137+
result.ShouldHaveValidationErrorFor(r => r.ModelId)
138+
.WithErrorMessage("Adjunct id contains at least one of the following restricted characters. Invalid values are: \\ /");
115139
}
116140

117141
[Fact]
118-
public void ModelId_Null_WhenCreate()
142+
public void ModelId_TooLong()
119143
{
120-
var adjunct = new Adjunct("https://localhost", 1, 1, "assetId", null)
144+
var adjunct = new Adjunct
121145
{
122146
MediaType = "mediaType",
123147
IIIFLink = "seeAlso",
124148
Type = "Image",
125-
ExternalId = "https://localhost/customers/1/spaces/1/images/assetId/valid"
149+
ExternalId = "https://localhost/customers/1/spaces/1/images/assetId/valid",
150+
ModelId = new string('a', 201),
126151
};
127152
var result = sut.TestValidate(adjunct);
128153
result.ShouldHaveValidationErrorFor(r => r.ModelId)
129-
.WithErrorMessage("Adjunct identifier could not be found");
154+
.WithErrorMessage("Adjunct id must be 200 characters or less");
130155
}
131156

132157
[Fact]
133158
public void Type_Error_WhenNotSet()
134159
{
135-
var adjunct = new Adjunct("https://localhost", 1, 1, "assetId", null)
160+
var adjunct = new Adjunct
136161
{
137162
MediaType = "mediaType",
138163
IIIFLink = "seeAlso",
@@ -143,4 +168,20 @@ public void Type_Error_WhenNotSet()
143168
result.ShouldHaveValidationErrorFor(r => r.Type)
144169
.WithErrorMessage("'@type' is required");
145170
}
171+
172+
[Fact]
173+
public void Language_Error_TooLong()
174+
{
175+
var adjunct = new Adjunct
176+
{
177+
MediaType = "mediaType",
178+
IIIFLink = "seeAlso",
179+
Type = "AnnotationPage",
180+
Language = ["en", "an overly long language string"],
181+
ExternalId = "https://localhost/customers/1/spaces/1/images/assetId"
182+
};
183+
var result = sut.TestValidate(adjunct);
184+
result.ShouldHaveValidationErrorFor(r => r.Language)
185+
.WithErrorMessage("All 'language' values must be 10 characters or less. e.g. ISO language codes, sub-codes or 'none'");
186+
}
146187
}

src/protagonist/API.Tests/Features/Assets/ApiAssetRepositoryTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,7 @@ public void AssetRepository_FailsToSaveAsset_WhichHasRestrictedCharacters()
304304

305305
var result = AssetPreparer.PrepareAssetForUpsert(null, newAsset, false, false, new []{' '});
306306
result.Success.Should().BeFalse();
307-
result.ErrorMessage.Should().Be("Asset id contains at least one of the following restricted characters. Valid values are: ");
307+
result.ErrorMessage.Should().Be("Asset id contains at least one of the following restricted characters. Invalid values are: ");
308308
}
309309

310310
[Fact]

src/protagonist/API.Tests/Features/Images/Validation/HydraImageValidatorTests.cs

Lines changed: 3 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,14 @@ namespace API.Tests.Features.Images.Validation;
88

99
public class HydraImageValidatorTests
1010
{
11-
public HydraImageValidator GetSut()
12-
{
13-
var apiSettings = new ApiSettings()
14-
{
15-
RestrictedAssetIdCharacterString = "\\ "
16-
};
17-
return new HydraImageValidator();
18-
}
19-
11+
private HydraImageValidator sut => new();
12+
2013
[Theory]
2114
[InlineData(null)]
2215
[InlineData("")]
2316
[InlineData(" ")]
2417
public void MediaType_NullOrEmpty_OnCreate(string mediaType)
2518
{
26-
var sut = GetSut();
2719
var model = new Image { MediaType = mediaType };
2820
var result = sut.TestValidate(model, options => options.IncludeRuleSets("default", "create"));
2921
result.ShouldHaveValidationErrorFor(a => a.MediaType);
@@ -32,7 +24,6 @@ public void MediaType_NullOrEmpty_OnCreate(string mediaType)
3224
[Fact]
3325
public void Batch_Provided()
3426
{
35-
var sut = GetSut();
3627
var model = new Image { Batch = "10" };
3728
var result = sut.TestValidate(model);
3829
result.ShouldHaveValidationErrorFor(a => a.Batch);
@@ -41,7 +32,6 @@ public void Batch_Provided()
4132
[Fact]
4233
public void Width_Provided()
4334
{
44-
var sut = GetSut();
4535
var model = new Image { Width = 10 };
4636
var result = sut.TestValidate(model);
4737
result
@@ -52,7 +42,6 @@ public void Width_Provided()
5242
[Fact]
5343
public void Height_Provided()
5444
{
55-
var sut = GetSut();
5645
var model = new Image { Height = 10 };
5746
var result = sut.TestValidate(model);
5847
result
@@ -63,7 +52,6 @@ public void Height_Provided()
6352
[Fact]
6453
public void Duration_Provided()
6554
{
66-
var sut = GetSut();
6755
var model = new Image { Duration = 10 };
6856
var result = sut.TestValidate(model);
6957
result
@@ -74,7 +62,6 @@ public void Duration_Provided()
7462
[Fact]
7563
public void Finished_Provided()
7664
{
77-
var sut = GetSut();
7865
var model = new Image { Finished = DateTime.Today };
7966
var result = sut.TestValidate(model);
8067
result.ShouldHaveValidationErrorFor(a => a.Finished);
@@ -83,7 +70,6 @@ public void Finished_Provided()
8370
[Fact]
8471
public void Created_Provided()
8572
{
86-
var sut = GetSut();
8773
var model = new Image { Created = DateTime.Today };
8874
var result = sut.TestValidate(model);
8975
result.ShouldHaveValidationErrorFor(a => a.Created);
@@ -92,7 +78,6 @@ public void Created_Provided()
9278
[Fact]
9379
public void DeliveryChannel_ValidationError_DeliveryChannelMissingChannel()
9480
{
95-
var sut = GetSut();
9681
var model = new Image { DeliveryChannels = new[]
9782
{
9883
new DeliveryChannel()
@@ -111,7 +96,6 @@ public void DeliveryChannel_ValidationError_DeliveryChannelMissingChannel()
11196
[Fact]
11297
public void DeliveryChannel_ValidationError_WhenNoneAndMoreDeliveryChannels()
11398
{
114-
var sut = GetSut();
11599
var model = new Image { DeliveryChannels = new[]
116100
{
117101
new DeliveryChannel()
@@ -130,7 +114,6 @@ public void DeliveryChannel_ValidationError_WhenNoneAndMoreDeliveryChannels()
130114
[Fact]
131115
public void DeliveryChannel_ValidationError_WhenDefaultAndMoreDeliveryChannels()
132116
{
133-
var sut = GetSut();
134117
var model = new Image { DeliveryChannels = new[]
135118
{
136119
new DeliveryChannel()
@@ -149,7 +132,6 @@ public void DeliveryChannel_ValidationError_WhenDefaultAndMoreDeliveryChannels()
149132
[Fact]
150133
public void DeliveryChannel_NoValidationError_WhenDeliveryChannelsWithNoNone()
151134
{
152-
var sut = GetSut();
153135
var model = new Image { DeliveryChannels = new[]
154136
{
155137
new DeliveryChannel()
@@ -168,7 +150,6 @@ public void DeliveryChannel_NoValidationError_WhenDeliveryChannelsWithNoNone()
168150
[Fact]
169151
public void DeliveryChannel_NoValidationError_WhenOnlyNone()
170152
{
171-
var sut = GetSut();
172153
var model = new Image { DeliveryChannels = new[]
173154
{
174155
new DeliveryChannel()
@@ -195,7 +176,6 @@ public void DeliveryChannel_NoValidationError_WhenOnlyNone()
195176
[InlineData("application/pdf", "none")]
196177
public void DeliveryChannel_NoValidationError_WhenChannelValidForMediaType(string mediaType, string channel)
197178
{
198-
var sut = GetSut();
199179
var model = new Image {
200180
MediaType = mediaType,
201181
DeliveryChannels = new[]
@@ -218,7 +198,6 @@ public void DeliveryChannel_NoValidationError_WhenChannelValidForMediaType(strin
218198
[InlineData("application/pdf", "iiif-av")]
219199
public void DeliveryChannel_ValidationError_WhenWrongChannelForMediaType(string mediaType, string channel)
220200
{
221-
var sut = GetSut();
222201
var model = new Image {
223202
MediaType = mediaType,
224203
DeliveryChannels = new[]
@@ -235,7 +214,6 @@ public void DeliveryChannel_ValidationError_WhenWrongChannelForMediaType(string
235214
[Fact]
236215
public void DeliveryChannel_ValidationError_WhenEmpty_OnPatch()
237216
{
238-
var sut = GetSut();
239217
var model = new Image
240218
{
241219
DeliveryChannels = Array.Empty<DeliveryChannel>()
@@ -248,7 +226,6 @@ public void DeliveryChannel_ValidationError_WhenEmpty_OnPatch()
248226
[Fact]
249227
public void ImageOptimisationPolicy_ValidationError()
250228
{
251-
var sut = GetSut();
252229
var model = new Image
253230
{
254231
ImageOptimisationPolicy = "some-iop-policy"
@@ -260,12 +237,11 @@ public void ImageOptimisationPolicy_ValidationError()
260237
[Fact]
261238
public void ThumbnailPolicy_ValidationError()
262239
{
263-
var sut = GetSut();
264240
var model = new Image
265241
{
266242
ThumbnailPolicy = "some-tp-policy"
267243
};
268244
var result = sut.TestValidate(model);
269245
result.ShouldHaveValidationErrorFor(a => a.ThumbnailPolicy);
270246
}
271-
}
247+
}

0 commit comments

Comments
 (0)