Skip to content

Commit

Permalink
fix: add enforcer API and tests (#88)
Browse files Browse the repository at this point in the history
  • Loading branch information
lywedo authored Nov 4, 2023
1 parent addaedc commit c519acd
Show file tree
Hide file tree
Showing 5 changed files with 213 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/Casdoor.Client/Abstractions/ICasdoorClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ namespace Casdoor.Client;

public interface ICasdoorClient :
ICasdoorUserClient, ICasdoorTokenClient, ICasdoorResourceClient, ICasdoorServiceClient,
ICasdoorApplicationClient, ICasdoorOrganizationClient, ICasdoorProviderClient, ICasdoorAccountClient, ICasdoorModelClient
ICasdoorApplicationClient, ICasdoorOrganizationClient, ICasdoorProviderClient, ICasdoorAccountClient, ICasdoorModelClient,
ICasdoorEnforcerClient
{

}
26 changes: 26 additions & 0 deletions src/Casdoor.Client/Abstractions/ICasdoorEnforcerlClient.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Copyright 2022 The Casdoor Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

namespace Casdoor.Client;

public interface ICasdoorEnforcerClient
{
public Task<CasdoorResponse?> AddEnforcerAsync(CasdoorEnforcer enforcer, CancellationToken cancellationToken = default);

public Task<CasdoorResponse?> UpdateEnforcerAsync(CasdoorEnforcer enforcer, string enforcerId, CancellationToken cancellationToken = default);
public Task<CasdoorResponse?> DeleteEnforcerAsync(CasdoorEnforcer enforcer, CancellationToken cancellationToken = default);
public Task<CasdoorEnforcer?> GetEnforcerAsync(string name, string? owner = null, CancellationToken cancellationToken = default);

public Task<IEnumerable<CasdoorEnforcer>?> GetEnforcersAsync(string? owner = null, CancellationToken cancellationToken = default);
}
52 changes: 52 additions & 0 deletions src/Casdoor.Client/CasdoorClient.EnforcerApi.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// Copyright 2022 The Casdoor Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

using System.Net.Http.Json;

namespace Casdoor.Client;

public partial class CasdoorClient
{
public virtual async Task<CasdoorResponse?> AddEnforcerAsync(CasdoorEnforcer enforcer, CancellationToken cancellationToken = default) =>
await PostAsJsonAsync(_options.GetActionUrl("add-enforcer"), enforcer, cancellationToken);

public virtual async Task<CasdoorResponse?> UpdateEnforcerAsync(CasdoorEnforcer enforcer, string enforcerId,
CancellationToken cancellationToken = default)
{
string url = _options.GetActionUrl("update-enforcer", new QueryMapBuilder().Add("id", enforcerId).QueryMap);
return await PostAsJsonAsync(url, enforcer, cancellationToken);
}

public virtual async Task<CasdoorResponse?> DeleteEnforcerAsync(CasdoorEnforcer enforcer, CancellationToken cancellationToken = default)
{
string url = _options.GetActionUrl("delete-enforcer");
return await PostAsJsonAsync(url, enforcer, cancellationToken);
}

public virtual async Task<CasdoorEnforcer?> GetEnforcerAsync(string name, string? owner = null, CancellationToken cancellationToken = default)
{
var queryMap = new QueryMapBuilder().Add("id", $"{owner ?? _options.OrganizationName}/{name}").QueryMap;
string url = _options.GetActionUrl("get-enforcer", queryMap);
var result = await _httpClient.GetFromJsonAsync<CasdoorResponse?>(url, cancellationToken: cancellationToken);
return result.DeserializeData<CasdoorEnforcer?>();
}

public virtual async Task<IEnumerable<CasdoorEnforcer>?> GetEnforcersAsync(string? owner = null, CancellationToken cancellationToken = default)
{
var queryMap = new QueryMapBuilder().Add("owner", owner ?? _options.OrganizationName).QueryMap;
string url = _options.GetActionUrl("get-enforcers", queryMap);
var result = await _httpClient.GetFromJsonAsync<CasdoorResponse?>(url, cancellationToken: cancellationToken);
return result.DeserializeData<IEnumerable<CasdoorEnforcer>?>();
}
}
47 changes: 47 additions & 0 deletions src/Casdoor.Client/Models/CasdoorEnforcer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// Copyright 2023 The Casdoor Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

using System.Text.Json.Serialization;

namespace Casdoor.Client;

public class CasdoorEnforcer
{
[JsonPropertyName("owner")]
public string? Owner { get; set; }

[JsonPropertyName("name")]
public string? Name { get; set; }

[JsonPropertyName("createdTime")]
public string? CreatedTime { get; set; }

[JsonPropertyName("updatedTime")]
public string? UpdatedTime { get; set; }

[JsonPropertyName("displayName")]
public string? DisplayName { get; set; }

[JsonPropertyName("description")]
public string? Description { get; set; }

[JsonPropertyName("model")]
public string? Model { get; set; }

[JsonPropertyName("adapter")]
public string? Adapter { get; set; }

[JsonPropertyName("isEnabled")]
public bool? IsEnabled { get; set; }
}
86 changes: 86 additions & 0 deletions tests/Casdoor.Client.UnitTests/ApiClientTests/EnforcerTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
using System.Globalization;
using Casdoor.Client.UnitTests.Fixtures;
using Microsoft.Extensions.DependencyInjection;
using Xunit.Abstractions;

namespace Casdoor.Client.UnitTests.ApiClientTests;

public class EnforcerTest : IClassFixture<ServicesFixture>
{
private readonly ServicesFixture _servicesFixture;
private readonly ITestOutputHelper _testOutputHelper;

public EnforcerTest(ServicesFixture servicesFixture, ITestOutputHelper testOutputHelper)
{
_servicesFixture = servicesFixture;
_testOutputHelper = testOutputHelper;
}

[Fact]
public async void TestEnforcer()
{
var userClient = _servicesFixture.ServiceProvider.GetService<ICasdoorClient>();

const string appName = $"enforce-name";
const string ownerName = "casbin";

var enforcer = new CasdoorEnforcer()
{
Owner = ownerName,
Name = appName,
CreatedTime = DateTime.Now.ToString(CultureInfo.InvariantCulture),
DisplayName = appName,
Model = "built-in/user-model-built-in",
Adapter = "built-in/user-adapter-built-in",
Description = "Casdoor website"
};
// Add a new object

Task<CasdoorResponse?> responseAsync = userClient.AddEnforcerAsync(enforcer);
CasdoorResponse? response = await responseAsync;
Assert.Equal(CasdoorConstants.DefaultCasdoorSuccessStatus, response.Status);
_testOutputHelper.WriteLine(response.Status);
// Get all objects, check if our added object is inside the list
Task<IEnumerable<CasdoorEnforcer>?> enforcesAsync = userClient.GetEnforcersAsync();
IEnumerable<CasdoorEnforcer>? getEnforcers = await enforcesAsync;
Assert.True(getEnforcers.Any());
bool found = false;
foreach (CasdoorEnforcer casdoorEnforcer in getEnforcers)
{
_testOutputHelper.WriteLine(casdoorEnforcer.Name);
if (casdoorEnforcer.Name is appName)
{
found = true;
}
}

Assert.True(found);

// Get the object
Task<CasdoorEnforcer?> enforcerAsync = userClient.GetEnforcerAsync($"{appName}");
CasdoorEnforcer? getEnforcer = await enforcerAsync;
Assert.Equal(appName, getEnforcer.Name);
//Update the object
const string updateDescription = "Update Casdoor Website";
enforcer.Description = updateDescription;
// Update the object
responseAsync =
userClient.UpdateEnforcerAsync(enforcer, $"{ownerName}/{appName}");
response = await responseAsync;
Assert.Equal(CasdoorConstants.DefaultCasdoorSuccessStatus, response.Status);
// Validate the update
enforcerAsync = userClient.GetEnforcerAsync($"{appName}");
getEnforcer = await enforcerAsync;
Assert.Equal(updateDescription, getEnforcer.Description);

// Delete the object
responseAsync = userClient.DeleteEnforcerAsync(enforcer);
response = await responseAsync;
Assert.Equal(CasdoorConstants.DefaultCasdoorSuccessStatus, response.Status);
// Validate the deletion
enforcerAsync = userClient.GetEnforcerAsync($"{appName}");
getEnforcer = await enforcerAsync;
Assert.Null(getEnforcer);

}
}

0 comments on commit c519acd

Please sign in to comment.