forked from bitwarden/server
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
PM-15066 added drop feature and unit tests. (bitwarden#5053)
- Loading branch information
1 parent
052235b
commit 92b94fd
Showing
11 changed files
with
278 additions
and
7 deletions.
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
31 changes: 31 additions & 0 deletions
31
src/Core/Tools/ReportFeatures/DropPasswordHealthReportApplicationCommand.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,31 @@ | ||
using Bit.Core.Exceptions; | ||
using Bit.Core.Tools.ReportFeatures.Interfaces; | ||
using Bit.Core.Tools.ReportFeatures.Requests; | ||
using Bit.Core.Tools.Repositories; | ||
|
||
namespace Bit.Core.Tools.ReportFeatures; | ||
|
||
public class DropPasswordHealthReportApplicationCommand : IDropPasswordHealthReportApplicationCommand | ||
{ | ||
private IPasswordHealthReportApplicationRepository _passwordHealthReportApplicationRepo; | ||
|
||
public DropPasswordHealthReportApplicationCommand( | ||
IPasswordHealthReportApplicationRepository passwordHealthReportApplicationRepository) | ||
{ | ||
_passwordHealthReportApplicationRepo = passwordHealthReportApplicationRepository; | ||
} | ||
|
||
public async Task DropPasswordHealthReportApplicationAsync(DropPasswordHealthReportApplicationRequest request) | ||
{ | ||
var data = await _passwordHealthReportApplicationRepo.GetByOrganizationIdAsync(request.OrganizationId); | ||
if (data == null) | ||
{ | ||
throw new BadRequestException("Organization does not have any records."); | ||
} | ||
|
||
data.Where(_ => request.PasswordHealthReportApplicationIds.Contains(_.Id)).ToList().ForEach(async _ => | ||
{ | ||
await _passwordHealthReportApplicationRepo.DeleteAsync(_); | ||
}); | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
src/Core/Tools/ReportFeatures/Interfaces/IAddPasswordHealthReportApplicationCommand.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
9 changes: 9 additions & 0 deletions
9
src/Core/Tools/ReportFeatures/Interfaces/IDropPasswordHealthReportApplicationCommand.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,9 @@ | ||
using Bit.Core.Tools.ReportFeatures.Requests; | ||
|
||
namespace Bit.Core.Tools.ReportFeatures.Interfaces; | ||
|
||
public interface IDropPasswordHealthReportApplicationCommand | ||
{ | ||
Task DropPasswordHealthReportApplicationAsync(DropPasswordHealthReportApplicationRequest request); | ||
} | ||
|
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
2 changes: 1 addition & 1 deletion
2
...PasswordHealthReportApplicationRequest.cs → ...PasswordHealthReportApplicationRequest.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
7 changes: 7 additions & 0 deletions
7
src/Core/Tools/ReportFeatures/Requests/DropPasswordHealthReportApplicationRequest.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,7 @@ | ||
namespace Bit.Core.Tools.ReportFeatures.Requests; | ||
|
||
public class DropPasswordHealthReportApplicationRequest | ||
{ | ||
public Guid OrganizationId { get; set; } | ||
public IEnumerable<Guid> PasswordHealthReportApplicationIds { get; set; } | ||
} |
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
104 changes: 104 additions & 0 deletions
104
test/Core.Test/Tools/ReportFeatures/DeletePasswordHealthReportApplicationCommandTests.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,104 @@ | ||
using AutoFixture; | ||
using Bit.Core.Exceptions; | ||
using Bit.Core.Tools.Entities; | ||
using Bit.Core.Tools.ReportFeatures; | ||
using Bit.Core.Tools.ReportFeatures.Requests; | ||
using Bit.Core.Tools.Repositories; | ||
using Bit.Test.Common.AutoFixture; | ||
using Bit.Test.Common.AutoFixture.Attributes; | ||
using NSubstitute; | ||
using Xunit; | ||
|
||
namespace Bit.Core.Test.Tools.ReportFeatures; | ||
|
||
[SutProviderCustomize] | ||
public class DeletePasswordHealthReportApplicationCommandTests | ||
{ | ||
[Theory, BitAutoData] | ||
public async Task DropPasswordHealthReportApplicationAsync_withValidRequest_Success( | ||
SutProvider<DropPasswordHealthReportApplicationCommand> sutProvider) | ||
{ | ||
// Arrange | ||
var fixture = new Fixture(); | ||
var passwordHealthReportApplications = fixture.CreateMany<PasswordHealthReportApplication>(2).ToList(); | ||
// only take one id from the list - we only want to drop one record | ||
var request = fixture.Build<DropPasswordHealthReportApplicationRequest>() | ||
.With(x => x.PasswordHealthReportApplicationIds, | ||
passwordHealthReportApplications.Select(x => x.Id).Take(1).ToList()) | ||
.Create(); | ||
|
||
sutProvider.GetDependency<IPasswordHealthReportApplicationRepository>() | ||
.GetByOrganizationIdAsync(Arg.Any<Guid>()) | ||
.Returns(passwordHealthReportApplications); | ||
|
||
// Act | ||
await sutProvider.Sut.DropPasswordHealthReportApplicationAsync(request); | ||
|
||
// Assert | ||
await sutProvider.GetDependency<IPasswordHealthReportApplicationRepository>() | ||
.Received(1) | ||
.GetByOrganizationIdAsync(request.OrganizationId); | ||
|
||
await sutProvider.GetDependency<IPasswordHealthReportApplicationRepository>() | ||
.Received(1) | ||
.DeleteAsync(Arg.Is<PasswordHealthReportApplication>(_ => | ||
request.PasswordHealthReportApplicationIds.Contains(_.Id))); | ||
} | ||
|
||
[Theory, BitAutoData] | ||
public async Task DropPasswordHealthReportApplicationAsync_withValidRequest_nothingToDrop( | ||
SutProvider<DropPasswordHealthReportApplicationCommand> sutProvider) | ||
{ | ||
// Arrange | ||
var fixture = new Fixture(); | ||
var passwordHealthReportApplications = fixture.CreateMany<PasswordHealthReportApplication>(2).ToList(); | ||
// we are passing invalid data | ||
var request = fixture.Build<DropPasswordHealthReportApplicationRequest>() | ||
.With(x => x.PasswordHealthReportApplicationIds, new List<Guid> { Guid.NewGuid() }) | ||
.Create(); | ||
|
||
sutProvider.GetDependency<IPasswordHealthReportApplicationRepository>() | ||
.GetByOrganizationIdAsync(Arg.Any<Guid>()) | ||
.Returns(passwordHealthReportApplications); | ||
|
||
// Act | ||
await sutProvider.Sut.DropPasswordHealthReportApplicationAsync(request); | ||
|
||
// Assert | ||
await sutProvider.GetDependency<IPasswordHealthReportApplicationRepository>() | ||
.Received(1) | ||
.GetByOrganizationIdAsync(request.OrganizationId); | ||
|
||
await sutProvider.GetDependency<IPasswordHealthReportApplicationRepository>() | ||
.Received(0) | ||
.DeleteAsync(Arg.Any<PasswordHealthReportApplication>()); | ||
} | ||
|
||
[Theory, BitAutoData] | ||
public async Task DropPasswordHealthReportApplicationAsync_withNodata_fails( | ||
SutProvider<DropPasswordHealthReportApplicationCommand> sutProvider) | ||
{ | ||
// Arrange | ||
var fixture = new Fixture(); | ||
// we are passing invalid data | ||
var request = fixture.Build<DropPasswordHealthReportApplicationRequest>() | ||
.Create(); | ||
|
||
sutProvider.GetDependency<IPasswordHealthReportApplicationRepository>() | ||
.GetByOrganizationIdAsync(Arg.Any<Guid>()) | ||
.Returns(null as List<PasswordHealthReportApplication>); | ||
|
||
// Act | ||
await Assert.ThrowsAsync<BadRequestException>(() => | ||
sutProvider.Sut.DropPasswordHealthReportApplicationAsync(request)); | ||
|
||
// Assert | ||
await sutProvider.GetDependency<IPasswordHealthReportApplicationRepository>() | ||
.Received(1) | ||
.GetByOrganizationIdAsync(request.OrganizationId); | ||
|
||
await sutProvider.GetDependency<IPasswordHealthReportApplicationRepository>() | ||
.Received(0) | ||
.DeleteAsync(Arg.Any<PasswordHealthReportApplication>()); | ||
} | ||
} |