Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for Rules Configs endpoints #552

Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Auth0.Net.sln
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.0.32014.148
VisualStudioVersion = 17.2.32210.308
MinimumVisualStudioVersion = 10.0.40219.1
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{C9550C3F-90EB-42C0-B530-E728566A13C2}"
EndProject
Expand Down
49 changes: 49 additions & 0 deletions src/Auth0.ManagementApi/Clients/RulesConfigClient.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
using Auth0.ManagementApi.Models;
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;

namespace Auth0.ManagementApi.Clients
{
/// <summary>
/// Contains methods to access the /rules-configs endpoint
/// </summary>
public class RulesConfigClient : BaseClient
{
/// <summary>
/// Initializes a new instance of <see cref="RulesConfigClient"/>.
/// </summary>
/// <param name="connection"><see cref="IManagementConnection"/> used to make all API calls.</param>
/// <param name="baseUri"><see cref="Uri"/> of the endpoint to use in making API calls.</param>
/// <param name="defaultHeaders">Dictionary containing default headers included with every request this client makes.</param>
public RulesConfigClient(IManagementConnection connection, Uri baseUri, IDictionary<string, string> defaultHeaders)
: base(connection, baseUri, defaultHeaders)
{
}

/// <summary>
/// Creates or updates a rules config variable according to the request.
/// </summary>
/// <param name="request">The <see cref="RuleCreateRequest" /> containing the details of the rule to create.</param>
/// <param name="cancellationToken">The cancellation token to cancel operation.</param>
/// <returns>The newly created <see cref="Rule" />.</returns>
public Task<RulesConfig> CreateOrUpdateAsync(RulesConfigCreateOrUpdateRequest request, CancellationToken cancellationToken = default)
{
return Connection.SendAsync<RulesConfig>(HttpMethod.Put, BuildUri($"rules-configs/{EncodePath(request.Key)}"), new { value = request.Value }, DefaultHeaders, cancellationToken: cancellationToken);
}

/// <summary>
/// Deletes a rules config variable.
/// </summary>
/// <param name="id">The key of the rules-config to delete.</param>
/// <param name="cancellationToken">The cancellation token to cancel operation.</param>
/// <returns>A <see cref="Task"/> that represents the asynchronous delete operation.</returns>
public Task DeleteAsync(string key, CancellationToken cancellationToken = default)
{
return Connection.SendAsync<object>(HttpMethod.Delete, BuildUri($"rules-configs/{EncodePath(key)}"), null, DefaultHeaders, cancellationToken: cancellationToken);
}

}
}
6 changes: 6 additions & 0 deletions src/Auth0.ManagementApi/ManagementApiClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,11 @@ public class ManagementApiClient : IDisposable
/// <value>The roles.</value>
public RolesClient Roles { get; }

/// <summary>
/// Contains all the methods to call the /rules-configs endpoints.
/// </summary>
public RulesConfigClient RulesConfig { get; }

/// <summary>
/// Contains all the methods to call the /rules endpoints.
/// </summary>
Expand Down Expand Up @@ -195,6 +200,7 @@ public ManagementApiClient(string token, Uri baseUri, IManagementConnection mana
Organizations = new OrganizationsClient(managementConnection, baseUri, DefaultHeaders);
ResourceServers = new ResourceServersClient(managementConnection, baseUri, DefaultHeaders);
Roles = new RolesClient(managementConnection, baseUri, DefaultHeaders);
RulesConfig = new RulesConfigClient(managementConnection, baseUri, DefaultHeaders);
Rules = new RulesClient(managementConnection, baseUri, DefaultHeaders);
Stats = new StatsClient(managementConnection, baseUri, DefaultHeaders);
TenantSettings = new TenantSettingsClient(managementConnection, baseUri, DefaultHeaders);
Expand Down
23 changes: 23 additions & 0 deletions src/Auth0.ManagementApi/Models/RulesConfig.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using Newtonsoft.Json;

namespace Auth0.ManagementApi.Models
{
/// <summary>
/// Represents a rules config variable.
/// A rules-config is a variable defined by its key that carries an encrypted value, accessible only from within the rules.
/// </summary>
public class RulesConfig
{
/// <summary>
/// Gets or sets the key for the rules config variable.
/// </summary>
[JsonProperty("key")]
public string Key { get; set; }

/// <summary>
/// Gets or sets the body for the rules config variable.
/// </summary>
[JsonProperty("value")]
public string Value { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace Auth0.ManagementApi.Models
{
/// <summary>
/// Represents a client request to create or update a rules config variable.
/// </summary>
public class RulesConfigCreateOrUpdateRequest : RulesConfig
{

}
}
83 changes: 83 additions & 0 deletions tests/Auth0.ManagementApi.IntegrationTests/RulesConfigTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
using Auth0.IntegrationTests.Shared.CleanUp;
using Auth0.ManagementApi.IntegrationTests.Testing;
using Auth0.ManagementApi.Models;
using FluentAssertions;
using System.Threading.Tasks;
using Xunit;


namespace Auth0.ManagementApi.IntegrationTests
{
/// <summary>
/// Tests functionality of the <see cref="RulesConfigClient"/> in the <see cref="ManagementApiClient"/>.
/// </summary>
public class RulesConfigTests : ManagementTestBase, IAsyncLifetime
{
public async Task InitializeAsync()
{
string token = await GenerateManagementApiToken();

ApiClient = new ManagementApiClient(token, GetVariable("AUTH0_MANAGEMENT_API_URL"), new HttpClientManagementConnection(options: new HttpClientManagementConnectionOptions { NumberOfHttpRetries = 9 }));
}

/// <summary>
/// Tests that a rules config variable can be created and then deleted
/// </summary>
/// <returns></returns>
[Fact]
public async Task Test_ruleConfigs_limited_crud_sequence()
{
//NOTE: cannot read config, so create a unique name and just test PUT + DELETE

// Add a new rule
var newRuleConfigRequest = new RulesConfigCreateOrUpdateRequest
{
Key = MakeRandomName(),
Value = "i am iron man!"
};
var newRuleConfigResponse = await ApiClient.RulesConfig.CreateOrUpdateAsync(newRuleConfigRequest);
newRuleConfigResponse.Should().NotBeNull();
newRuleConfigResponse.Key.Should().Be(newRuleConfigRequest.Key);
newRuleConfigResponse.Value.Should().NotBeNullOrEmpty();

// delete the rule
await ApiClient.RulesConfig.DeleteAsync(newRuleConfigRequest.Key);
}

/// <summary>
/// Tests that the same method can be used to create a new variable and also update an existing variable.
/// This is necessary because we cannot use the API to get existing variables, so it cannot be known if we are creating or updating an entry.
/// </summary>
/// <returns></returns>
[Fact]
public async Task Test_ruleConfigs_can_update_existing()
{
// Add a new rule
var newRuleConfigRequest = new RulesConfigCreateOrUpdateRequest
{
Key = MakeRandomName(),
Value = "i am iron man!"
};
var newRuleConfigResponse = await ApiClient.RulesConfig.CreateOrUpdateAsync(newRuleConfigRequest);
newRuleConfigResponse.Should().NotBeNull();
newRuleConfigResponse.Key.Should().Be(newRuleConfigRequest.Key);
newRuleConfigResponse.Value.Should().NotBeNullOrEmpty();

// update the rule
var updateRuleConfigRequest = new RulesConfigCreateOrUpdateRequest
{
Key = newRuleConfigResponse.Key,
Value = "avengers assemble!"
};
var updateRuleConfigResponse = await ApiClient.RulesConfig.CreateOrUpdateAsync(updateRuleConfigRequest);
updateRuleConfigResponse.Should().NotBeNull();
updateRuleConfigResponse.Key.Should().Be(newRuleConfigRequest.Key);
updateRuleConfigResponse.Value.Should().NotBeNullOrEmpty();
updateRuleConfigResponse.Value.Should().NotBe(newRuleConfigRequest.Value);

// delete the rule
await ApiClient.RulesConfig.DeleteAsync(newRuleConfigRequest.Key);

}
}
}