Skip to content

Commit 9dee70f

Browse files
committed
wip
1 parent e2a1ac4 commit 9dee70f

File tree

6 files changed

+424
-119
lines changed

6 files changed

+424
-119
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,380 @@
1+
using FluentAssertions;
2+
using FluentAssertions.Specialized;
3+
using JsonApiDotNetCore.OpenApi.Client.NSwag;
4+
using Newtonsoft.Json;
5+
using OpenApiNSwagEndToEndTests.ModelValidation.GeneratedCode;
6+
using OpenApiTests;
7+
using OpenApiTests.ModelValidation;
8+
using TestBuildingBlocks;
9+
using Xunit;
10+
11+
namespace OpenApiEndToEndTests.ModelValidation;
12+
13+
public sealed class ModelValidationTests : IClassFixture<IntegrationTestContext<OpenApiStartup<ModelValidationDbContext>, ModelValidationDbContext>>
14+
{
15+
private readonly IntegrationTestContext<OpenApiStartup<ModelValidationDbContext>, ModelValidationDbContext> _testContext;
16+
private readonly ModelValidationFakers _fakers = new();
17+
18+
public ModelValidationTests(IntegrationTestContext<OpenApiStartup<ModelValidationDbContext>, ModelValidationDbContext> testContext)
19+
{
20+
_testContext = testContext;
21+
22+
testContext.UseController<FingerprintsController>();
23+
}
24+
25+
[Fact]
26+
public async Task Omitting_a_required_attribute_should_return_an_error()
27+
{
28+
using HttpClient httpClient = _testContext.Factory.CreateClient();
29+
var apiClient = new ModelValidationClient(httpClient);
30+
31+
// Act
32+
Func<Task<FingerprintPrimaryResponseDocument>> action = () => apiClient.PostFingerprintAsync(null, new FingerprintPostRequestDocument
33+
{
34+
Data = new FingerprintDataInPostRequest
35+
{
36+
Attributes = new FingerprintAttributesInPostRequest
37+
{
38+
}
39+
}
40+
});
41+
42+
// Assert
43+
ExceptionAssertions<JsonSerializationException> assertion = await action.Should().ThrowExactlyAsync<JsonSerializationException>();
44+
assertion.Which.Message.Should().Be("Cannot write a null value for property 'lastName'. Property requires a value. Path 'data.attributes'.");
45+
}
46+
47+
[Theory]
48+
[InlineData("ab")]
49+
[InlineData("abcdefghijklmnopqrs")]
50+
public async Task imbadathis(string userName)
51+
{
52+
// Arrange
53+
Fingerprint fingerprint = _fakers.Fingerprint.Generate();
54+
55+
using HttpClient httpClient = _testContext.Factory.CreateClient();
56+
var apiClient = new ModelValidationClient(httpClient);
57+
58+
// Act
59+
Func<Task<FingerprintPrimaryResponseDocument>> action = () => apiClient.PostFingerprintAsync(null, new FingerprintPostRequestDocument
60+
{
61+
Data = new FingerprintDataInPostRequest
62+
{
63+
Attributes = new FingerprintAttributesInPostRequest
64+
{
65+
LastName = fingerprint.LastName,
66+
UserName = userName
67+
}
68+
}
69+
});
70+
71+
// Assert
72+
ErrorResponseDocument document = (await action.Should().ThrowExactlyAsync<ApiException<ErrorResponseDocument>>()).Which.Result;
73+
document.Errors.ShouldHaveCount(1);
74+
75+
ErrorObject errorObject = document.Errors.First();
76+
errorObject.Title.Should().Be("Input validation failed.");
77+
errorObject.Detail.Should().Be("The field UserName must be a string with a minimum length of 3 and a maximum length of 18.");
78+
errorObject.Source.ShouldNotBeNull();
79+
errorObject.Source.Pointer.Should().Be("/data/attributes/userName");
80+
}
81+
82+
[Fact]
83+
public async Task imbadathis2()
84+
{
85+
// Arrange
86+
Fingerprint fingerprint = _fakers.Fingerprint.Generate();
87+
88+
using HttpClient httpClient = _testContext.Factory.CreateClient();
89+
var apiClient = new ModelValidationClient(httpClient);
90+
91+
// Act
92+
Func<Task<FingerprintPrimaryResponseDocument>> action = () => apiClient.PostFingerprintAsync(null, new FingerprintPostRequestDocument
93+
{
94+
Data = new FingerprintDataInPostRequest
95+
{
96+
Attributes = new FingerprintAttributesInPostRequest
97+
{
98+
LastName = fingerprint.LastName,
99+
UserName = "aB1"
100+
}
101+
}
102+
});
103+
104+
// Assert
105+
ErrorResponseDocument document = (await action.Should().ThrowExactlyAsync<ApiException<ErrorResponseDocument>>()).Which.Result;
106+
document.Errors.ShouldHaveCount(1);
107+
108+
ErrorObject errorObject = document.Errors.First();
109+
errorObject.Title.Should().Be("Input validation failed.");
110+
errorObject.Detail.Should().Be("Only letters are allowed.");
111+
errorObject.Source.ShouldNotBeNull();
112+
errorObject.Source.Pointer.Should().Be("/data/attributes/userName");
113+
}
114+
115+
[Fact]
116+
public async Task imbadathis3()
117+
{
118+
// Arrange
119+
Fingerprint fingerprint = _fakers.Fingerprint.Generate();
120+
121+
using HttpClient httpClient = _testContext.Factory.CreateClient();
122+
var apiClient = new ModelValidationClient(httpClient);
123+
124+
// Act
125+
Func<Task<FingerprintPrimaryResponseDocument>> action = () => apiClient.PostFingerprintAsync(null, new FingerprintPostRequestDocument
126+
{
127+
Data = new FingerprintDataInPostRequest
128+
{
129+
Attributes = new FingerprintAttributesInPostRequest
130+
{
131+
LastName = fingerprint.LastName,
132+
CreditCard = "123-456"
133+
}
134+
}
135+
});
136+
137+
// Assert
138+
ErrorResponseDocument document = (await action.Should().ThrowExactlyAsync<ApiException<ErrorResponseDocument>>()).Which.Result;
139+
document.Errors.ShouldHaveCount(1);
140+
141+
ErrorObject errorObject = document.Errors.First();
142+
errorObject.Title.Should().Be("Input validation failed.");
143+
errorObject.Detail.Should().Be("The CreditCard field is not a valid credit card number.");
144+
errorObject.Source.ShouldNotBeNull();
145+
errorObject.Source.Pointer.Should().Be("/data/attributes/creditCard");
146+
}
147+
148+
[Fact]
149+
public async Task imbadathis5()
150+
{
151+
// Arrange
152+
Fingerprint fingerprint = _fakers.Fingerprint.Generate();
153+
154+
using HttpClient httpClient = _testContext.Factory.CreateClient();
155+
var apiClient = new ModelValidationClient(httpClient);
156+
157+
// Act
158+
Func<Task<FingerprintPrimaryResponseDocument>> action = () => apiClient.PostFingerprintAsync(null, new FingerprintPostRequestDocument
159+
{
160+
Data = new FingerprintDataInPostRequest
161+
{
162+
Attributes = new FingerprintAttributesInPostRequest
163+
{
164+
LastName = fingerprint.LastName,
165+
Email = "abc"
166+
}
167+
}
168+
});
169+
170+
// Assert
171+
ErrorResponseDocument document = (await action.Should().ThrowExactlyAsync<ApiException<ErrorResponseDocument>>()).Which.Result;
172+
document.Errors.ShouldHaveCount(1);
173+
174+
ErrorObject errorObject = document.Errors.First();
175+
errorObject.Title.Should().Be("Input validation failed.");
176+
errorObject.Detail.Should().Be("The Email field is not a valid e-mail address.");
177+
errorObject.Source.ShouldNotBeNull();
178+
errorObject.Source.Pointer.Should().Be("/data/attributes/email");
179+
}
180+
181+
[Theory]
182+
[InlineData(-1)]
183+
[InlineData(124)]
184+
public async Task imbadathis6(int age)
185+
{
186+
// Arrange
187+
Fingerprint fingerprint = _fakers.Fingerprint.Generate();
188+
189+
using HttpClient httpClient = _testContext.Factory.CreateClient();
190+
var apiClient = new ModelValidationClient(httpClient);
191+
192+
// Act
193+
Func<Task<FingerprintPrimaryResponseDocument>> action = () => apiClient.PostFingerprintAsync(null, new FingerprintPostRequestDocument
194+
{
195+
Data = new FingerprintDataInPostRequest
196+
{
197+
Attributes = new FingerprintAttributesInPostRequest
198+
{
199+
LastName = fingerprint.LastName,
200+
Age = age
201+
}
202+
}
203+
});
204+
205+
// Assert
206+
ErrorResponseDocument document = (await action.Should().ThrowExactlyAsync<ApiException<ErrorResponseDocument>>()).Which.Result;
207+
document.Errors.ShouldHaveCount(1);
208+
209+
ErrorObject errorObject = document.Errors.First();
210+
errorObject.Title.Should().Be("Input validation failed.");
211+
errorObject.Detail.Should().Be("The field Age must be between 0 and 123.");
212+
errorObject.Source.ShouldNotBeNull();
213+
errorObject.Source.Pointer.Should().Be("/data/attributes/age");
214+
}
215+
216+
[Fact]
217+
public async Task imbadathis7()
218+
{
219+
// Arrange
220+
Fingerprint fingerprint = _fakers.Fingerprint.Generate();
221+
222+
using HttpClient httpClient = _testContext.Factory.CreateClient();
223+
var apiClient = new ModelValidationClient(httpClient);
224+
225+
// Act
226+
Func<Task<FingerprintPrimaryResponseDocument>> action = () => apiClient.PostFingerprintAsync(null, new FingerprintPostRequestDocument
227+
{
228+
Data = new FingerprintDataInPostRequest
229+
{
230+
Attributes = new FingerprintAttributesInPostRequest
231+
{
232+
LastName = fingerprint.LastName,
233+
ProfilePicture = new Uri("/justapath", UriKind.Relative)
234+
}
235+
}
236+
});
237+
238+
// Assert
239+
ErrorResponseDocument document = (await action.Should().ThrowExactlyAsync<ApiException<ErrorResponseDocument>>()).Which.Result;
240+
document.Errors.ShouldHaveCount(1);
241+
242+
ErrorObject errorObject = document.Errors.First();
243+
errorObject.Title.Should().Be("Input validation failed.");
244+
errorObject.Detail.Should().Be("The ProfilePicture field is not a valid fully-qualified http, https, or ftp URL.");
245+
errorObject.Source.ShouldNotBeNull();
246+
errorObject.Source.Pointer.Should().Be("/data/attributes/profilePicture");
247+
}
248+
249+
[Fact]
250+
public async Task imbadathis8()
251+
{
252+
// Arrange
253+
Fingerprint fingerprint = _fakers.Fingerprint.Generate();
254+
255+
using HttpClient httpClient = _testContext.Factory.CreateClient();
256+
var apiClient = new ModelValidationClient(httpClient);
257+
258+
// Act
259+
Func<Task<FingerprintPrimaryResponseDocument>> action = () => apiClient.PostFingerprintAsync(null, new FingerprintPostRequestDocument
260+
{
261+
Data = new FingerprintDataInPostRequest
262+
{
263+
Attributes = new FingerprintAttributesInPostRequest
264+
{
265+
LastName = fingerprint.LastName,
266+
NextRevalidation = new OpenApiNSwagEndToEndTests.ModelValidation.GeneratedCode.TimeSpan { TotalSeconds = 1 }
267+
}
268+
}
269+
});
270+
271+
// Assert
272+
ErrorResponseDocument document = (await action.Should().ThrowExactlyAsync<ApiException<ErrorResponseDocument>>()).Which.Result;
273+
document.Errors.ShouldHaveCount(1);
274+
275+
ErrorObject errorObject = document.Errors.First();
276+
errorObject.Title.Should().Be("Input validation failed.");
277+
errorObject.Detail.Should().Be("");
278+
errorObject.Source.ShouldNotBeNull();
279+
errorObject.Source.Pointer.Should().Be("/data/attributes/nextRevalidation");
280+
}
281+
282+
[Fact]
283+
public async Task imbadathis10()
284+
{
285+
// Arrange
286+
Fingerprint fingerprint = _fakers.Fingerprint.Generate();
287+
288+
using HttpClient httpClient = _testContext.Factory.CreateClient();
289+
var apiClient = new ModelValidationClient(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+
ValidatedAt = DateTimeOffset.MinValue
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("");
311+
errorObject.Source.ShouldNotBeNull();
312+
errorObject.Source.Pointer.Should().Be("/data/attributes/");
313+
}
314+
315+
[Fact]
316+
public async Task imbadathis11()
317+
{
318+
// Arrange
319+
Fingerprint fingerprint = _fakers.Fingerprint.Generate();
320+
321+
using HttpClient httpClient = _testContext.Factory.CreateClient();
322+
var apiClient = new ModelValidationClient(httpClient);
323+
324+
// Act
325+
Func<Task<FingerprintPrimaryResponseDocument>> action = () => apiClient.PostFingerprintAsync(null, new FingerprintPostRequestDocument
326+
{
327+
Data = new FingerprintDataInPostRequest
328+
{
329+
Attributes = new FingerprintAttributesInPostRequest
330+
{
331+
LastName = fingerprint.LastName,
332+
ValidatedDateAt = DateTimeOffset.Now
333+
}
334+
}
335+
});
336+
337+
// Assert
338+
ErrorResponseDocument document = (await action.Should().ThrowExactlyAsync<ApiException<ErrorResponseDocument>>()).Which.Result;
339+
document.Errors.ShouldHaveCount(1);
340+
341+
ErrorObject errorObject = document.Errors.First();
342+
errorObject.Title.Should().Be("Input validation failed.");
343+
errorObject.Detail.Should().Be("");
344+
errorObject.Source.ShouldNotBeNull();
345+
errorObject.Source.Pointer.Should().Be("/data/attributes/");
346+
}
347+
348+
[Fact]
349+
public async Task imbadathis9()
350+
{
351+
// Arrange
352+
Fingerprint fingerprint = _fakers.Fingerprint.Generate();
353+
354+
using HttpClient httpClient = _testContext.Factory.CreateClient();
355+
var apiClient = new ModelValidationClient(httpClient);
356+
357+
// Act
358+
Func<Task<FingerprintPrimaryResponseDocument>> action = () => apiClient.PostFingerprintAsync(null, new FingerprintPostRequestDocument
359+
{
360+
Data = new FingerprintDataInPostRequest
361+
{
362+
Attributes = new FingerprintAttributesInPostRequest
363+
{
364+
LastName = fingerprint.LastName,
365+
ValidatedTimeAt = System.TimeSpan.FromSeconds(-1)
366+
}
367+
}
368+
});
369+
370+
// Assert
371+
ErrorResponseDocument document = (await action.Should().ThrowExactlyAsync<ApiException<ErrorResponseDocument>>()).Which.Result;
372+
document.Errors.ShouldHaveCount(1);
373+
374+
ErrorObject errorObject = document.Errors.First();
375+
errorObject.Title.Should().Be("Input validation failed.");
376+
errorObject.Detail.Should().Be("");
377+
errorObject.Source.ShouldNotBeNull();
378+
errorObject.Source.Pointer.Should().Be("/data/attributes/");
379+
}
380+
}

test/OpenApiNSwagEndToEndTests/OpenApiNSwagEndToEndTests.csproj

+7
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,13 @@
3030
<CodeGenerator>NSwagCSharp</CodeGenerator>
3131
<Options>/ClientClassAccessModifier:internal /GenerateExceptionClasses:false /AdditionalNamespaceUsages:JsonApiDotNetCore.OpenApi.Client.NSwag /GenerateNullableReferenceTypes:true</Options>
3232
</OpenApiReference>
33+
<OpenApiReference Include="..\OpenApiTests\ModelValidation\GeneratedSwagger\swagger.g.json">
34+
<Namespace>OpenApiNSwagEndToEndTests.ModelValidation.GeneratedCode</Namespace>
35+
<ClassName>ModelValidationClient</ClassName>
36+
<OutputPath>ModelValidationClient.cs</OutputPath>
37+
<CodeGenerator>NSwagCSharp</CodeGenerator>
38+
<Options>/ClientClassAccessModifier:internal /GenerateExceptionClasses:false /AdditionalNamespaceUsages:JsonApiDotNetCore.OpenApi.Client.NSwag /GenerateNullableReferenceTypes:true</Options>
39+
</OpenApiReference>
3340
<OpenApiReference Include="..\OpenApiTests\Headers\GeneratedSwagger\swagger.g.json">
3441
<Namespace>OpenApiNSwagEndToEndTests.Headers.GeneratedCode</Namespace>
3542
<ClassName>HeadersClient</ClassName>

0 commit comments

Comments
 (0)