Skip to content

Commit 861b375

Browse files
committed
Add test for failed authorization during restore
Add a unit test RestoreArchiveItemAsync_ShouldReturnFailure_WhenAuthorizationCheckFails to ArchiveRecoveryServiceTests. The test sets up an archived item and mocks the authorization service to return a NotFound error for CanWriteBoardAsync, then asserts the service returns a failure with ErrorCode NotFound and an error message indicating the board is missing. This covers the authorization failure path in RestoreArchiveItemAsync.
1 parent 2b2b6fd commit 861b375

1 file changed

Lines changed: 24 additions & 0 deletions

File tree

backend/tests/Taskdeck.Application.Tests/Services/ArchiveRecoveryServiceTests.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,30 @@ public async Task RestoreArchiveItemAsync_ShouldReturnForbidden_WhenUserLacksPer
372372
result.ErrorCode.Should().Be(ErrorCodes.Forbidden);
373373
}
374374

375+
[Fact]
376+
public async Task RestoreArchiveItemAsync_ShouldReturnFailure_WhenAuthorizationCheckFails()
377+
{
378+
// Arrange
379+
var userId = Guid.NewGuid();
380+
var boardId = Guid.NewGuid();
381+
var snapshotJson = JsonSerializer.Serialize(new { Name = "Test", Description = (string?)null });
382+
var item = CreateArchiveItem("board", Guid.NewGuid(), boardId, "Test", userId, RestoreStatus.Available, snapshotJson);
383+
var dto = new RestoreArchiveItemDto(null, RestoreMode.InPlace, ConflictStrategy.Fail);
384+
385+
_archiveItemRepoMock.Setup(r => r.GetByIdAsync(item.Id, default))
386+
.ReturnsAsync(item);
387+
_authorizationServiceMock.Setup(s => s.CanWriteBoardAsync(userId, boardId))
388+
.ReturnsAsync(Result.Failure<bool>(ErrorCodes.NotFound, "Board missing"));
389+
390+
// Act
391+
var result = await _service.RestoreArchiveItemAsync(item.Id, dto, userId);
392+
393+
// Assert
394+
result.IsSuccess.Should().BeFalse();
395+
result.ErrorCode.Should().Be(ErrorCodes.NotFound);
396+
result.ErrorMessage.Should().Contain("missing");
397+
}
398+
375399
[Fact]
376400
public async Task RestoreArchiveItemAsync_ShouldReturnNotFound_WhenTargetBoardDoesNotExist()
377401
{

0 commit comments

Comments
 (0)