98 lines
4.5 KiB
C#
98 lines
4.5 KiB
C#
using System;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
using FluentAssertions;
|
|
using NUnit.Framework;
|
|
using Strata.Analytics.Models.Security;
|
|
using Strata.Analytics.Biz.Workbooks.Services;
|
|
using Strata.Analytics.Test.Unit.Biz.TestData;
|
|
using Strata.CoreLib.Claims.Extensions;
|
|
using Strata.Testing.Categories;
|
|
using Strata.Analytics.Models.Workbooks;
|
|
|
|
namespace Strata.Analytics.Test.Unit.Biz.Workbooks
|
|
{
|
|
[NUnit.Framework.Category(TestCategory.Unit)]
|
|
[Parallelizable(ParallelScope.Self)]
|
|
public class WorkbookGetCurrentUserRoleTest
|
|
{
|
|
[Test]
|
|
public async Task WorkbookCurrentUserRole_IsNull_GivenNoRoleAssignments()
|
|
{
|
|
// arrange
|
|
var claimsPrincipal = IClaimsPrincipalAccessorMock.GetClaimsPrincipalAccessor();
|
|
var dbContext = AnalyticsDbContextMock.GetAnalyticsDbContext(claimsPrincipal);
|
|
var workbookService = new WorkbookService(dbContext, claimsPrincipal, AutoMapperMock.GetAutoMapper());
|
|
|
|
// act
|
|
var role = await workbookService.GetCurrentUserRoleAsync(5, CancellationToken.None);
|
|
|
|
// assert
|
|
role.HasValue.Should().BeFalse();
|
|
}
|
|
|
|
|
|
[Test]
|
|
public async Task WorkbookCurrentUserRole_IsEditor_GivenUserIsStrata()
|
|
{
|
|
// arrange
|
|
var claimsPrincipal = IClaimsPrincipalAccessorMock.GetClaimsPrincipalAccessor(5, true);
|
|
var dbContext = AnalyticsDbContextMock.GetAnalyticsDbContext(claimsPrincipal);
|
|
var workbookService = new WorkbookService(dbContext, claimsPrincipal, AutoMapperMock.GetAutoMapper());
|
|
|
|
// act
|
|
var role = await workbookService.GetCurrentUserRoleAsync(5, CancellationToken.None);
|
|
|
|
// assert
|
|
role.Should().Be(WorkbookRole.Editor);
|
|
}
|
|
|
|
[Test]
|
|
public async Task WorkbookCurrentUserRole_UsesCurrentUser_GivenMultipleUsersAssignedToMultipleRoles()
|
|
{
|
|
// arrange
|
|
var claimsPrincipal = IClaimsPrincipalAccessorMock.GetClaimsPrincipalAccessor();
|
|
var dbContext = AnalyticsDbContextMock.GetAnalyticsDbContext(claimsPrincipal);
|
|
var workbookService = new WorkbookService(dbContext, claimsPrincipal, AutoMapperMock.GetAutoMapper());
|
|
|
|
var userRoleAssignemnt = new WorkbookRoleAssignment() { StrataId = 5, WorkbookId = 5, IdentityGuid = claimsPrincipal.GetUserGuid(), IdentityName = claimsPrincipal.GetUsername(), IdentityType = IdentityType.User, Role = WorkbookRole.Viewer };
|
|
var differentUserRoleAssignemnt = new WorkbookRoleAssignment() { StrataId = 5, WorkbookId = 5, IdentityGuid = Guid.NewGuid(), IdentityName = "Random User", IdentityType = IdentityType.User, Role = WorkbookRole.Editor };
|
|
|
|
dbContext.WorkbookRoleAssignments.Add(userRoleAssignemnt);
|
|
dbContext.WorkbookRoleAssignments.Add(differentUserRoleAssignemnt);
|
|
|
|
await dbContext.SaveChangesAsync();
|
|
|
|
// act
|
|
var role = await workbookService.GetCurrentUserRoleAsync(5, CancellationToken.None);
|
|
|
|
// assert
|
|
role.Should().Be(userRoleAssignemnt.Role);
|
|
}
|
|
|
|
[Test]
|
|
public async Task WorkbookCurrentUserRole_ReturnsMostPrivilege_GivenUserAssignedToMultipleRoles()
|
|
{
|
|
// arrange
|
|
var userGroup = "DSS - Admin";
|
|
var userGroupGuid = Guid.NewGuid();
|
|
var claimsPrincipal = IClaimsPrincipalAccessorMock.GetClaimsPrincipalAccessor(5, false, false, new string[] { userGroup });
|
|
var dbContext = AnalyticsDbContextMock.GetAnalyticsDbContext(claimsPrincipal);
|
|
var workbookService = new WorkbookService(dbContext, claimsPrincipal, AutoMapperMock.GetAutoMapper());
|
|
|
|
var viewerRoleAssignment = new WorkbookRoleAssignment() { StrataId = 5, WorkbookId = 5, IdentityGuid = claimsPrincipal.GetUserGuid(), IdentityName = claimsPrincipal.GetUsername(), IdentityType = IdentityType.User, Role = WorkbookRole.Viewer };
|
|
var editorRoleAssignment = new WorkbookRoleAssignment() { StrataId = 5, WorkbookId = 5, IdentityGuid = userGroupGuid, IdentityName = userGroup, IdentityType = IdentityType.UserGroup, Role = WorkbookRole.Editor };
|
|
dbContext.WorkbookRoleAssignments.Add(viewerRoleAssignment);
|
|
dbContext.WorkbookRoleAssignments.Add(editorRoleAssignment);
|
|
|
|
await dbContext.SaveChangesAsync();
|
|
|
|
// act
|
|
var role = await workbookService.GetCurrentUserRoleAsync(5, CancellationToken.None);
|
|
|
|
// assert
|
|
role.Should().Be(WorkbookRole.Editor);
|
|
}
|
|
}
|
|
}
|