@@ -26,7 +26,7 @@ public ModelStateValidationTests(IntegrationTestContext<OpenApiStartup<ModelStat
26
26
}
27
27
28
28
[ Fact ]
29
- public async Task xxx ( )
29
+ public async Task Cannot_violate_compare_constraint ( )
30
30
{
31
31
// Arrange
32
32
SocialMediaAccount socialMediaAccount = _fakers . SocialMediaAccount . Generate ( ) ;
@@ -39,23 +39,66 @@ public async Task xxx()
39
39
{
40
40
Data = new SocialMediaAccountDataInPostRequest
41
41
{
42
- Attributes = new SocialMediaAccountAttributesInPostRequest ( )
42
+ Attributes = new SocialMediaAccountAttributesInPostRequest
43
+ {
44
+ FirstName = socialMediaAccount . FirstName ,
45
+ GivenName = "something else" ,
46
+ LastName = ""
47
+ }
48
+ }
49
+ } ) ;
50
+
51
+ // Assert
52
+ ErrorResponseDocument document = ( await action . Should ( ) . ThrowExactlyAsync < ApiException < ErrorResponseDocument > > ( ) ) . Which . Result ;
53
+ document . Errors . ShouldHaveCount ( 1 ) ;
54
+
55
+ ErrorObject errorObject = document . Errors . First ( ) ;
56
+ errorObject . Title . Should ( ) . Be ( "Input validation failed." ) ;
57
+ errorObject . Detail . Should ( ) . Be ( "'GivenName' and 'FirstName' do not match." ) ;
58
+ errorObject . Source . ShouldNotBeNull ( ) ;
59
+ errorObject . Source . Pointer . Should ( ) . Be ( "/data/attributes/givenName" ) ;
60
+ }
61
+
62
+ [ Theory ]
63
+ [ InlineData ( "a" ) ]
64
+ [ InlineData ( "abcdefghijklmnopqrstu" ) ]
65
+ public async Task Cannot_exceed_length_constraint ( string firstName )
66
+ {
67
+ // Arrange
68
+ using HttpClient httpClient = _testContext . Factory . CreateDefaultClient ( _logHttpMessageHandler ) ;
69
+ ModelStateValidationClient apiClient = new ( httpClient ) ;
70
+
71
+ // Act
72
+ Func < Task < SocialMediaAccountPrimaryResponseDocument > > action = ( ) => apiClient . PostSocialMediaAccountAsync ( new SocialMediaAccountPostRequestDocument
73
+ {
74
+ Data = new SocialMediaAccountDataInPostRequest
75
+ {
76
+ Attributes = new SocialMediaAccountAttributesInPostRequest
77
+ {
78
+ FirstName = firstName ,
79
+ GivenName = firstName ,
80
+ LastName = ""
81
+ }
43
82
}
44
83
} ) ;
45
84
46
85
// Assert
47
- ExceptionAssertions < JsonSerializationException > assertion = await action . Should ( ) . ThrowExactlyAsync < JsonSerializationException > ( ) ;
48
- assertion . Which . Message . Should ( ) . Be ( "Cannot write a null value for property 'lastName'. Property requires a value. Path 'data.attributes'." ) ;
86
+ ErrorResponseDocument document = ( await action . Should ( ) . ThrowExactlyAsync < ApiException < ErrorResponseDocument > > ( ) ) . Which . Result ;
87
+ document . Errors . ShouldHaveCount ( 1 ) ;
88
+
89
+ ErrorObject errorObject = document . Errors . First ( ) ;
90
+ errorObject . Title . Should ( ) . Be ( "Input validation failed." ) ;
91
+ errorObject . Detail . Should ( ) . Be ( "The field FirstName must be a string or collection type with a minimum length of '2' and maximum length of '20'." ) ;
92
+ errorObject . Source . ShouldNotBeNull ( ) ;
93
+ errorObject . Source . Pointer . Should ( ) . Be ( "/data/attributes/firstName" ) ;
49
94
}
50
95
51
96
[ Theory ]
52
97
[ InlineData ( "ab" ) ]
53
98
[ InlineData ( "abcdefghijklmnopqrs" ) ]
54
- public async Task Cannot_exceed_length_constraint ( string userName )
99
+ public async Task Cannot_exceed_string_length_constraint ( string userName )
55
100
{
56
101
// Arrange
57
- SocialMediaAccount socialMediaAccount = _fakers . SocialMediaAccount . Generate ( ) ;
58
-
59
102
using HttpClient httpClient = _testContext . Factory . CreateDefaultClient ( _logHttpMessageHandler ) ;
60
103
ModelStateValidationClient apiClient = new ( httpClient ) ;
61
104
@@ -66,8 +109,8 @@ public async Task Cannot_exceed_length_constraint(string userName)
66
109
{
67
110
Attributes = new SocialMediaAccountAttributesInPostRequest
68
111
{
69
- LastName = socialMediaAccount . LastName ,
70
- UserName = userName
112
+ LastName = "" ,
113
+ UserName = userName ,
71
114
}
72
115
}
73
116
} ) ;
@@ -87,8 +130,6 @@ public async Task Cannot_exceed_length_constraint(string userName)
87
130
public async Task Cannot_violate_regular_expression_constraint ( )
88
131
{
89
132
// Arrange
90
- SocialMediaAccount socialMediaAccount = _fakers . SocialMediaAccount . Generate ( ) ;
91
-
92
133
using HttpClient httpClient = _testContext . Factory . CreateDefaultClient ( _logHttpMessageHandler ) ;
93
134
ModelStateValidationClient apiClient = new ( httpClient ) ;
94
135
@@ -99,8 +140,8 @@ public async Task Cannot_violate_regular_expression_constraint()
99
140
{
100
141
Attributes = new SocialMediaAccountAttributesInPostRequest
101
142
{
102
- LastName = socialMediaAccount . LastName ,
103
- UserName = "aB1"
143
+ LastName = "" ,
144
+ UserName = "aB1" ,
104
145
}
105
146
}
106
147
} ) ;
@@ -120,8 +161,6 @@ public async Task Cannot_violate_regular_expression_constraint()
120
161
public async Task Cannot_use_invalid_credit_card ( )
121
162
{
122
163
// Arrange
123
- SocialMediaAccount socialMediaAccount = _fakers . SocialMediaAccount . Generate ( ) ;
124
-
125
164
using HttpClient httpClient = _testContext . Factory . CreateDefaultClient ( _logHttpMessageHandler ) ;
126
165
ModelStateValidationClient apiClient = new ( httpClient ) ;
127
166
@@ -132,8 +171,8 @@ public async Task Cannot_use_invalid_credit_card()
132
171
{
133
172
Attributes = new SocialMediaAccountAttributesInPostRequest
134
173
{
135
- LastName = socialMediaAccount . LastName ,
136
- CreditCard = "123-456"
174
+ LastName = "" ,
175
+ CreditCard = "123-456" ,
137
176
}
138
177
}
139
178
} ) ;
@@ -153,8 +192,6 @@ public async Task Cannot_use_invalid_credit_card()
153
192
public async Task Cannot_use_invalid_email ( )
154
193
{
155
194
// Arrange
156
- SocialMediaAccount socialMediaAccount = _fakers . SocialMediaAccount . Generate ( ) ;
157
-
158
195
using HttpClient httpClient = _testContext . Factory . CreateDefaultClient ( _logHttpMessageHandler ) ;
159
196
ModelStateValidationClient apiClient = new ( httpClient ) ;
160
197
@@ -165,8 +202,8 @@ public async Task Cannot_use_invalid_email()
165
202
{
166
203
Attributes = new SocialMediaAccountAttributesInPostRequest
167
204
{
168
- LastName = socialMediaAccount . LastName ,
169
- Email = "abc"
205
+ LastName = "" ,
206
+ Email = "abc" ,
170
207
}
171
208
}
172
209
} ) ;
@@ -190,8 +227,6 @@ public async Task Cannot_use_invalid_email()
190
227
public async Task Cannot_use_double_outside_of_valid_range ( int age )
191
228
{
192
229
// Arrange
193
- SocialMediaAccount socialMediaAccount = _fakers . SocialMediaAccount . Generate ( ) ;
194
-
195
230
using HttpClient httpClient = _testContext . Factory . CreateDefaultClient ( _logHttpMessageHandler ) ;
196
231
ModelStateValidationClient apiClient = new ( httpClient ) ;
197
232
@@ -202,8 +237,8 @@ public async Task Cannot_use_double_outside_of_valid_range(int age)
202
237
{
203
238
Attributes = new SocialMediaAccountAttributesInPostRequest
204
239
{
205
- LastName = socialMediaAccount . LastName ,
206
- Age = age
240
+ LastName = "" ,
241
+ Age = age ,
207
242
}
208
243
}
209
244
} ) ;
@@ -214,7 +249,7 @@ public async Task Cannot_use_double_outside_of_valid_range(int age)
214
249
215
250
ErrorObject errorObject = document . Errors . First ( ) ;
216
251
errorObject . Title . Should ( ) . Be ( "Input validation failed." ) ;
217
- errorObject . Detail . Should ( ) . Be ( "The field Age must be between 0 and 123 ." ) ;
252
+ errorObject . Detail . Should ( ) . Be ( "The field Age must be between 0.1 and 122.9 ." ) ;
218
253
errorObject . Source . ShouldNotBeNull ( ) ;
219
254
errorObject . Source . Pointer . Should ( ) . Be ( "/data/attributes/age" ) ;
220
255
}
@@ -223,8 +258,6 @@ public async Task Cannot_use_double_outside_of_valid_range(int age)
223
258
public async Task Cannot_use_invalid_url ( )
224
259
{
225
260
// Arrange
226
- SocialMediaAccount socialMediaAccount = _fakers . SocialMediaAccount . Generate ( ) ;
227
-
228
261
using HttpClient httpClient = _testContext . Factory . CreateDefaultClient ( _logHttpMessageHandler ) ;
229
262
ModelStateValidationClient apiClient = new ( httpClient ) ;
230
263
@@ -235,8 +268,8 @@ public async Task Cannot_use_invalid_url()
235
268
{
236
269
Attributes = new SocialMediaAccountAttributesInPostRequest
237
270
{
238
- LastName = socialMediaAccount . LastName ,
239
- BackgroundPicture = new Uri ( "/justapath" , UriKind . Relative )
271
+ LastName = "" ,
272
+ BackgroundPicture = new Uri ( "/justapath" , UriKind . Relative ) ,
240
273
}
241
274
}
242
275
} ) ;
@@ -253,11 +286,40 @@ public async Task Cannot_use_invalid_url()
253
286
}
254
287
255
288
[ Fact ]
256
- public async Task Cannot_use_TimeSpan_outside_of_valid_range ( )
289
+ public async Task Cannot_use_non_allowed_value ( )
257
290
{
258
291
// Arrange
259
- SocialMediaAccount socialMediaAccount = _fakers . SocialMediaAccount . Generate ( ) ;
292
+ using HttpClient httpClient = _testContext . Factory . CreateDefaultClient ( _logHttpMessageHandler ) ;
293
+ ModelStateValidationClient apiClient = new ( httpClient ) ;
294
+
295
+ // Act
296
+ Func < Task < SocialMediaAccountPrimaryResponseDocument > > action = ( ) => apiClient . PostSocialMediaAccountAsync ( new SocialMediaAccountPostRequestDocument
297
+ {
298
+ Data = new SocialMediaAccountDataInPostRequest
299
+ {
300
+ Attributes = new SocialMediaAccountAttributesInPostRequest
301
+ {
302
+ LastName = "" ,
303
+ CountryCode = "US"
304
+ }
305
+ }
306
+ } ) ;
307
+
308
+ // Assert
309
+ ErrorResponseDocument document = ( await action . Should ( ) . ThrowExactlyAsync < ApiException < ErrorResponseDocument > > ( ) ) . Which . Result ;
310
+ document . Errors . ShouldHaveCount ( 1 ) ;
311
+
312
+ ErrorObject errorObject = document . Errors . First ( ) ;
313
+ errorObject . Title . Should ( ) . Be ( "Input validation failed." ) ;
314
+ errorObject . Detail . Should ( ) . Be ( "The CountryCode field does not equal any of the values specified in AllowedValuesAttribute." ) ;
315
+ errorObject . Source . ShouldNotBeNull ( ) ;
316
+ errorObject . Source . Pointer . Should ( ) . Be ( "/data/attributes/countryCode" ) ;
317
+ }
260
318
319
+ [ Fact ]
320
+ public async Task Cannot_use_denied_value ( )
321
+ {
322
+ // Arrange
261
323
using HttpClient httpClient = _testContext . Factory . CreateDefaultClient ( _logHttpMessageHandler ) ;
262
324
ModelStateValidationClient apiClient = new ( httpClient ) ;
263
325
@@ -268,8 +330,39 @@ public async Task Cannot_use_TimeSpan_outside_of_valid_range()
268
330
{
269
331
Attributes = new SocialMediaAccountAttributesInPostRequest
270
332
{
271
- LastName = socialMediaAccount . LastName ,
272
- NextRevalidation = "00:00:01"
333
+ LastName = "" ,
334
+ Planet = "pluto"
335
+ }
336
+ }
337
+ } ) ;
338
+
339
+ // Assert
340
+ ErrorResponseDocument document = ( await action . Should ( ) . ThrowExactlyAsync < ApiException < ErrorResponseDocument > > ( ) ) . Which . Result ;
341
+ document . Errors . ShouldHaveCount ( 1 ) ;
342
+
343
+ ErrorObject errorObject = document . Errors . First ( ) ;
344
+ errorObject . Title . Should ( ) . Be ( "Input validation failed." ) ;
345
+ errorObject . Detail . Should ( ) . Be ( "The Planet field equals one of the values specified in DeniedValuesAttribute." ) ;
346
+ errorObject . Source . ShouldNotBeNull ( ) ;
347
+ errorObject . Source . Pointer . Should ( ) . Be ( "/data/attributes/planet" ) ;
348
+ }
349
+
350
+ [ Fact ]
351
+ public async Task Cannot_use_TimeSpan_outside_of_valid_range ( )
352
+ {
353
+ // Arrange
354
+ using HttpClient httpClient = _testContext . Factory . CreateDefaultClient ( _logHttpMessageHandler ) ;
355
+ ModelStateValidationClient apiClient = new ( httpClient ) ;
356
+
357
+ // Act
358
+ Func < Task < SocialMediaAccountPrimaryResponseDocument > > action = ( ) => apiClient . PostSocialMediaAccountAsync ( new SocialMediaAccountPostRequestDocument
359
+ {
360
+ Data = new SocialMediaAccountDataInPostRequest
361
+ {
362
+ Attributes = new SocialMediaAccountAttributesInPostRequest
363
+ {
364
+ LastName = "" ,
365
+ NextRevalidation = "00:00:01" ,
273
366
}
274
367
}
275
368
} ) ;
@@ -289,8 +382,6 @@ public async Task Cannot_use_TimeSpan_outside_of_valid_range()
289
382
public async Task Cannot_use_invalid_TimeOnly ( )
290
383
{
291
384
// Arrange
292
- SocialMediaAccount socialMediaAccount = _fakers . SocialMediaAccount . Generate ( ) ;
293
-
294
385
using HttpClient httpClient = _testContext . Factory . CreateDefaultClient ( _logHttpMessageHandler ) ;
295
386
ModelStateValidationClient apiClient = new ( httpClient ) ;
296
387
@@ -301,8 +392,8 @@ public async Task Cannot_use_invalid_TimeOnly()
301
392
{
302
393
Attributes = new SocialMediaAccountAttributesInPostRequest
303
394
{
304
- LastName = socialMediaAccount . LastName ,
305
- ValidatedAtTime = TimeSpan . FromSeconds ( - 1 )
395
+ LastName = "" ,
396
+ ValidatedAtTime = TimeSpan . FromSeconds ( - 1 ) ,
306
397
}
307
398
}
308
399
} ) ;
@@ -344,6 +435,8 @@ public async Task Can_create_resource_with_valid_properties()
344
435
Age = socialMediaAccount . Age ,
345
436
ProfilePicture = socialMediaAccount . ProfilePicture ,
346
437
BackgroundPicture = new Uri ( socialMediaAccount . BackgroundPicture ! ) ,
438
+ Tags = socialMediaAccount . Tags ,
439
+ Planet = socialMediaAccount . Planet ,
347
440
NextRevalidation = "02:00:00" ,
348
441
ValidatedAt = socialMediaAccount . ValidatedAt ! ,
349
442
ValidatedAtDate = new DateTimeOffset ( socialMediaAccount . ValidatedAtDate ! . Value . ToDateTime ( new TimeOnly ( ) ) . ToUniversalTime ( ) ) ,
0 commit comments