using System.Net;
using GameList.Tests.Support;

namespace GameList.Tests;

public class MiddlewareTests
{
    [Fact]
    public async Task Deleted_player_cookie_is_signed_out()
    {
        using var factory = new TestWebApplicationFactory();
        var client = factory.CreateClientWithCookies();
        await client.RegisterAsync("ghost");

        var playerId = await client.GetProfileIdAsync();

        await factory.WithDbContextAsync(async db =>
        {
            var player = await db.Players.FindAsync(playerId);
            db.Players.Remove(player!);
            await db.SaveChangesAsync();
        });

        var resp = await client.GetAsync("/api/state");
        Assert.Equal(HttpStatusCode.Unauthorized, resp.StatusCode);
        Assert.Contains(resp.Headers, h => h.Key.Equals("Set-Cookie", StringComparison.OrdinalIgnoreCase));
    }
}
