Skip to content

Commit

Permalink
Merge pull request #2 from 0xor1/init-bunit-test
Browse files Browse the repository at this point in the history
Init bunit test
  • Loading branch information
0xor1 authored Dec 6, 2023
2 parents b288ce5 + 9781340 commit 391a0d5
Show file tree
Hide file tree
Showing 28 changed files with 85 additions and 75 deletions.
35 changes: 23 additions & 12 deletions Common.Client/Client.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,19 +35,30 @@ public static async Task Run<TApp, TApi>(
ns.Notify(NotificationSeverity.Error, l.S(CS.ApiError), message, duration: 6000D),
enableRequestStreaming
);
var api = apiFactory(rpcClient);
builder.Services.AddSingleton(httpClient);
builder.Services.AddSingleton<IRpcClient>(rpcClient);
builder.Services.AddSingleton(s);
builder.Services.AddSingleton<L>(l);
builder.Services.AddSingleton(api);
builder.Services.AddSingleton<IApi>(api);
builder.Services.AddSingleton<IAuthService, AuthService<TApi>>();
builder.Services.AddSingleton(ns);
builder.Services.AddSingleton<DialogService>();
builder.Services.AddSingleton<TooltipService>();
builder.Services.AddSingleton<ContextMenuService>();
addServices?.Invoke(builder.Services);
Setup(builder.Services, l, s, ns, apiFactory(rpcClient), addServices);
await builder.Build().RunAsync();
}

public static void Setup<TApi>(
IServiceCollection services,
L l,
S s,
NotificationService ns,
TApi api,
Action<IServiceCollection>? addServices = null
)
where TApi : class, IApi
{
services.AddSingleton(s);
services.AddSingleton(l);
services.AddSingleton(api);
services.AddSingleton<IApi>(api);
services.AddSingleton<IAuthService, AuthService<TApi>>();
services.AddSingleton(ns);
services.AddSingleton<DialogService>();
services.AddSingleton<TooltipService>();
services.AddSingleton<ContextMenuService>();
addServices?.Invoke(services);
}
}
7 changes: 2 additions & 5 deletions Common.Client/Common.Client.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<PackageId>0xor1.Common.Client</PackageId>
<Version>3.0.12</Version>
<Version>3.0.13</Version>
<Authors>Daniel Robinson</Authors>
<Company>Personal</Company>
<Product>Personal</Product>
Expand All @@ -18,7 +18,7 @@

<ItemGroup>
<PackageReference Include="DartSassBuilder" Version="1.0.0" />
<PackageReference Include="0xor1.Common.Shared" Version="3.0.12" />
<PackageReference Include="0xor1.Common.Shared" Version="3.0.13" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly" Version="8.0.0" />
<PackageReference Include="Microsoft.AspNetCore.Components.Web" Version="8.0.0" />
<PackageReference Include="Microsoft.AspNetCore.WebUtilities" Version="8.0.0" />
Expand All @@ -31,7 +31,4 @@
<ItemGroup>
<None Include="..\README.md" Pack="true" PackagePath="\" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Common.Shared\Common.Shared.csproj" />
</ItemGroup>
</Project>
2 changes: 1 addition & 1 deletion Common.Client/Localizer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Common.Client;

internal class Localizer : L
public class Localizer : L
{
private readonly S _s;
private DateFmt _dateFmt;
Expand Down
8 changes: 4 additions & 4 deletions Common.Server/Auth/AuthEps.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ public class AuthEps<TDbCtx>
{
private readonly int _maxAuthAttemptsPerSecond;
private readonly Func<IRpcCtx, TDbCtx, string, string, Task> _onActivation;
private readonly Func<IRpcCtx, TDbCtx, Session, Task> _onDelete;
private readonly Func<IRpcCtx, TDbCtx, Session, IReadOnlyList<string>, Task> _validateFcmTopic;
private readonly Func<IRpcCtx, TDbCtx, ISession, Task> _onDelete;
private readonly Func<IRpcCtx, TDbCtx, ISession, IReadOnlyList<string>, Task> _validateFcmTopic;

public AuthEps(
int maxAuthAttemptsPerSecond,
Func<IRpcCtx, TDbCtx, string, string, Task> onActivation,
Func<IRpcCtx, TDbCtx, Session, Task> onDelete,
Func<IRpcCtx, TDbCtx, Session, IReadOnlyList<string>, Task> validateFcmTopic
Func<IRpcCtx, TDbCtx, ISession, Task> onDelete,
Func<IRpcCtx, TDbCtx, ISession, IReadOnlyList<string>, Task> validateFcmTopic
)
{
_maxAuthAttemptsPerSecond = maxAuthAttemptsPerSecond;
Expand Down
5 changes: 3 additions & 2 deletions Common.Server/Auth/CommonEps.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using Common.Shared.Auth;
using Microsoft.EntityFrameworkCore;

namespace Common.Server.Auth;
Expand All @@ -10,8 +11,8 @@ public class CommonEps<TDbCtx>
public CommonEps(
int maxAuthAttemptsPerSecond,
Func<IRpcCtx, TDbCtx, string, string, Task> onActivation,
Func<IRpcCtx, TDbCtx, Session, Task> onDelete,
Func<IRpcCtx, TDbCtx, Session, IReadOnlyList<string>, Task> validateFcmTopic
Func<IRpcCtx, TDbCtx, ISession, Task> onDelete,
Func<IRpcCtx, TDbCtx, ISession, IReadOnlyList<string>, Task> validateFcmTopic
)
{
_authEps = new AuthEps<TDbCtx>(
Expand Down
4 changes: 2 additions & 2 deletions Common.Server/Common.Server.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<PackageId>0xor1.Common.Server</PackageId>
<Version>3.0.12</Version>
<Version>3.0.13</Version>
<Authors>Daniel Robinson</Authors>
<Company>Personal</Company>
<Product>Personal</Product>
Expand All @@ -17,7 +17,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="0xor1.Common.Shared" Version="3.0.12" />
<PackageReference Include="0xor1.Common.Shared" Version="3.0.13" />
<PackageReference Include="AWSSDK.S3" Version="3.7.300.2" />
<PackageReference Include="AWSSDK.SimpleEmail" Version="3.7.300.2" />
<PackageReference Include="CSharpier.MsBuild" Version="0.26.2">
Expand Down
2 changes: 1 addition & 1 deletion Common.Server/FcmClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public async Task Send(Message msg, bool fnf = true, CancellationToken ctkn = de
public async Task SendTopic<TDbCtx>(
IRpcCtx ctx,
TDbCtx db,
Session ses,
ISession ses,
IReadOnlyList<string> topic,
object? data,
bool fnf = true
Expand Down
2 changes: 1 addition & 1 deletion Common.Server/FcmNopClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public async Task Send(Message msg, bool fnf = true, CancellationToken ctkn = de
public async Task SendTopic<TDbCtx>(
IRpcCtx ctx,
TDbCtx db,
Session ses,
ISession ses,
IReadOnlyList<string> topic,
object? data,
bool fnf = true
Expand Down
2 changes: 1 addition & 1 deletion Common.Server/IFcmClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public interface IFcmClient
Task SendTopic<TDbCtx>(
IRpcCtx ctx,
TDbCtx db,
Session ses,
ISession ses,
IReadOnlyList<string> topic,
object? data,
bool fnf = true
Expand Down
7 changes: 4 additions & 3 deletions Common.Server/IRpcCtx.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Common.Shared;
using Common.Shared.Auth;

namespace Common.Server;

Expand All @@ -10,9 +11,9 @@ public T Get<T>()
public T GetFeature<T>()
where T : notnull;

public Session GetSession();
public ISession GetSession();

public Session CreateSession(
public ISession CreateSession(
string userId,
bool isAuthed,
bool rememberMe,
Expand All @@ -24,7 +25,7 @@ public Session CreateSession(
string decimalSeparator,
bool fcmEnabled
);
public Session ClearSession();
public ISession ClearSession();

public string? GetHeader(string name);

Expand Down
7 changes: 4 additions & 3 deletions Common.Server/IRpcHttpSessionManager.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
using Common.Shared;
using Microsoft.AspNetCore.Http;
using ISession = Common.Shared.Auth.ISession;

namespace Common.Server;

public interface IRpcHttpSessionManager
{
internal Session Get(HttpContext ctx);
internal ISession Get(HttpContext ctx);

internal Session Create(
internal ISession Create(
HttpContext ctx,
string userId,
bool isAuthed,
Expand All @@ -21,5 +22,5 @@ internal Session Create(
bool fcmEnabled
);

internal Session Clear(HttpContext ctx);
internal ISession Clear(HttpContext ctx);
}
5 changes: 3 additions & 2 deletions Common.Server/RpcCtxExts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using CS = Common.Shared.I18n.S;
using ISession = Common.Shared.Auth.ISession;

namespace Common.Server;

Expand All @@ -17,7 +18,7 @@ public static T Get<T>(this HttpContext ctx)
public static T GetFeature<T>(this HttpContext ctx)
where T : notnull => ctx.Features.GetRequiredFeature<T>();

public static Session GetAuthedSession(this IRpcCtx ctx)
public static ISession GetAuthedSession(this IRpcCtx ctx)
{
var ses = ctx.GetSession();
ctx.ErrorIf(ses.IsAnon, CS.AuthNotAuthenticated, null, HttpStatusCode.Unauthorized);
Expand All @@ -26,7 +27,7 @@ public static Session GetAuthedSession(this IRpcCtx ctx)

public static async Task<TRes> DbTx<TDbCtx, TRes>(
this IRpcCtx ctx,
Func<TDbCtx, Session, Task<TRes>> fn,
Func<TDbCtx, ISession, Task<TRes>> fn,
bool mustBeAuthedSession = true
)
where TDbCtx : DbContext
Expand Down
7 changes: 4 additions & 3 deletions Common.Server/RpcHttpCtx.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.Net;
using Common.Shared;
using Microsoft.AspNetCore.Http;
using ISession = Common.Shared.Auth.ISession;

namespace Common.Server;

Expand All @@ -17,9 +18,9 @@ public RpcHttpCtx(HttpContext ctx)

public CancellationToken Ctkn => _ctx.RequestAborted;

public Session GetSession() => _sessionManager.Get(_ctx);
public ISession GetSession() => _sessionManager.Get(_ctx);

public Session CreateSession(
public ISession CreateSession(
string userId,
bool isAuthed,
bool rememberMe,
Expand All @@ -45,7 +46,7 @@ bool fcmEnabled
fcmEnabled
);

public Session ClearSession() => _sessionManager.Clear(_ctx);
public ISession ClearSession() => _sessionManager.Clear(_ctx);

public string? GetHeader(string name) =>
_ctx.Request.Headers.ContainsKey(name) ? _ctx.Request.Headers[name].ToString() : null;
Expand Down
11 changes: 6 additions & 5 deletions Common.Server/RpcHttpSessionManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Common.Shared;
using MessagePack;
using Microsoft.AspNetCore.Http;
using ISession = Common.Shared.Auth.ISession;

namespace Common.Server;

Expand All @@ -27,9 +28,9 @@ public RpcHttpSessionManager(IReadOnlyList<string> signatureKeys, S s)
);
}

public Session Get(HttpContext ctx)
public ISession Get(HttpContext ctx)
{
Session ses;
ISession ses;
if (!ctx.Items.ContainsKey(SessionKey))
{
ses = GetCookie(ctx);
Expand All @@ -43,7 +44,7 @@ public Session Get(HttpContext ctx)
return ses;
}

public Session Create(
public ISession Create(
HttpContext ctx,
string userId,
bool isAuthed,
Expand Down Expand Up @@ -76,7 +77,7 @@ bool fcmEnabled
return ses;
}

public Session Clear(HttpContext ctx)
public ISession Clear(HttpContext ctx)
{
var ses = _Clear(ctx);
ctx.Items[SessionKey] = ses;
Expand All @@ -85,7 +86,7 @@ public Session Clear(HttpContext ctx)

private Session _Clear(HttpContext ctx)
{
return Create(
return (Session)Create(
ctx,
Id.New(),
false,
Expand Down
3 changes: 2 additions & 1 deletion Common.Server/Session.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
using Common.Shared;
using Common.Shared.Auth;
using MessagePack;

namespace Common.Server;

[MessagePackObject]
public record Session
public record Session : ISession
{
[Key(0)]
public string Id { get; init; }
Expand Down
12 changes: 6 additions & 6 deletions Common.Server/Test/RpcTestCtx.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public record RpcTestCtx : IRpcCtxInternal
public RpcTestCtx(
IServiceProvider services,
IFeatureCollection features,
Session? session,
ISession? session,
S s,
Dictionary<string, string> headers,
object arg,
Expand All @@ -30,7 +30,7 @@ CancellationToken ctkn
_services = services;
_features = features;
_s = s;
Session = session ?? ClearSession();
Session = (Session)(session ?? ClearSession());
Headers = headers;
Arg = arg;
Ctkn = ctkn;
Expand All @@ -44,9 +44,9 @@ public T Get<T>()
public T GetFeature<T>()
where T : notnull => _features.GetRequiredFeature<T>();

public Session GetSession() => Session;
public ISession GetSession() => Session;

public Session CreateSession(
public ISession CreateSession(
string userId,
bool isAuthed,
bool rememberMe,
Expand Down Expand Up @@ -76,9 +76,9 @@ bool fcmEnabled
return Session;
}

public Session ClearSession()
public ISession ClearSession()
{
Session = new()
Session = new Session()
{
Id = Id.New(),
IsAuthed = false,
Expand Down
5 changes: 3 additions & 2 deletions Common.Server/Test/RpcTestRig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Common.Server.Auth;
using Common.Shared;
using Common.Shared.Auth;
using Common.Shared.Test;
using Microsoft.AspNetCore.Http.Features;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
Expand Down Expand Up @@ -58,9 +59,9 @@ public T RunDb<T>(Func<TDbCtx, T> fn)
return fn(db);
}

private async Task<(Session, object)> Exe(
private async Task<(ISession, object)> Exe(
string path,
Session? session,
ISession? session,
Dictionary<string, string> headers,
object arg,
CancellationToken ctkn = default
Expand Down
Loading

0 comments on commit 391a0d5

Please sign in to comment.