mirror of
https://github.com/bitwarden/server
synced 2026-01-05 18:13:31 +00:00
Organization report tables, repos, services, and endpoints (#6158)
* PM-23754 initial commit * pm-23754 fixing controller tests * pm-23754 adding commands and queries * pm-23754 adding endpoints, command/queries, repositories, and sql migrations * pm-23754 add new sql scripts * PM-23754 adding sql scripts * pm-23754 * PM-23754 fixing migration script * PM-23754 fixing migration script again * PM-23754 fixing migration script validation * PM-23754 fixing db validation script issue * PM-23754 fixing endpoint and db validation * PM-23754 fixing unit tests * PM-23754 fixing implementation based on comments and tests * PM-23754 updating logging statements * PM-23754 making changes based on PR comments. * updating migration scripts * removing old migration files * update code based testing for whole data object for OrganizationReport and add a stored procedure. * updating services, unit tests, repository tests * fixing unit tests * fixing migration script * fixing migration script again * fixing migration script * another fix * fixing sql file, updating controller to account for different orgIds in the url and body. * updating error message in controllers without a body * making a change to the command * Refactor ReportsController by removing organization reports The IDropOrganizationReportCommand is no longer needed * will code based on PR comments. * fixing unit test * fixing migration script based on last changes. * adding another check in endpoint and adding unit tests * fixing route parameter. * PM-23754 updating data fields to return just the column * PM-23754 fixing repository method signatures * PM-23754 making change to orgId parameter through out code to align with api naming --------- Co-authored-by: Tom <144813356+ttalty@users.noreply.github.com>
This commit is contained in:
1165
test/Api.Test/Dirt/OrganizationReportsControllerTests.cs
Normal file
1165
test/Api.Test/Dirt/OrganizationReportsControllerTests.cs
Normal file
File diff suppressed because it is too large
Load Diff
@@ -1,14 +1,12 @@
|
||||
using AutoFixture;
|
||||
using Bit.Api.Dirt.Controllers;
|
||||
using Bit.Api.Dirt.Models;
|
||||
using Bit.Api.Dirt.Models.Response;
|
||||
using Bit.Core.Context;
|
||||
using Bit.Core.Dirt.Reports.ReportFeatures.Interfaces;
|
||||
using Bit.Core.Dirt.Reports.ReportFeatures.Requests;
|
||||
using Bit.Core.Exceptions;
|
||||
using Bit.Test.Common.AutoFixture;
|
||||
using Bit.Test.Common.AutoFixture.Attributes;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using NSubstitute;
|
||||
using Xunit;
|
||||
|
||||
@@ -144,323 +142,4 @@ public class ReportsControllerTests
|
||||
_.OrganizationId == request.OrganizationId &&
|
||||
_.PasswordHealthReportApplicationIds == request.PasswordHealthReportApplicationIds));
|
||||
}
|
||||
|
||||
[Theory, BitAutoData]
|
||||
public async Task AddOrganizationReportAsync_withAccess_success(SutProvider<ReportsController> sutProvider)
|
||||
{
|
||||
// Arrange
|
||||
sutProvider.GetDependency<ICurrentContext>().AccessReports(Arg.Any<Guid>()).Returns(true);
|
||||
|
||||
// Act
|
||||
var request = new AddOrganizationReportRequest
|
||||
{
|
||||
OrganizationId = Guid.NewGuid(),
|
||||
ReportData = "Report Data",
|
||||
Date = DateTime.UtcNow
|
||||
};
|
||||
await sutProvider.Sut.AddOrganizationReport(request);
|
||||
|
||||
// Assert
|
||||
_ = sutProvider.GetDependency<IAddOrganizationReportCommand>()
|
||||
.Received(1)
|
||||
.AddOrganizationReportAsync(Arg.Is<AddOrganizationReportRequest>(_ =>
|
||||
_.OrganizationId == request.OrganizationId &&
|
||||
_.ReportData == request.ReportData &&
|
||||
_.Date == request.Date));
|
||||
}
|
||||
|
||||
[Theory, BitAutoData]
|
||||
public async Task AddOrganizationReportAsync_withoutAccess(SutProvider<ReportsController> sutProvider)
|
||||
{
|
||||
// Arrange
|
||||
sutProvider.GetDependency<ICurrentContext>().AccessReports(Arg.Any<Guid>()).Returns(false);
|
||||
// Act
|
||||
var request = new AddOrganizationReportRequest
|
||||
{
|
||||
OrganizationId = Guid.NewGuid(),
|
||||
ReportData = "Report Data",
|
||||
Date = DateTime.UtcNow
|
||||
};
|
||||
await Assert.ThrowsAsync<NotFoundException>(async () =>
|
||||
await sutProvider.Sut.AddOrganizationReport(request));
|
||||
// Assert
|
||||
_ = sutProvider.GetDependency<IAddOrganizationReportCommand>()
|
||||
.Received(0);
|
||||
}
|
||||
|
||||
[Theory, BitAutoData]
|
||||
public async Task DropOrganizationReportAsync_withAccess_success(SutProvider<ReportsController> sutProvider)
|
||||
{
|
||||
// Arrange
|
||||
sutProvider.GetDependency<ICurrentContext>().AccessReports(Arg.Any<Guid>()).Returns(true);
|
||||
// Act
|
||||
var request = new DropOrganizationReportRequest
|
||||
{
|
||||
OrganizationId = Guid.NewGuid(),
|
||||
OrganizationReportIds = new List<Guid> { Guid.NewGuid(), Guid.NewGuid() }
|
||||
};
|
||||
await sutProvider.Sut.DropOrganizationReport(request);
|
||||
// Assert
|
||||
_ = sutProvider.GetDependency<IDropOrganizationReportCommand>()
|
||||
.Received(1)
|
||||
.DropOrganizationReportAsync(Arg.Is<DropOrganizationReportRequest>(_ =>
|
||||
_.OrganizationId == request.OrganizationId &&
|
||||
_.OrganizationReportIds.SequenceEqual(request.OrganizationReportIds)));
|
||||
}
|
||||
[Theory, BitAutoData]
|
||||
public async Task DropOrganizationReportAsync_withoutAccess(SutProvider<ReportsController> sutProvider)
|
||||
{
|
||||
// Arrange
|
||||
sutProvider.GetDependency<ICurrentContext>().AccessReports(Arg.Any<Guid>()).Returns(false);
|
||||
// Act
|
||||
var request = new DropOrganizationReportRequest
|
||||
{
|
||||
OrganizationId = Guid.NewGuid(),
|
||||
OrganizationReportIds = new List<Guid> { Guid.NewGuid(), Guid.NewGuid() }
|
||||
};
|
||||
await Assert.ThrowsAsync<NotFoundException>(async () =>
|
||||
await sutProvider.Sut.DropOrganizationReport(request));
|
||||
// Assert
|
||||
_ = sutProvider.GetDependency<IDropOrganizationReportCommand>()
|
||||
.Received(0);
|
||||
}
|
||||
[Theory, BitAutoData]
|
||||
public async Task GetOrganizationReportAsync_withAccess_success(SutProvider<ReportsController> sutProvider)
|
||||
{
|
||||
// Arrange
|
||||
sutProvider.GetDependency<ICurrentContext>().AccessReports(Arg.Any<Guid>()).Returns(true);
|
||||
// Act
|
||||
var orgId = Guid.NewGuid();
|
||||
var result = await sutProvider.Sut.GetOrganizationReports(orgId);
|
||||
// Assert
|
||||
_ = sutProvider.GetDependency<IGetOrganizationReportQuery>()
|
||||
.Received(1)
|
||||
.GetOrganizationReportAsync(Arg.Is<Guid>(_ => _ == orgId));
|
||||
}
|
||||
|
||||
[Theory, BitAutoData]
|
||||
public async Task GetOrganizationReportAsync_withoutAccess(SutProvider<ReportsController> sutProvider)
|
||||
{
|
||||
// Arrange
|
||||
sutProvider.GetDependency<ICurrentContext>().AccessReports(Arg.Any<Guid>()).Returns(false);
|
||||
// Act
|
||||
var orgId = Guid.NewGuid();
|
||||
await Assert.ThrowsAsync<NotFoundException>(async () => await sutProvider.Sut.GetOrganizationReports(orgId));
|
||||
// Assert
|
||||
_ = sutProvider.GetDependency<IGetOrganizationReportQuery>()
|
||||
.Received(0);
|
||||
|
||||
}
|
||||
|
||||
[Theory, BitAutoData]
|
||||
public async Task GetLastestOrganizationReportAsync_withAccess_success(SutProvider<ReportsController> sutProvider)
|
||||
{
|
||||
// Arrange
|
||||
sutProvider.GetDependency<ICurrentContext>().AccessReports(Arg.Any<Guid>()).Returns(true);
|
||||
|
||||
// Act
|
||||
var orgId = Guid.NewGuid();
|
||||
var result = await sutProvider.Sut.GetLatestOrganizationReport(orgId);
|
||||
|
||||
// Assert
|
||||
_ = sutProvider.GetDependency<IGetOrganizationReportQuery>()
|
||||
.Received(1)
|
||||
.GetLatestOrganizationReportAsync(Arg.Is<Guid>(_ => _ == orgId));
|
||||
}
|
||||
|
||||
[Theory, BitAutoData]
|
||||
public async Task GetLastestOrganizationReportAsync_withoutAccess(SutProvider<ReportsController> sutProvider)
|
||||
{
|
||||
// Arrange
|
||||
sutProvider.GetDependency<ICurrentContext>().AccessReports(Arg.Any<Guid>()).Returns(false);
|
||||
|
||||
// Act
|
||||
var orgId = Guid.NewGuid();
|
||||
await Assert.ThrowsAsync<NotFoundException>(async () => await sutProvider.Sut.GetLatestOrganizationReport(orgId));
|
||||
|
||||
// Assert
|
||||
_ = sutProvider.GetDependency<IGetOrganizationReportQuery>()
|
||||
.Received(0);
|
||||
}
|
||||
|
||||
[Theory, BitAutoData]
|
||||
public void CreateOrganizationReportSummary_ReturnsNoContent_WhenAccessGranted(SutProvider<ReportsController> sutProvider)
|
||||
{
|
||||
// Arrange
|
||||
var orgId = Guid.NewGuid();
|
||||
var model = new OrganizationReportSummaryModel
|
||||
{
|
||||
OrganizationId = orgId,
|
||||
EncryptedData = "mock-data",
|
||||
EncryptionKey = "mock-key",
|
||||
Date = DateTime.UtcNow
|
||||
};
|
||||
sutProvider.GetDependency<ICurrentContext>().AccessReports(orgId).Returns(true);
|
||||
|
||||
// Act
|
||||
var result = sutProvider.Sut.CreateOrganizationReportSummary(model);
|
||||
|
||||
// Assert
|
||||
Assert.IsType<NoContentResult>(result);
|
||||
}
|
||||
|
||||
[Theory, BitAutoData]
|
||||
public void CreateOrganizationReportSummary_ThrowsNotFoundException_WhenAccessDenied(SutProvider<ReportsController> sutProvider)
|
||||
{
|
||||
// Arrange
|
||||
var orgId = Guid.NewGuid();
|
||||
var model = new OrganizationReportSummaryModel
|
||||
{
|
||||
OrganizationId = orgId,
|
||||
EncryptedData = "mock-data",
|
||||
EncryptionKey = "mock-key",
|
||||
Date = DateTime.UtcNow
|
||||
};
|
||||
sutProvider.GetDependency<ICurrentContext>().AccessReports(orgId).Returns(false);
|
||||
|
||||
// Act & Assert
|
||||
Assert.Throws<Bit.Core.Exceptions.NotFoundException>(
|
||||
() => sutProvider.Sut.CreateOrganizationReportSummary(model));
|
||||
}
|
||||
|
||||
[Theory, BitAutoData]
|
||||
public void GetOrganizationReportSummary_ThrowsNotFoundException_WhenAccessDenied(
|
||||
SutProvider<ReportsController> sutProvider
|
||||
)
|
||||
{
|
||||
// Arrange
|
||||
var orgId = Guid.NewGuid();
|
||||
|
||||
sutProvider.GetDependency<ICurrentContext>().AccessReports(orgId).Returns(false);
|
||||
|
||||
// Act & Assert
|
||||
Assert.Throws<Bit.Core.Exceptions.NotFoundException>(
|
||||
() => sutProvider.Sut.GetOrganizationReportSummary(orgId, DateOnly.FromDateTime(DateTime.UtcNow), DateOnly.FromDateTime(DateTime.UtcNow)));
|
||||
}
|
||||
|
||||
[Theory, BitAutoData]
|
||||
public void GetOrganizationReportSummary_returnsExpectedResult(
|
||||
SutProvider<ReportsController> sutProvider
|
||||
)
|
||||
{
|
||||
// Arrange
|
||||
var orgId = Guid.NewGuid();
|
||||
var dates = new[]
|
||||
{
|
||||
DateOnly.FromDateTime(DateTime.UtcNow),
|
||||
DateOnly.FromDateTime(DateTime.UtcNow.AddMonths(-1))
|
||||
};
|
||||
|
||||
sutProvider.GetDependency<ICurrentContext>().AccessReports(orgId).Returns(true);
|
||||
|
||||
// Act
|
||||
var result = sutProvider.Sut.GetOrganizationReportSummary(orgId, dates[0], dates[1]);
|
||||
|
||||
// Assert
|
||||
Assert.NotNull(result);
|
||||
}
|
||||
|
||||
[Theory, BitAutoData]
|
||||
public void CreateOrganizationReportSummary_ReturnsNoContent_WhenModelIsValidAndAccessGranted(
|
||||
SutProvider<ReportsController> sutProvider
|
||||
)
|
||||
{
|
||||
// Arrange
|
||||
var orgId = Guid.NewGuid();
|
||||
var model = new OrganizationReportSummaryModel
|
||||
{
|
||||
OrganizationId = orgId,
|
||||
EncryptedData = "mock-data",
|
||||
EncryptionKey = "mock-key"
|
||||
};
|
||||
sutProvider.Sut.ModelState.Clear();
|
||||
sutProvider.GetDependency<ICurrentContext>().AccessReports(orgId).Returns(true);
|
||||
|
||||
// Act
|
||||
var result = sutProvider.Sut.CreateOrganizationReportSummary(model);
|
||||
|
||||
// Assert
|
||||
Assert.IsType<NoContentResult>(result);
|
||||
}
|
||||
|
||||
[Theory, BitAutoData]
|
||||
public void CreateOrganizationReportSummary_ThrowsBadRequestException_WhenModelStateIsInvalid(
|
||||
SutProvider<ReportsController> sutProvider
|
||||
)
|
||||
{
|
||||
// Arrange
|
||||
var orgId = Guid.NewGuid();
|
||||
var model = new OrganizationReportSummaryModel
|
||||
{
|
||||
OrganizationId = orgId,
|
||||
EncryptedData = "mock-data",
|
||||
EncryptionKey = "mock-key"
|
||||
};
|
||||
sutProvider.Sut.ModelState.AddModelError("key", "error");
|
||||
|
||||
// Act & Assert
|
||||
Assert.Throws<BadRequestException>(() => sutProvider.Sut.CreateOrganizationReportSummary(model));
|
||||
}
|
||||
|
||||
[Theory, BitAutoData]
|
||||
public void UpdateOrganizationReportSummary_ReturnsNoContent_WhenModelIsValidAndAccessGranted(
|
||||
SutProvider<ReportsController> sutProvider
|
||||
)
|
||||
{
|
||||
// Arrange
|
||||
var orgId = Guid.NewGuid();
|
||||
var model = new OrganizationReportSummaryModel
|
||||
{
|
||||
OrganizationId = orgId,
|
||||
EncryptedData = "mock-data",
|
||||
EncryptionKey = "mock-key"
|
||||
};
|
||||
sutProvider.Sut.ModelState.Clear();
|
||||
sutProvider.GetDependency<ICurrentContext>().AccessReports(orgId).Returns(true);
|
||||
|
||||
// Act
|
||||
var result = sutProvider.Sut.UpdateOrganizationReportSummary(model);
|
||||
|
||||
// Assert
|
||||
Assert.IsType<NoContentResult>(result);
|
||||
}
|
||||
|
||||
[Theory, BitAutoData]
|
||||
public void UpdateOrganizationReportSummary_ThrowsBadRequestException_WhenModelStateIsInvalid(
|
||||
SutProvider<ReportsController> sutProvider
|
||||
)
|
||||
{
|
||||
// Arrange
|
||||
var orgId = Guid.NewGuid();
|
||||
var model = new OrganizationReportSummaryModel
|
||||
{
|
||||
OrganizationId = orgId,
|
||||
EncryptedData = "mock-data",
|
||||
EncryptionKey = "mock-key"
|
||||
};
|
||||
sutProvider.Sut.ModelState.AddModelError("key", "error");
|
||||
|
||||
// Act & Assert
|
||||
Assert.Throws<BadRequestException>(() => sutProvider.Sut.UpdateOrganizationReportSummary(model));
|
||||
}
|
||||
|
||||
[Theory, BitAutoData]
|
||||
public void UpdateOrganizationReportSummary_ThrowsNotFoundException_WhenAccessDenied(
|
||||
SutProvider<ReportsController> sutProvider
|
||||
)
|
||||
{
|
||||
// Arrange
|
||||
var orgId = Guid.NewGuid();
|
||||
var model = new OrganizationReportSummaryModel
|
||||
{
|
||||
OrganizationId = orgId,
|
||||
EncryptedData = "mock-data",
|
||||
EncryptionKey = "mock-key"
|
||||
};
|
||||
sutProvider.Sut.ModelState.Clear();
|
||||
sutProvider.GetDependency<ICurrentContext>().AccessReports(orgId).Returns(false);
|
||||
|
||||
// Act & Assert
|
||||
Assert.Throws<NotFoundException>(() => sutProvider.Sut.UpdateOrganizationReportSummary(model));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user