diff options
Diffstat (limited to 'docker/crupest-api/CrupestApi/CrupestApi.Commons.Tests/Crud/CrudTestBase.cs')
-rw-r--r-- | docker/crupest-api/CrupestApi/CrupestApi.Commons.Tests/Crud/CrudTestBase.cs | 34 |
1 files changed, 32 insertions, 2 deletions
diff --git a/docker/crupest-api/CrupestApi/CrupestApi.Commons.Tests/Crud/CrudTestBase.cs b/docker/crupest-api/CrupestApi/CrupestApi.Commons.Tests/Crud/CrudTestBase.cs index 117d0cf..98c0dfd 100644 --- a/docker/crupest-api/CrupestApi/CrupestApi.Commons.Tests/Crud/CrudTestBase.cs +++ b/docker/crupest-api/CrupestApi/CrupestApi.Commons.Tests/Crud/CrudTestBase.cs @@ -1,8 +1,10 @@ +using System.Net; +using CrupestApi.Commons.Secrets; using Microsoft.AspNetCore.TestHost; namespace CrupestApi.Commons.Crud.Tests; -public abstract class CrudTestBase : IAsyncDisposable +public abstract class CrudTestBase<TEntity> : IAsyncDisposable where TEntity : class { protected readonly WebApplication _app; @@ -18,10 +20,19 @@ public abstract class CrudTestBase : IAsyncDisposable var builder = WebApplication.CreateBuilder(); builder.WebHost.UseTestServer(); - builder.Services.AddCrudCore(); + builder.Services.AddCrud<TEntity>(); ConfigureApplication(builder); _app = builder.Build(); + if (authKey is not null) + { + using (var scope = _app.Services.CreateScope()) + { + var secretService = scope.ServiceProvider.GetRequiredService<ISecretService>(); + secretService.CreateTestSecret(authKey, "test-secret"); + } + } + _client = CreateHttpClient(); } @@ -41,4 +52,23 @@ public abstract class CrudTestBase : IAsyncDisposable { return GetTestServer().CreateClient(); } + + public async Task TestAuth() + { + if (_authKey is null) + { + return; + } + + { + using var response = await _client.GetAsync(_path); + Assert.Equal(HttpStatusCode.Unauthorized, response.StatusCode); + } + + { + var entity = Activator.CreateInstance<TEntity>(); + using var response = await _client.PostAsJsonAsync(_path, entity); + Assert.Equal(HttpStatusCode.Unauthorized, response.StatusCode); + } + } } |