From d7009f10c85ec7ccc33d313883f0a9b3e1c9a24d Mon Sep 17 00:00:00 2001 From: Daniels Danilins Date: Mon, 22 Jul 2024 13:50:55 +0300 Subject: [PATCH] fix: fixed delete group endpoint --- .../Admin/ApiUrls.cs | 2 +- .../KeycloakGroupClientTests.cs | 54 +++++++++++++++++++ 2 files changed, 55 insertions(+), 1 deletion(-) create mode 100644 tests/Keycloak.AuthServices.Sdk.Tests/KeycloakGroupClientTests.cs diff --git a/src/Keycloak.AuthServices.Sdk/Admin/ApiUrls.cs b/src/Keycloak.AuthServices.Sdk/Admin/ApiUrls.cs index 8d379d09..561de4a0 100644 --- a/src/Keycloak.AuthServices.Sdk/Admin/ApiUrls.cs +++ b/src/Keycloak.AuthServices.Sdk/Admin/ApiUrls.cs @@ -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"; diff --git a/tests/Keycloak.AuthServices.Sdk.Tests/KeycloakGroupClientTests.cs b/tests/Keycloak.AuthServices.Sdk.Tests/KeycloakGroupClientTests.cs new file mode 100644 index 00000000..74efe720 --- /dev/null +++ b/tests/Keycloak.AuthServices.Sdk.Tests/KeycloakGroupClientTests.cs @@ -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(); + + exception.And.StatusCode.Should().Be((int)HttpStatusCode.NotFound); + + handler.VerifyNoOutstandingExpectation(); + } +}