Skip to content

Commit

Permalink
feat: add Group API and tests (#89)
Browse files Browse the repository at this point in the history
  • Loading branch information
lywedo authored Nov 12, 2023
1 parent c519acd commit 59412fe
Show file tree
Hide file tree
Showing 5 changed files with 221 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 @@ -17,7 +17,7 @@ namespace Casdoor.Client;
public interface ICasdoorClient :
ICasdoorUserClient, ICasdoorTokenClient, ICasdoorResourceClient, ICasdoorServiceClient,
ICasdoorApplicationClient, ICasdoorOrganizationClient, ICasdoorProviderClient, ICasdoorAccountClient, ICasdoorModelClient,
ICasdoorEnforcerClient
ICasdoorEnforcerClient, ICasdoorGroupClient
{

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

public Task<CasdoorResponse?> UpdateGroupAsync(CasdoorGroup group, string modelId, CancellationToken cancellationToken = default);
public Task<CasdoorResponse?> DeleteGroupAsync(CasdoorGroup group, CancellationToken cancellationToken = default);
public Task<CasdoorGroup?> GetGroupAsync(string name, string? owner = null, CancellationToken cancellationToken = default);

public Task<IEnumerable<CasdoorGroup>?> GetGroupsAsync(string? owner = null, CancellationToken cancellationToken = default);
}
52 changes: 52 additions & 0 deletions src/Casdoor.Client/CasdoorClient.GroupApi.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?> AddGroupAsync(CasdoorGroup group, CancellationToken cancellationToken = default) =>
await PostAsJsonAsync(_options.GetActionUrl("add-group"), group, cancellationToken);

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

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

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

public virtual async Task<IEnumerable<CasdoorGroup>?> GetGroupsAsync(string? owner = null, CancellationToken cancellationToken = default)
{
var queryMap = new QueryMapBuilder().Add("owner", owner ?? _options.OrganizationName).QueryMap;
string url = _options.GetActionUrl("get-groups", queryMap);
var result = await _httpClient.GetFromJsonAsync<CasdoorResponse?>(url, cancellationToken: cancellationToken);
return result.DeserializeData<IEnumerable<CasdoorGroup>?>();
}
}
56 changes: 56 additions & 0 deletions src/Casdoor.Client/Models/CasdoorGroup.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// 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 CasdoorGroup
{
[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("manager")]
public string? Manager { get; set; }
[JsonPropertyName("contactEmail")]
public string? ContactEmail { get; set; }
[JsonPropertyName("type")]
public string? Type { get; set; }
[JsonPropertyName("parentId")]
public string? ParentId { get; set; }
[JsonPropertyName("isTopGroup")]
public bool? IsTopGroup { get; set; }
[JsonPropertyName("users")]
public List<CasdoorUser>? Users { get; set; }
[JsonPropertyName("title")]
public string? Title { get; set; }
[JsonPropertyName("key")]
public string? Key { get; set; }
[JsonPropertyName("children")]
public List<CasdoorGroup>? Children { get; set; }
[JsonPropertyName("isEnabled")]
public bool? IsEnabled { get; set; }
}


86 changes: 86 additions & 0 deletions tests/Casdoor.Client.UnitTests/ApiClientTests/GroupTest.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 GroupTest : IClassFixture<ServicesFixture>
{
private readonly ServicesFixture _servicesFixture;
private readonly ITestOutputHelper _testOutputHelper;

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

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

const string appName = $"group-a";
const string ownerName = "casbin";

var group = new CasdoorGroup()
{
Owner = ownerName,
Name = appName,
CreatedTime = DateTime.Now.ToString(CultureInfo.InvariantCulture),
DisplayName = appName,
};

// Add a new object

Task<CasdoorResponse?> responseAsync = userClient.AddGroupAsync(group);
CasdoorResponse? response = await responseAsync;
_testOutputHelper.WriteLine($"{response.Status} {response.Msg}");
Assert.Equal(CasdoorConstants.DefaultCasdoorSuccessStatus, response.Status);

// Get all objects, check if our added object is inside the list
Task<IEnumerable<CasdoorGroup>?> groupsAsync = userClient.GetGroupsAsync();
IEnumerable<CasdoorGroup>? getGroups = await groupsAsync;
Assert.True(getGroups.Any());
_testOutputHelper.WriteLine($"{getGroups.Count()}");
bool found = false;
foreach (CasdoorGroup casdoorGroup in getGroups)
{
_testOutputHelper.WriteLine(casdoorGroup.Name);
if (casdoorGroup.Name is appName)
{
found = true;
}
}

Assert.True(found);

// Get the object
Task<CasdoorGroup?> groupAsync = userClient.GetGroupAsync($"{appName}");
CasdoorGroup? getGroup = await groupAsync;
Assert.Equal(appName, getGroup.Name);
//Update the object
const string displayName = "Update Casdoor Website";
group.DisplayName = displayName;
// Update the object
responseAsync =
userClient.UpdateGroupAsync(group, $"{ownerName}/{appName}");
response = await responseAsync;
Assert.Equal(CasdoorConstants.DefaultCasdoorSuccessStatus, response.Status);
// Validate the update
groupAsync = userClient.GetGroupAsync($"{appName}");
getGroup = await groupAsync;
Assert.Equal(displayName, getGroup.DisplayName);

// Delete the object
responseAsync = userClient.DeleteGroupAsync(group);
response = await responseAsync;
Assert.Equal(CasdoorConstants.DefaultCasdoorSuccessStatus, response.Status);
// Validate the deletion
groupAsync = userClient.GetGroupAsync($"{appName}");
getGroup = await groupAsync;
Assert.Null(getGroup);

}
}

0 comments on commit 59412fe

Please sign in to comment.