mirror of
https://github.com/Sea-Haven-Industries/shoc-backend.git
synced 2026-10-04 23:02:10 +00:00
feat(work-orders): add authorized media content GET
This commit is contained in:
parent
b4f2c8b763
commit
9d8ae5ec24
2 changed files with 58 additions and 0 deletions
|
|
@ -5,6 +5,7 @@ using Microsoft.AspNetCore.Mvc;
|
||||||
using Moq;
|
using Moq;
|
||||||
using SeaHaven.DataServices.Interfaces;
|
using SeaHaven.DataServices.Interfaces;
|
||||||
using SeaHaven.Services.DTOs;
|
using SeaHaven.Services.DTOs;
|
||||||
|
using SeaHaven.Services.Exceptions;
|
||||||
using SeaHaven.Services.Helpers;
|
using SeaHaven.Services.Helpers;
|
||||||
using SeaHaven.Services.Implementation;
|
using SeaHaven.Services.Implementation;
|
||||||
using SeaHaven.Services.Interfaces;
|
using SeaHaven.Services.Interfaces;
|
||||||
|
|
@ -77,6 +78,42 @@ public class WorkOrderMediaControllerTests
|
||||||
Assert.Equal("UnsupportedMediaType", error.Code);
|
Assert.Equal("UnsupportedMediaType", error.Code);
|
||||||
storage.VerifyNoOtherCalls();
|
storage.VerifyNoOtherCalls();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task GetMediaContent_DeletedMedia_ReturnsNotFound()
|
||||||
|
{
|
||||||
|
var service = new Mock<IWorkOrderMediaService>(MockBehavior.Strict);
|
||||||
|
service.Setup(candidate => candidate.GetMediaContentAsync(
|
||||||
|
1, 10, It.IsAny<ClaimsPrincipal>(), It.IsAny<string?>(), It.IsAny<CancellationToken>()))
|
||||||
|
.ThrowsAsync(new WorkOrderBoardValidationException("NotFound", "Media not found."));
|
||||||
|
var storage = new Mock<IFileStoragePort>(MockBehavior.Strict);
|
||||||
|
var controller = CreateController(service, storage);
|
||||||
|
|
||||||
|
var result = await controller.GetMediaContent(1, 10, CancellationToken.None);
|
||||||
|
|
||||||
|
var response = Assert.IsType<NotFoundObjectResult>(result);
|
||||||
|
var error = Assert.IsType<WorkOrderBoardValidationErrorDto>(response.Value);
|
||||||
|
Assert.Equal("NotFound", error.Code);
|
||||||
|
storage.Verify(candidate => candidate.TryDelete(It.IsAny<string>()), Times.Never);
|
||||||
|
storage.VerifyNoOtherCalls();
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task DeleteMedia_Success_DoesNotCallTryDelete()
|
||||||
|
{
|
||||||
|
var service = new Mock<IWorkOrderMediaService>(MockBehavior.Strict);
|
||||||
|
service.Setup(candidate => candidate.DeleteMediaAsync(
|
||||||
|
1, 10, null, It.IsAny<ClaimsPrincipal>(), It.IsAny<string?>(), It.IsAny<CancellationToken>()))
|
||||||
|
.Returns(Task.CompletedTask);
|
||||||
|
var storage = new Mock<IFileStoragePort>(MockBehavior.Strict);
|
||||||
|
var controller = CreateController(service, storage);
|
||||||
|
|
||||||
|
var result = await controller.DeleteMedia(1, 10);
|
||||||
|
|
||||||
|
Assert.IsType<NoContentResult>(result);
|
||||||
|
storage.Verify(candidate => candidate.TryDelete(It.IsAny<string>()), Times.Never);
|
||||||
|
storage.VerifyNoOtherCalls();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public class WorkOrderCompletionControllerUploadLimitTests
|
public class WorkOrderCompletionControllerUploadLimitTests
|
||||||
|
|
|
||||||
|
|
@ -52,6 +52,27 @@ namespace Api.SeaHavenIndustries.Controllers
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[HttpGet("{id:int}/media/{mediaId:int}/content")]
|
||||||
|
public async Task<IActionResult> GetMediaContent(int id, int mediaId, CancellationToken cancellationToken)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var actorId = User.FindFirstValue(ClaimTypes.NameIdentifier);
|
||||||
|
var content = await _workOrderMediaService.GetMediaContentAsync(
|
||||||
|
id, mediaId, User, actorId, cancellationToken);
|
||||||
|
return File(content.Content, content.ContentType, content.FileName);
|
||||||
|
}
|
||||||
|
catch (WorkOrderBoardValidationException ex) when (ex.Code == "NotFound")
|
||||||
|
{
|
||||||
|
return NotFound(new WorkOrderBoardValidationErrorDto { Code = ex.Code, Message = ex.Message });
|
||||||
|
}
|
||||||
|
catch (WorkOrderBoardValidationException ex) when (ex.Code == "Forbidden")
|
||||||
|
{
|
||||||
|
return StatusCode(StatusCodes.Status403Forbidden,
|
||||||
|
new WorkOrderBoardValidationErrorDto { Code = ex.Code, Message = ex.Message });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
[HttpPost("{id:int}/media")]
|
[HttpPost("{id:int}/media")]
|
||||||
[RequestSizeLimit(MaxUploadBytes)]
|
[RequestSizeLimit(MaxUploadBytes)]
|
||||||
public async Task<IActionResult> AddMedia(
|
public async Task<IActionResult> AddMedia(
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue