|
8 | 8 | using Bit.Core.Dirt.Reports.ReportFeatures.Interfaces; |
9 | 9 | using Bit.Core.Dirt.Reports.ReportFeatures.Requests; |
10 | 10 | using Bit.Core.Dirt.Reports.Services; |
| 11 | +using Bit.Core.Dirt.Repositories; |
11 | 12 | using Bit.Core.Enums; |
12 | 13 | using Bit.Core.Exceptions; |
13 | 14 | using Bit.Core.Models.Data.Organizations; |
@@ -384,6 +385,277 @@ await sutProvider.GetDependency<IGetOrganizationReportQuery>() |
384 | 385 | .GetOrganizationReportAsync(Arg.Any<Guid>()); |
385 | 386 | } |
386 | 387 |
|
| 388 | + // DeleteOrganizationReportAsync |
| 389 | + |
| 390 | + [Theory, BitAutoData] |
| 391 | + public async Task DeleteOrganizationReportAsync_WithFile_DeletesDbThenStorage( |
| 392 | + SutProvider<OrganizationReportsController> sutProvider, |
| 393 | + Guid orgId, |
| 394 | + OrganizationReport report) |
| 395 | + { |
| 396 | + // Arrange |
| 397 | + var reportFile = new ReportFile { Id = "file-id", FileName = "report.json", Size = 1024, Validated = true }; |
| 398 | + report.OrganizationId = orgId; |
| 399 | + report.SetReportFile(reportFile); |
| 400 | + |
| 401 | + SetupAuthorization(sutProvider, orgId); |
| 402 | + |
| 403 | + sutProvider.GetDependency<IOrganizationReportRepository>() |
| 404 | + .GetByIdAsync(report.Id) |
| 405 | + .Returns(report); |
| 406 | + |
| 407 | + // Act |
| 408 | + await sutProvider.Sut.DeleteOrganizationReportAsync(orgId, report.Id); |
| 409 | + |
| 410 | + // Assert |
| 411 | + await sutProvider.GetDependency<IOrganizationReportRepository>() |
| 412 | + .Received(1) |
| 413 | + .DeleteAsync(report); |
| 414 | + |
| 415 | + await sutProvider.GetDependency<IOrganizationReportStorageService>() |
| 416 | + .Received(1) |
| 417 | + .DeleteReportFilesAsync(report, "file-id"); |
| 418 | + } |
| 419 | + |
| 420 | + [Theory, BitAutoData] |
| 421 | + public async Task DeleteOrganizationReportAsync_WithNoFile_DeletesDbOnly( |
| 422 | + SutProvider<OrganizationReportsController> sutProvider, |
| 423 | + Guid orgId, |
| 424 | + OrganizationReport report) |
| 425 | + { |
| 426 | + // Arrange |
| 427 | + report.OrganizationId = orgId; |
| 428 | + report.ReportFile = null; |
| 429 | + |
| 430 | + SetupAuthorization(sutProvider, orgId); |
| 431 | + |
| 432 | + sutProvider.GetDependency<IOrganizationReportRepository>() |
| 433 | + .GetByIdAsync(report.Id) |
| 434 | + .Returns(report); |
| 435 | + |
| 436 | + // Act |
| 437 | + await sutProvider.Sut.DeleteOrganizationReportAsync(orgId, report.Id); |
| 438 | + |
| 439 | + // Assert |
| 440 | + await sutProvider.GetDependency<IOrganizationReportRepository>() |
| 441 | + .Received(1) |
| 442 | + .DeleteAsync(report); |
| 443 | + |
| 444 | + await sutProvider.GetDependency<IOrganizationReportStorageService>() |
| 445 | + .DidNotReceive() |
| 446 | + .DeleteReportFilesAsync(Arg.Any<OrganizationReport>(), Arg.Any<string>()); |
| 447 | + } |
| 448 | + |
| 449 | + [Theory, BitAutoData] |
| 450 | + public async Task DeleteOrganizationReportAsync_ReportNotFound_ThrowsNotFoundException( |
| 451 | + SutProvider<OrganizationReportsController> sutProvider, |
| 452 | + Guid orgId, |
| 453 | + Guid reportId) |
| 454 | + { |
| 455 | + // Arrange |
| 456 | + SetupAuthorization(sutProvider, orgId); |
| 457 | + |
| 458 | + sutProvider.GetDependency<IOrganizationReportRepository>() |
| 459 | + .GetByIdAsync(reportId) |
| 460 | + .Returns((OrganizationReport)null); |
| 461 | + |
| 462 | + // Act & Assert |
| 463 | + await Assert.ThrowsAsync<NotFoundException>(() => |
| 464 | + sutProvider.Sut.DeleteOrganizationReportAsync(orgId, reportId)); |
| 465 | + |
| 466 | + await sutProvider.GetDependency<IOrganizationReportRepository>() |
| 467 | + .DidNotReceive() |
| 468 | + .DeleteAsync(Arg.Any<OrganizationReport>()); |
| 469 | + } |
| 470 | + |
| 471 | + [Theory, BitAutoData] |
| 472 | + public async Task DeleteOrganizationReportAsync_OrgMismatch_ThrowsBadRequestException( |
| 473 | + SutProvider<OrganizationReportsController> sutProvider, |
| 474 | + Guid orgId, |
| 475 | + OrganizationReport report) |
| 476 | + { |
| 477 | + // Arrange |
| 478 | + report.OrganizationId = Guid.NewGuid(); |
| 479 | + |
| 480 | + SetupAuthorization(sutProvider, orgId); |
| 481 | + |
| 482 | + sutProvider.GetDependency<IOrganizationReportRepository>() |
| 483 | + .GetByIdAsync(report.Id) |
| 484 | + .Returns(report); |
| 485 | + |
| 486 | + // Act & Assert |
| 487 | + var exception = await Assert.ThrowsAsync<BadRequestException>(() => |
| 488 | + sutProvider.Sut.DeleteOrganizationReportAsync(orgId, report.Id)); |
| 489 | + |
| 490 | + Assert.Equal("Invalid report ID", exception.Message); |
| 491 | + |
| 492 | + await sutProvider.GetDependency<IOrganizationReportRepository>() |
| 493 | + .DidNotReceive() |
| 494 | + .DeleteAsync(Arg.Any<OrganizationReport>()); |
| 495 | + } |
| 496 | + |
| 497 | + [Theory, BitAutoData] |
| 498 | + public async Task DeleteOrganizationReportAsync_WithoutAccess_ThrowsNotFoundException( |
| 499 | + SutProvider<OrganizationReportsController> sutProvider, |
| 500 | + Guid orgId, |
| 501 | + Guid reportId) |
| 502 | + { |
| 503 | + // Arrange |
| 504 | + sutProvider.GetDependency<ICurrentContext>() |
| 505 | + .AccessReports(orgId) |
| 506 | + .Returns(false); |
| 507 | + |
| 508 | + // Act & Assert |
| 509 | + await Assert.ThrowsAsync<NotFoundException>(() => |
| 510 | + sutProvider.Sut.DeleteOrganizationReportAsync(orgId, reportId)); |
| 511 | + |
| 512 | + await sutProvider.GetDependency<IOrganizationReportRepository>() |
| 513 | + .DidNotReceive() |
| 514 | + .DeleteAsync(Arg.Any<OrganizationReport>()); |
| 515 | + } |
| 516 | + |
| 517 | + // RenewFileUploadUrlAsync |
| 518 | + |
| 519 | + [Theory, BitAutoData] |
| 520 | + public async Task RenewFileUploadUrlAsync_WithUnvalidatedFile_ReturnsRenewedUrl( |
| 521 | + SutProvider<OrganizationReportsController> sutProvider, |
| 522 | + Guid orgId, |
| 523 | + OrganizationReport report, |
| 524 | + string uploadUrl) |
| 525 | + { |
| 526 | + // Arrange |
| 527 | + var reportFile = new ReportFile { Id = "file-id", FileName = "report.json", Size = 1024, Validated = false }; |
| 528 | + report.OrganizationId = orgId; |
| 529 | + report.SetReportFile(reportFile); |
| 530 | + |
| 531 | + SetupV2Authorization(sutProvider, orgId); |
| 532 | + |
| 533 | + sutProvider.GetDependency<IOrganizationReportRepository>() |
| 534 | + .GetByIdAsync(report.Id) |
| 535 | + .Returns(report); |
| 536 | + |
| 537 | + sutProvider.GetDependency<IOrganizationReportStorageService>() |
| 538 | + .GetReportFileUploadUrlAsync(report, Arg.Any<ReportFile>()) |
| 539 | + .Returns(uploadUrl); |
| 540 | + |
| 541 | + sutProvider.GetDependency<IOrganizationReportStorageService>() |
| 542 | + .FileUploadType |
| 543 | + .Returns(FileUploadType.Azure); |
| 544 | + |
| 545 | + // Act |
| 546 | + var result = await sutProvider.Sut.RenewFileUploadUrlAsync(orgId, report.Id, "file-id"); |
| 547 | + |
| 548 | + // Assert |
| 549 | + Assert.Equal(uploadUrl, result.ReportFileUploadUrl); |
| 550 | + Assert.Equal(FileUploadType.Azure, result.FileUploadType); |
| 551 | + Assert.NotNull(result.ReportResponse); |
| 552 | + } |
| 553 | + |
| 554 | + [Theory, BitAutoData] |
| 555 | + public async Task RenewFileUploadUrlAsync_ReportNotFound_ThrowsNotFoundException( |
| 556 | + SutProvider<OrganizationReportsController> sutProvider, |
| 557 | + Guid orgId, |
| 558 | + Guid reportId) |
| 559 | + { |
| 560 | + // Arrange |
| 561 | + SetupV2Authorization(sutProvider, orgId); |
| 562 | + |
| 563 | + sutProvider.GetDependency<IOrganizationReportRepository>() |
| 564 | + .GetByIdAsync(reportId) |
| 565 | + .Returns((OrganizationReport)null); |
| 566 | + |
| 567 | + // Act & Assert |
| 568 | + await Assert.ThrowsAsync<NotFoundException>(() => |
| 569 | + sutProvider.Sut.RenewFileUploadUrlAsync(orgId, reportId, "file-id")); |
| 570 | + } |
| 571 | + |
| 572 | + [Theory, BitAutoData] |
| 573 | + public async Task RenewFileUploadUrlAsync_OrgMismatch_ThrowsBadRequestException( |
| 574 | + SutProvider<OrganizationReportsController> sutProvider, |
| 575 | + Guid orgId, |
| 576 | + OrganizationReport report) |
| 577 | + { |
| 578 | + // Arrange |
| 579 | + report.OrganizationId = Guid.NewGuid(); |
| 580 | + |
| 581 | + SetupV2Authorization(sutProvider, orgId); |
| 582 | + |
| 583 | + sutProvider.GetDependency<IOrganizationReportRepository>() |
| 584 | + .GetByIdAsync(report.Id) |
| 585 | + .Returns(report); |
| 586 | + |
| 587 | + // Act & Assert |
| 588 | + var exception = await Assert.ThrowsAsync<BadRequestException>(() => |
| 589 | + sutProvider.Sut.RenewFileUploadUrlAsync(orgId, report.Id, "file-id")); |
| 590 | + |
| 591 | + Assert.Equal("Invalid report ID", exception.Message); |
| 592 | + } |
| 593 | + |
| 594 | + [Theory, BitAutoData] |
| 595 | + public async Task RenewFileUploadUrlAsync_FileAlreadyValidated_ThrowsNotFoundException( |
| 596 | + SutProvider<OrganizationReportsController> sutProvider, |
| 597 | + Guid orgId, |
| 598 | + OrganizationReport report) |
| 599 | + { |
| 600 | + // Arrange |
| 601 | + var reportFile = new ReportFile { Id = "file-id", FileName = "report.json", Size = 1024, Validated = true }; |
| 602 | + report.OrganizationId = orgId; |
| 603 | + report.SetReportFile(reportFile); |
| 604 | + |
| 605 | + SetupV2Authorization(sutProvider, orgId); |
| 606 | + |
| 607 | + sutProvider.GetDependency<IOrganizationReportRepository>() |
| 608 | + .GetByIdAsync(report.Id) |
| 609 | + .Returns(report); |
| 610 | + |
| 611 | + // Act & Assert |
| 612 | + await Assert.ThrowsAsync<NotFoundException>(() => |
| 613 | + sutProvider.Sut.RenewFileUploadUrlAsync(orgId, report.Id, "file-id")); |
| 614 | + } |
| 615 | + |
| 616 | + [Theory, BitAutoData] |
| 617 | + public async Task RenewFileUploadUrlAsync_NoFileData_ThrowsNotFoundException( |
| 618 | + SutProvider<OrganizationReportsController> sutProvider, |
| 619 | + Guid orgId, |
| 620 | + OrganizationReport report) |
| 621 | + { |
| 622 | + // Arrange |
| 623 | + report.OrganizationId = orgId; |
| 624 | + report.ReportFile = null; |
| 625 | + |
| 626 | + SetupV2Authorization(sutProvider, orgId); |
| 627 | + |
| 628 | + sutProvider.GetDependency<IOrganizationReportRepository>() |
| 629 | + .GetByIdAsync(report.Id) |
| 630 | + .Returns(report); |
| 631 | + |
| 632 | + // Act & Assert |
| 633 | + await Assert.ThrowsAsync<NotFoundException>(() => |
| 634 | + sutProvider.Sut.RenewFileUploadUrlAsync(orgId, report.Id, "file-id")); |
| 635 | + } |
| 636 | + |
| 637 | + [Theory, BitAutoData] |
| 638 | + public async Task RenewFileUploadUrlAsync_MismatchedFileId_ThrowsNotFoundException( |
| 639 | + SutProvider<OrganizationReportsController> sutProvider, |
| 640 | + Guid orgId, |
| 641 | + OrganizationReport report) |
| 642 | + { |
| 643 | + // Arrange |
| 644 | + var reportFile = new ReportFile { Id = "file-id", FileName = "report.json", Size = 1024, Validated = false }; |
| 645 | + report.OrganizationId = orgId; |
| 646 | + report.SetReportFile(reportFile); |
| 647 | + |
| 648 | + SetupV2Authorization(sutProvider, orgId); |
| 649 | + |
| 650 | + sutProvider.GetDependency<IOrganizationReportRepository>() |
| 651 | + .GetByIdAsync(report.Id) |
| 652 | + .Returns(report); |
| 653 | + |
| 654 | + // Act & Assert |
| 655 | + await Assert.ThrowsAsync<NotFoundException>(() => |
| 656 | + sutProvider.Sut.RenewFileUploadUrlAsync(orgId, report.Id, "wrong-file-id")); |
| 657 | + } |
| 658 | + |
387 | 659 | // UpdateOrganizationReportAsync - V1 (flag off) |
388 | 660 |
|
389 | 661 | [Theory, BitAutoData] |
|
0 commit comments