Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions tests/Carina.Api.Tests/Feature/Epg/ProgrammeFeedStreamTests.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using System.Net;

using Carina.Api.Events;
using Carina.Api.Tests.FeatureTest;
using Carina.Domain.Auth;
using Carina.Domain.Base;
using Carina.Domain.Channels;
using Carina.Domain.Programmes;
Expand All @@ -13,6 +15,8 @@ public sealed class ProgrammeFeedStreamTests
{
private static readonly DateTime At = new(2026, 8, 18, 12, 0, 0, DateTimeKind.Utc);

private static readonly Uri Bulk = new(ProgrammeFeedStream.Path, UriKind.Relative);

[Fact]
public async Task TheFeedHandsBackOneProgrammePerLineAndSaysHowFarItGot()
{
Expand Down Expand Up @@ -205,6 +209,48 @@ public async Task ARebuildDiscardsTheGuideAndAFreshCollectionRestoresIt()
StringComparison.Ordinal);
}

[Fact]
public async Task TheFeedIsHandedToACallerCarryingASessionCookie()
{
await using AuthProbe probe = AuthProbe.OverHttp();
await probe.SignedInAsync();

using HttpResponseMessage response = await probe.Client.GetAsync(
Bulk,
HttpCompletionOption.ResponseHeadersRead);

Assert.Equal(HttpStatusCode.OK, response.StatusCode);
Assert.Equal(ProgrammeFeedStream.ContentType, response.Content.Headers.ContentType?.MediaType);
}

[Fact]
public async Task TheFeedIsRefusedToACallerCarryingNoCookie()
{
await using AuthProbe probe = AuthProbe.OverHttp().WithAnAccount();

using HttpResponseMessage response = await probe.Client.GetAsync(
Bulk,
HttpCompletionOption.ResponseHeadersRead);

Assert.Equal(HttpStatusCode.Unauthorized, response.StatusCode);
Assert.Empty(await response.Content.ReadAsByteArrayAsync());
}

[Fact]
public async Task TheNextFeedAfterThatSessionEndedIsRefused()
{
await using AuthProbe probe = AuthProbe.OverHttp();
AuthSession session = await probe.SignedInAsync();

session.Revoke(DateTime.UtcNow);

using HttpResponseMessage response = await probe.Client.GetAsync(
Bulk,
HttpCompletionOption.ResponseHeadersRead);

Assert.Equal(HttpStatusCode.Unauthorized, response.StatusCode);
}

private static string? Cursor(HttpResponseMessage response)
=> response.Headers.TryGetValues(ProgrammeFeedStream.CursorHeader, out IEnumerable<string>? carried)
? carried.First()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,120 +1,165 @@
using System.Net;
using System.Net.Http.Headers;
using System.Text.Json.Nodes;

using Carina.Api.Events;
using Carina.Api.Tests.FeatureTest;
using Carina.Domain.Auth;
using Carina.Infrastructure.Events;

using Microsoft.AspNetCore.Mvc.Testing;
using Microsoft.Extensions.DependencyInjection;

using Puppis.Contracts;

namespace Carina.Api.Tests.FeatureTest;
namespace Carina.Api.Tests.Feature.Events;

public sealed class LongLivedConnectionTests
[Trait("Category", "Feature")]
public sealed class AppEventStreamTests(TestingWebApplicationFactory factory)
: IClassFixture<TestingWebApplicationFactory>
{
private static readonly Uri Events = new(AppEventStream.Path, UriKind.Relative);

private static readonly Uri Bulk = new(ProgrammeFeedStream.Path, UriKind.Relative);

[Fact]
public async Task AStreamIsOpenedForACallerCarryingASessionCookie()
public async Task TheStreamIsBehindTheSameDenialAsEveryOtherSurfaceOnceASchemeIsRegistered()
{
await using AuthProbe probe = AuthProbe.OverHttp();
await probe.SignedInAsync();
using HttpClient client = factory.WithTestScheme().CreateClient();
using HttpResponseMessage response = await client.GetAsync(Events);

using HttpResponseMessage response = await probe.Client.GetAsync(
Events,
HttpCompletionOption.ResponseHeadersRead);
Assert.Equal(HttpStatusCode.Unauthorized, response.StatusCode);
}

[Fact]
public async Task AnAuthenticatedListenerIsAnsweredWithAnEventStream()
{
using HttpClient client = factory.CreateAuthenticatedClient();
using HttpResponseMessage response = await client.GetAsync(Events, HttpCompletionOption.ResponseHeadersRead);

Assert.Equal(HttpStatusCode.OK, response.StatusCode);
Assert.Equal(AppEventStream.ContentType, response.Content.Headers.ContentType?.MediaType);
}

[Fact]
public async Task AStreamIsRefusedToACallerCarryingNoCookie()
public async Task ASignalRaisedOnTheHubArrivesOnTheOpenStream()
{
using WebApplicationFactory<Program> authenticated = factory.WithTestScheme();
AppEventHub hub = authenticated.Services.GetRequiredService<AppEventHub>();

using HttpClient client = authenticated.CreateClient();
client.DefaultRequestHeaders.Authorization =
new AuthenticationHeaderValue(TestAuthenticationHandler.SchemeName, "anything");

using HttpResponseMessage response = await client.GetAsync(Events, HttpCompletionOption.ResponseHeadersRead);
await using Stream body = await response.Content.ReadAsStreamAsync();
using var reader = new StreamReader(body);

hub.Signal(AppEventName.Tuners);

string? name = await reader.ReadLineAsync();

Assert.Equal("event: tuners", name);
}

[Fact]
public async Task TheStreamIsAbsentFromTheDocumentThatNamesIt()
{
JsonNode document = await ServedOpenApi.FetchAsync(factory);

Assert.DoesNotContain(
AppEventStream.Path,
document["paths"]!.AsObject().Select(path => path.Key),
StringComparer.Ordinal);
}

[Fact]
public async Task AListenerCarryingNoCookieIsRefusedRatherThanHandedAStreamThatCarriesTheRefusal()
{
await using AuthProbe probe = AuthProbe.OverHttp().WithAnAccount();
probe.Client.DefaultRequestHeaders.Accept.Add(
new MediaTypeWithQualityHeaderValue(AppEventStream.ContentType));

using HttpResponseMessage response = await probe.Client.GetAsync(
Events,
HttpCompletionOption.ResponseHeadersRead);

Assert.Equal(HttpStatusCode.Unauthorized, response.StatusCode);
Assert.NotEqual(AppEventStream.ContentType, response.Content.Headers.ContentType?.MediaType);
Assert.Empty(response.Headers.WwwAuthenticate);
Assert.Null(response.Headers.Location);
Assert.Empty(await response.Content.ReadAsByteArrayAsync());
}

[Fact]
public async Task AStreamThatIsAlreadyOpenKeepsCarryingSignalsAfterItsSessionIsEnded()
public async Task AStreamOpenedForACallerIsMarkedSoNothingInFrontHandsItToTheNextCaller()
{
await using AuthProbe probe = AuthProbe.OverHttp();
AuthSession session = await probe.SignedInAsync();
AppEventHub hub = probe.Wired.Services.GetRequiredService<AppEventHub>();
await probe.SignedInAsync();

using HttpResponseMessage response = await probe.Client.GetAsync(
Events,
HttpCompletionOption.ResponseHeadersRead);
await using Stream body = await response.Content.ReadAsStreamAsync();
using var reader = new StreamReader(body);

session.Revoke(DateTime.UtcNow);

hub.Signal(AppEventName.Tuners);

Assert.Equal(HttpStatusCode.OK, response.StatusCode);
Assert.Equal("event: tuners", await reader.ReadLineAsync());
Assert.True(response.Headers.CacheControl?.NoCache);
}

[Fact]
public async Task TheNextConnectionAfterThatSessionEndedIsRefused()
public async Task AStreamIsOpenedForACallerCarryingASessionCookie()
{
await using AuthProbe probe = AuthProbe.OverHttp();
AuthSession session = await probe.SignedInAsync();

session.Revoke(DateTime.UtcNow);
await probe.SignedInAsync();

using HttpResponseMessage response = await probe.Client.GetAsync(
Events,
HttpCompletionOption.ResponseHeadersRead);

Assert.Equal(HttpStatusCode.Unauthorized, response.StatusCode);
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
Assert.Equal(AppEventStream.ContentType, response.Content.Headers.ContentType?.MediaType);
}

[Fact]
public async Task TheFeedIsHandedToACallerCarryingASessionCookie()
public async Task AStreamIsRefusedToACallerCarryingNoCookie()
{
await using AuthProbe probe = AuthProbe.OverHttp();
await probe.SignedInAsync();
await using AuthProbe probe = AuthProbe.OverHttp().WithAnAccount();

using HttpResponseMessage response = await probe.Client.GetAsync(
Bulk,
Events,
HttpCompletionOption.ResponseHeadersRead);

Assert.Equal(HttpStatusCode.OK, response.StatusCode);
Assert.Equal(ProgrammeFeedStream.ContentType, response.Content.Headers.ContentType?.MediaType);
Assert.Equal(HttpStatusCode.Unauthorized, response.StatusCode);
}

[Fact]
public async Task TheFeedIsRefusedToACallerCarryingNoCookie()
public async Task AStreamThatIsAlreadyOpenKeepsCarryingSignalsAfterItsSessionIsEnded()
{
await using AuthProbe probe = AuthProbe.OverHttp().WithAnAccount();
await using AuthProbe probe = AuthProbe.OverHttp();
AuthSession session = await probe.SignedInAsync();
AppEventHub hub = probe.Wired.Services.GetRequiredService<AppEventHub>();

using HttpResponseMessage response = await probe.Client.GetAsync(
Bulk,
Events,
HttpCompletionOption.ResponseHeadersRead);
await using Stream body = await response.Content.ReadAsStreamAsync();
using var reader = new StreamReader(body);

Assert.Equal(HttpStatusCode.Unauthorized, response.StatusCode);
Assert.Empty(await response.Content.ReadAsByteArrayAsync());
session.Revoke(DateTime.UtcNow);

hub.Signal(AppEventName.Tuners);

Assert.Equal(HttpStatusCode.OK, response.StatusCode);
Assert.Equal("event: tuners", await reader.ReadLineAsync());
}

[Fact]
public async Task TheNextFeedAfterThatSessionEndedIsRefused()
public async Task TheNextConnectionAfterThatSessionEndedIsRefused()
{
await using AuthProbe probe = AuthProbe.OverHttp();
AuthSession session = await probe.SignedInAsync();

session.Revoke(DateTime.UtcNow);

using HttpResponseMessage response = await probe.Client.GetAsync(
Bulk,
Events,
HttpCompletionOption.ResponseHeadersRead);

Assert.Equal(HttpStatusCode.Unauthorized, response.StatusCode);
Expand Down
Loading
Loading