Skip to content

Commit

Permalink
feat: add delete user method to sdk (#59)
Browse files Browse the repository at this point in the history
  • Loading branch information
Persistent13 authored Mar 26, 2024
1 parent 9b92256 commit 02bacd8
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ internal static class KeycloakClientApiConstants

internal const string UpdateUser = $"{GetRealm}/users/{{id}}";

internal const string DeleteUser = $"{GetRealm}/users/{{id}}";

internal const string SendVerifyEmail = $"{GetRealm}/users/{{id}}/send-verify-email";

internal const string ExecuteActionsEmail = $"{GetRealm}/users/{{id}}/execute-actions-email";
Expand Down
10 changes: 10 additions & 0 deletions src/Keycloak.AuthServices.Sdk/Admin/IKeycloakUserClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,16 @@ public interface IKeycloakUserClient
[Headers("Content-Type: application/json")]
Task UpdateUser(string realm, [AliasAs("id")] string userId, [Body] User user);

/// <summary>
/// Delete the given user.
/// </summary>
/// <param name="realm">Realm name (not ID).</param>
/// <param name="userId">User ID.</param>
/// <returns></returns>
[Delete(KeycloakClientApiConstants.DeleteUser)]
[Headers("Content-Type: application/json")]
Task DeleteUser(string realm, [AliasAs("id")] string userId);

/// <summary>
/// Send an email-verification email to the user.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,39 @@ public async Task UpdateUserShouldThrowNotFoundApiExceptionWhenUserDoesNotExist(
this.handler.VerifyNoOutstandingExpectation();
}

[Fact]
public async Task DeleteUserShouldCallCorrectEndpoint()
{
var userId = Guid.NewGuid();

this.handler.Expect(HttpMethod.Delete, $"{BaseAddress}/admin/realms/master/users/{userId}")
.WithAcceptAndContentTypeHeaders()
.WithContent(string.Empty)
.Respond(HttpStatusCode.NoContent);

await this.keycloakUserClient.DeleteUser("master", userId.ToString());

this.handler.VerifyNoOutstandingExpectation();
}

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

this.handler.Expect(HttpMethod.Delete, $"{BaseAddress}/admin/realms/master/users/{userId.ToString()}")
.WithAcceptAndContentTypeHeaders()
.Respond(HttpStatusCode.BadRequest, "application/json", errorMessage);

var exception = await Assert.ThrowsAsync<ApiException>(() =>
this.keycloakUserClient.DeleteUser("master", userId.ToString()));

exception.StatusCode.Should().Be(HttpStatusCode.BadRequest);
exception.Content.Should().Be(errorMessage);
this.handler.VerifyNoOutstandingExpectation();
}

[Fact]
public async Task SendVerifyEmailShouldCallCorrectEndpoint()
{
Expand Down

0 comments on commit 02bacd8

Please sign in to comment.