using Febe.Core.Abstractions;
using Febe.Core.Collection;
using Febe.Core.Collection.Requests;
using Febe.SitoDiProva;
using Febe.Tests.Support;
using VirtualClock = Febe.Tests.Browser.TestSupport.VirtualClock;
namespace Febe.Tests.Collection;
///
/// What the test site gives its pages for the copy at «Ho finito» (issue #36, S4, spec §14.2): the cookies a real site
/// sets (the session, csrftoken, one limited to /api/), a module registry of its own whose names are the
/// page section of richieste.prova.json (never Instagram's), and a /api/graphql that refuses a
/// wrong or missing value of the page as it refuses a wrong token. No browser.
///
[TestClass]
[TestCategory(SyntheticSite.Category)]
public sealed class TestSiteSessionTests
{
public TestContext TestContext { get; set; } = null!;
[TestMethod]
public async Task AVisitSetsTheSessionCsrftokenAndTheApiPathCookie()
{
await using var site = await SyntheticSite.StartAsync();
using var client = new HttpClient(new SocketsHttpHandler { UseProxy = false, UseCookies = false, AllowAutoRedirect = false });
using var response = await client.GetAsync(new Uri(site.Url("/")), TestContext.CancellationToken);
var lines = response.Headers.GetValues("Set-Cookie").ToList();
string Line(string name) => lines.Single(l => l.StartsWith(name + "=", StringComparison.Ordinal));
StringAssert.Contains(Line(TestSite.SessionCookie), "path=/;");
StringAssert.Contains(Line(TestSite.SessionCookie).ToLowerInvariant(), "httponly");
var csrf = Line(TestSite.CsrfCookie);
StringAssert.Contains(csrf, "path=/;");
Assert.DoesNotContain("httponly", csrf.ToLowerInvariant(), "the page reads it to send it back in x-csrftoken");
StringAssert.Contains(Line(TestSite.ApiPathCookie), "path=/api/");
// A second visit with the session and the token keeps them: no new cookie.
var session = Line(TestSite.SessionCookie).Split(';')[0];
var token = csrf.Split(';')[0];
using var again = new HttpRequestMessage(HttpMethod.Get, new Uri(site.Url("/")));
again.Headers.TryAddWithoutValidation("Cookie", $"{session}; {token}");
using var second = await client.SendAsync(again, TestContext.CancellationToken);
Assert.IsFalse(second.Headers.Contains("Set-Cookie"));
}
[TestMethod]
public async Task ThePageModulesAreThoseOfTheCatalogsPageSectionWithTheValuesTheSiteChecks()
{
var catalog = RequestCatalog.LoadTestSite();
var page = catalog.Page!;
var modules = TestSite.PageModules("1234");
var expected = HttpSiteRequesterTests.PageValues();
foreach (var field in page.Order)
{
var source = page.Values[field];
Assert.IsTrue(modules.TryGetValue(source.Module, out var module), $"{field}: module {source.Module}");
Assert.IsTrue(module.TryGetValue(source.Property, out var value), $"{field}: {source}");
Assert.AreEqual(field == "account_id" ? "1234" : expected[field], value, field);
}
foreach (var operation in catalog.Operations.Where(o => o.Kind == OperationKind.GraphQl))
{
var module = page.DocIdModuleFor(operation.Name)!;
Assert.AreEqual(operation.Name + TestSite.DocIdModuleSuffix, module);
Assert.AreEqual(TestSite.DocIds[operation.Name], modules[module][string.Empty]);
}
// Never Instagram's names in the test site.
foreach (var name in modules.Keys)
{
Assert.DoesNotContain("instagram", name.ToLowerInvariant());
Assert.IsFalse(RequestCatalog.LoadInstagram().Page!.Values.Values.Any(s => s.Module == name), name);
}
// Every page of the site carries the registry first in its head.
await using var site = await SyntheticSite.StartAsync();
using var client = new HttpClient(new SocketsHttpHandler { UseProxy = false, UseCookies = false, AllowAutoRedirect = false });
foreach (var path in new[] { "/", "/benvenuto", "/profilo/marta.prova/" })
{
var html = await client.GetStringAsync(new Uri(site.Url(path)), TestContext.CancellationToken);
StringAssert.Contains(html, "