Skip to content

Commit

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

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

}
26 changes: 26 additions & 0 deletions src/Casdoor.Client/Abstractions/ICasdoorModelClient.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 ICasdoorModelClient
{
public Task<CasdoorResponse?> AddModelAsync(CasdoorModel application, CancellationToken cancellationToken = default);

public Task<CasdoorResponse?> UpdateModelAsync(CasdoorModel model, string modelId, CancellationToken cancellationToken = default);
public Task<CasdoorResponse?> DeleteModelAsync(CasdoorModel model, CancellationToken cancellationToken = default);
public Task<CasdoorModel?> GetModelAsync(string name, string? owner = null, CancellationToken cancellationToken = default);

public Task<IEnumerable<CasdoorModel>?> GetModelsAsync(string? owner = null, CancellationToken cancellationToken = default);
}
97 changes: 97 additions & 0 deletions tests/Casdoor.Client.UnitTests/ApiClientTests/ModelTest.cs
Original file line number Diff line number Diff line change
@@ -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<ServicesFixture>
{
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<ICasdoorClient>();

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<CasdoorResponse?> responseAsync = userClient.AddModelAsync(model);

Check warning on line 50 in tests/Casdoor.Client.UnitTests/ApiClientTests/ModelTest.cs

View workflow job for this annotation

GitHub Actions / build

Dereference of a possibly null reference.

Check warning on line 50 in tests/Casdoor.Client.UnitTests/ApiClientTests/ModelTest.cs

View workflow job for this annotation

GitHub Actions / release-build-version

Dereference of a possibly null reference.

Check warning on line 50 in tests/Casdoor.Client.UnitTests/ApiClientTests/ModelTest.cs

View workflow job for this annotation

GitHub Actions / release

Dereference of a possibly null reference.
CasdoorResponse? response = await responseAsync;
Assert.Equal(CasdoorConstants.DefaultCasdoorSuccessStatus, response.Status);

Check warning on line 52 in tests/Casdoor.Client.UnitTests/ApiClientTests/ModelTest.cs

View workflow job for this annotation

GitHub Actions / build

Dereference of a possibly null reference.

Check warning on line 52 in tests/Casdoor.Client.UnitTests/ApiClientTests/ModelTest.cs

View workflow job for this annotation

GitHub Actions / release-build-version

Dereference of a possibly null reference.

Check warning on line 52 in tests/Casdoor.Client.UnitTests/ApiClientTests/ModelTest.cs

View workflow job for this annotation

GitHub Actions / release

Dereference of a possibly null reference.
_testOutputHelper.WriteLine(response.Status);
// Get all objects, check if our added object is inside the list
Task<IEnumerable<CasdoorModel>?> modelsAsync = userClient.GetModelsAsync();
IEnumerable<CasdoorModel>? getModels = await modelsAsync;
Assert.True(getModels.Any());

Check warning on line 57 in tests/Casdoor.Client.UnitTests/ApiClientTests/ModelTest.cs

View workflow job for this annotation

GitHub Actions / release-build-version

Possible null reference argument for parameter 'source' in 'bool Enumerable.Any<CasdoorModel>(IEnumerable<CasdoorModel> source)'.

Check warning on line 57 in tests/Casdoor.Client.UnitTests/ApiClientTests/ModelTest.cs

View workflow job for this annotation

GitHub Actions / release

Possible null reference argument for parameter 'source' in 'bool Enumerable.Any<CasdoorModel>(IEnumerable<CasdoorModel> source)'.
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<CasdoorModel?> modelAsync = userClient.GetModelAsync($"{appName}");
CasdoorModel? getModel = await modelAsync;
Assert.Equal(appName, getModel.Name);

Check warning on line 73 in tests/Casdoor.Client.UnitTests/ApiClientTests/ModelTest.cs

View workflow job for this annotation

GitHub Actions / release

Dereference of a possibly null reference.
//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);

}
}

0 comments on commit addaedc

Please sign in to comment.