61 lines
No EOL
2.5 KiB
C#
61 lines
No EOL
2.5 KiB
C#
using System.Net;
|
|
using System.Net.Http.Json;
|
|
using FluentResults;
|
|
using IdentityShroud.Core.Messages.Realm;
|
|
using IdentityShroud.Core.Services;
|
|
using IdentityShroud.Core.Tests.Fixtures;
|
|
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,
|
|
}),
|
|
TestContext.Current.CancellationToken);
|
|
#if DEBUG
|
|
string contents = await response.Content.ReadAsStringAsync(TestContext.Current.CancellationToken);
|
|
#endif
|
|
|
|
if (succeeds)
|
|
{
|
|
Assert.Equal(HttpStatusCode.Created, response.StatusCode);
|
|
await factory.RealmService.Received(1).Create(
|
|
Arg.Is<RealmCreateRequest>(r => r.Id == inputId && r.Slug == slug && r.Name == name),
|
|
Arg.Any<CancellationToken>());
|
|
}
|
|
else
|
|
{
|
|
Assert.Equal(HttpStatusCode.BadRequest, response.StatusCode);
|
|
var problemDetails = await response.Content.ReadFromJsonAsync<ValidationProblemDetails>(TestContext.Current.CancellationToken);
|
|
|
|
Assert.Contains(problemDetails!.Errors, e => e.Key == fieldName);
|
|
await factory.RealmService.DidNotReceive().Create(
|
|
Arg.Any<RealmCreateRequest>(),
|
|
Arg.Any<CancellationToken>());
|
|
}
|
|
}
|
|
} |