零度框架 1.0 版本正式发布,共 184 项改进。

This commit is contained in:
administrator committed 2024-05-31 12:28:02 +08:00
1 parent c14a872b77
commit baf06cd88f
182 files changed
+2537 -1613

No files matched your search

@@ -0,0 +1,59 @@
// Copyright (c) HelloShop Corporation. All rights reserved.
// See the license file in the project root for more information.
using HelloShop.ProductService.EntityFrameworks;
using HelloShop.ServiceDefaults.Authorization;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc.Testing;
using Microsoft.Data.Sqlite;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Hosting;
using System.Data.Common;
using System.Net.Http.Headers;
namespace HelloShop.ProductService.FunctionalTests.Utilities
{
public class CustomWebApplicationFactory<TProgram> : WebApplicationFactory<TProgram> where TProgram : class
{
protected override void ConfigureWebHost(IWebHostBuilder builder)
{
builder.ConfigureServices(services =>
{
ServiceDescriptor? dbContextDescriptor = services.SingleOrDefault(d => d.ServiceType == typeof(DbContextOptions<ProductServiceDbContext>));
if (dbContextDescriptor != null)
{
services.Remove(dbContextDescriptor);
}
// Create open SqliteConnection so EF won't automatically close it.
services.AddSingleton(container =>
{
DbConnection connection = new SqliteConnection("DataSource=:memory:");
connection.Open();
return connection;
});
services.AddDbContext<ProductServiceDbContext>((container, options) =>
{
var connection = container.GetRequiredService<DbConnection>();
options.UseSqlite(connection);
});
services.Replace(ServiceDescriptor.Transient<IPermissionChecker, FakePermissionChecker>());
});
builder.UseEnvironment(Environments.Development);
}
protected override void ConfigureClient(HttpClient client)
{
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", FakeAccessTokenCreator.Create());
base.ConfigureClient(client);
}
}
}
@@ -0,0 +1,36 @@
// Copyright (c) HelloShop Corporation. All rights reserved.
// See the license file in the project root for more information.
using HelloShop.ServiceDefaults.Constants;
using Microsoft.IdentityModel.Tokens;
using System.IdentityModel.Tokens.Jwt;
using System.Security.Claims;
using System.Text;
namespace HelloShop.ProductService.FunctionalTests.Utilities
{
public class FakeAccessTokenCreator
{
public static string Create()
{
DateTimeOffset utcNow = TimeProvider.System.GetUtcNow();
SymmetricSecurityKey signingKey = new(Encoding.Default.GetBytes(IdentityConstants.IssuerSigningKey));
var claimsIdentity = new ClaimsIdentity([new Claim(ClaimTypes.NameIdentifier, "1"), new Claim(ClaimTypes.Name, "admin"), new Claim(CustomClaimTypes.RoleIdentifier, "1")]);
SecurityTokenDescriptor accessTokenDescriptor = new()
{
Subject = claimsIdentity,
SigningCredentials = new(signingKey, SecurityAlgorithms.HmacSha256),
Expires = utcNow.Add(TimeSpan.FromDays(byte.MaxValue)).LocalDateTime,
};
JwtSecurityTokenHandler tokenHandler = new();
JwtSecurityToken jwtSecurityToken = tokenHandler.CreateJwtSecurityToken(accessTokenDescriptor);
return tokenHandler.WriteToken(jwtSecurityToken);
}
}
}