-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: add enforcer API and tests (#88)
- Loading branch information
Showing
5 changed files
with
213 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
src/Casdoor.Client/Abstractions/ICasdoorEnforcerlClient.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>?>(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
86
tests/Casdoor.Client.UnitTests/ApiClientTests/EnforcerTest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
|
||
} | ||
} |