156 lines
6.3 KiB
C#
156 lines
6.3 KiB
C#
using System.Text.Json;
|
|
using Bifrost.Web.Data;
|
|
using Bifrost.Web.Features.Invites;
|
|
using Bifrost.Web.Tests.Support;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
|
namespace Bifrost.Web.Tests;
|
|
|
|
/// <summary>A Google sign-in start asks Heimdall for an invite grant only while it carries a live invite; redemption stays ChatInviteService's.</summary>
|
|
public sealed class GoogleInviteSignInTests
|
|
{
|
|
private static async Task<(string Token, Guid Id)> MintAsync(HeimdallHost host)
|
|
{
|
|
using var scope = host.Services.CreateScope();
|
|
var service = scope.ServiceProvider.GetRequiredService<ChatInviteService>();
|
|
var (invite, token) = await service.MintAsync(Guid.NewGuid(), "google", CancellationToken.None);
|
|
|
|
await scope.ServiceProvider.GetRequiredService<BifrostDbContext>().SaveChangesAsync();
|
|
return (token, invite.Id);
|
|
}
|
|
|
|
private static string Grant(HeimdallHost host)
|
|
{
|
|
var root = JsonDocument.Parse(host.Heimdall.StartBodies[^1]).RootElement;
|
|
return root.TryGetProperty("firstLoginGrant", out var grant) ? grant.GetString()! : "<absent>";
|
|
}
|
|
|
|
private static string Jar(string token) => $"{ChatInviteService.CookieName}={token}";
|
|
|
|
[Fact]
|
|
public async Task Google_is_offered_in_the_sign_in_chooser()
|
|
{
|
|
using var host = new HeimdallHost();
|
|
using var client = host.NewClient();
|
|
var chooser = await client.GetStringAsync("/auth/sign-in?returnUrl=%2F");
|
|
Assert.Contains("/auth/heimdall/google", chooser);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task A_google_start_with_a_live_invite_carries_the_grant()
|
|
{
|
|
using var host = new HeimdallHost();
|
|
var (token, _) = await MintAsync(host);
|
|
|
|
var attempt = await host.StartAsync(provider: "google", cookieHeader: Jar(token));
|
|
|
|
Assert.NotEqual(string.Empty, attempt.Nonce);
|
|
Assert.Equal("invite", Grant(host));
|
|
}
|
|
|
|
[Fact]
|
|
public async Task A_google_start_with_no_cookie_or_a_junk_cookie_carries_no_grant()
|
|
{
|
|
using var host = new HeimdallHost();
|
|
await MintAsync(host);
|
|
|
|
foreach (var cookie in new string?[] { null, Jar("not-a-token") })
|
|
{
|
|
await host.StartAsync(provider: "google", cookieHeader: cookie);
|
|
Assert.Equal("<absent>", Grant(host));
|
|
}
|
|
}
|
|
|
|
[Fact]
|
|
public async Task A_google_start_with_a_redeemed_invite_carries_no_grant()
|
|
{
|
|
using var host = new HeimdallHost();
|
|
var (token, _) = await MintAsync(host);
|
|
using (var scope = host.Services.CreateScope())
|
|
{
|
|
var db = scope.ServiceProvider.GetRequiredService<BifrostDbContext>();
|
|
var hash = ChatInviteService.Hash(token);
|
|
(await db.ChatInvites.SingleAsync(x => x.TokenHash == hash)).RedeemedAtUtc = host.Clock.Now;
|
|
await db.SaveChangesAsync();
|
|
}
|
|
|
|
await host.StartAsync(provider: "google", cookieHeader: Jar(token));
|
|
Assert.Equal("<absent>", Grant(host));
|
|
}
|
|
|
|
[Fact]
|
|
public async Task A_google_start_with_a_revoked_invite_carries_no_grant()
|
|
{
|
|
using var host = new HeimdallHost();
|
|
var (token, id) = await MintAsync(host);
|
|
using (var scope = host.Services.CreateScope())
|
|
{
|
|
Assert.True(await scope.ServiceProvider.GetRequiredService<ChatInviteService>().RevokeAsync(id, Guid.NewGuid(), CancellationToken.None));
|
|
}
|
|
|
|
await host.StartAsync(provider: "google", cookieHeader: Jar(token));
|
|
Assert.Equal("<absent>", Grant(host));
|
|
}
|
|
|
|
[Fact]
|
|
public async Task A_google_start_with_an_expired_invite_carries_no_grant()
|
|
{
|
|
using var host = new HeimdallHost();
|
|
var (token, _) = await MintAsync(host);
|
|
await host.StartAsync(provider: "google", cookieHeader: Jar(token));
|
|
Assert.Equal("invite", Grant(host));
|
|
|
|
host.Clock.Advance(ChatInviteService.Lifetime + TimeSpan.FromMinutes(1));
|
|
|
|
await host.StartAsync(provider: "google", cookieHeader: Jar(token));
|
|
Assert.Equal("<absent>", Grant(host));
|
|
}
|
|
|
|
[Fact]
|
|
public async Task A_link_or_proof_start_with_a_live_invite_held_sends_no_grant()
|
|
{
|
|
using var host = new HeimdallHost();
|
|
var (token, _) = await MintAsync(host);
|
|
var cookies = new[] { new KeyValuePair<string, string>(ChatInviteService.CookieName, token) };
|
|
using var scope = host.Services.CreateScope();
|
|
var service = scope.ServiceProvider.GetRequiredService<Bifrost.Web.Features.Heimdall.HeimdallAuthService>();
|
|
|
|
foreach (var purpose in Enum.GetValues<Bifrost.Web.Features.Heimdall.HeimdallAttemptPurpose>().Where(x => x != Bifrost.Web.Features.Heimdall.HeimdallAttemptPurpose.SignIn))
|
|
{
|
|
var before = host.Heimdall.StartBodies.Count;
|
|
await service.StartAsync("google", "https://localhost", Bifrost.Web.Features.Shared.LocalReturnPath.Parse("/"), false, cookies, CancellationToken.None, purpose, Guid.NewGuid().ToString());
|
|
Assert.Equal(before + 1, host.Heimdall.StartBodies.Count);
|
|
Assert.Equal("<absent>", Grant(host));
|
|
}
|
|
}
|
|
|
|
[Fact]
|
|
public async Task A_discord_start_with_a_live_invite_is_unchanged()
|
|
{
|
|
using var host = new HeimdallHost();
|
|
var (token, _) = await MintAsync(host);
|
|
|
|
await host.StartAsync(provider: "discord", cookieHeader: Jar(token));
|
|
|
|
Assert.Equal("<absent>", Grant(host));
|
|
}
|
|
|
|
[Fact]
|
|
public async Task A_google_sign_in_under_the_grant_is_invited_by_the_redemption_alone()
|
|
{
|
|
using var host = new HeimdallHost();
|
|
var (token, _) = await MintAsync(host);
|
|
var accountId = HeimdallShapes.NewAccountId();
|
|
var attempt = await host.StartAsync(provider: "google", cookieHeader: Jar(token));
|
|
Assert.Equal("invite", Grant(host));
|
|
Assert.Null(await host.Account(accountId)); // the grant minted nothing and invited no one
|
|
|
|
var done = await host.CompleteWithHeaderAsync(attempt.Nonce, host.CodeFor(attempt, accountId), $"{attempt.CookieName}={attempt.Cookie}; {Jar(token)}");
|
|
|
|
Assert.True(done.SignedIn);
|
|
var account = await host.Account(accountId);
|
|
Assert.NotNull(account!.ChatInvitedAtUtc);
|
|
Assert.Equal(account.Id, (await host.Db(db => db.ChatInvites.AsNoTracking().SingleAsync(x => x.TokenHash == ChatInviteService.Hash(token)))).RedeemedByUserAccountId);
|
|
}
|
|
}
|