Skip to content

Commit

Permalink
fix: fixed delete group endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniels Danilins committed Jul 22, 2024
1 parent f8a9d8a commit d7009f1
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Keycloak.AuthServices.Sdk/Admin/ApiUrls.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ internal static class ApiUrls

internal const string UpdateGroup = $"{GetRealm}/groups/{{id}}";

internal const string DeleteGroup = $"{GetRealm}/groups";
internal const string DeleteGroup = $"{GetRealm}/groups/{{id}}";

internal const string CreateChildGroup = $"{GetRealm}/groups/{{id}}/children";

Expand Down
54 changes: 54 additions & 0 deletions tests/Keycloak.AuthServices.Sdk.Tests/KeycloakGroupClientTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
namespace Keycloak.AuthServices.Sdk.Tests;

using System.Net;
using Admin;
using RichardSzalay.MockHttp;

public class KeycloakGroupClientTests
{
private const string BaseAddress = "http://localhost:8080";
private const string MediaType = "application/json";
private readonly MockHttpMessageHandler handler = new();
private readonly IKeycloakGroupClient keycloakGroupClient;

public KeycloakGroupClientTests()
{
var httpClient = handler.ToHttpClient();
httpClient.BaseAddress = new Uri(BaseAddress);

keycloakGroupClient = new KeycloakClient(httpClient);
}

[Fact]
public async Task DeleteGroupShouldCallCorrectEndpoint()
{
var groupId = Guid.NewGuid();

handler.Expect(HttpMethod.Delete, $"/admin/realms/master/groups/{groupId}")
.Respond(HttpStatusCode.NoContent);

await keycloakGroupClient.DeleteGroupAsync("master", groupId.ToString());

handler.VerifyNoOutstandingExpectation();
}

[Fact]
public async Task DeleteGroupShouldThrowNotFoundApiExceptionWhenGroupDoesNotExist()
{
var groupId = Guid.NewGuid().ToString();
const string errorMessage = /*lang=json,strict*/
"{\"errorMessage\":\"Group name is missing\"}";

handler.Expect(HttpMethod.Delete, $"/admin/realms/master/groups/{groupId}")
.Respond(HttpStatusCode.NotFound, MediaType, errorMessage);

var exception = await FluentActions
.Invoking(() => keycloakGroupClient.DeleteGroupAsync("master", groupId))
.Should()
.ThrowAsync<KeycloakHttpClientException>();

exception.And.StatusCode.Should().Be((int)HttpStatusCode.NotFound);

handler.VerifyNoOutstandingExpectation();
}
}

0 comments on commit d7009f1

Please sign in to comment.