diff options
| author | crupest <crupest@outlook.com> | 2022-12-20 19:14:32 +0800 | 
|---|---|---|
| committer | crupest <crupest@outlook.com> | 2022-12-20 20:32:53 +0800 | 
| commit | 807ffbc0edf25754351c71af147cb58beaaf1498 (patch) | |
| tree | 8367de0e8fa3ddfd2d0c313aa8d2d2510634b520 /docker | |
| parent | c7494c4243f3bd0f484aca692f9c878ffd5a097f (diff) | |
| download | crupest-807ffbc0edf25754351c71af147cb58beaaf1498.tar.gz crupest-807ffbc0edf25754351c71af147cb58beaaf1498.tar.bz2 crupest-807ffbc0edf25754351c71af147cb58beaaf1498.zip  | |
Develop secret api. v49
Diffstat (limited to 'docker')
6 files changed, 49 insertions, 8 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); +        } +    }  } diff --git a/docker/crupest-api/CrupestApi/CrupestApi.Commons/HttpContextExtensions.cs b/docker/crupest-api/CrupestApi/CrupestApi.Commons/HttpContextExtensions.cs index 1d7d858..2ad2c1f 100644 --- a/docker/crupest-api/CrupestApi/CrupestApi.Commons/HttpContextExtensions.cs +++ b/docker/crupest-api/CrupestApi/CrupestApi.Commons/HttpContextExtensions.cs @@ -1,4 +1,5 @@  using System.Text.Json; +using CrupestApi.Commons.Secrets;  using Microsoft.Extensions.Options;  namespace CrupestApi.Commons; diff --git a/docker/crupest-api/CrupestApi/CrupestApi.Commons/ISecretService.cs b/docker/crupest-api/CrupestApi/CrupestApi.Commons/ISecretService.cs deleted file mode 100644 index c4c4467..0000000 --- a/docker/crupest-api/CrupestApi/CrupestApi.Commons/ISecretService.cs +++ /dev/null @@ -1,6 +0,0 @@ -namespace CrupestApi.Commons; - -public interface ISecretService -{ -    List<string> GetPermissions(string secret); -} diff --git a/docker/crupest-api/CrupestApi/CrupestApi.Commons/Secrets/ISecretService.cs b/docker/crupest-api/CrupestApi/CrupestApi.Commons/Secrets/ISecretService.cs new file mode 100644 index 0000000..83025f8 --- /dev/null +++ b/docker/crupest-api/CrupestApi/CrupestApi.Commons/Secrets/ISecretService.cs @@ -0,0 +1,8 @@ +namespace CrupestApi.Commons.Secrets; + +public interface ISecretService +{ +    void CreateTestSecret(string key, string secret); + +    List<string> GetPermissions(string secret); +} diff --git a/docker/crupest-api/CrupestApi/CrupestApi.Commons/Secrets/SecretService.cs b/docker/crupest-api/CrupestApi/CrupestApi.Commons/Secrets/SecretService.cs index 4d9b14c..47e9583 100644 --- a/docker/crupest-api/CrupestApi/CrupestApi.Commons/Secrets/SecretService.cs +++ b/docker/crupest-api/CrupestApi/CrupestApi.Commons/Secrets/SecretService.cs @@ -1,4 +1,5 @@  using CrupestApi.Commons.Crud; +using Dapper;  namespace CrupestApi.Commons.Secrets; @@ -23,6 +24,12 @@ public class SecretService : CrudService<SecretInfo>, ISecretService          transaction.Commit();      } +    public void CreateTestSecret(string key, string secret) +    { +        var connection = _dbConnection; +        connection.Execute("INSERT INTO secrets (key, secret, description) VALUES (@key, @secret, @desc)", new { key, secret, desc = "Test key." }); +    } +      public List<string> GetPermissions(string secret)      {          var list = _table.Select<SecretInfo>(_dbConnection, diff --git a/docker/crupest-api/CrupestApi/CrupestApi/Program.cs b/docker/crupest-api/CrupestApi/CrupestApi/Program.cs index be72617..cafed44 100644 --- a/docker/crupest-api/CrupestApi/CrupestApi/Program.cs +++ b/docker/crupest-api/CrupestApi/CrupestApi/Program.cs @@ -11,6 +11,7 @@ builder.Services.AddJsonOptions();  builder.Services.AddCrupestApiConfig();
  builder.Services.AddTodos();
 +builder.Services.AddSecrets();
  var app = builder.Build();
  | 
