Skip to content

Commit

Permalink
[PM-14380] Add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
shane-melton committed Dec 13, 2024
1 parent 13ace2d commit ed9c14d
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 5 deletions.
6 changes: 2 additions & 4 deletions src/Core/Vault/Queries/GetTasksForOrganizationQuery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ ICurrentContext currentContext
_currentContext = currentContext;
}

public async Task<Collection<SecurityTask>> GetTasksAsync(Guid organizationId,
public async Task<ICollection<SecurityTask>> GetTasksAsync(Guid organizationId,
SecurityTaskStatus? status = null)
{
var organization = _currentContext.GetOrganization(organizationId);
Expand All @@ -40,8 +40,6 @@ public async Task<Collection<SecurityTask>> GetTasksAsync(Guid organizationId,

await _authorizationService.AuthorizeOrThrowAsync(_currentContext.HttpContext.User, organization, SecurityTaskOperations.ListAllForOrganization);

return new Collection<SecurityTask>(
(await _securityTaskRepository.GetManyByOrganizationIdStatusAsync(organizationId, status)).ToList()
);
return (await _securityTaskRepository.GetManyByOrganizationIdStatusAsync(organizationId, status)).ToList();
}
}
2 changes: 1 addition & 1 deletion src/Core/Vault/Queries/IGetTasksForOrganizationQuery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@ public interface IGetTasksForOrganizationQuery
/// <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<Collection<SecurityTask>> GetTasksAsync(Guid organizationId, SecurityTaskStatus? status = null);
Task<ICollection<SecurityTask>> GetTasksAsync(Guid organizationId, SecurityTaskStatus? status = null);
}
93 changes: 93 additions & 0 deletions test/Core.Test/Vault/Queries/GetTasksForOrganizationQueryTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
using System.Collections;

Check warning on line 1 in test/Core.Test/Vault/Queries/GetTasksForOrganizationQueryTests.cs

View workflow job for this annotation

GitHub Actions / Lint

Using directive is unnecessary.
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);
}
}

0 comments on commit ed9c14d

Please sign in to comment.