-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[PM-14380] Add GET /tasks/organization endpoint (#5149)
* [PM-14380] Add GetManyByOrganizationIdStatusAsync to SecurityTaskRepository * [PM-14380] Introduce IGetTasksForOrganizationQuery * [PM-14380] Add /tasks/organization endpoint * [PM-14380] Add unit tests * [PM-14380] Formatting * [PM-14380] Bump migration script date * [PM-14380] Bump migration script date
- Loading branch information
1 parent
a99f82d
commit 0605590
Showing
9 changed files
with
257 additions
and
1 deletion.
There are no files selected for viewing
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,44 @@ | ||
using Bit.Core.Context; | ||
using Bit.Core.Exceptions; | ||
using Bit.Core.Utilities; | ||
using Bit.Core.Vault.Authorization.SecurityTasks; | ||
using Bit.Core.Vault.Entities; | ||
using Bit.Core.Vault.Enums; | ||
using Bit.Core.Vault.Repositories; | ||
using Microsoft.AspNetCore.Authorization; | ||
|
||
namespace Bit.Core.Vault.Queries; | ||
|
||
public class GetTasksForOrganizationQuery : IGetTasksForOrganizationQuery | ||
{ | ||
private readonly ISecurityTaskRepository _securityTaskRepository; | ||
private readonly IAuthorizationService _authorizationService; | ||
private readonly ICurrentContext _currentContext; | ||
|
||
public GetTasksForOrganizationQuery( | ||
ISecurityTaskRepository securityTaskRepository, | ||
IAuthorizationService authorizationService, | ||
ICurrentContext currentContext | ||
) | ||
{ | ||
_securityTaskRepository = securityTaskRepository; | ||
_authorizationService = authorizationService; | ||
_currentContext = currentContext; | ||
} | ||
|
||
public async Task<ICollection<SecurityTask>> GetTasksAsync(Guid organizationId, | ||
SecurityTaskStatus? status = null) | ||
{ | ||
var organization = _currentContext.GetOrganization(organizationId); | ||
var userId = _currentContext.UserId; | ||
|
||
if (organization == null || !userId.HasValue) | ||
{ | ||
throw new NotFoundException(); | ||
} | ||
|
||
await _authorizationService.AuthorizeOrThrowAsync(_currentContext.HttpContext.User, organization, SecurityTaskOperations.ListAllForOrganization); | ||
|
||
return (await _securityTaskRepository.GetManyByOrganizationIdStatusAsync(organizationId, status)).ToList(); | ||
} | ||
} |
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,15 @@ | ||
using Bit.Core.Vault.Entities; | ||
using Bit.Core.Vault.Enums; | ||
|
||
namespace Bit.Core.Vault.Queries; | ||
|
||
public interface IGetTasksForOrganizationQuery | ||
{ | ||
/// <summary> | ||
/// Retrieves all security tasks for an organization. | ||
/// </summary> | ||
/// <param name="organizationId">The Id of the organization</param> | ||
/// <param name="status">Optional filter for task status. If not provided, returns tasks of all statuses</param> | ||
/// <returns>A collection of security tasks</returns> | ||
Task<ICollection<SecurityTask>> GetTasksAsync(Guid organizationId, SecurityTaskStatus? status = null); | ||
} |
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
19 changes: 19 additions & 0 deletions
19
src/Sql/Vault/dbo/Stored Procedures/SecurityTask/SecurityTask_ReadByOrganizationIdStatus.sql
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,19 @@ | ||
CREATE PROCEDURE [dbo].[SecurityTask_ReadByOrganizationIdStatus] | ||
@OrganizationId UNIQUEIDENTIFIER, | ||
@Status TINYINT = NULL | ||
AS | ||
BEGIN | ||
SET NOCOUNT ON | ||
|
||
SELECT | ||
ST.* | ||
FROM | ||
[dbo].[SecurityTaskView] ST | ||
INNER JOIN | ||
[dbo].[Organization] O ON O.[Id] = ST.[OrganizationId] | ||
WHERE | ||
ST.[OrganizationId] = @OrganizationId | ||
AND O.[Enabled] = 1 | ||
AND ST.[Status] = COALESCE(@Status, ST.[Status]) | ||
ORDER BY ST.[CreationDate] DESC | ||
END |
92 changes: 92 additions & 0 deletions
92
test/Core.Test/Vault/Queries/GetTasksForOrganizationQueryTests.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,92 @@ | ||
using System.Security.Claims; | ||
using Bit.Core.Context; | ||
using Bit.Core.Exceptions; | ||
using Bit.Core.Vault.Authorization.SecurityTasks; | ||
using Bit.Core.Vault.Entities; | ||
using Bit.Core.Vault.Enums; | ||
using Bit.Core.Vault.Queries; | ||
using Bit.Core.Vault.Repositories; | ||
using Bit.Test.Common.AutoFixture; | ||
using Bit.Test.Common.AutoFixture.Attributes; | ||
using Microsoft.AspNetCore.Authorization; | ||
using NSubstitute; | ||
using Xunit; | ||
|
||
namespace Bit.Core.Test.Vault.Queries; | ||
|
||
[SutProviderCustomize] | ||
public class GetTasksForOrganizationQueryTests | ||
{ | ||
[Theory, BitAutoData] | ||
public async Task GetTasksAsync_Success( | ||
Guid userId, CurrentContextOrganization org, | ||
SutProvider<GetTasksForOrganizationQuery> sutProvider) | ||
{ | ||
var status = SecurityTaskStatus.Pending; | ||
sutProvider.GetDependency<ICurrentContext>().HttpContext.User.Returns(new ClaimsPrincipal()); | ||
sutProvider.GetDependency<ICurrentContext>().UserId.Returns(userId); | ||
sutProvider.GetDependency<ICurrentContext>().GetOrganization(org.Id).Returns(org); | ||
sutProvider.GetDependency<IAuthorizationService>().AuthorizeAsync( | ||
Arg.Any<ClaimsPrincipal>(), org, Arg.Is<IEnumerable<IAuthorizationRequirement>>( | ||
e => e.Contains(SecurityTaskOperations.ListAllForOrganization) | ||
) | ||
).Returns(AuthorizationResult.Success()); | ||
sutProvider.GetDependency<ISecurityTaskRepository>().GetManyByOrganizationIdStatusAsync(org.Id, status).Returns(new List<SecurityTask>() | ||
{ | ||
new() { Id = Guid.NewGuid() }, | ||
new() { Id = Guid.NewGuid() }, | ||
}); | ||
|
||
var result = await sutProvider.Sut.GetTasksAsync(org.Id, status); | ||
|
||
Assert.Equal(2, result.Count); | ||
sutProvider.GetDependency<IAuthorizationService>().Received(1).AuthorizeAsync( | ||
Arg.Any<ClaimsPrincipal>(), org, Arg.Is<IEnumerable<IAuthorizationRequirement>>( | ||
e => e.Contains(SecurityTaskOperations.ListAllForOrganization) | ||
) | ||
); | ||
sutProvider.GetDependency<ISecurityTaskRepository>().Received(1).GetManyByOrganizationIdStatusAsync(org.Id, SecurityTaskStatus.Pending); | ||
} | ||
|
||
[Theory, BitAutoData] | ||
public async Task GetTaskAsync_MissingOrg_Failure(Guid userId, SutProvider<GetTasksForOrganizationQuery> sutProvider) | ||
{ | ||
sutProvider.GetDependency<ICurrentContext>().UserId.Returns(userId); | ||
sutProvider.GetDependency<ICurrentContext>().GetOrganization(Arg.Any<Guid>()).Returns((CurrentContextOrganization)null); | ||
|
||
await Assert.ThrowsAsync<NotFoundException>(() => sutProvider.Sut.GetTasksAsync(Guid.NewGuid())); | ||
} | ||
|
||
[Theory, BitAutoData] | ||
public async Task GetTaskAsync_MissingUser_Failure(CurrentContextOrganization org, SutProvider<GetTasksForOrganizationQuery> sutProvider) | ||
{ | ||
sutProvider.GetDependency<ICurrentContext>().UserId.Returns(null as Guid?); | ||
sutProvider.GetDependency<ICurrentContext>().GetOrganization(org.Id).Returns(org); | ||
|
||
await Assert.ThrowsAsync<NotFoundException>(() => sutProvider.Sut.GetTasksAsync(org.Id)); | ||
} | ||
|
||
[Theory, BitAutoData] | ||
public async Task GetTasksAsync_Unauthorized_Failure( | ||
Guid userId, CurrentContextOrganization org, | ||
SutProvider<GetTasksForOrganizationQuery> sutProvider) | ||
{ | ||
sutProvider.GetDependency<ICurrentContext>().HttpContext.User.Returns(new ClaimsPrincipal()); | ||
sutProvider.GetDependency<ICurrentContext>().UserId.Returns(userId); | ||
sutProvider.GetDependency<ICurrentContext>().GetOrganization(org.Id).Returns(org); | ||
sutProvider.GetDependency<IAuthorizationService>().AuthorizeAsync( | ||
Arg.Any<ClaimsPrincipal>(), org, Arg.Is<IEnumerable<IAuthorizationRequirement>>( | ||
e => e.Contains(SecurityTaskOperations.ListAllForOrganization) | ||
) | ||
).Returns(AuthorizationResult.Failed()); | ||
|
||
await Assert.ThrowsAsync<NotFoundException>(() => sutProvider.Sut.GetTasksAsync(org.Id)); | ||
|
||
sutProvider.GetDependency<IAuthorizationService>().Received(1).AuthorizeAsync( | ||
Arg.Any<ClaimsPrincipal>(), org, Arg.Is<IEnumerable<IAuthorizationRequirement>>( | ||
e => e.Contains(SecurityTaskOperations.ListAllForOrganization) | ||
) | ||
); | ||
sutProvider.GetDependency<ISecurityTaskRepository>().Received(0).GetManyByOrganizationIdStatusAsync(org.Id, SecurityTaskStatus.Pending); | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
util/Migrator/DbScripts/2025-01-09_00_SecurityTaskReadByOrganization.sql
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,20 @@ | ||
CREATE OR ALTER PROCEDURE [dbo].[SecurityTask_ReadByOrganizationIdStatus] | ||
@OrganizationId UNIQUEIDENTIFIER, | ||
@Status TINYINT = NULL | ||
AS | ||
BEGIN | ||
SET NOCOUNT ON | ||
|
||
SELECT | ||
ST.* | ||
FROM | ||
[dbo].[SecurityTaskView] ST | ||
INNER JOIN | ||
[dbo].[Organization] O ON O.[Id] = ST.[OrganizationId] | ||
WHERE | ||
ST.[OrganizationId] = @OrganizationId | ||
AND O.[Enabled] = 1 | ||
AND ST.[Status] = COALESCE(@Status, ST.[Status]) | ||
ORDER BY ST.[CreationDate] DESC | ||
END | ||
GO |