重构单元测试和集成测试
This commit is contained in:
1 parent
e7e9afe11b
commit
81c8f608dc
34 files changed
+135
-300
No files matched your search
@@ -0,0 +1,72 @@
|
||||
// Copyright (c) HelloShop Corporation. All rights reserved.
|
||||
// See the license file in the project root for more information.
|
||||
|
||||
using HelloShop.ProductService.Infrastructure;
|
||||
using HelloShop.ServiceDefaults.Authorization;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.AspNetCore.Mvc.Testing;
|
||||
using Microsoft.Data.Sqlite;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Diagnostics;
|
||||
using Microsoft.EntityFrameworkCore.Infrastructure;
|
||||
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.Helpers
|
||||
{
|
||||
public class CustomWebApplicationFactory<TProgram> : WebApplicationFactory<TProgram> where TProgram : class
|
||||
{
|
||||
protected override void ConfigureWebHost(IWebHostBuilder builder)
|
||||
{
|
||||
builder.ConfigureServices(static services =>
|
||||
{
|
||||
var dbContextDescriptor = services.SingleOrDefault(d => d.ServiceType == typeof(DbContextOptions<ProductServiceDbContext>));
|
||||
if (dbContextDescriptor != null)
|
||||
{
|
||||
services.Remove(dbContextDescriptor);
|
||||
}
|
||||
|
||||
var dbConnectionDescriptor = services.SingleOrDefault(d => d.ServiceType == typeof(DbConnection));
|
||||
if (dbConnectionDescriptor != null)
|
||||
{
|
||||
services.Remove(dbConnectionDescriptor);
|
||||
}
|
||||
|
||||
var optionsConfigurationDescriptor = services.SingleOrDefault(d => d.ServiceType == typeof(IDbContextOptionsConfiguration<ProductServiceDbContext>));
|
||||
if (optionsConfigurationDescriptor != null)
|
||||
{
|
||||
services.Remove(optionsConfigurationDescriptor);
|
||||
}
|
||||
|
||||
// Create open SqliteConnection so EF won't automatically close it.
|
||||
services.AddSingleton<DbConnection>(container =>
|
||||
{
|
||||
var connection = new SqliteConnection("DataSource=file::memory:?cache=shared");
|
||||
connection.Open();
|
||||
|
||||
return connection;
|
||||
});
|
||||
|
||||
services.AddDbContextPool<ProductServiceDbContext>((container, options) =>
|
||||
{
|
||||
var connection = container.GetRequiredService<DbConnection>();
|
||||
options.UseSqlite(connection).UseSnakeCaseNamingConvention();
|
||||
options.ConfigureWarnings(warnings => warnings.Ignore(RelationalEventId.PendingModelChangesWarning));
|
||||
});
|
||||
|
||||
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.Helpers
|
||||
{
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user