Skip to content

Commit

Permalink
Adds support for managing custom-text and partial-prompts (#749)
Browse files Browse the repository at this point in the history
  • Loading branch information
kailash-b authored Nov 26, 2024
2 parents 68828fc + c5f562b commit d107e84
Show file tree
Hide file tree
Showing 3 changed files with 144 additions and 1 deletion.
34 changes: 34 additions & 0 deletions src/Auth0.ManagementApi/Clients/IPromptsClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,39 @@ public interface IPromptsClient
/// <param name="cancellationToken">The cancellation token to cancel operation.</param>
/// <returns>The <see cref="Prompt"/> that was updated.</returns>
Task<Prompt> UpdateAsync(PromptUpdateRequest request, CancellationToken cancellationToken = default);

/// <summary>
/// Retrieve custom text for a specific prompt and language.
/// </summary>
/// <param name="promptName">Name of the prompt.</param>
/// <param name="language">Language to update.</param>
/// <param name="cancellationToken"><see cref="CancellationToken"/></param>
/// <returns>An object containing custom dictionaries for a group of screens.</returns>
Task<object> GetCustomTextForPromptAsync(string promptName, string language, CancellationToken cancellationToken = default);

/// <summary>
/// Set custom text for a specific prompt. Existing texts will be overwritten.
/// </summary>
/// <param name="promptName">Name of the prompt.</param>
/// <param name="language">Language to update.</param>
/// <param name="customText">An object containing custom dictionaries for a group of screens.</param>
/// <param name="cancellationToken"><see cref="CancellationToken"/></param>
Task SetCustomTextForPromptAsync(string promptName, string language, object customText, CancellationToken cancellationToken = default);

/// <summary>
/// Get template partials for a prompt
/// </summary>
/// <param name="promptName">Name of the prompt.</param>
/// <param name="cancellationToken"><see cref="CancellationToken"/></param>
/// <returns>An object containing template partials for a group of screens.</returns>
Task<object> GetPartialsAsync(string promptName, CancellationToken cancellationToken = default);

/// <summary>
/// Set template partials for a prompt
/// </summary>
/// <param name="promptName">Name of the prompt.</param>
/// <param name="partials">An object containing template partials for a group of screens.</param>
/// <param name="cancellationToken"><see cref="CancellationToken"/></param>
Task SetPartialsAsync(string promptName, object partials, CancellationToken cancellationToken = default);
}
}
58 changes: 58 additions & 0 deletions src/Auth0.ManagementApi/Clients/PromptsClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;

using Auth0.ManagementApi.Models.Prompts;

namespace Auth0.ManagementApi.Clients
Expand Down Expand Up @@ -50,5 +51,62 @@ public Task<Prompt> UpdateAsync(PromptUpdateRequest request, CancellationToken c
{
return Connection.SendAsync<Prompt>(new HttpMethod("PATCH"), BuildUri($"{PromptsBasePath}"), request, DefaultHeaders, cancellationToken: cancellationToken);
}

/// <inheritdoc cref="IPromptsClient.GetCustomTextForPromptAsync"/>
public Task<object> GetCustomTextForPromptAsync(string promptName, string language, CancellationToken cancellationToken)
{
if (string.IsNullOrEmpty(promptName))
throw new ArgumentNullException(nameof(promptName));
if (string.IsNullOrEmpty(language))
throw new ArgumentNullException(nameof(language));

return Connection.GetAsync<object>(
BuildUri($"{PromptsBasePath}/{promptName}/custom-text/{language}"),
DefaultHeaders,
cancellationToken: cancellationToken);
}

/// <inheritdoc cref="IPromptsClient.SetCustomTextForPromptAsync"/>
public Task SetCustomTextForPromptAsync(string promptName, string language, object customText,
CancellationToken cancellationToken = default)
{
if (string.IsNullOrEmpty(promptName))
throw new ArgumentNullException(nameof(promptName));
if (string.IsNullOrEmpty(language))
throw new ArgumentNullException(nameof(language));

return Connection.SendAsync<object>(
HttpMethod.Put,
BuildUri($"{PromptsBasePath}/{promptName}/custom-text/{language}"),
customText,
DefaultHeaders,
cancellationToken: cancellationToken);
}

/// <inheritdoc cref="IPromptsClient.GetPartialsAsync"/>
public Task<object> GetPartialsAsync(string promptName, CancellationToken cancellationToken = default)
{
if (string.IsNullOrEmpty(promptName))
throw new ArgumentNullException(nameof(promptName));

return Connection.GetAsync<object>(
BuildUri($"{PromptsBasePath}/{promptName}/partials"),
DefaultHeaders,
cancellationToken: cancellationToken);
}

/// <inheritdoc cref="IPromptsClient.SetPartialsAsync"/>
public Task SetPartialsAsync(string promptName, object partials, CancellationToken cancellationToken = default)
{
if (string.IsNullOrEmpty(promptName))
throw new ArgumentNullException(nameof(promptName));

return Connection.SendAsync<object>(
HttpMethod.Put,
BuildUri($"{PromptsBasePath}/{promptName}/partials"),
partials,
DefaultHeaders,
cancellationToken: cancellationToken);
}
}
}
53 changes: 52 additions & 1 deletion tests/Auth0.ManagementApi.IntegrationTests/PromptsTests.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
using Auth0.Tests.Shared;
using System.Collections.Generic;
using FluentAssertions;
using System.Threading.Tasks;
using Auth0.ManagementApi.IntegrationTests.Testing;
using Auth0.ManagementApi.Models.Prompts;
using Newtonsoft.Json;
using Xunit;

namespace Auth0.ManagementApi.IntegrationTests
Expand Down Expand Up @@ -37,5 +38,55 @@ public async Task Test_get_and_update_prompts()
prompts.Should().NotBeNull();
prompts.UniversalLoginExperience.Should().Be(originalExperience);
}

[Fact]
public async Task Test_set_and_get_custom_text_for_prompt()
{
var customText = new Dictionary<string, Dictionary<string,string>>()
{
{ "login", new Dictionary<string, string>()
{
{ "title", "welcome to auth0" }
}
}
};
await ApiClient.Prompts.SetCustomTextForPromptAsync("login", "en", customText);

var updatedCustomText = await ApiClient.Prompts.GetCustomTextForPromptAsync("login", "en");

updatedCustomText.Should().NotBeNull();
var udpatedCustomTextObject =
JsonConvert.DeserializeObject<Dictionary<string, Dictionary<string,string>>>(
updatedCustomText?.ToString());
customText.Should().BeEquivalentTo(udpatedCustomTextObject);
}

[Fact]
public async Task Test_set_and_get_partials()
{
string token = await GenerateBruckeManagementApiToken();

using (var apiClient = new ManagementApiClient(token, GetVariable("BRUCKE_MANAGEMENT_API_URL")))
{
var partial = new Dictionary<string, Dictionary<string,string>>()
{
{ "signup-id", new Dictionary<string, string>()
{
{ "form-content-start", "<div>HTML or Liquid</div>" },
{ "form-content-end", "<div>HTML or Liquid</div>" }
}
}
};
await apiClient.Prompts.SetPartialsAsync("signup-id", partial);

var updatedPartials = await apiClient.Prompts.GetPartialsAsync("signup-id");

updatedPartials.Should().NotBeNull();
var updatedPartialsObject =
JsonConvert.DeserializeObject<Dictionary<string, Dictionary<string,string>>>(
updatedPartials?.ToString());
partial.Should().BeEquivalentTo(updatedPartialsObject);
}
}
}
}

0 comments on commit d107e84

Please sign in to comment.