Still working on getting client credential flow complete, most of the request works but still working on generating the JWT.
This commit is contained in:
parent
1a8c63808a
commit
8782ef39c6
80 changed files with 1331 additions and 414 deletions
|
|
@ -1,16 +1,26 @@
|
|||
using System.Net;
|
||||
using System.Net.Http.Json;
|
||||
using IdentityShroud.Core;
|
||||
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)
|
||||
|
|
@ -32,7 +42,7 @@ public class ClientApiTests : IClassFixture<ApplicationFactory>
|
|||
public async Task Create_Validation(string? clientId, bool succeeds, string fieldName)
|
||||
{
|
||||
// setup
|
||||
Realm realm = await CreateRealmAsync("test-realm", "Test Realm");
|
||||
var realm = await CreateRealmAsync("test-realm", "Test Realm");
|
||||
|
||||
var client = _factory.CreateClient();
|
||||
|
||||
|
|
@ -64,32 +74,56 @@ public class ClientApiTests : IClassFixture<ApplicationFactory>
|
|||
[Fact]
|
||||
public async Task Create_Success_ReturnsCreatedWithLocation()
|
||||
{
|
||||
// setup
|
||||
Realm realm = await CreateRealmAsync("create-realm", "Create Realm");
|
||||
|
||||
var client = _factory.CreateClient();
|
||||
|
||||
// act
|
||||
var response = await client.PostAsync(
|
||||
$"/api/v1/realms/{realm.Id}/clients",
|
||||
JsonContent.Create(new { ClientId = "new-client", Name = "New Client" }),
|
||||
TestContext.Current.CancellationToken);
|
||||
|
||||
#if DEBUG
|
||||
string contents = await response.Content.ReadAsStringAsync(TestContext.Current.CancellationToken);
|
||||
#endif
|
||||
var body = await DoCreateRequest("""
|
||||
{
|
||||
"clientId": "new-client",
|
||||
"name": "New Client"
|
||||
}
|
||||
""");
|
||||
|
||||
// verify
|
||||
Assert.Equal(HttpStatusCode.Created, response.StatusCode);
|
||||
|
||||
var body = await response.Content.ReadFromJsonAsync<ClientCreateReponse>(
|
||||
TestContext.Current.CancellationToken);
|
||||
|
||||
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()
|
||||
{
|
||||
|
|
@ -107,7 +141,7 @@ public class ClientApiTests : IClassFixture<ApplicationFactory>
|
|||
public async Task Get_Success()
|
||||
{
|
||||
// setup
|
||||
Realm realm = await CreateRealmAsync("get-realm", "Get Realm");
|
||||
var realm = await CreateRealmAsync("get-realm", "Get Realm");
|
||||
Client dbClient = await CreateClientAsync(realm, "get-client", "Get Client");
|
||||
|
||||
var httpClient = _factory.CreateClient();
|
||||
|
|
@ -138,7 +172,7 @@ public class ClientApiTests : IClassFixture<ApplicationFactory>
|
|||
public async Task Get_UnknownClient_ReturnsNotFound()
|
||||
{
|
||||
// setup
|
||||
Realm realm = await CreateRealmAsync("notfound-realm", "NotFound Realm");
|
||||
var realm = await CreateRealmAsync("notfound-realm", "NotFound Realm");
|
||||
|
||||
var httpClient = _factory.CreateClient();
|
||||
|
||||
|
|
@ -154,11 +188,11 @@ public class ClientApiTests : IClassFixture<ApplicationFactory>
|
|||
private async Task<Realm> CreateRealmAsync(string slug, string name)
|
||||
{
|
||||
using var scope = _factory.Services.CreateScope();
|
||||
var db = scope.ServiceProvider.GetRequiredService<Db>();
|
||||
var realm = new Realm { Slug = slug, Name = name };
|
||||
db.Realms.Add(realm);
|
||||
await db.SaveChangesAsync(TestContext.Current.CancellationToken);
|
||||
return realm;
|
||||
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)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue