Files
StoreWeb/Tests/Store.Shops.Tests/ShopTestContext.cs
T
2026-10-29 16:34:25 +08:00

85 lines
3.7 KiB
C#

using System.Net;
using System.Security.Claims;
using FreeSql;
using MAX.Framework.Security;
using Microsoft.Extensions.Configuration;
using Store.Shops.Entities;
using Store.Shops.Services;
using Store.Shops.Repositories;
using Store.Users.Entities;
using YourNamespace;
using Store.Users.Contracts;
using Store.Users.Contracts.Dtos;
using Store.Users.Contracts.Enums;
namespace Store.Shops.Tests;
internal sealed class ShopTestContext : IDisposable, ICurrentUser, IUserQueryService
{
private readonly string path = Path.Combine(Path.GetTempPath(), $"store-shops-{Guid.NewGuid():N}.db");
public IFreeSql Db { get; }
public string? UserName { get; set; } = "owner";
public string? NickName => UserName;
public bool IsAuthenticated => UserName != null;
public IEnumerable<Claim> Claims => [];
public string? GetClaimValue(string type) => null;
public T? GetClaimValue<T>(string type) => default;
public Task<UserInfo> GetUser(string name) => Task.FromResult(new UserInfo
{
UserName = name, SaLevel = name == "admin" ? UserAdminLevel.SuperAdmin : UserAdminLevel.Normal
});
public StubHttp Handler { get; } = new();
public PddTokenClient Gateway { get; }
public ShopManagementService Service { get; }
public IShopRepository Repository { get; }
public IConfiguration Configuration { get; } = new ConfigurationBuilder().AddInMemoryCollection(new Dictionary<string, string?>
{
["Shops:PddClients:0:ClientId"] = "test-client",
["Shops:PddClients:0:ClientSecret"] = "test-secret",
["Shops:PddClients:0:RedirectUri"] = "https://example.test/callback"
}).Build();
public ShopTestContext()
{
Db = new FreeSqlBuilder().UseMappingPriority(FreeSql.Internal.MappingPriorityType.Attribute,
FreeSql.Internal.MappingPriorityType.Aop, FreeSql.Internal.MappingPriorityType.FluentApi)
.UseConnectionString(DataType.Sqlite, $"Data Source={path};Pooling=False").Build();
Db.CodeFirst.ConfigEntity<PddShopEntity>(e => e.Name("pdd_shops"));
Db.CodeFirst.ConfigEntity<WeidianShopEntity>(e => e.Name("weidian_shops"));
Db.CodeFirst.ConfigEntity<ShopApiAuthEntity>(e => e.Name("shop_auth"));
Db.CodeFirst.ConfigEntity<ShopProductQuotaEntity>(e => e.Name("shop_quota"));
Db.CodeFirst.SyncStructure(typeof(PddShopEntity), typeof(WeidianShopEntity), typeof(ShopApiAuthEntity), typeof(ShopProductQuotaEntity));
Db.CodeFirst.ConfigEntity<ShopEntity>(e => e.Name("shops"));
Db.CodeFirst.ConfigEntity<ShopExpireEntity>(e => e.Name("shop_expirations"));
Db.CodeFirst.SyncStructure(typeof(ShopEntity), typeof(ShopExpireEntity));
Repository = (IShopRepository)Activator.CreateInstance(
typeof(ShopService).Assembly.GetType("Store.Shops.Repositories.ShopRepository", true)!, Db)!;
Gateway = new(new HttpClient(Handler));
Service = new(Db, this, this, Configuration, Gateway);
}
public void Dispose()
{
Gateway.Dispose();
Db.Dispose();
File.Delete(path);
}
}
internal sealed class StubHttp : HttpMessageHandler
{
public Queue<string> Responses { get; } = new();
public int Calls { get; private set; }
public Uri? LastUri { get; private set; }
protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken ct)
{
ct.ThrowIfCancellationRequested();
Calls++;
LastUri = request.RequestUri;
return Task.FromResult(new HttpResponseMessage(HttpStatusCode.OK)
{
Content = new StringContent(Responses.Count == 0 ? """{"code":0,"data":{"accessToken":"test-token"}}""" : Responses.Dequeue())
});
}
}