fix(template-folder): bound cycle-walk to defend against malformed graphs

This commit is contained in:
Joseph Doherty
2026-05-11 10:58:02 -04:00
parent 1269054651
commit e44bbc0caf
2 changed files with 24 additions and 1 deletions

View File

@@ -167,4 +167,23 @@ public class TemplateFolderServiceTests
Assert.True(result.IsSuccess);
Assert.Null(result.Value.ParentFolderId);
}
[Fact]
public async Task MoveFolder_PreExistingCycleInGraph_ReturnsFailure_DoesNotInfiniteLoop()
{
// Manufactured malformed graph: X.parent=Y, Y.parent=X. We move Z under X.
// The ancestor walk would loop forever without a guard.
var x = new TemplateFolder("X") { Id = 1, ParentFolderId = 2 };
var y = new TemplateFolder("Y") { Id = 2, ParentFolderId = 1 };
var z = new TemplateFolder("Z") { Id = 3, ParentFolderId = null };
_repoMock.Setup(r => r.GetFolderByIdAsync(3, It.IsAny<CancellationToken>())).ReturnsAsync(z);
_repoMock.Setup(r => r.GetFolderByIdAsync(1, It.IsAny<CancellationToken>())).ReturnsAsync(x);
_repoMock.Setup(r => r.GetAllFoldersAsync(It.IsAny<CancellationToken>()))
.ReturnsAsync(new List<TemplateFolder> { x, y, z });
var result = await _sut.MoveFolderAsync(3, 1, "admin");
Assert.True(result.IsFailure);
Assert.Contains("cycle", result.Error, StringComparison.OrdinalIgnoreCase);
}
}