aboutsummaryrefslogtreecommitdiff
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
parentb022122b0a697f063433d5fe525e536bd23e8372 (diff)
downloadcrupest-3469ac050b13b73d76f13dcbdfa77a1aefc49ae0.tar.gz
crupest-3469ac050b13b73d76f13dcbdfa77a1aefc49ae0.tar.bz2
crupest-3469ac050b13b73d76f13dcbdfa77a1aefc49ae0.zip
Develop secret api. v58
-rw-r--r--docker/crupest-api/CrupestApi/CrupestApi.Commons.Tests/Crud/CrudIntegratedTest.cs59
-rw-r--r--docker/crupest-api/CrupestApi/CrupestApi.Commons.Tests/Crud/CrudServiceTest.cs10
-rw-r--r--docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/ColumnInfo.cs17
-rw-r--r--docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/ColumnMetadata.cs4
-rw-r--r--docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/ColumnTypeInfo.cs10
-rw-r--r--docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/EntityJsonHelper.cs2
-rw-r--r--docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/TableInfo.cs13
-rw-r--r--docker/crupest-api/CrupestApi/CrupestApi.Commons/Secrets/SecretInfo.cs2
8 files changed, 107 insertions, 10 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);
+ }
+}
diff --git a/docker/crupest-api/CrupestApi/CrupestApi.Commons.Tests/Crud/CrudServiceTest.cs b/docker/crupest-api/CrupestApi/CrupestApi.Commons.Tests/Crud/CrudServiceTest.cs
index a515d2f..284dbe2 100644
--- a/docker/crupest-api/CrupestApi/CrupestApi.Commons.Tests/Crud/CrudServiceTest.cs
+++ b/docker/crupest-api/CrupestApi/CrupestApi.Commons.Tests/Crud/CrudServiceTest.cs
@@ -64,5 +64,13 @@ public class CrudServiceTest
Assert.Equal(0, count);
}
-
+ [Fact]
+ public void EntityNotExistTest()
+ {
+ Assert.Throws<EntityNotExistException>(() => _crudService.GetByKey("KeyNotExist"));
+ Assert.Throws<EntityNotExistException>(() => _crudService.UpdateByKey("KeyNotExist", new TestEntity
+ {
+ Name = "crupest"
+ }));
+ }
}
diff --git a/docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/ColumnInfo.cs b/docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/ColumnInfo.cs
index 7e38469..7f1782b 100644
--- a/docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/ColumnInfo.cs
+++ b/docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/ColumnInfo.cs
@@ -186,6 +186,23 @@ public class ColumnInfo
}
}
+ public object? GenerateDefaultValue()
+ {
+ if (DefaultValueGeneratorMethod is not null)
+ {
+ return InvokeDefaultValueGenerator();
+ }
+
+ if (Metadata.TryGetValue(ColumnMetadataKeys.DefaultValue, out object? value))
+ {
+ return value;
+ }
+ else
+ {
+ return null;
+ }
+ }
+
public string GenerateCreateTableColumnString(string? dbProviderId = null)
{
StringBuilder result = new StringBuilder();
diff --git a/docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/ColumnMetadata.cs b/docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/ColumnMetadata.cs
index 5d20772..26f6105 100644
--- a/docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/ColumnMetadata.cs
+++ b/docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/ColumnMetadata.cs
@@ -42,6 +42,8 @@ public static class ColumnMetadataKeys
/// </summary>
public const string ActAsKey = nameof(ColumnAttribute.ActAsKey);
+ public const string DefaultValue = nameof(ColumnAttribute.DefaultValue);
+
/// <summary>
/// Default value is null, aka, whether a default value generator is specified.
/// Set to true or false to override it.
@@ -130,6 +132,8 @@ public class ColumnAttribute : Attribute, IColumnMetadata
/// <seealso cref="ColumnMetadataKeys.ActAsKey"/>
public bool ActAsKey { get; init; }
+ public object? DefaultValue { get; init; }
+
public bool? CanBeGenerated { get; init; } = null;
public bool TryGetValue(string key, out object? value)
diff --git a/docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/ColumnTypeInfo.cs b/docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/ColumnTypeInfo.cs
index 2f15e50..19eff52 100644
--- a/docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/ColumnTypeInfo.cs
+++ b/docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/ColumnTypeInfo.cs
@@ -8,6 +8,7 @@ namespace CrupestApi.Commons.Crud;
public interface IColumnTypeInfo
{
+ public static IColumnTypeInfo BoolColumnTypeInfo { get; } = new SimpleColumnTypeInfo<bool>();
public static IColumnTypeInfo IntColumnTypeInfo { get; } = new SimpleColumnTypeInfo<int>();
public static IColumnTypeInfo ShortColumnTypeInfo { get; } = new SimpleColumnTypeInfo<short>();
public static IColumnTypeInfo SByteColumnTypeInfo { get; } = new SimpleColumnTypeInfo<sbyte>();
@@ -25,7 +26,11 @@ public interface IColumnTypeInfo
{
get
{
- if (DatabaseClrType == typeof(int))
+ if (DatabaseClrType == typeof(bool))
+ {
+ return DbType.Boolean;
+ }
+ else if (DatabaseClrType == typeof(int))
{
return DbType.Int32;
}
@@ -70,7 +75,7 @@ public interface IColumnTypeInfo
return DbType switch
{
DbType.String => "TEXT",
- DbType.Int16 or DbType.Int32 or DbType.Int64 => "INTEGER",
+ DbType.Boolean or DbType.Int16 or DbType.Int32 or DbType.Int64 => "INTEGER",
DbType.Single or DbType.Double => "REAL",
DbType.Binary => "BLOB",
_ => throw new Exception($"Unsupported DbType: {DbType}"),
@@ -175,6 +180,7 @@ public class ColumnTypeProvider : IColumnTypeProvider
public ColumnTypeProvider()
{
+ _typeMap.Add(IColumnTypeInfo.BoolColumnTypeInfo.ClrType, IColumnTypeInfo.BoolColumnTypeInfo);
_typeMap.Add(IColumnTypeInfo.IntColumnTypeInfo.ClrType, IColumnTypeInfo.IntColumnTypeInfo);
_typeMap.Add(IColumnTypeInfo.ShortColumnTypeInfo.ClrType, IColumnTypeInfo.ShortColumnTypeInfo);
_typeMap.Add(IColumnTypeInfo.SByteColumnTypeInfo.ClrType, IColumnTypeInfo.SByteColumnTypeInfo);
diff --git a/docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/EntityJsonHelper.cs b/docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/EntityJsonHelper.cs
index ea8197a..39c7b43 100644
--- a/docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/EntityJsonHelper.cs
+++ b/docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/EntityJsonHelper.cs
@@ -11,7 +11,7 @@ public class EntityJsonHelper<TEntity> where TEntity : class
private readonly TableInfo _table;
private readonly IOptionsMonitor<JsonSerializerOptions> _jsonSerializerOptions;
- public EntityJsonHelper(TableInfoFactory tableInfoFactory, IOptionsMonitor<JsonSerializerOptions> jsonSerializerOptions)
+ public EntityJsonHelper(ITableInfoFactory tableInfoFactory, IOptionsMonitor<JsonSerializerOptions> jsonSerializerOptions)
{
_table = tableInfoFactory.Get(typeof(TEntity));
_jsonSerializerOptions = jsonSerializerOptions;
diff --git a/docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/TableInfo.cs b/docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/TableInfo.cs
index 6a88ee1..e9f2d85 100644
--- a/docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/TableInfo.cs
+++ b/docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/TableInfo.cs
@@ -511,7 +511,7 @@ CREATE TABLE {tableName}(
if (value is null)
{
- value = column.InvokeDefaultValueGenerator();
+ value = column.GenerateDefaultValue();
}
if (value is null && column.IsAutoIncrement)
@@ -526,6 +526,8 @@ CREATE TABLE {tableName}(
column.InvokeValidator(value);
+ InsertItem realInsertItem;
+
if (value is DbNullValue)
{
if (column.IsNotNull)
@@ -533,17 +535,18 @@ CREATE TABLE {tableName}(
throw new Exception($"Column '{column.ColumnName}' is not nullable. Please specify a non-null value.");
}
- realInsert.Add(column.ColumnName, null);
+ realInsertItem = new InsertItem(column.ColumnName, null);
}
else
{
- realInsert.Add(column.ColumnName, value);
+ realInsertItem = new InsertItem(column.ColumnName, value);
}
+ realInsert.Add(realInsertItem);
- if (item?.ColumnName == KeyColumn.ColumnName)
+ if (realInsertItem.ColumnName == KeyColumn.ColumnName)
{
- key = item.Value;
+ key = realInsertItem.Value;
}
}
diff --git a/docker/crupest-api/CrupestApi/CrupestApi.Commons/Secrets/SecretInfo.cs b/docker/crupest-api/CrupestApi/CrupestApi.Commons/Secrets/SecretInfo.cs
index 1f00f9a..c3a4de0 100644
--- a/docker/crupest-api/CrupestApi/CrupestApi.Commons/Secrets/SecretInfo.cs
+++ b/docker/crupest-api/CrupestApi/CrupestApi.Commons/Secrets/SecretInfo.cs
@@ -14,7 +14,7 @@ public class SecretInfo
public string Description { get; set; } = default!;
[Column(NotNull = false)]
public DateTime? ExpireTime { get; set; }
- [Column(NotNull = true)]
+ [Column(NotNull = true, DefaultValue = false)]
public bool Revoked { get; set; }
[Column(NotNull = true)]
public DateTime CreateTime { get; set; }