fix(template-folder): bound cycle-walk to defend against malformed graphs
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user