using System; using System.Collections.Generic; using System.Linq; using FluentAssertions; using Moq; using NUnit.Framework; using NzbDrone.Common.Disk; using NzbDrone.Common.Http; using NzbDrone.Core.Download; using NzbDrone.Core.Download.Clients; using NzbDrone.Core.Download.Clients.DownloadStation; using NzbDrone.Core.Download.Clients.DownloadStation.Proxies; using NzbDrone.Core.Organizer; using NzbDrone.Core.Parser.Model; using NzbDrone.Test.Common; namespace NzbDrone.Core.Test.Download.DownloadClientTests.DownloadStationTests { [TestFixture] public class UsenetDownloadStationFixture : DownloadClientFixtureBase { protected DownloadStationSettings _settings; protected DownloadStationTask _queued; protected DownloadStationTask _downloading; protected DownloadStationTask _failed; protected DownloadStationTask _completed; protected DownloadStationTask _seeding; protected string _serialNumber = "SERIALNUMBER"; protected string _category = "readarr"; protected string _musicDirectory = @"music/Author"; protected string _defaultDestination = "somepath"; protected OsPath _physicalPath = new OsPath("/mnt/sdb1/mydata"); protected RemoteBook _remoteBook; protected Dictionary _downloadStationConfigItems; [SetUp] public void Setup() { _remoteBook = CreateRemoteBook(); _settings = new DownloadStationSettings() { Host = "127.0.0.1", Port = 5000, Username = "admin", Password = "pass" }; Subject.Definition = new DownloadClientDefinition(); Subject.Definition.Settings = _settings; _queued = new DownloadStationTask() { Id = "id1", Size = 1000, Status = DownloadStationTaskStatus.Waiting, Type = DownloadStationTaskType.NZB.ToString(), Username = "admin", Title = "title", Additional = new DownloadStationTaskAdditional { Detail = new Dictionary { { "destination", "shared/folder" }, { "uri", FileNameBuilder.CleanFileName(_remoteBook.Release.Title) + ".nzb" } }, Transfer = new Dictionary { { "size_downloaded", "0" }, { "speed_download", "0" } } } }; _completed = new DownloadStationTask() { Id = "id2", Size = 1000, Status = DownloadStationTaskStatus.Finished, Type = DownloadStationTaskType.NZB.ToString(), Username = "admin", Title = "title", Additional = new DownloadStationTaskAdditional { Detail = new Dictionary { { "destination", "shared/folder" }, { "uri", FileNameBuilder.CleanFileName(_remoteBook.Release.Title) + ".nzb" } }, Transfer = new Dictionary { { "size_downloaded", "1000" }, { "speed_download", "0" } }, } }; _seeding = new DownloadStationTask() { Id = "id2", Size = 1000, Status = DownloadStationTaskStatus.Seeding, Type = DownloadStationTaskType.NZB.ToString(), Username = "admin", Title = "title", Additional = new DownloadStationTaskAdditional { Detail = new Dictionary { { "destination", "shared/folder" }, { "uri", FileNameBuilder.CleanFileName(_remoteBook.Release.Title) + ".nzb" } }, Transfer = new Dictionary { { "size_downloaded", "1000" }, { "speed_download", "0" } } } }; _downloading = new DownloadStationTask() { Id = "id3", Size = 1000, Status = DownloadStationTaskStatus.Downloading, Type = DownloadStationTaskType.NZB.ToString(), Username = "admin", Title = "title", Additional = new DownloadStationTaskAdditional { Detail = new Dictionary { { "destination", "shared/folder" }, { "uri", FileNameBuilder.CleanFileName(_remoteBook.Release.Title) + ".nzb" } }, Transfer = new Dictionary { { "size_downloaded", "100" }, { "speed_download", "50" } } } }; _failed = new DownloadStationTask() { Id = "id4", Size = 1000, Status = DownloadStationTaskStatus.Error, Type = DownloadStationTaskType.NZB.ToString(), Username = "admin", Title = "title", Additional = new DownloadStationTaskAdditional { Detail = new Dictionary { { "destination", "shared/folder" }, { "uri", FileNameBuilder.CleanFileName(_remoteBook.Release.Title) + ".nzb" } }, Transfer = new Dictionary { { "size_downloaded", "10" }, { "speed_download", "0" } } } }; Mocker.GetMock() .Setup(s => s.Get(It.IsAny())) .Returns(r => new HttpResponse(r, new HttpHeader(), new byte[0])); _downloadStationConfigItems = new Dictionary { { "default_destination", _defaultDestination }, }; Mocker.GetMock() .Setup(v => v.GetConfig(It.IsAny())) .Returns(_downloadStationConfigItems); } protected void GivenSharedFolder() { Mocker.GetMock() .Setup(s => s.RemapToFullPath(It.IsAny(), It.IsAny(), It.IsAny())) .Returns((path, setttings, serial) => _physicalPath); } protected void GivenSharedFolder(string share) { Mocker.GetMock() .Setup(s => s.RemapToFullPath(It.IsAny(), It.IsAny(), It.IsAny())) .Throws(new DownloadClientException("There is no matching shared folder")); Mocker.GetMock() .Setup(s => s.RemapToFullPath(It.Is(x => x.FullPath == share), It.IsAny(), It.IsAny())) .Returns((path, setttings, serial) => _physicalPath); } protected void GivenSerialNumber() { Mocker.GetMock() .Setup(s => s.GetSerialNumber(It.IsAny())) .Returns(_serialNumber); } protected void GivenMusicCategory() { _settings.MusicCategory = _category; } protected void GivenTvDirectory() { _settings.TvDirectory = _musicDirectory; } protected virtual void GivenTasks(List nzbs) { if (nzbs == null) { nzbs = new List(); } Mocker.GetMock() .Setup(s => s.GetTasks(It.IsAny())) .Returns(nzbs); } protected void PrepareClientToReturnQueuedItem() { GivenTasks(new List { _queued }); } protected void GivenSuccessfulDownload() {/* Mocker.GetMock() .Setup(s => s.Get(It.IsAny())) .Returns(r => new HttpResponse(r, new HttpHeader(), new byte[1000])); */ Mocker.GetMock() .Setup(s => s.AddTaskFromData(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny())) .Callback(PrepareClientToReturnQueuedItem); } protected void GivenAllKindOfTasks() { var tasks = new List() { _queued, _completed, _failed, _downloading, _seeding }; Mocker.GetMock() .Setup(d => d.GetTasks(_settings)) .Returns(tasks); } [Test] public void Download_with_TvDirectory_should_force_directory() { GivenSerialNumber(); GivenTvDirectory(); GivenSuccessfulDownload(); var remoteBook = CreateRemoteBook(); var id = Subject.Download(remoteBook); id.Should().NotBeNullOrEmpty(); Mocker.GetMock() .Verify(v => v.AddTaskFromData(It.IsAny(), It.IsAny(), _musicDirectory, It.IsAny()), Times.Once()); } [Test] public void Download_with_category_should_force_directory() { GivenSerialNumber(); GivenMusicCategory(); GivenSuccessfulDownload(); var remoteBook = CreateRemoteBook(); var id = Subject.Download(remoteBook); id.Should().NotBeNullOrEmpty(); Mocker.GetMock() .Verify(v => v.AddTaskFromData(It.IsAny(), It.IsAny(), $"{_defaultDestination}/{_category}", It.IsAny()), Times.Once()); } [Test] public void Download_without_TvDirectory_and_Category_should_use_default() { GivenSerialNumber(); GivenSuccessfulDownload(); var remoteBook = CreateRemoteBook(); var id = Subject.Download(remoteBook); id.Should().NotBeNullOrEmpty(); Mocker.GetMock() .Verify(v => v.AddTaskFromData(It.IsAny(), It.IsAny(), null, It.IsAny()), Times.Once()); } [Test] public void GetItems_should_return_empty_list_if_no_tasks_available() { GivenSerialNumber(); GivenSharedFolder(); GivenTasks(new List()); Subject.GetItems().Should().BeEmpty(); } [Test] public void GetItems_should_return_ignore_tasks_of_unknown_type() { GivenSerialNumber(); GivenSharedFolder(); GivenTasks(new List { _completed }); _completed.Type = "ipfs"; Subject.GetItems().Should().BeEmpty(); } [Test] public void GetItems_should_ignore_downloads_in_wrong_folder() { _settings.TvDirectory = @"/shared/folder/sub"; GivenSerialNumber(); GivenSharedFolder(); GivenTasks(new List { _completed }); Subject.GetItems().Should().BeEmpty(); } [Test] public void GetItems_should_throw_if_shared_folder_resolve_fails() { Mocker.GetMock() .Setup(s => s.RemapToFullPath(It.IsAny(), It.IsAny(), It.IsAny())) .Throws(new ApplicationException("Some unknown exception, HttpException or DownloadClientException")); GivenSerialNumber(); GivenAllKindOfTasks(); Assert.Throws(Is.InstanceOf(), () => Subject.GetItems()); ExceptionVerification.ExpectedErrors(0); } [Test] public void GetItems_should_throw_if_serial_number_unavailable() { Mocker.GetMock() .Setup(s => s.GetSerialNumber(_settings)) .Throws(new ApplicationException("Some unknown exception, HttpException or DownloadClientException")); GivenSharedFolder(); GivenAllKindOfTasks(); Assert.Throws(Is.InstanceOf(), () => Subject.GetItems()); ExceptionVerification.ExpectedErrors(0); } [Test] public void Download_should_throw_and_not_add_task_if_cannot_get_serial_number() { var remoteBook = CreateRemoteBook(); Mocker.GetMock() .Setup(s => s.GetSerialNumber(_settings)) .Throws(new ApplicationException("Some unknown exception, HttpException or DownloadClientException")); Assert.Throws(Is.InstanceOf(), () => Subject.Download(remoteBook)); Mocker.GetMock() .Verify(v => v.AddTaskFromUrl(It.IsAny(), null, _settings), Times.Never()); } [Test] public void GetStatus_should_map_outputpath_when_using_default() { GivenSerialNumber(); GivenSharedFolder("/somepath"); var status = Subject.GetStatus(); status.OutputRootFolders.First().Should().Be(_physicalPath); } [Test] public void GetStatus_should_map_outputpath_when_using_destination() { GivenSerialNumber(); GivenTvDirectory(); GivenSharedFolder($"/{_musicDirectory}"); var status = Subject.GetStatus(); status.OutputRootFolders.First().Should().Be(_physicalPath); } [Test] public void GetStatus_should_map_outputpath_when_using_category() { GivenSerialNumber(); GivenMusicCategory(); GivenSharedFolder($"/somepath/{_category}"); var status = Subject.GetStatus(); status.OutputRootFolders.First().Should().Be(_physicalPath); } [Test] public void GetItems_should_not_map_outputpath_for_queued_or_downloading_tasks() { GivenSerialNumber(); GivenSharedFolder(); GivenTasks(new List { _queued, _downloading }); var items = Subject.GetItems(); items.Should().HaveCount(2); items.Should().OnlyContain(v => v.OutputPath.IsEmpty); } [Test] public void GetItems_should_map_outputpath_for_completed_or_failed_tasks() { GivenSerialNumber(); GivenSharedFolder(); GivenTasks(new List { _completed, _failed, _seeding }); var items = Subject.GetItems(); items.Should().HaveCount(3); items.Should().OnlyContain(v => !v.OutputPath.IsEmpty); } [TestCase(DownloadStationTaskStatus.Downloading, DownloadItemStatus.Downloading)] [TestCase(DownloadStationTaskStatus.Error, DownloadItemStatus.Failed)] [TestCase(DownloadStationTaskStatus.Extracting, DownloadItemStatus.Downloading)] [TestCase(DownloadStationTaskStatus.Finished, DownloadItemStatus.Completed)] [TestCase(DownloadStationTaskStatus.Finishing, DownloadItemStatus.Downloading)] [TestCase(DownloadStationTaskStatus.HashChecking, DownloadItemStatus.Downloading)] [TestCase(DownloadStationTaskStatus.CaptchaNeeded, DownloadItemStatus.Downloading)] [TestCase(DownloadStationTaskStatus.Paused, DownloadItemStatus.Paused)] [TestCase(DownloadStationTaskStatus.Seeding, DownloadItemStatus.Completed)] [TestCase(DownloadStationTaskStatus.FilehostingWaiting, DownloadItemStatus.Queued)] [TestCase(DownloadStationTaskStatus.Waiting, DownloadItemStatus.Queued)] [TestCase(DownloadStationTaskStatus.Unknown, DownloadItemStatus.Queued)] public void GetItems_should_return_item_as_downloadItemStatus(DownloadStationTaskStatus apiStatus, DownloadItemStatus expectedItemStatus) { GivenSerialNumber(); GivenSharedFolder(); _queued.Status = apiStatus; GivenTasks(new List() { _queued }); var items = Subject.GetItems(); items.Should().HaveCount(1); items.First().Status.Should().Be(expectedItemStatus); } } }