2026-02-08 18:00:24 +01:00
|
|
|
using System.Net;
|
|
|
|
|
using System.Net.Http.Json;
|
2026-02-14 14:50:06 +01:00
|
|
|
using System.Text.Json.Nodes;
|
2026-02-08 18:00:24 +01:00
|
|
|
using FluentResults;
|
|
|
|
|
using IdentityShroud.Core.Messages.Realm;
|
2026-02-14 14:50:06 +01:00
|
|
|
using IdentityShroud.Core.Model;
|
2026-02-08 18:00:24 +01:00
|
|
|
using IdentityShroud.Core.Services;
|
|
|
|
|
using IdentityShroud.Core.Tests.Fixtures;
|
2026-02-14 14:50:06 +01:00
|
|
|
using IdentityShroud.TestUtils.Asserts;
|
2026-02-08 18:00:24 +01:00
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
using NSubstitute.ClearExtensions;
|
|
|
|
|
|
|
|
|
|
namespace IdentityShroud.Api.Tests.Apis;
|
|
|
|
|
|
|
|
|
|
public class RealmApisTests(ApplicationFactory factory) : IClassFixture<ApplicationFactory>
|
|
|
|
|
{
|
|
|
|
|
[Theory]
|
|
|
|
|
[InlineData(null, null, null, false, "Name")]
|
|
|
|
|
[InlineData(null, null, "Foo", true, "")]
|
|
|
|
|
[InlineData(null, null, "", false, "Name")]
|
|
|
|
|
[InlineData(null, "foo", "Foo", true, "")]
|
|
|
|
|
[InlineData(null, "f/oo", "Foo", false, "Slug")]
|
|
|
|
|
[InlineData(null, "", "Foo", false, "Slug")]
|
|
|
|
|
[InlineData("0814934a-efe2-4784-ba84-a184c0b9de9e", "foo", "Foo", true, "")]
|
|
|
|
|
[InlineData("00000000-0000-0000-0000-000000000000", "foo", "Foo", false, "Id")]
|
|
|
|
|
public async Task Create(string? id, string? slug, string? name, bool succeeds, string fieldName)
|
|
|
|
|
{
|
|
|
|
|
var client = factory.CreateClient();
|
|
|
|
|
|
|
|
|
|
factory.RealmService.ClearSubstitute();
|
|
|
|
|
factory.RealmService.Create(Arg.Any<RealmCreateRequest>(), Arg.Any<CancellationToken>())
|
|
|
|
|
.Returns(Result.Ok(new RealmCreateResponse(Guid.NewGuid(), "foo", "Foo")));
|
|
|
|
|
|
|
|
|
|
Guid? inputId = id is null ? (Guid?)null : new Guid(id);
|
|
|
|
|
var response = await client.PostAsync("/realms", JsonContent.Create(new
|
|
|
|
|
{
|
|
|
|
|
Id = inputId,
|
|
|
|
|
Slug = slug,
|
|
|
|
|
Name = name,
|
2026-02-14 14:50:06 +01:00
|
|
|
}),
|
2026-02-08 18:00:24 +01:00
|
|
|
TestContext.Current.CancellationToken);
|
|
|
|
|
#if DEBUG
|
2026-02-14 14:50:06 +01:00
|
|
|
string contents = await response.Content.ReadAsStringAsync(TestContext.Current.CancellationToken);
|
|
|
|
|
#endif
|
|
|
|
|
|
2026-02-08 18:00:24 +01:00
|
|
|
if (succeeds)
|
|
|
|
|
{
|
|
|
|
|
Assert.Equal(HttpStatusCode.Created, response.StatusCode);
|
|
|
|
|
await factory.RealmService.Received(1).Create(
|
2026-02-14 14:50:06 +01:00
|
|
|
Arg.Is<RealmCreateRequest>(r => r.Id == inputId && r.Slug == slug && r.Name == name),
|
2026-02-08 18:00:24 +01:00
|
|
|
Arg.Any<CancellationToken>());
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Assert.Equal(HttpStatusCode.BadRequest, response.StatusCode);
|
2026-02-14 14:50:06 +01:00
|
|
|
var problemDetails =
|
|
|
|
|
await response.Content.ReadFromJsonAsync<ValidationProblemDetails>(
|
|
|
|
|
TestContext.Current.CancellationToken);
|
2026-02-08 18:00:24 +01:00
|
|
|
|
|
|
|
|
Assert.Contains(problemDetails!.Errors, e => e.Key == fieldName);
|
|
|
|
|
await factory.RealmService.DidNotReceive().Create(
|
2026-02-14 14:50:06 +01:00
|
|
|
Arg.Any<RealmCreateRequest>(),
|
2026-02-08 18:00:24 +01:00
|
|
|
Arg.Any<CancellationToken>());
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-02-14 14:50:06 +01:00
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public async Task GetOpenIdConfiguration_Success()
|
|
|
|
|
{
|
|
|
|
|
// setup
|
|
|
|
|
factory.RealmService.FindBySlug(Arg.Is<string>("foo"), Arg.Any<CancellationToken>())
|
|
|
|
|
.Returns(new Realm());
|
|
|
|
|
|
|
|
|
|
// act
|
|
|
|
|
var client = factory.CreateClient();
|
|
|
|
|
var response = await client.GetAsync("/realms/foo/.well-known/openid-configuration",
|
|
|
|
|
TestContext.Current.CancellationToken);
|
|
|
|
|
|
|
|
|
|
// verify
|
|
|
|
|
var result = await response.Content.ReadFromJsonAsync<JsonObject>(TestContext.Current.CancellationToken);
|
|
|
|
|
Assert.NotNull(result);
|
|
|
|
|
JsonObjectAssert.Equal("http://localhost/realms/foo/openid-connect/auth", result, "authorization_endpoint");
|
|
|
|
|
JsonObjectAssert.Equal("http://localhost/realms/foo", result, "issuer");
|
|
|
|
|
JsonObjectAssert.Equal("http://localhost/realms/foo/openid-connect/token", result, "token_endpoint");
|
|
|
|
|
JsonObjectAssert.Equal("http://localhost/realms/foo/openid-connect/jwks", result, "jwks_uri");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Theory]
|
|
|
|
|
[InlineData("")]
|
|
|
|
|
[InlineData("bar")]
|
|
|
|
|
public async Task GetOpenIdConfiguration_NotFound(string slug)
|
|
|
|
|
{
|
|
|
|
|
// act
|
|
|
|
|
var client = factory.CreateClient();
|
|
|
|
|
var response = await client.GetAsync("/realms/bar/.well-known/openid-configuration",
|
|
|
|
|
TestContext.Current.CancellationToken);
|
|
|
|
|
|
|
|
|
|
// verify
|
|
|
|
|
Assert.Equal(HttpStatusCode.NotFound, response.StatusCode);
|
|
|
|
|
}
|
2026-02-08 18:00:24 +01:00
|
|
|
}
|