零度框架 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,42 @@
// Copyright (c) HelloShop Corporation. All rights reserved.
// See the license file in the project root for more information.
using HelloShop.ProductService.FunctionalTests.Utilities;
using HelloShop.ProductService.Models.Products;
using System.Net;
using System.Net.Http.Json;
namespace HelloShop.ProductService.FunctionalTests
{
public class BrandApiIntegrationTest(CustomWebApplicationFactory<Program> factory) : IClassFixture<CustomWebApplicationFactory<Program>>
{
[Fact]
public async Task GetBrandByIdReturnsUnauthorizedStatusCode()
{
// Arrange
HttpClient client = factory.CreateClient();
// Act
HttpResponseMessage response = await client.GetAsync("api/Brands/1");
// Assert
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
}
[Fact]
public async Task GetBrandByIdReturnsBrandDetailsResponse()
{
// Arrange
HttpClient client = factory.CreateClient();
// Act
HttpResponseMessage response = await client.GetAsync("api/Brands/1");
BrandDetailsResponse? brandDetails = await response.Content.ReadFromJsonAsync<BrandDetailsResponse>();
// Assert
Assert.NotNull(brandDetails);
Assert.Equal(1, brandDetails.Id);
}
}
}
@@ -1 +0,0 @@
global using Xunit;
@@ -1,21 +1,35 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net9.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<IsPackable>false</IsPackable>
<IsTestProject>true</IsTestProject>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.10.0-release-24177-07" />
<PackageReference Include="xunit" Version="2.7.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.8">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="6.0.2">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>
</Project>
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<IsPackable>false</IsPackable>
<IsTestProject>true</IsTestProject>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="coverlet.collector" Version="6.0.2">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="8.0.6" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="8.0.6" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.10.0" />
<PackageReference Include="xunit" Version="2.8.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.1">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\src\HelloShop.ProductService\HelloShop.ProductService.csproj" />
</ItemGroup>
<ItemGroup>
<Using Include="Xunit" />
</ItemGroup>
</Project>
@@ -1,11 +0,0 @@
namespace HelloShop.ProductService.FunctionalTests
{
public class UnitTest1
{
[Fact]
public void Test1()
{
}
}
}
@@ -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);
}
}
}