-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add permission management * Add role management
- Loading branch information
1 parent
5f78f0f
commit 0e51344
Showing
9 changed files
with
584 additions
and
1 deletion.
There are no files selected for viewing
105 changes: 105 additions & 0 deletions
105
Descope.Test/IntegrationTests/Management/PermissionTests.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,105 @@ | ||
using Xunit; | ||
|
||
namespace Descope.Test.Integration | ||
{ | ||
public class PermissionTests | ||
{ | ||
private readonly DescopeClient _descopeClient = IntegrationTestSetup.InitDescopeClient(); | ||
|
||
[Fact] | ||
public async Task Permission_CreateAndLoad() | ||
{ | ||
string? name = null; | ||
try | ||
{ | ||
// Create a permission | ||
name = Guid.NewGuid().ToString(); | ||
var desc = "desc"; | ||
await _descopeClient.Management.Permission.Create(name, desc); | ||
|
||
// Load and compare | ||
var loadedPermissions = await _descopeClient.Management.Permission.LoadAll(); | ||
var loadedPermission = loadedPermissions.Find(permission => permission.Name == name); | ||
Assert.NotNull(loadedPermission); | ||
Assert.Equal(loadedPermission.Description, desc); | ||
} | ||
finally | ||
{ | ||
if (!string.IsNullOrEmpty(name)) | ||
{ | ||
try { await _descopeClient.Management.Permission.Delete(name); } | ||
catch { } | ||
} | ||
} | ||
} | ||
|
||
[Fact] | ||
public async Task Permission_UpdateAndSearch() | ||
{ | ||
string? name = null; | ||
string? updatedName = null; | ||
try | ||
{ | ||
// Create a permission | ||
name = Guid.NewGuid().ToString(); | ||
string desc = "desc"; | ||
await _descopeClient.Management.Permission.Create(name, desc); | ||
updatedName = name + "updated"; | ||
|
||
// Update and compare | ||
await _descopeClient.Management.Permission.Update(name, updatedName); | ||
// Load and compare | ||
var loadedPermissions = await _descopeClient.Management.Permission.LoadAll(); | ||
var loadedPermission = loadedPermissions.Find(permission => permission.Name == updatedName); | ||
var originalNamePermission = loadedPermissions.Find(permission => permission.Name == name); | ||
Assert.Null(originalNamePermission); | ||
Assert.NotNull(loadedPermission); | ||
Assert.True(string.IsNullOrEmpty(loadedPermission.Description)); | ||
name = null; | ||
} | ||
finally | ||
{ | ||
if (!string.IsNullOrEmpty(name)) | ||
{ | ||
try { await _descopeClient.Management.Permission.Delete(name); } | ||
catch { } | ||
} | ||
if (!string.IsNullOrEmpty(updatedName)) | ||
{ | ||
try { await _descopeClient.Management.Permission.Delete(updatedName); } | ||
catch { } | ||
} | ||
} | ||
} | ||
|
||
[Fact] | ||
public async Task Permission_DeleteAndLoadAll() | ||
{ | ||
string? name = null; | ||
try | ||
{ | ||
// Create a permission | ||
name = Guid.NewGuid().ToString(); | ||
await _descopeClient.Management.Permission.Create(name); | ||
|
||
// Delete it | ||
await _descopeClient.Management.Permission.Delete(name); | ||
name = null; | ||
|
||
// Load all and make sure it's gone | ||
var loadedPermissions = await _descopeClient.Management.Permission.LoadAll(); | ||
var loadedPermission = loadedPermissions.Find(permission => permission.Name == name); | ||
Assert.Null(loadedPermission); | ||
} | ||
finally | ||
{ | ||
if (!string.IsNullOrEmpty(name)) | ||
{ | ||
try { await _descopeClient.Management.Permission.Delete(name); } | ||
catch { } | ||
} | ||
} | ||
} | ||
} | ||
|
||
} |
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,105 @@ | ||
using Xunit; | ||
|
||
namespace Descope.Test.Integration | ||
{ | ||
public class RoleTests | ||
{ | ||
private readonly DescopeClient _descopeClient = IntegrationTestSetup.InitDescopeClient(); | ||
|
||
[Fact] | ||
public async Task Role_CreateAndLoad() | ||
{ | ||
string? name = null; | ||
try | ||
{ | ||
// Create a role | ||
name = Guid.NewGuid().ToString(); | ||
var desc = "desc"; | ||
await _descopeClient.Management.Role.Create(name, desc); | ||
|
||
// Load and compare | ||
var loadedRoles = await _descopeClient.Management.Role.LoadAll(); | ||
var loadedRole = loadedRoles.Find(role => role.Name == name); | ||
Assert.NotNull(loadedRole); | ||
Assert.Equal(loadedRole.Description, desc); | ||
} | ||
finally | ||
{ | ||
if (!string.IsNullOrEmpty(name)) | ||
{ | ||
try { await _descopeClient.Management.Role.Delete(name); } | ||
catch { } | ||
} | ||
} | ||
} | ||
|
||
[Fact] | ||
public async Task Role_UpdateAndSearch() | ||
{ | ||
string? name = null; | ||
string? updatedName = null; | ||
try | ||
{ | ||
// Create a role | ||
name = Guid.NewGuid().ToString(); | ||
string desc = "desc"; | ||
await _descopeClient.Management.Role.Create(name, desc); | ||
updatedName = name + "updated"; | ||
|
||
// Update and compare | ||
await _descopeClient.Management.Role.Update(name, updatedName); | ||
// Load and compare | ||
var foundRoles = await _descopeClient.Management.Role.SearchAll(new RoleSearchOptions { RoleNames = new List<string> { updatedName } }); | ||
var role = foundRoles.Find(role => role.Name == updatedName); | ||
Assert.NotNull(role); | ||
Assert.True(string.IsNullOrEmpty(role.Description)); | ||
foundRoles = await _descopeClient.Management.Role.SearchAll(new RoleSearchOptions { RoleNames = new List<string> { name } }); | ||
role = foundRoles.Find(role => role.Name == name); | ||
Assert.Null(role); | ||
name = null; | ||
} | ||
finally | ||
{ | ||
if (!string.IsNullOrEmpty(name)) | ||
{ | ||
try { await _descopeClient.Management.Role.Delete(name); } | ||
catch { } | ||
} | ||
if (!string.IsNullOrEmpty(updatedName)) | ||
{ | ||
try { await _descopeClient.Management.Role.Delete(updatedName); } | ||
catch { } | ||
} | ||
} | ||
} | ||
|
||
[Fact] | ||
public async Task Role_DeleteAndLoadAll() | ||
{ | ||
string? name = null; | ||
try | ||
{ | ||
// Create a role | ||
name = Guid.NewGuid().ToString(); | ||
await _descopeClient.Management.Role.Create(name); | ||
|
||
// Delete it | ||
await _descopeClient.Management.Role.Delete(name); | ||
name = null; | ||
|
||
// Load all and make sure it's gone | ||
var loadedRoles = await _descopeClient.Management.Role.LoadAll(); | ||
var loadedRole = loadedRoles.Find(role => role.Name == name); | ||
Assert.Null(loadedRole); | ||
} | ||
finally | ||
{ | ||
if (!string.IsNullOrEmpty(name)) | ||
{ | ||
try { await _descopeClient.Management.Role.Delete(name); } | ||
catch { } | ||
} | ||
} | ||
} | ||
} | ||
} |
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
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
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,55 @@ | ||
using System.Text.Json.Serialization; | ||
|
||
namespace Descope.Internal.Management | ||
{ | ||
internal class Permission : IPermission | ||
{ | ||
private readonly IHttpClient _httpClient; | ||
private readonly string _managementKey; | ||
|
||
internal Permission(IHttpClient httpClient, string managementKey) | ||
{ | ||
_httpClient = httpClient; | ||
_managementKey = managementKey; | ||
} | ||
|
||
public async Task Create(string name, string? description = null) | ||
{ | ||
if (string.IsNullOrEmpty(name)) throw new DescopeException("name is required for creation"); | ||
var body = new { name, description }; | ||
await _httpClient.Post<object>(Routes.PermissionCreate, _managementKey, body); | ||
} | ||
|
||
public async Task Update(string name, string newName, string? description = null) | ||
{ | ||
if (string.IsNullOrEmpty(name)) throw new DescopeException("name is required for update"); | ||
if (string.IsNullOrEmpty(newName)) throw new DescopeException("new name cannot be updated to empty"); | ||
var body = new { name, newName, description }; | ||
await _httpClient.Post<object>(Routes.PermissionUpdate, _managementKey, body); | ||
} | ||
|
||
public async Task Delete(string name) | ||
{ | ||
if (string.IsNullOrEmpty(name)) throw new DescopeException("name is required for deletion"); | ||
var body = new { name }; | ||
await _httpClient.Post<object>(Routes.PermissionDelete, _managementKey, body); | ||
} | ||
|
||
public async Task<List<PermissionResponse>> LoadAll() | ||
{ | ||
var permissionList = await _httpClient.Get<PermissionListResponse>(Routes.PermissionLoadAll, _managementKey); | ||
return permissionList.Permissions; | ||
} | ||
} | ||
|
||
internal class PermissionListResponse | ||
{ | ||
[JsonPropertyName("permissions")] | ||
public List<PermissionResponse> Permissions { get; set; } | ||
|
||
public PermissionListResponse(List<PermissionResponse> permissions) | ||
{ | ||
Permissions = permissions; | ||
} | ||
} | ||
} |
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,61 @@ | ||
using System.Text.Json.Serialization; | ||
|
||
namespace Descope.Internal.Management | ||
{ | ||
internal class Role : IRole | ||
{ | ||
private readonly IHttpClient _httpClient; | ||
private readonly string _managementKey; | ||
|
||
internal Role(IHttpClient httpClient, string managementKey) | ||
{ | ||
_httpClient = httpClient; | ||
_managementKey = managementKey; | ||
} | ||
|
||
public async Task Create(string name, string? description = null, List<string>? permissionNames = null, string? tenantId = null) | ||
{ | ||
if (string.IsNullOrEmpty(name)) throw new DescopeException("name is required for creation"); | ||
var body = new { name, description, permissionNames, tenantId }; | ||
await _httpClient.Post<object>(Routes.RoleCreate, _managementKey, body); | ||
} | ||
|
||
public async Task Update(string name, string newName, string? description = null, List<string>? permissionNames = null, string? tenantId = null) | ||
{ | ||
if (string.IsNullOrEmpty(name)) throw new DescopeException("name is required for update"); | ||
if (string.IsNullOrEmpty(newName)) throw new DescopeException("new name cannot be updated to empty"); | ||
var body = new { name, newName, description, permissionNames, tenantId }; | ||
await _httpClient.Post<object>(Routes.RoleUpdate, _managementKey, body); | ||
} | ||
|
||
public async Task Delete(string name, string? tenantId) | ||
{ | ||
if (string.IsNullOrEmpty(name)) throw new DescopeException("name is required for deletion"); | ||
var body = new { name, tenantId }; | ||
await _httpClient.Post<object>(Routes.RoleDelete, _managementKey, body); | ||
} | ||
|
||
public async Task<List<RoleResponse>> LoadAll() | ||
{ | ||
var roleList = await _httpClient.Get<RoleListResponse>(Routes.RoleLoadAll, _managementKey); | ||
return roleList.Roles; | ||
} | ||
|
||
public async Task<List<RoleResponse>> SearchAll(RoleSearchOptions? options) | ||
{ | ||
var roleList = await _httpClient.Post<RoleListResponse>(Routes.RoleSearchAll, _managementKey, options); | ||
return roleList.Roles; | ||
} | ||
} | ||
|
||
internal class RoleListResponse | ||
{ | ||
[JsonPropertyName("roles")] | ||
public List<RoleResponse> Roles { get; set; } | ||
|
||
public RoleListResponse(List<RoleResponse> roles) | ||
{ | ||
Roles = roles; | ||
} | ||
} | ||
} |
Oops, something went wrong.