diff --git a/src/Casdoor.Client/Abstractions/ICasdoorClient.cs b/src/Casdoor.Client/Abstractions/ICasdoorClient.cs index 5910ff1..aa52c50 100644 --- a/src/Casdoor.Client/Abstractions/ICasdoorClient.cs +++ b/src/Casdoor.Client/Abstractions/ICasdoorClient.cs @@ -16,7 +16,7 @@ namespace Casdoor.Client; public interface ICasdoorClient : ICasdoorUserClient, ICasdoorTokenClient, ICasdoorResourceClient, ICasdoorServiceClient, - ICasdoorApplicationClient, ICasdoorOrganizationClient, ICasdoorProviderClient, ICasdoorAccountClient + ICasdoorApplicationClient, ICasdoorOrganizationClient, ICasdoorProviderClient, ICasdoorAccountClient, ICasdoorModelClient { } diff --git a/src/Casdoor.Client/Abstractions/ICasdoorModelClient.cs b/src/Casdoor.Client/Abstractions/ICasdoorModelClient.cs new file mode 100644 index 0000000..ad4253d --- /dev/null +++ b/src/Casdoor.Client/Abstractions/ICasdoorModelClient.cs @@ -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 ICasdoorModelClient +{ + public Task AddModelAsync(CasdoorModel application, CancellationToken cancellationToken = default); + + public Task UpdateModelAsync(CasdoorModel model, string modelId, CancellationToken cancellationToken = default); + public Task DeleteModelAsync(CasdoorModel model, CancellationToken cancellationToken = default); + public Task GetModelAsync(string name, string? owner = null, CancellationToken cancellationToken = default); + + public Task?> GetModelsAsync(string? owner = null, CancellationToken cancellationToken = default); +} diff --git a/tests/Casdoor.Client.UnitTests/ApiClientTests/ModelTest.cs b/tests/Casdoor.Client.UnitTests/ApiClientTests/ModelTest.cs new file mode 100644 index 0000000..e3fc435 --- /dev/null +++ b/tests/Casdoor.Client.UnitTests/ApiClientTests/ModelTest.cs @@ -0,0 +1,97 @@ +using System.Globalization; +using Casdoor.Client.UnitTests.Fixtures; +using Microsoft.Extensions.DependencyInjection; +using Xunit.Abstractions; + +namespace Casdoor.Client.UnitTests.ApiClientTests; + +public class ModelTest : IClassFixture +{ + private readonly ServicesFixture _servicesFixture; + private readonly ITestOutputHelper _testOutputHelper; + + public ModelTest(ServicesFixture servicesFixture, ITestOutputHelper testOutputHelper) + { + _servicesFixture = servicesFixture; + _testOutputHelper = testOutputHelper; + } + + [Fact] + public async void TestModel() + { + var userClient = _servicesFixture.ServiceProvider.GetService(); + + const string appName = $"model-name-4"; + const string ownerName = "casbin"; + const string modelText = @"[request_definition] +r = sub, obj, act + +[policy_definition] +p = sub, obj, act + +[role_definition] +g = _, _ + +[policy_effect] +e = some(where (p.eft == allow)) + +[matchers] +m = g(r.sub, p.sub) && r.obj == p.obj && r.act == p.act"; + var model = new CasdoorModel() + { + Owner = ownerName, + Name = appName, + CreatedTime = DateTime.Now.ToString(CultureInfo.InvariantCulture), + Description = "Casdoor website", + ModelText = modelText + }; + // Add a new object + + Task responseAsync = userClient.AddModelAsync(model); + 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?> modelsAsync = userClient.GetModelsAsync(); + IEnumerable? getModels = await modelsAsync; + Assert.True(getModels.Any()); + bool found = false; + foreach (CasdoorModel casdoorModel in getModels) + { + _testOutputHelper.WriteLine(casdoorModel.Name); + if (casdoorModel.Name is appName) + { + found = true; + } + } + + Assert.True(found); + + // Get the object + Task modelAsync = userClient.GetModelAsync($"{appName}"); + CasdoorModel? getModel = await modelAsync; + Assert.Equal(appName, getModel.Name); + //Update the object + const string updateDescription = "Update Casdoor Website"; + model.Description = updateDescription; + // Update the object + responseAsync = + userClient.UpdateModelAsync(model, $"{ownerName}/{appName}"); + response = await responseAsync; + Assert.Equal(CasdoorConstants.DefaultCasdoorSuccessStatus, response.Status); + // Validate the update + modelAsync = userClient.GetModelAsync($"{appName}"); + getModel = await modelAsync; + Assert.Equal(updateDescription, getModel.Description); + + // Delete the object + responseAsync = userClient.DeleteModelAsync(model); + response = await responseAsync; + Assert.Equal(CasdoorConstants.DefaultCasdoorSuccessStatus, response.Status); + // Validate the deletion + modelAsync = userClient.GetModelAsync($"{appName}"); + getModel = await modelAsync; + Assert.Null(getModel); + + } +}