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.DownloadStation; using NzbDrone.Core.Download.Clients.DownloadStation.Proxies; using NzbDrone.Core.MediaFiles.TorrentInfo; using NzbDrone.Core.Parser.Model; using NzbDrone.Test.Common; using NzbDrone.Core.Organizer; 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 = "sonarr"; protected string _tvDirectory = @"video/Series"; protected string _defaultDestination = "somepath"; protected OsPath _physicalPath = new OsPath("/mnt/sdb1/mydata"); protected RemoteEpisode _remoteEpisode; protected Dictionary _downloadStationConfigItems; [SetUp] public void Setup() { _remoteEpisode = CreateRemoteEpisode(); _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, Username = "admin", Title = "title", Additional = new DownloadStationTaskAdditional { Detail = new Dictionary { { "destination","shared/folder" }, { "uri", FileNameBuilder.CleanFileName(_remoteEpisode.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, Username = "admin", Title = "title", Additional = new DownloadStationTaskAdditional { Detail = new Dictionary { { "destination","shared/folder" }, { "uri", FileNameBuilder.CleanFileName(_remoteEpisode.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, Username = "admin", Title = "title", Additional = new DownloadStationTaskAdditional { Detail = new Dictionary { { "destination","shared/folder" }, { "uri", FileNameBuilder.CleanFileName(_remoteEpisode.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, Username = "admin", Title = "title", Additional = new DownloadStationTaskAdditional { Detail = new Dictionary { { "destination","shared/folder" }, { "uri", FileNameBuilder.CleanFileName(_remoteEpisode.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, Username = "admin", Title = "title", Additional = new DownloadStationTaskAdditional { Detail = new Dictionary { { "destination","shared/folder" }, { "uri", FileNameBuilder.CleanFileName(_remoteEpisode.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 GivenSerialNumber() { Mocker.GetMock() .Setup(s => s.GetSerialNumber(It.IsAny())) .Returns(_serialNumber); } protected void GivenTvCategory() { _settings.TvCategory = _category; } protected void GivenTvDirectory() { _settings.TvDirectory = _tvDirectory; } protected virtual void GivenNZBs(List nzbs) { if (nzbs == null) { nzbs = new List(); } Mocker.GetMock() .Setup(s => s.GetTasks(DownloadStationTaskType.NZB, It.IsAny())) .Returns(nzbs); } protected void PrepareClientToReturnQueuedItem() { GivenNZBs(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(DownloadStationTaskType.NZB, _settings)) .Returns(tasks); } [Test] public void Download_with_TvDirectory_should_force_directory() { GivenSerialNumber(); GivenTvDirectory(); GivenSuccessfulDownload(); var remoteEpisode = CreateRemoteEpisode(); var id = Subject.Download(remoteEpisode); id.Should().NotBeNullOrEmpty(); Mocker.GetMock() .Verify(v => v.AddTaskFromData(It.IsAny(), It.IsAny(), _tvDirectory, It.IsAny()), Times.Once()); } [Test] public void Download_with_category_should_force_directory() { GivenSerialNumber(); GivenTvCategory(); GivenSuccessfulDownload(); var remoteEpisode = CreateRemoteEpisode(); var id = Subject.Download(remoteEpisode); 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 remoteEpisode = CreateRemoteEpisode(); var id = Subject.Download(remoteEpisode); 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() { _settings.TvDirectory = @"/shared/folder/sub"; GivenSerialNumber(); GivenSharedFolder(); GivenNZBs(new List()); Subject.GetItems().Should().BeEmpty(); } [Test] public void GetItems_should_ignore_downloads_in_wrong_folder() { _settings.TvDirectory = @"/shared/folder/sub"; GivenSerialNumber(); GivenSharedFolder(); GivenNZBs(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_tasks_if_cannot_get_serial_number() { var remoteEpisode = CreateRemoteEpisode(); Mocker.GetMock() .Setup(s => s.GetSerialNumber(_settings)) .Throws(new ApplicationException("Some unknown exception, HttpException or DownloadClientException")); Assert.Throws(Is.InstanceOf(), () => Subject.Download(remoteEpisode)); Mocker.GetMock() .Verify(v => v.AddTaskFromUrl(It.IsAny(), null, _settings), Times.Never()); } [Test] public void GetItems_should_not_map_outputpath_for_queued_or_downloading_tasks() { GivenSerialNumber(); GivenSharedFolder(); GivenNZBs(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(); GivenNZBs(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, true)] [TestCase(DownloadStationTaskStatus.Finished, DownloadItemStatus.Completed, false)] [TestCase(DownloadStationTaskStatus.Waiting, DownloadItemStatus.Queued, true)] public void GetItems_should_return_readonly_expected(DownloadStationTaskStatus apiStatus, DownloadItemStatus expectedItemStatus, bool readOnlyExpected) { GivenSerialNumber(); GivenSharedFolder(); _queued.Status = apiStatus; GivenNZBs(new List() { _queued }); var items = Subject.GetItems(); items.Should().HaveCount(1); items.First().IsReadOnly.Should().Be(readOnlyExpected); } [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.Paused, DownloadItemStatus.Paused)] [TestCase(DownloadStationTaskStatus.Waiting, DownloadItemStatus.Queued)] public void GetItems_should_return_item_as_downloadItemStatus(DownloadStationTaskStatus apiStatus, DownloadItemStatus expectedItemStatus) { GivenSerialNumber(); GivenSharedFolder(); _queued.Status = apiStatus; GivenNZBs(new List() { _queued }); var items = Subject.GetItems(); items.Should().HaveCount(1); items.First().Status.Should().Be(expectedItemStatus); } } }