|
| 1 | +using API.Features.Adjuncts.Validation; |
| 2 | +using API.Settings; |
| 3 | +using DLCS.HydraModel; |
| 4 | +using FluentValidation.TestHelper; |
| 5 | +using Microsoft.Extensions.Options; |
| 6 | + |
| 7 | +namespace API.Tests.Features.Adjuncts.Validation; |
| 8 | + |
| 9 | +public class HydraAdjunctValidatorTests |
| 10 | +{ |
| 11 | + private readonly HydraAdjunctValidator sut = new(Options.Create( |
| 12 | + new ApiSettings |
| 13 | + { |
| 14 | + RestrictedResourceIdCharacterString = "\\ /" |
| 15 | + })); |
| 16 | + |
| 17 | + [Fact] |
| 18 | + public void Valid_WhenAllValidatorsUsed() |
| 19 | + { |
| 20 | + var adjunct = new Adjunct("https://localhost", 1, 1, "assetId", "adjunctId") |
| 21 | + { |
| 22 | + ExternalId = "https://localhost:2000/some-id", |
| 23 | + MediaType = "mediaType", |
| 24 | + IIIFLink = "seeAlso", |
| 25 | + Type = "Image", |
| 26 | + Language = ["fra", "en"] |
| 27 | + }; |
| 28 | + var result = sut.TestValidate(adjunct); |
| 29 | + result.ShouldNotHaveAnyValidationErrors(); |
| 30 | + } |
| 31 | + |
| 32 | + [Fact] |
| 33 | + public void ExternalId_NotValidUri() |
| 34 | + { |
| 35 | + var adjunct = new Adjunct("https://localhost", 1, 1, "assetId", "adjunctId") |
| 36 | + { |
| 37 | + ExternalId = "not-uri", |
| 38 | + MediaType = "mediaType", |
| 39 | + IIIFLink = "seeAlso", |
| 40 | + Type = "Image" |
| 41 | + }; |
| 42 | + var result = sut.TestValidate(adjunct); |
| 43 | + result.ShouldHaveValidationErrorFor(r => r.ExternalId) |
| 44 | + .WithErrorMessage("'externalId' is required and must be a well formed URI"); |
| 45 | + } |
| 46 | + |
| 47 | + [Fact] |
| 48 | + public void ExternalId_Null() |
| 49 | + { |
| 50 | + var adjunct = new Adjunct |
| 51 | + { |
| 52 | + ExternalId = null, |
| 53 | + MediaType = "mediaType", |
| 54 | + IIIFLink = "seeAlso", |
| 55 | + Type = "Image" |
| 56 | + }; |
| 57 | + var result = sut.TestValidate(adjunct); |
| 58 | + result.ShouldHaveValidationErrorFor(r => r.ExternalId) |
| 59 | + .WithErrorMessage("'externalId' is required and must be a well formed URI"); |
| 60 | + } |
| 61 | + |
| 62 | + [Theory] |
| 63 | + [InlineData("SeeAlso")] |
| 64 | + [InlineData("Invalid")] |
| 65 | + public void IIIFLink_NotValid(string iiifLink) |
| 66 | + { |
| 67 | + var adjunct = new Adjunct |
| 68 | + { |
| 69 | + MediaType = "mediaType", |
| 70 | + IIIFLink = iiifLink, |
| 71 | + Type = "Image", |
| 72 | + ExternalId = "https://localhost/customers/1/spaces/1/images/assetId/valid" |
| 73 | + }; |
| 74 | + var result = sut.TestValidate(adjunct); |
| 75 | + result.ShouldHaveValidationErrorFor(r => r.IIIFLink) |
| 76 | + .WithErrorMessage("Valid values for 'iiifLink' are 'seeAlso', 'annotations', 'rendering'"); |
| 77 | + } |
| 78 | + |
| 79 | + [Fact] |
| 80 | + public void IIIFLink_Null() |
| 81 | + { |
| 82 | + var adjunct = new Adjunct |
| 83 | + { |
| 84 | + MediaType = "mediaType", |
| 85 | + Type = "Image", |
| 86 | + ExternalId = "https://localhost/customers/1/spaces/1/images/assetId/valid" |
| 87 | + }; |
| 88 | + var result = sut.TestValidate(adjunct); |
| 89 | + result.ShouldHaveValidationErrorFor(r => r.IIIFLink) |
| 90 | + .WithErrorMessage("'iiifLink' is required"); |
| 91 | + } |
| 92 | + |
| 93 | + [Fact] |
| 94 | + public void MediaType_Null() |
| 95 | + { |
| 96 | + var adjunct = new Adjunct |
| 97 | + { |
| 98 | + IIIFLink = "seeAlso", |
| 99 | + Type = "Image", |
| 100 | + ExternalId = "https://localhost/customers/1/spaces/1/images/assetId/valid" |
| 101 | + }; |
| 102 | + var result = sut.TestValidate(adjunct); |
| 103 | + result.ShouldHaveValidationErrorFor(r => r.MediaType) |
| 104 | + .WithErrorMessage("'mediaType' is required"); |
| 105 | + } |
| 106 | + |
| 107 | + [Fact] |
| 108 | + public void ModelId_Null() |
| 109 | + { |
| 110 | + var adjunct = new Adjunct |
| 111 | + { |
| 112 | + MediaType = "mediaType", |
| 113 | + IIIFLink = "seeAlso", |
| 114 | + Type = "Image", |
| 115 | + ExternalId = "https://localhost/customers/1/spaces/1/images/assetId/valid" |
| 116 | + }; |
| 117 | + var result = sut.TestValidate(adjunct); |
| 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: \\ /"); |
| 139 | + } |
| 140 | + |
| 141 | + [Fact] |
| 142 | + public void ModelId_TooLong() |
| 143 | + { |
| 144 | + var adjunct = new Adjunct |
| 145 | + { |
| 146 | + MediaType = "mediaType", |
| 147 | + IIIFLink = "seeAlso", |
| 148 | + Type = "Image", |
| 149 | + ExternalId = "https://localhost/customers/1/spaces/1/images/assetId/valid", |
| 150 | + ModelId = new string('a', 201), |
| 151 | + }; |
| 152 | + var result = sut.TestValidate(adjunct); |
| 153 | + result.ShouldHaveValidationErrorFor(r => r.ModelId) |
| 154 | + .WithErrorMessage("Adjunct id must be 200 characters or less"); |
| 155 | + } |
| 156 | + |
| 157 | + [Fact] |
| 158 | + public void Type_Error_WhenNotSet() |
| 159 | + { |
| 160 | + var adjunct = new Adjunct |
| 161 | + { |
| 162 | + MediaType = "mediaType", |
| 163 | + IIIFLink = "seeAlso", |
| 164 | + Type = null, |
| 165 | + ExternalId = "https://localhost/customers/1/spaces/1/images/assetId" |
| 166 | + }; |
| 167 | + var result = sut.TestValidate(adjunct); |
| 168 | + result.ShouldHaveValidationErrorFor(r => r.Type) |
| 169 | + .WithErrorMessage("'@type' is required"); |
| 170 | + } |
| 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 | + } |
| 187 | +} |
0 commit comments