Fix some null checks when qBittorrent returns an empty response (#784)

This commit is contained in:
Flaminel authored and GitHub committed 2026-09-11 15:03:06 +03:00
1 parent 9a41af9f7e
commit 95b0ccac64
5 files changed
+40 -4

No files matched your search

@@ -247,6 +247,25 @@ public class QBitServiceTests : IClassFixture<QBitServiceFixture>
result.DeleteFromClient.ShouldBeFalse();
}
[Fact]
public async Task TorrentListIsNull_ReturnsEmptyResult()
{
// Arrange
const string hash = "test-hash";
QBitService sut = _fixture.CreateSut();
_fixture.ClientWrapper
.GetTorrentListAsync(Arg.Any<TorrentListQuery>())
.Returns((IReadOnlyList<TorrentInfo>?)null);
// Act
DownloadCheckResult result = await sut.ShouldRemoveFromArrQueueAsync(hash, Array.Empty<string>());
// Assert
result.Found.ShouldBeFalse();
result.ShouldRemove.ShouldBeFalse();
}
[Fact]
public async Task TorrentTrackersNotFound_ReturnsEmptyResult()
{
@@ -1487,6 +1506,23 @@ public class QBitServiceTests : IClassFixture<QBitServiceFixture>
result.DeleteReason.ShouldBe(DeleteReason.None);
}
[Fact]
public async Task TorrentListIsNull_ReturnsNotFound()
{
const string hash = "deleted-hash";
QBitService sut = _fixture.CreateSut();
SetMalwareBlockerContext();
_fixture.ClientWrapper
.GetTorrentListAsync(Arg.Any<TorrentListQuery>())
.Returns((IReadOnlyList<TorrentInfo>?)null);
BlockFilesResult result = await sut.BlockUnwantedFilesAsync(hash, Array.Empty<string>());
result.Found.ShouldBeFalse();
result.ShouldRemove.ShouldBeFalse();
}
[Fact]
public async Task TrackersNotFound_ReturnsNotFound()
{
@@ -9,7 +9,7 @@ public interface IQBittorrentClientWrapper : IDisposable
{
Task LoginAsync(string username, string password);
Task<ApiVersion> GetApiVersionAsync();
Task<IReadOnlyList<TorrentInfo>> GetTorrentListAsync(TorrentListQuery query);
Task<IReadOnlyList<TorrentInfo>?> GetTorrentListAsync(TorrentListQuery query);
Task<TorrentProperties?> GetTorrentPropertiesAsync(string hash);
Task<IReadOnlyList<TorrentContent>?> GetTorrentContentsAsync(string hash);
Task<IReadOnlyList<TorrentTracker>?> GetTorrentTrackersAsync(string hash);
@@ -15,7 +15,7 @@ public partial class QBitService
public override async Task<BlockFilesResult> BlockUnwantedFilesAsync(string hash, IReadOnlyList<string> ignoredDownloads)
{
TorrentInfo? download = (await _client.GetTorrentListAsync(new TorrentListQuery { Hashes = [hash] }))
.FirstOrDefault();
?.FirstOrDefault();
BlockFilesResult result = new();
if (download is null)
@@ -15,7 +15,7 @@ public partial class QBitService
{
DownloadCheckResult result = new();
TorrentInfo? download = (await _client.GetTorrentListAsync(new TorrentListQuery { Hashes = [hash] }))
.FirstOrDefault();
?.FirstOrDefault();
if (download is null)
{
@@ -20,7 +20,7 @@ public sealed class QBittorrentClientWrapper : IQBittorrentClientWrapper
public Task<ApiVersion> GetApiVersionAsync()
=> _client.GetApiVersionAsync();
public Task<IReadOnlyList<TorrentInfo>> GetTorrentListAsync(TorrentListQuery query)
public Task<IReadOnlyList<TorrentInfo>?> GetTorrentListAsync(TorrentListQuery query)
=> _client.GetTorrentListAsync(query);
public Task<TorrentProperties?> GetTorrentPropertiesAsync(string hash)