aboutsummaryrefslogtreecommitdiff
path: root/docker/crupest-api/CrupestApi/CrupestApi.Commons.Tests/Crud/CrudIntegratedTest.cs
diff options
context:
space:
mode:
authorcrupest <crupest@outlook.com>2022-12-22 17:47:07 +0800
committercrupest <crupest@outlook.com>2022-12-22 17:47:07 +0800
commit3469ac050b13b73d76f13dcbdfa77a1aefc49ae0 (patch)
tree0fb74f6fb4e8603f9625a72ea82a06dbf72436e6 /docker/crupest-api/CrupestApi/CrupestApi.Commons.Tests/Crud/CrudIntegratedTest.cs
parentb022122b0a697f063433d5fe525e536bd23e8372 (diff)
downloadcrupest-3469ac050b13b73d76f13dcbdfa77a1aefc49ae0.tar.gz
crupest-3469ac050b13b73d76f13dcbdfa77a1aefc49ae0.tar.bz2
crupest-3469ac050b13b73d76f13dcbdfa77a1aefc49ae0.zip
Develop secret api. v58
Diffstat (limited to 'docker/crupest-api/CrupestApi/CrupestApi.Commons.Tests/Crud/CrudIntegratedTest.cs')
-rw-r--r--docker/crupest-api/CrupestApi/CrupestApi.Commons.Tests/Crud/CrudIntegratedTest.cs59
1 files changed, 59 insertions, 0 deletions
diff --git a/docker/crupest-api/CrupestApi/CrupestApi.Commons.Tests/Crud/CrudIntegratedTest.cs b/docker/crupest-api/CrupestApi/CrupestApi.Commons.Tests/Crud/CrudIntegratedTest.cs
new file mode 100644
index 0000000..ddda5f6
--- /dev/null
+++ b/docker/crupest-api/CrupestApi/CrupestApi.Commons.Tests/Crud/CrudIntegratedTest.cs
@@ -0,0 +1,59 @@
+using System.Net;
+using System.Net.Http.Headers;
+using CrupestApi.Commons.Secrets;
+using Microsoft.AspNetCore.TestHost;
+
+namespace CrupestApi.Commons.Crud.Tests;
+
+public class CrudIntegratedTest : IAsyncLifetime
+{
+ private readonly WebApplication _app;
+ private HttpClient _httpClient = default!;
+ private HttpClient _authorizedHttpClient = default!;
+ private string _token = default!;
+
+ public CrudIntegratedTest()
+ {
+ var builder = WebApplication.CreateBuilder();
+ builder.Services.AddSingleton<SqliteMemoryConnectionFactory>();
+ builder.Services.AddCrud<TestEntity>();
+ builder.WebHost.UseTestServer();
+ _app = builder.Build();
+ _app.MapCrud<TestEntity>("/test", "test-perm");
+ }
+
+ public async Task InitializeAsync()
+ {
+ await _app.StartAsync();
+ _httpClient = _app.GetTestClient();
+
+ using (var scope = _app.Services.CreateScope())
+ {
+ var secretService = (SecretService)scope.ServiceProvider.GetRequiredService<ISecretService>();
+ var key = secretService.Create(new SecretInfo
+ {
+ Key = "test-perm"
+ });
+ _token = secretService.GetByKey(key).Secret;
+ }
+
+ _authorizedHttpClient = _app.GetTestClient();
+ _authorizedHttpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", _token);
+ }
+
+ public async Task DisposeAsync()
+ {
+ await _app.StopAsync();
+ }
+
+
+ [Fact]
+ public async Task Test()
+ {
+ var response = await _authorizedHttpClient.GetAsync($"/test?secret={_token}");
+ Assert.Equal(HttpStatusCode.OK, response.StatusCode);
+ var body = await response.Content.ReadFromJsonAsync<List<TestEntity>>();
+ Assert.NotNull(body);
+ Assert.Empty(body);
+ }
+}