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

summaryrefslogtreecommitdiffhomepage
path: root/apps/api-tests/UnconfiguredGoogleTests.cs
blob: 9a7ca7784484062c33a025cbe16791ff48c52dd9 (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
87
88
89
90
91
92
93
94
95
96
97
// 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 System.Net.Http.Json;
using Progmasoft.Progmaweb.Api.Accounts;

namespace Progmasoft.Progmaweb.Api.Tests;

/// <summary>
/// The service without a Google client. It used to answer every request with 500, because the Google scheme was
/// registered with empty credentials and failed its option validation in the authentication middleware.
/// </summary>
public sealed class UnconfiguredGoogleTests
{
    [Theory]
    [InlineData("/api/v1/status", HttpStatusCode.OK)]
    [InlineData("/health", HttpStatusCode.OK)]
    [InlineData("/api/v1/accounts/me", HttpStatusCode.Unauthorized)]
    public async Task OrdinaryRoutesAnswerWithoutGoogle(string path, HttpStatusCode expected)
    {
        await using AccountApiFactory factory = new(configureGoogle: false);
        using HttpClient client = factory.CreateAccountClient();

        HttpResponseMessage response = await client.GetAsync(path, TestContext.Current.CancellationToken);

        Assert.Equal(expected, response.StatusCode);
    }

    [Fact]
    public async Task PasswordAccountsWorkWithoutGoogle()
    {
        await using AccountApiFactory factory = new(configureGoogle: false);
        using HttpClient client = factory.CreateAccountClient();
        const string password = "a sufficiently long passphrase";

        HttpResponseMessage registration = await client.PostAsJsonAsync(
            "/api/v1/accounts/register",
            new RegisterAccountRequest("LocalUser1", "local@example.com", password, true),
            TestContext.Current.CancellationToken);
        Assert.Equal(HttpStatusCode.Created, registration.StatusCode);

        // The registration set the session cookie, so the same client is now signed in.
        HttpResponseMessage me = await client.GetAsync("/api/v1/accounts/me", TestContext.Current.CancellationToken);
        Assert.Equal(HttpStatusCode.OK, me.StatusCode);

        HttpResponseMessage logout = await client.PostAsync(
            "/api/v1/accounts/logout", null, TestContext.Current.CancellationToken);
        Assert.Equal(HttpStatusCode.NoContent, logout.StatusCode);

        HttpResponseMessage login = await client.PostAsJsonAsync(
            "/api/v1/accounts/login",
            new LoginAccountRequest("local@example.com", password),
            TestContext.Current.CancellationToken);
        Assert.Equal(HttpStatusCode.OK, login.StatusCode);
    }

    [Fact]
    public async Task GoogleStartReportsThatSignInIsUnavailable()
    {
        await using AccountApiFactory factory = new(configureGoogle: false);
        using HttpClient client = factory.CreateAccountClient(handleCookies: false);

        HttpResponseMessage response = await client.GetAsync(
            "/api/v1/accounts/oauth/google/start?accountName=GoogleUser",
            TestContext.Current.CancellationToken);

        Assert.Equal(HttpStatusCode.ServiceUnavailable, response.StatusCode);
        Assert.Null(response.Headers.Location);
    }

    [Fact]
    public async Task GoogleCompleteFailsClosedWithoutGoogle()
    {
        await using AccountApiFactory factory = new(configureGoogle: false);
        using HttpClient client = factory.CreateAccountClient(handleCookies: false);

        HttpResponseMessage response = await client.GetAsync(
            "/api/v1/accounts/oauth/google/complete", TestContext.Current.CancellationToken);

        Assert.Equal(HttpStatusCode.Redirect, response.StatusCode);
        Assert.Equal("/login?google=failed", response.Headers.Location?.OriginalString);
        Assert.False(response.Headers.Contains("Set-Cookie"));
    }

    [Fact]
    public async Task GoogleCallbackIsNotARouteWithoutGoogle()
    {
        await using AccountApiFactory factory = new(configureGoogle: false);
        using HttpClient client = factory.CreateAccountClient(handleCookies: false);

        HttpResponseMessage response = await client.GetAsync(
            "/api/v1/accounts/oauth/google/callback", TestContext.Current.CancellationToken);

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