Read-only mirrors. Development, issues and pull requests are on GitHub.

summaryrefslogtreecommitdiffhomepage
path: root/backend/Progmasoft.Tala.Website.Api.Tests/SiteEndpointTests.cs
blob: 39cb8d48808f05e2a4ba44bc6f0e3453d94110ce (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
// SPDX-FileCopyrightText: 2026 Progmasoft <support@progmasoft.com>
// SPDX-License-Identifier: AGPL-3.0-or-later WITH AdditionRef-Progmasoft-Patent-Grant-1.1

using System.Net;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.TestHost;
using Xunit;

namespace Progmasoft.Tala.Website.Api.Tests;

/// <summary>The endpoints of the site, served from an in-memory host.</summary>
public sealed class SiteEndpointTests : IAsyncLifetime
{
    private WebApplication? application;
    private HttpClient? client;

    private HttpClient Client => client ?? throw new InvalidOperationException("the host has not started");

    public async ValueTask InitializeAsync()
    {
        WebApplicationBuilder builder = WebApplication.CreateBuilder();
        builder.WebHost.UseTestServer();
        application = builder.Build();
        SiteApplication.Map(application);
        await application.StartAsync(TestContext.Current.CancellationToken);
        client = application.GetTestClient();
    }

    public async ValueTask DisposeAsync()
    {
        client?.Dispose();
        if (application is not null) await application.DisposeAsync();
    }

    [Fact]
    public async Task SiteInformationIsAboutTalaRatherThanThePackageRegistry()
    {
        using HttpResponseMessage response =
            await Client.GetAsync(new Uri("/api/v1/site", UriKind.Relative), TestContext.Current.CancellationToken);
        string body = await response.Content.ReadAsStringAsync(TestContext.Current.CancellationToken);

        Assert.Equal(HttpStatusCode.OK, response.StatusCode);
        Assert.Equal("application/json", response.Content.Headers.ContentType?.MediaType);
        Assert.Equal(
            """{"name":"Tala","phase":"development","defaultLocale":"en-US","supportedLocales":["en-US","de-DE","ru-RU","he-IL"],"forumOpen":false}""",
            body);
        Assert.DoesNotContain("viget", body, StringComparison.OrdinalIgnoreCase);
    }

    [Fact]
    public async Task HealthHasAnEmptySuccessfulResponse()
    {
        using HttpResponseMessage response =
            await Client.GetAsync(new Uri("/health", UriKind.Relative), TestContext.Current.CancellationToken);

        Assert.Equal(HttpStatusCode.OK, response.StatusCode);
        Assert.Equal(string.Empty, await response.Content.ReadAsStringAsync(TestContext.Current.CancellationToken));
    }

    [Fact]
    public async Task SiteInformationIsReadOnly()
    {
        using StringContent empty = new(string.Empty);
        using HttpResponseMessage response = await Client.PostAsync(
            new Uri("/api/v1/site", UriKind.Relative), empty, TestContext.Current.CancellationToken);

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

    [Theory]
    [InlineData("/api/v1/packages")]
    [InlineData("/api/v1/accounts")]
    [InlineData("/repo")]
    [InlineData("/")]
    public async Task RegistryAndAccountEndpointsAreAbsent(string path)
    {
        using HttpResponseMessage response =
            await Client.GetAsync(new Uri(path, UriKind.Relative), TestContext.Current.CancellationToken);

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

    [Fact]
    public void TheServiceListensOnTheFormerPortUnlessToldOtherwise() => Assert.Equal(5086, SiteApplication.DefaultPort);
}