Skip to content

Commit ca15550

Browse files
committed
[PM-14378] Add tests for SecurityTaskOrganizationAuthorizationHandler
1 parent b40d144 commit ca15550

File tree

2 files changed

+106
-1
lines changed

2 files changed

+106
-1
lines changed

src/Core/Vault/Authorization/SecurityTasks/SecurityTaskOrganizationAuthorizationHandler.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,6 @@ private static bool CanListAllTasksForOrganization(CurrentContextOrganization or
4242
{
4343
return org is
4444
{ Type: OrganizationUserType.Admin or OrganizationUserType.Owner } or
45-
{ Permissions.AccessReports: true };
45+
{ Type: OrganizationUserType.Custom, Permissions.AccessReports: true };
4646
}
4747
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
using System.Security.Claims;
2+
using Bit.Core.Context;
3+
using Bit.Core.Enums;
4+
using Bit.Core.Test.AdminConsole.AutoFixture;
5+
using Bit.Core.Vault.Authorization.SecurityTasks;
6+
using Bit.Core.Vault.Entities;
7+
using Bit.Test.Common.AutoFixture;
8+
using Bit.Test.Common.AutoFixture.Attributes;
9+
using Microsoft.AspNetCore.Authorization;
10+
using NSubstitute;
11+
using Xunit;
12+
13+
namespace Bit.Core.Test.Vault.Authorization;
14+
15+
[SutProviderCustomize]
16+
public class SecurityTaskOrganizationAuthorizationHandlerTests
17+
{
18+
[Theory, CurrentContextOrganizationCustomize, BitAutoData]
19+
public async Task MissingOrg_Failure(
20+
CurrentContextOrganization organization,
21+
SutProvider<SecurityTaskOrganizationAuthorizationHandler> sutProvider)
22+
{
23+
var userId = Guid.NewGuid();
24+
25+
sutProvider.GetDependency<ICurrentContext>().UserId.Returns(userId);
26+
sutProvider.GetDependency<ICurrentContext>().GetOrganization(organization.Id).Returns((CurrentContextOrganization)null);
27+
28+
var context = new AuthorizationHandlerContext(
29+
new[] { SecurityTaskOperations.ListAllForOrganization },
30+
new ClaimsPrincipal(),
31+
organization);
32+
33+
await sutProvider.Sut.HandleAsync(context);
34+
35+
Assert.False(context.HasSucceeded);
36+
}
37+
38+
[Theory, CurrentContextOrganizationCustomize, BitAutoData]
39+
public async Task MissingUserId_Failure(
40+
CurrentContextOrganization organization,
41+
SutProvider<SecurityTaskOrganizationAuthorizationHandler> sutProvider)
42+
{
43+
var userId = Guid.NewGuid();
44+
45+
sutProvider.GetDependency<ICurrentContext>().UserId.Returns(null as Guid?);
46+
47+
var context = new AuthorizationHandlerContext(
48+
new[] { SecurityTaskOperations.ListAllForOrganization },
49+
new ClaimsPrincipal(),
50+
organization);
51+
52+
await sutProvider.Sut.HandleAsync(context);
53+
54+
Assert.False(context.HasSucceeded);
55+
}
56+
57+
[Theory, CurrentContextOrganizationCustomize]
58+
[BitAutoData(OrganizationUserType.Owner)]
59+
[BitAutoData(OrganizationUserType.Admin)]
60+
[BitAutoData(OrganizationUserType.Custom)]
61+
public async Task ListAllForOrganization_Admin_Success(
62+
OrganizationUserType userType,
63+
CurrentContextOrganization organization,
64+
SutProvider<SecurityTaskOrganizationAuthorizationHandler> sutProvider)
65+
{
66+
var userId = Guid.NewGuid();
67+
organization.Type = userType;
68+
if (organization.Type == OrganizationUserType.Custom)
69+
{
70+
organization.Permissions.AccessReports = true;
71+
}
72+
sutProvider.GetDependency<ICurrentContext>().UserId.Returns(userId);
73+
sutProvider.GetDependency<ICurrentContext>().GetOrganization(organization.Id).Returns(organization);
74+
75+
var context = new AuthorizationHandlerContext(
76+
new[] { SecurityTaskOperations.ListAllForOrganization },
77+
new ClaimsPrincipal(),
78+
organization);
79+
80+
await sutProvider.Sut.HandleAsync(context);
81+
82+
Assert.True(context.HasSucceeded);
83+
}
84+
85+
[Theory, CurrentContextOrganizationCustomize(Type = OrganizationUserType.User), BitAutoData]
86+
public async Task ListAllForOrganization_User_Failure(
87+
CurrentContextOrganization organization,
88+
SutProvider<SecurityTaskOrganizationAuthorizationHandler> sutProvider)
89+
{
90+
var userId = Guid.NewGuid();
91+
92+
sutProvider.GetDependency<ICurrentContext>().UserId.Returns(userId);
93+
sutProvider.GetDependency<ICurrentContext>().GetOrganization(organization.Id).Returns(organization);
94+
95+
var context = new AuthorizationHandlerContext(
96+
new[] { SecurityTaskOperations.ListAllForOrganization },
97+
new ClaimsPrincipal(),
98+
organization);
99+
100+
await sutProvider.Sut.HandleAsync(context);
101+
102+
Assert.False(context.HasSucceeded);
103+
}
104+
105+
}

0 commit comments

Comments
 (0)