8
8
using TestBuildingBlocks ;
9
9
using Xunit ;
10
10
11
- namespace OpenApiEndToEndTests . ModelValidation ;
11
+ namespace OpenApiNSwagEndToEndTests . ModelValidation ;
12
12
13
13
public sealed class ModelValidationTests : IClassFixture < IntegrationTestContext < OpenApiStartup < ModelValidationDbContext > , ModelValidationDbContext > >
14
14
{
@@ -26,7 +26,7 @@ public ModelValidationTests(IntegrationTestContext<OpenApiStartup<ModelValidatio
26
26
public async Task Omitting_a_required_attribute_should_return_an_error ( )
27
27
{
28
28
using HttpClient httpClient = _testContext . Factory . CreateClient ( ) ;
29
- var apiClient = new ModelValidationClient ( httpClient ) ;
29
+ ModelValidationClient apiClient = new ( httpClient ) ;
30
30
31
31
// Act
32
32
Func < Task < FingerprintPrimaryResponseDocument > > action = ( ) => apiClient . PostFingerprintAsync ( null , new FingerprintPostRequestDocument
@@ -47,13 +47,13 @@ public async Task Omitting_a_required_attribute_should_return_an_error()
47
47
[ Theory ]
48
48
[ InlineData ( "ab" ) ]
49
49
[ InlineData ( "abcdefghijklmnopqrs" ) ]
50
- public async Task imbadathis ( string userName )
50
+ public async Task Not_fitting_the_length_constraint_should_return_an_error ( string userName )
51
51
{
52
52
// Arrange
53
53
Fingerprint fingerprint = _fakers . Fingerprint . Generate ( ) ;
54
54
55
55
using HttpClient httpClient = _testContext . Factory . CreateClient ( ) ;
56
- var apiClient = new ModelValidationClient ( httpClient ) ;
56
+ ModelValidationClient apiClient = new ( httpClient ) ;
57
57
58
58
// Act
59
59
Func < Task < FingerprintPrimaryResponseDocument > > action = ( ) => apiClient . PostFingerprintAsync ( null , new FingerprintPostRequestDocument
@@ -80,13 +80,13 @@ public async Task imbadathis(string userName)
80
80
}
81
81
82
82
[ Fact ]
83
- public async Task imbadathis2 ( )
83
+ public async Task Not_matching_a_regex_should_return_an_error ( )
84
84
{
85
85
// Arrange
86
86
Fingerprint fingerprint = _fakers . Fingerprint . Generate ( ) ;
87
87
88
88
using HttpClient httpClient = _testContext . Factory . CreateClient ( ) ;
89
- var apiClient = new ModelValidationClient ( httpClient ) ;
89
+ ModelValidationClient apiClient = new ( httpClient ) ;
90
90
91
91
// Act
92
92
Func < Task < FingerprintPrimaryResponseDocument > > action = ( ) => apiClient . PostFingerprintAsync ( null , new FingerprintPostRequestDocument
@@ -113,13 +113,13 @@ public async Task imbadathis2()
113
113
}
114
114
115
115
[ Fact ]
116
- public async Task imbadathis3 ( )
116
+ public async Task Invalid_credit_card_should_return_an_error ( )
117
117
{
118
118
// Arrange
119
119
Fingerprint fingerprint = _fakers . Fingerprint . Generate ( ) ;
120
120
121
121
using HttpClient httpClient = _testContext . Factory . CreateClient ( ) ;
122
- var apiClient = new ModelValidationClient ( httpClient ) ;
122
+ ModelValidationClient apiClient = new ( httpClient ) ;
123
123
124
124
// Act
125
125
Func < Task < FingerprintPrimaryResponseDocument > > action = ( ) => apiClient . PostFingerprintAsync ( null , new FingerprintPostRequestDocument
@@ -146,13 +146,13 @@ public async Task imbadathis3()
146
146
}
147
147
148
148
[ Fact ]
149
- public async Task imbadathis5 ( )
149
+ public async Task Invalid_email_should_return_an_error ( )
150
150
{
151
151
// Arrange
152
152
Fingerprint fingerprint = _fakers . Fingerprint . Generate ( ) ;
153
153
154
154
using HttpClient httpClient = _testContext . Factory . CreateClient ( ) ;
155
- var apiClient = new ModelValidationClient ( httpClient ) ;
155
+ ModelValidationClient apiClient = new ( httpClient ) ;
156
156
157
157
// Act
158
158
Func < Task < FingerprintPrimaryResponseDocument > > action = ( ) => apiClient . PostFingerprintAsync ( null , new FingerprintPostRequestDocument
@@ -181,13 +181,13 @@ public async Task imbadathis5()
181
181
[ Theory ]
182
182
[ InlineData ( - 1 ) ]
183
183
[ InlineData ( 124 ) ]
184
- public async Task imbadathis6 ( int age )
184
+ public async Task Out_of_range_integer_should_return_an_error ( int age )
185
185
{
186
186
// Arrange
187
187
Fingerprint fingerprint = _fakers . Fingerprint . Generate ( ) ;
188
188
189
189
using HttpClient httpClient = _testContext . Factory . CreateClient ( ) ;
190
- var apiClient = new ModelValidationClient ( httpClient ) ;
190
+ ModelValidationClient apiClient = new ( httpClient ) ;
191
191
192
192
// Act
193
193
Func < Task < FingerprintPrimaryResponseDocument > > action = ( ) => apiClient . PostFingerprintAsync ( null , new FingerprintPostRequestDocument
@@ -214,13 +214,13 @@ public async Task imbadathis6(int age)
214
214
}
215
215
216
216
[ Fact ]
217
- public async Task imbadathis7 ( )
217
+ public async Task Invalid_url_should_return_an_error ( )
218
218
{
219
219
// Arrange
220
220
Fingerprint fingerprint = _fakers . Fingerprint . Generate ( ) ;
221
221
222
222
using HttpClient httpClient = _testContext . Factory . CreateClient ( ) ;
223
- var apiClient = new ModelValidationClient ( httpClient ) ;
223
+ ModelValidationClient apiClient = new ( httpClient ) ;
224
224
225
225
// Act
226
226
Func < Task < FingerprintPrimaryResponseDocument > > action = ( ) => apiClient . PostFingerprintAsync ( null , new FingerprintPostRequestDocument
@@ -247,13 +247,13 @@ public async Task imbadathis7()
247
247
}
248
248
249
249
[ Fact ]
250
- public async Task imbadathis8 ( )
250
+ public async Task Invalid_url_as_string_should_return_an_error ( )
251
251
{
252
252
// Arrange
253
253
Fingerprint fingerprint = _fakers . Fingerprint . Generate ( ) ;
254
254
255
255
using HttpClient httpClient = _testContext . Factory . CreateClient ( ) ;
256
- var apiClient = new ModelValidationClient ( httpClient ) ;
256
+ ModelValidationClient apiClient = new ( httpClient ) ;
257
257
258
258
// Act
259
259
Func < Task < FingerprintPrimaryResponseDocument > > action = ( ) => apiClient . PostFingerprintAsync ( null , new FingerprintPostRequestDocument
@@ -263,7 +263,7 @@ public async Task imbadathis8()
263
263
Attributes = new FingerprintAttributesInPostRequest
264
264
{
265
265
LastName = fingerprint . LastName ,
266
- NextRevalidation = new OpenApiNSwagEndToEndTests . ModelValidation . GeneratedCode . TimeSpan { TotalSeconds = 1 }
266
+ BackgroundPicture = new Uri ( "/justapath" , UriKind . Relative )
267
267
}
268
268
}
269
269
} ) ;
@@ -274,19 +274,52 @@ public async Task imbadathis8()
274
274
275
275
ErrorObject errorObject = document . Errors . First ( ) ;
276
276
errorObject . Title . Should ( ) . Be ( "Input validation failed." ) ;
277
- errorObject . Detail . Should ( ) . Be ( "" ) ;
277
+ errorObject . Detail . Should ( ) . Be ( "The BackgroundPicture field is not a valid fully-qualified http, https, or ftp URL." ) ;
278
+ errorObject . Source . ShouldNotBeNull ( ) ;
279
+ errorObject . Source . Pointer . Should ( ) . Be ( "/data/attributes/backgroundPicture" ) ;
280
+ }
281
+
282
+ [ Fact ]
283
+ public async Task Out_of_range_timespan_should_return_an_error ( )
284
+ {
285
+ // Arrange
286
+ Fingerprint fingerprint = _fakers . Fingerprint . Generate ( ) ;
287
+
288
+ using HttpClient httpClient = _testContext . Factory . CreateClient ( ) ;
289
+ ModelValidationClient apiClient = new ( httpClient ) ;
290
+
291
+ // Act
292
+ Func < Task < FingerprintPrimaryResponseDocument > > action = ( ) => apiClient . PostFingerprintAsync ( null , new FingerprintPostRequestDocument
293
+ {
294
+ Data = new FingerprintDataInPostRequest
295
+ {
296
+ Attributes = new FingerprintAttributesInPostRequest
297
+ {
298
+ LastName = fingerprint . LastName ,
299
+ NextRevalidation = "00:00:01" ,
300
+ }
301
+ }
302
+ } ) ;
303
+
304
+ // Assert
305
+ ErrorResponseDocument document = ( await action . Should ( ) . ThrowExactlyAsync < ApiException < ErrorResponseDocument > > ( ) ) . Which . Result ;
306
+ document . Errors . ShouldHaveCount ( 1 ) ;
307
+
308
+ ErrorObject errorObject = document . Errors . First ( ) ;
309
+ errorObject . Title . Should ( ) . Be ( "Input validation failed." ) ;
310
+ errorObject . Detail . Should ( ) . Be ( "The field NextRevalidation must be between 01:00:00 and 05:00:00." ) ;
278
311
errorObject . Source . ShouldNotBeNull ( ) ;
279
312
errorObject . Source . Pointer . Should ( ) . Be ( "/data/attributes/nextRevalidation" ) ;
280
313
}
281
314
282
315
[ Fact ]
283
- public async Task imbadathis10 ( )
316
+ public async Task Invalid_datetime_should_return_an_error ( )
284
317
{
285
318
// Arrange
286
319
Fingerprint fingerprint = _fakers . Fingerprint . Generate ( ) ;
287
320
288
321
using HttpClient httpClient = _testContext . Factory . CreateClient ( ) ;
289
- var apiClient = new ModelValidationClient ( httpClient ) ;
322
+ ModelValidationClient apiClient = new ( httpClient ) ;
290
323
291
324
// Act
292
325
Func < Task < FingerprintPrimaryResponseDocument > > action = ( ) => apiClient . PostFingerprintAsync ( null , new FingerprintPostRequestDocument
@@ -313,13 +346,13 @@ public async Task imbadathis10()
313
346
}
314
347
315
348
[ Fact ]
316
- public async Task imbadathis11 ( )
349
+ public async Task Invalid_date_should_return_an_error ( )
317
350
{
318
351
// Arrange
319
352
Fingerprint fingerprint = _fakers . Fingerprint . Generate ( ) ;
320
353
321
354
using HttpClient httpClient = _testContext . Factory . CreateClient ( ) ;
322
- var apiClient = new ModelValidationClient ( httpClient ) ;
355
+ ModelValidationClient apiClient = new ( httpClient ) ;
323
356
324
357
// Act
325
358
Func < Task < FingerprintPrimaryResponseDocument > > action = ( ) => apiClient . PostFingerprintAsync ( null , new FingerprintPostRequestDocument
@@ -346,13 +379,13 @@ public async Task imbadathis11()
346
379
}
347
380
348
381
[ Fact ]
349
- public async Task imbadathis9 ( )
382
+ public async Task Invalid_time_only_should_return_an_error ( )
350
383
{
351
384
// Arrange
352
385
Fingerprint fingerprint = _fakers . Fingerprint . Generate ( ) ;
353
386
354
387
using HttpClient httpClient = _testContext . Factory . CreateClient ( ) ;
355
- var apiClient = new ModelValidationClient ( httpClient ) ;
388
+ ModelValidationClient apiClient = new ( httpClient ) ;
356
389
357
390
// Act
358
391
Func < Task < FingerprintPrimaryResponseDocument > > action = ( ) => apiClient . PostFingerprintAsync ( null , new FingerprintPostRequestDocument
@@ -362,7 +395,7 @@ public async Task imbadathis9()
362
395
Attributes = new FingerprintAttributesInPostRequest
363
396
{
364
397
LastName = fingerprint . LastName ,
365
- ValidatedTimeAt = System . TimeSpan . FromSeconds ( - 1 )
398
+ ValidatedTimeAt = TimeSpan . FromSeconds ( - 1 )
366
399
}
367
400
}
368
401
} ) ;
@@ -372,9 +405,46 @@ public async Task imbadathis9()
372
405
document . Errors . ShouldHaveCount ( 1 ) ;
373
406
374
407
ErrorObject errorObject = document . Errors . First ( ) ;
375
- errorObject . Title . Should ( ) . Be ( "Input validation failed ." ) ;
376
- errorObject . Detail . Should ( ) . Be ( "" ) ;
408
+ errorObject . Title . Should ( ) . Be ( "Failed to deserialize request body: Incompatible attribute value found ." ) ;
409
+ errorObject . Detail . Should ( ) . Be ( "Failed to convert attribute 'validatedTimeAt' with value '-00:00:01' of type 'String' to type 'Nullable<TimeOnly>'. " ) ;
377
410
errorObject . Source . ShouldNotBeNull ( ) ;
378
- errorObject . Source . Pointer . Should ( ) . Be ( "/data/attributes/" ) ;
411
+ errorObject . Source . Pointer . Should ( ) . Be ( "/data/attributes/validatedTimeAt" ) ;
412
+ }
413
+
414
+ [ Fact ]
415
+ public async Task Fitting_all_the_constraints_should_work ( )
416
+ {
417
+ // Arrange
418
+ Fingerprint fingerprint = _fakers . Fingerprint . Generate ( ) ;
419
+
420
+ using HttpClient httpClient = _testContext . Factory . CreateClient ( ) ;
421
+ ModelValidationClient apiClient = new ( httpClient ) ;
422
+
423
+ // Act
424
+ Func < Task < FingerprintPrimaryResponseDocument > > action = ( ) => apiClient . PostFingerprintAsync ( null , new FingerprintPostRequestDocument
425
+ {
426
+ Data = new FingerprintDataInPostRequest
427
+ {
428
+ Attributes = new FingerprintAttributesInPostRequest
429
+ {
430
+ FirstName = fingerprint . FirstName ,
431
+ LastName = fingerprint . LastName ,
432
+ UserName = fingerprint . UserName ,
433
+ CreditCard = fingerprint . CreditCard ,
434
+ Email = fingerprint . Email ,
435
+ Phone = fingerprint . Phone ,
436
+ Age = fingerprint . Age ,
437
+ ProfilePicture = fingerprint . ProfilePicture ,
438
+ BackgroundPicture = new Uri ( fingerprint . BackgroundPicture ! ) ,
439
+ NextRevalidation = "02:00:00" ,
440
+ ValidatedAt = fingerprint . ValidatedAt ! . Value . ToUniversalTime ( ) ,
441
+ // TODO: ValidatedDateAt = new DateTimeOffset(fingerprint.ValidatedDateAt!.Value.ToDateTime(new TimeOnly()).ToUniversalTime()),
442
+ ValidatedTimeAt = fingerprint . ValidatedTimeAt ! . Value . ToTimeSpan ( )
443
+ }
444
+ }
445
+ } ) ;
446
+
447
+ // Assert
448
+ await action . Should ( ) . NotThrowAsync ( ) ;
379
449
}
380
450
}
0 commit comments