/
WebTests.cs
210 lines (172 loc) · 6.36 KB
/
WebTests.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
using FakeItEasy;
using FastEndpoints;
using Microsoft.AspNetCore.Http.HttpResults;
using Microsoft.AspNetCore.Routing;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using TestCases.TypedResultTest;
using Web.Services;
using Xunit;
namespace Web;
public class WebTests
{
[Fact]
public async Task endpoint_with_mapper_returns_correct_response()
{
//arrange
var logger = A.Fake<ILogger<TestCases.MapperTest.Endpoint>>();
var ep = Factory.Create<TestCases.MapperTest.Endpoint>(logger);
ep.Map = new TestCases.MapperTest.Mapper();
var req = new TestCases.MapperTest.Request
{
FirstName = "john",
LastName = "doe",
Age = 22
};
//act
await ep.HandleAsync(req, default);
//assert
ep.Response.Should().NotBeNull();
ep.Response.Name.Should().Be("john doe");
ep.Response.Age.Should().Be(22);
}
[Fact]
public async Task handle_with_correct_input_without_context_should_set_create_customer_response_correctly()
{
var emailer = A.Fake<IEmailService>();
A.CallTo(() => emailer.SendEmail()).Returns("test email");
var ep = Factory.Create<Customers.Create.Endpoint>(emailer);
var req = new Customers.Create.Request
{
CreatedBy = "by harry potter",
};
await ep.HandleAsync(req, default);
ep.Response.Should().Be("test email by harry potter");
}
[Fact]
public async Task handle_with_correct_input_with_property_di_without_context_should_set_create_customer_response_correctly()
{
var emailer = A.Fake<IEmailService>();
A.CallTo(() => emailer.SendEmail()).Returns("test email");
var ep = Factory.Create<Customers.CreateWithPropertiesDI.Endpoint>(ctx =>
{
ctx.AddTestServices(s => s.AddSingleton(emailer));
});
var req = new Customers.CreateWithPropertiesDI.Request
{
CreatedBy = "by harry potter",
};
await ep.HandleAsync(req, default);
ep.Response.Should().Be("test email by harry potter");
}
[Fact]
public async Task handle_with_correct_input_with_context_should_set_login_admin_response_correctly()
{
//arrange
var fakeConfig = A.Fake<IConfiguration>();
A.CallTo(() => fakeConfig["TokenKey"]).Returns("0000000000000000");
var ep = Factory.Create<Admin.Login.Endpoint>(
A.Fake<ILogger<Admin.Login.Endpoint>>(),
A.Fake<IEmailService>(),
fakeConfig);
var req = new Admin.Login.Request
{
UserName = "admin",
Password = "pass"
};
//act
await ep.HandleAsync(req, default);
var rsp = ep.Response;
//assert
ep.ValidationFailed.Should().BeFalse();
rsp.Should().NotBeNull();
rsp.Permissions.Should().Contain("Inventory_Delete_Item");
}
[Fact]
public async Task handle_with_bad_input_should_set_admin_login_validation_failed()
{
//arrange
var ep = Factory.Create<Admin.Login.Endpoint>(
A.Fake<ILogger<Admin.Login.Endpoint>>(),
A.Fake<IEmailService>(),
A.Fake<IConfiguration>());
var req = new Admin.Login.Request
{
UserName = "x",
Password = "y"
};
//act
await ep.HandleAsync(req, default);
//assert
ep.ValidationFailed.Should().BeTrue();
ep.ValidationFailures.Any(f => f.ErrorMessage == "Authentication Failed!").Should().BeTrue();
}
[Fact]
public async Task execute_customer_recent_list_should_return_correct_data()
{
var endpoint = Factory.Create<Customers.List.Recent.Endpoint>();
var res = await endpoint.ExecuteAsync(default) as Customers.List.Recent.Response;
res?.Customers?.Count().Should().Be(3);
res?.Customers?.First().Key.Should().Be("ryan gunner");
res?.Customers?.Last().Key.Should().Be(res?.Customers?.Last().Key);
}
[Fact]
public async Task union_type_result_returning_endpoint()
{
var ep = new MultiResultEndpoint();
var res0 = await ep.ExecuteAsync(new Request { Id = 0 }, CancellationToken.None);
res0.Result.Should().BeOfType<NotFound>();
var res1 = await ep.ExecuteAsync(new Request { Id = 1 }, CancellationToken.None);
var errors = res1.Result.As<ProblemDetails>()!.Errors;
errors.Count().Should().Be(1);
errors.Single(e => e.Name == nameof(Request.Id)).Reason.Should().Be("value has to be greater than 1");
var res2 = await ep.ExecuteAsync(new Request { Id = 2 }, CancellationToken.None);
var response = res2.Result.As<Ok<Response>>();
response.StatusCode.Should().Be(200);
response.Value!.RequestId.Should().Be(2);
}
[Fact]
public async Task created_at_success()
{
var linkgen = A.Fake<LinkGenerator>();
var ep = Factory.Create<Inventory.Manage.Create.Endpoint>(ctx =>
{
ctx.AddTestServices(s => s.AddSingleton(linkgen));
});
await ep.HandleAsync(new()
{
Name = "Grape Juice",
Description = "description",
ModifiedBy = "me",
Price = 100,
GenerateFullUrl = false
}, default);
}
[Fact]
public async Task processor_state_access_from_unit_test()
{
//arrange
var ep = Factory.Create<TestCases.ProcessorStateTest.Endpoint>();
var state = ep.ProcessorState<TestCases.ProcessorStateTest.Thingy>();
state.Id = 101;
state.Name = "blah";
//act
await ep.HandleAsync(new() { Id = 0 }, default);
//assert
ep.Response.Should().Be("101 blah");
state.Duration.Should().BeGreaterThan(250);
}
[Fact]
public async Task unit_test_concurrency_and_httpContext_isolation()
{
await Parallel.ForEachAsync(Enumerable.Range(1, 100), async (id, _) =>
{
var ep = Factory.Create<TestCases.UnitTestConcurrencyTest.Endpoint>(ctx =>
{
ctx.AddTestServices(s => s.AddSingleton(new TestCases.UnitTestConcurrencyTest.SingltonSVC(id)));
});
(await ep.ExecuteAsync(new() { Id = id }, default)).Should().Be(id);
});
}
}