213 lines
6.7 KiB
C#
213 lines
6.7 KiB
C#
using System.Net;
|
|
using System.Net.Http.Json;
|
|
using System.Text;
|
|
using System.Text.Json;
|
|
using FluentResults;
|
|
using IdentityShroud.Core.Contracts;
|
|
using IdentityShroud.Core.EFCore;
|
|
using IdentityShroud.Core.Model;
|
|
using IdentityShroud.Core.Tests;
|
|
using IdentityShroud.Core.Tests.Fixtures;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using Shouldly;
|
|
|
|
namespace IdentityShroud.Api.Tests.Apis;
|
|
|
|
public class ClientApiTests : IClassFixture<ApplicationFactory>
|
|
{
|
|
private readonly JsonSerializerOptions _jsonOptions = new(JsonSerializerDefaults.Web)
|
|
{
|
|
TypeInfoResolver = AppJsonSerializerContext.Default,
|
|
};
|
|
private readonly ApplicationFactory _factory;
|
|
|
|
public ClientApiTests(ApplicationFactory factory)
|
|
{
|
|
_factory = factory;
|
|
|
|
using var scope = _factory.Services.CreateScope();
|
|
var db = scope.ServiceProvider.GetRequiredService<Db>();
|
|
if (!db.Database.EnsureCreated())
|
|
{
|
|
db.Database.ExecuteSqlRaw("TRUNCATE realm CASCADE;");
|
|
}
|
|
}
|
|
|
|
[Theory]
|
|
[InlineData(null, false, "ClientId")]
|
|
[InlineData("", false, "ClientId")]
|
|
[InlineData("my-client", true, "")]
|
|
public async Task Create_Validation(string? clientId, bool succeeds, string fieldName)
|
|
{
|
|
// setup
|
|
var realm = await CreateRealmAsync("test-realm", "Test Realm");
|
|
|
|
var client = _factory.CreateClient();
|
|
|
|
// act
|
|
var response = await client.PostAsync(
|
|
$"/api/v1/realms/{realm.Id}/clients",
|
|
JsonContent.Create(new { ClientId = clientId }),
|
|
TestContext.Current.CancellationToken);
|
|
|
|
#if DEBUG
|
|
string contents = await response.Content.ReadAsStringAsync(TestContext.Current.CancellationToken);
|
|
#endif
|
|
|
|
if (succeeds)
|
|
{
|
|
Assert.Equal(HttpStatusCode.Created, response.StatusCode);
|
|
}
|
|
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);
|
|
}
|
|
}
|
|
|
|
[Fact]
|
|
public async Task Create_Success_ReturnsCreatedWithLocation()
|
|
{
|
|
// act
|
|
var body = await DoCreateRequest("""
|
|
{
|
|
"clientId": "new-client",
|
|
"name": "New Client"
|
|
}
|
|
""");
|
|
|
|
// verify
|
|
Assert.NotNull(body);
|
|
Assert.Equal("new-client", body.ClientId);
|
|
Assert.True(body.Id > 0);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task Create_Success_CreatesSecret()
|
|
{
|
|
// act
|
|
var body = await DoCreateRequest("""
|
|
{
|
|
"clientId": "new-client",
|
|
"name": "New Client",
|
|
"confidential": true,
|
|
"generateSecret": true
|
|
}
|
|
""");
|
|
|
|
// verify
|
|
body.ShouldNotBeNull();
|
|
body.Secret.ShouldNotBeNullOrWhiteSpace();
|
|
}
|
|
|
|
private async Task<ClientRepresentation?> DoCreateRequest(
|
|
string request)
|
|
{
|
|
var realm = await CreateRealmAsync("create-realm", "Create Realm");
|
|
|
|
var client = _factory.CreateClient();
|
|
var response = await client.PostAsync(
|
|
$"/api/v1/realms/{realm.Id}/clients",
|
|
//JsonContent.Create(request),
|
|
new StringContent(request, Encoding.UTF8, "application/json"),
|
|
TestContext.Current.CancellationToken);
|
|
|
|
string contents = await response.Content.ReadAsStringAsync(TestContext.Current.CancellationToken);
|
|
Assert.True(HttpStatusCode.Created == response.StatusCode, contents);
|
|
|
|
return JsonSerializer.Deserialize<ClientRepresentation>(contents, _jsonOptions);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task Create_UnknownRealm_ReturnsNotFound()
|
|
{
|
|
var client = _factory.CreateClient();
|
|
|
|
var response = await client.PostAsync(
|
|
$"/api/v1/realms/{Guid.NewGuid()}/clients",
|
|
JsonContent.Create(new { ClientId = "some-client" }),
|
|
TestContext.Current.CancellationToken);
|
|
|
|
Assert.Equal(HttpStatusCode.NotFound, response.StatusCode);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task Get_Success()
|
|
{
|
|
// setup
|
|
var realm = await CreateRealmAsync("get-realm", "Get Realm");
|
|
Client dbClient = await CreateClientAsync(realm, "get-client", "Get Client");
|
|
|
|
var httpClient = _factory.CreateClient();
|
|
|
|
// act
|
|
var response = await httpClient.GetAsync(
|
|
$"/api/v1/realms/{realm.Id}/clients/{dbClient.Id}",
|
|
TestContext.Current.CancellationToken);
|
|
|
|
#if DEBUG
|
|
string contents = await response.Content.ReadAsStringAsync(TestContext.Current.CancellationToken);
|
|
#endif
|
|
|
|
// verify
|
|
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
|
|
|
|
var body = await response.Content.ReadFromJsonAsync<ClientRepresentation>(
|
|
TestContext.Current.CancellationToken);
|
|
|
|
Assert.NotNull(body);
|
|
Assert.Equal(dbClient.Id, body.Id);
|
|
Assert.Equal("get-client", body.ClientId);
|
|
Assert.Equal("Get Client", body.Name);
|
|
Assert.Equal(realm.Id, body.RealmId);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task Get_UnknownClient_ReturnsNotFound()
|
|
{
|
|
// setup
|
|
var realm = await CreateRealmAsync("notfound-realm", "NotFound Realm");
|
|
|
|
var httpClient = _factory.CreateClient();
|
|
|
|
// act
|
|
var response = await httpClient.GetAsync(
|
|
$"/api/v1/realms/{realm.Id}/clients/99999",
|
|
TestContext.Current.CancellationToken);
|
|
|
|
// verify
|
|
Assert.Equal(HttpStatusCode.NotFound, response.StatusCode);
|
|
}
|
|
|
|
private async Task<Realm> CreateRealmAsync(string slug, string name)
|
|
{
|
|
using var scope = _factory.Services.CreateScope();
|
|
var realmService = scope.ServiceProvider.GetRequiredService<IRealmService>();
|
|
Result<Realm> result = await realmService.Create(
|
|
new(null, slug, name),
|
|
TestContext.Current.CancellationToken);
|
|
return ResultAssert.Success(result);
|
|
}
|
|
|
|
private async Task<Client> CreateClientAsync(Realm realm, string clientId, string? name = null)
|
|
{
|
|
using var scope = _factory.Services.CreateScope();
|
|
var db = scope.ServiceProvider.GetRequiredService<Db>();
|
|
var client = new Client
|
|
{
|
|
RealmId = realm.Id,
|
|
ClientId = clientId,
|
|
Name = name,
|
|
CreatedAt = DateTime.UtcNow,
|
|
};
|
|
db.Clients.Add(client);
|
|
await db.SaveChangesAsync(TestContext.Current.CancellationToken);
|
|
return client;
|
|
}
|
|
}
|