Skip to content

Commit 3f781fb

Browse files
committed
wip
1 parent bff1cb3 commit 3f781fb

File tree

6 files changed

+424
-117
lines changed

6 files changed

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

0 commit comments

Comments
 (0)