diff options
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/slskd.Tests.Unit/Files/FileServiceTests.cs | 137 |
1 files changed, 137 insertions, 0 deletions
diff --git a/tests/slskd.Tests.Unit/Files/FileServiceTests.cs b/tests/slskd.Tests.Unit/Files/FileServiceTests.cs new file mode 100644 index 00000000..7c9931ab --- /dev/null +++ b/tests/slskd.Tests.Unit/Files/FileServiceTests.cs @@ -0,0 +1,137 @@ +using Microsoft.Extensions.Options; + +namespace slskd.Tests.Unit.Files +{ + using System; + using System.IO; + using System.Threading.Tasks; + using Moq; + using slskd.Files; + using Xunit; + + public class FileServiceTests : IDisposable + { + public FileServiceTests() + { + OptionsSnapshotMock = new Mock<IOptionsSnapshot<Options>>(); + + Temp = Path.Combine(Path.GetTempPath(), $"slskd.test.{Guid.NewGuid()}"); + Directory.CreateDirectory(Temp); + + FileService = new FileService( + optionsSnapshot: OptionsSnapshotMock.Object); + } + + public void Dispose() + { + Directory.Delete(Temp, recursive: true); + } + + private Mock<IOptionsSnapshot<Options>> OptionsSnapshotMock { get; init; } + private string Temp { get; init; } + private IFileService FileService { get; init; } + + [Fact] + public async Task ListContentsAsync_Throws_ArgumentException_Given_Relative_Path() + { + var ex = await Record.ExceptionAsync(() => FileService.ListContentsAsync(directory: "../")); + + Assert.NotNull(ex); + Assert.IsType<ArgumentException>(ex); + Assert.Equal("directory", ((ArgumentException)ex).ParamName); + } + + [Fact] + public async Task ListContentsAsync_Throws_UnauthorizedException_Given_Disallowed_Directory() + { + OptionsSnapshotMock.Setup(o => o.Value).Returns(new Options + { + Directories = new Options.DirectoriesOptions + { + Downloads = Path.Combine(Temp, "downloads"), + Incomplete = Path.Combine(Temp, "incomplete"), + } + }); + + var ex = await Record.ExceptionAsync(() => FileService.ListContentsAsync(directory: Path.Combine(Temp, "foo"))); + + Assert.NotNull(ex); + Assert.IsType<UnauthorizedException>(ex); + } + + [Fact] + public async Task ListContentsAsync_Throws_NotFoundException_Given_NonExistent_Directory() + { + OptionsSnapshotMock.Setup(o => o.Value).Returns(new Options + { + Directories = new Options.DirectoriesOptions + { + Downloads = Path.Combine(Temp, "downloads"), + Incomplete = Path.Combine(Temp, "incomplete"), + } + }); + + var ex = await Record.ExceptionAsync(() => FileService.ListContentsAsync(directory: Path.Combine(Temp, "downloads", "foo"))); + + Assert.NotNull(ex); + Assert.IsType<NotFoundException>(ex); + } + + [Fact] + public async Task DeleteDirectoriesAsync_Throws_ArgumentException_Given_Relative_Path() + { + var ex = await Record.ExceptionAsync(() => FileService.DeleteDirectoriesAsync("../foo")); + + Assert.NotNull(ex); + Assert.IsType<ArgumentException>(ex); + Assert.Equal("directories", ((ArgumentException)ex).ParamName); + } + + [Fact] + public async Task DeleteDirectoriesAsync_Throws_ArgumentException_Given_Disallowed_Path() + { + OptionsSnapshotMock.Setup(o => o.Value).Returns(new Options + { + Directories = new Options.DirectoriesOptions + { + Downloads = Path.Combine(Temp, "downloads"), + Incomplete = Path.Combine(Temp, "incomplete"), + } + }); + + var ex = await Record.ExceptionAsync(() => FileService.DeleteDirectoriesAsync(Path.Combine(Temp, "foo"))); + + Assert.NotNull(ex); + Assert.IsType<UnauthorizedException>(ex); + } + + [Fact] + public async Task DeleteFilesAsync_Throws_ArgumentException_Given_Relative_Path() + { + var ex = await Record.ExceptionAsync(() => FileService.DeleteFilesAsync("../foo.bar")); + + Assert.NotNull(ex); + Assert.IsType<ArgumentException>(ex); + Assert.Equal("files", ((ArgumentException)ex).ParamName); + } + + [Fact] + public async Task DeleteFilesAsync_Throws_ArgumentException_Given_Disallowed_Path() + { + OptionsSnapshotMock.Setup(o => o.Value).Returns(new Options + { + Directories = new Options.DirectoriesOptions + { + Downloads = Path.Combine(Temp, "downloads"), + Incomplete = Path.Combine(Temp, "incomplete"), + } + }); + + var ex = await Record.ExceptionAsync(() => FileService.DeleteFilesAsync(Path.Combine(Temp, "foo"))); + + Assert.NotNull(ex); + Assert.IsType<UnauthorizedException>(ex); + } + } +} + |
