11using API . Features . Adjuncts . Validation ;
2+ using API . Settings ;
23using DLCS . HydraModel ;
34using FluentValidation . TestHelper ;
5+ using Microsoft . Extensions . Options ;
46
57namespace API . Tests . Features . Adjuncts . Validation ;
68
79public 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}
0 commit comments