Add DSM version test and fixed incomplete test in api versions

This commit is contained in:
margaale 2017-08-15 17:12:27 -03:00 committed by Taloth Saldono
parent e042388a97
commit 4e74fe2eec
8 changed files with 99 additions and 17 deletions

View File

@ -7,6 +7,7 @@ using NzbDrone.Core.Download.Clients.DownloadStation;
using NzbDrone.Core.Download.Clients.DownloadStation.Proxies; using NzbDrone.Core.Download.Clients.DownloadStation.Proxies;
using NzbDrone.Core.Test.Framework; using NzbDrone.Core.Test.Framework;
using NzbDrone.Test.Common; using NzbDrone.Test.Common;
using NzbDrone.Core.Download.Clients.DownloadStation.Responses;
namespace NzbDrone.Core.Test.Download.DownloadClientTests.DownloadStationTests namespace NzbDrone.Core.Test.Download.DownloadClientTests.DownloadStationTests
{ {
@ -24,14 +25,14 @@ namespace NzbDrone.Core.Test.Download.DownloadClientTests.DownloadStationTests
private void GivenValidResponse() private void GivenValidResponse()
{ {
Mocker.GetMock<IDSMInfoProxy>() Mocker.GetMock<IDSMInfoProxy>()
.Setup(d => d.GetSerialNumber(It.IsAny<DownloadStationSettings>())) .Setup(d => d.GetInfo(It.IsAny<DownloadStationSettings>()))
.Returns("serial"); .Returns(new DSMInfoResponse() { SerialNumber = "serial", Version = "DSM 6.0.1" });
} }
private void GivenInvalidResponse() private void GivenInvalidResponse()
{ {
Mocker.GetMock<IDSMInfoProxy>() Mocker.GetMock<IDSMInfoProxy>()
.Setup(d => d.GetSerialNumber(It.IsAny<DownloadStationSettings>())) .Setup(d => d.GetInfo(It.IsAny<DownloadStationSettings>()))
.Throws(new DownloadClientException("Serial response invalid")); .Throws(new DownloadClientException("Serial response invalid"));
} }
@ -46,7 +47,7 @@ namespace NzbDrone.Core.Test.Download.DownloadClientTests.DownloadStationTests
serial.Should().Be("50DE66B735D30738618568294742FCF1DFA52A47"); serial.Should().Be("50DE66B735D30738618568294742FCF1DFA52A47");
Mocker.GetMock<IDSMInfoProxy>() Mocker.GetMock<IDSMInfoProxy>()
.Verify(d => d.GetSerialNumber(It.IsAny<DownloadStationSettings>()), Times.Once()); .Verify(d => d.GetInfo(It.IsAny<DownloadStationSettings>()), Times.Once());
} }
[Test] [Test]
@ -60,7 +61,7 @@ namespace NzbDrone.Core.Test.Download.DownloadClientTests.DownloadStationTests
serial2.Should().Be(serial1); serial2.Should().Be(serial1);
Mocker.GetMock<IDSMInfoProxy>() Mocker.GetMock<IDSMInfoProxy>()
.Verify(d => d.GetSerialNumber(It.IsAny<DownloadStationSettings>()), Times.Once()); .Verify(d => d.GetInfo(It.IsAny<DownloadStationSettings>()), Times.Once());
} }
[Test] [Test]

View File

@ -12,6 +12,7 @@ using NzbDrone.Core.Download.Clients.DownloadStation.Proxies;
using NzbDrone.Core.MediaFiles.TorrentInfo; using NzbDrone.Core.MediaFiles.TorrentInfo;
using NzbDrone.Core.Parser.Model; using NzbDrone.Core.Parser.Model;
using NzbDrone.Test.Common; using NzbDrone.Test.Common;
using NzbDrone.Core.Download.Clients.DownloadStation.Responses;
namespace NzbDrone.Core.Test.Download.DownloadClientTests.DownloadStationTests namespace NzbDrone.Core.Test.Download.DownloadClientTests.DownloadStationTests
{ {
@ -280,6 +281,21 @@ namespace NzbDrone.Core.Test.Download.DownloadClientTests.DownloadStationTests
.Returns(_downloadStationConfigItems); .Returns(_downloadStationConfigItems);
} }
protected void GivenApiVersions()
{
Mocker.GetMock<IDownloadStationTaskProxy>()
.Setup(s => s.GetApiInfo(It.IsAny<DownloadStationSettings>()))
.Returns(new DiskStationApiInfo() { Name = "Task", MinVersion = 1, MaxVersion = 2 });
Mocker.GetMock<IDownloadStationInfoProxy>()
.Setup(s => s.GetApiInfo(It.IsAny<DownloadStationSettings>()))
.Returns(new DiskStationApiInfo() { Name = "Info", MinVersion = 1, MaxVersion = 3 });
Mocker.GetMock<IFileStationProxy>()
.Setup(s => s.GetApiInfo(It.IsAny<DownloadStationSettings>()))
.Returns(new DiskStationApiInfo() { Name = "File", MinVersion = 1, MaxVersion = 2 });
}
protected void GivenSharedFolder() protected void GivenSharedFolder()
{ {
Mocker.GetMock<ISharedFolderResolver>() Mocker.GetMock<ISharedFolderResolver>()
@ -359,6 +375,26 @@ namespace NzbDrone.Core.Test.Download.DownloadClientTests.DownloadStationTests
return tasks.Count; return tasks.Count;
} }
protected void GivenDSMVersion(string version)
{
Mocker.GetMock<IDSMInfoProxy>()
.Setup(d => d.GetInfo(It.IsAny<DownloadStationSettings>()))
.Returns(new DSMInfoResponse() { Version = version });
}
[TestCase("DSM 6.0.0", 0)]
[TestCase("DSM 5.0.0", 1)]
public void TestConnection_should_return_validation_failure_as_expected(string version, int count)
{
GivenApiVersions();
GivenDSMVersion(version);
var result = Subject.Test();
result.Errors.Should().HaveCount(count);
}
[Test] [Test]
public void Download_with_TvDirectory_should_force_directory() public void Download_with_TvDirectory_should_force_directory()
{ {

View File

@ -7,9 +7,9 @@ using NzbDrone.Core.Download.Clients.DownloadStation.Responses;
namespace NzbDrone.Core.Download.Clients.DownloadStation.Proxies namespace NzbDrone.Core.Download.Clients.DownloadStation.Proxies
{ {
public interface IDSMInfoProxy public interface IDSMInfoProxy : IDiskStationProxy
{ {
string GetSerialNumber(DownloadStationSettings settings); DSMInfoResponse GetInfo(DownloadStationSettings settings);
} }
public class DSMInfoProxy : DiskStationProxyBase, IDSMInfoProxy public class DSMInfoProxy : DiskStationProxyBase, IDSMInfoProxy
@ -19,15 +19,15 @@ namespace NzbDrone.Core.Download.Clients.DownloadStation.Proxies
{ {
} }
public string GetSerialNumber(DownloadStationSettings settings) public DSMInfoResponse GetInfo(DownloadStationSettings settings)
{ {
var info = GetApiInfo(settings); var info = GetApiInfo(settings);
var requestBuilder = BuildRequest(settings, "getinfo", info.MinVersion); var requestBuilder = BuildRequest(settings, "getinfo", info.MinVersion);
var response = ProcessRequest<DSMInfoResponse>(requestBuilder, "get serial number", settings); var response = ProcessRequest<DSMInfoResponse>(requestBuilder, "get info", settings);
return response.Data.SerialNumber; return response.Data;
} }
} }
} }

View File

@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace NzbDrone.Core.Download.Clients.DownloadStation.Proxies
{
public class ExpectedVersion
{
public int Version { get; set; }
public IDiskStationProxy Proxy { get; set; }
}
}

View File

@ -6,5 +6,8 @@ namespace NzbDrone.Core.Download.Clients.DownloadStation.Responses
{ {
[JsonProperty("serial")] [JsonProperty("serial")]
public string SerialNumber { get; set; } public string SerialNumber { get; set; }
[JsonProperty("version_string")]
public string Version { get; set; }
} }
} }

View File

@ -42,7 +42,7 @@ namespace NzbDrone.Core.Download.Clients.DownloadStation
private string GetHashedSerialNumber(DownloadStationSettings settings) private string GetHashedSerialNumber(DownloadStationSettings settings)
{ {
var serialNumber = _proxy.GetSerialNumber(settings); var serialNumber = _proxy.GetInfo(settings).SerialNumber;
return HashConverter.GetHash(serialNumber).ToHexString(); return HashConverter.GetHash(serialNumber).ToHexString();
} }
} }

View File

@ -14,6 +14,7 @@ using NzbDrone.Core.MediaFiles.TorrentInfo;
using NzbDrone.Core.Parser.Model; using NzbDrone.Core.Parser.Model;
using NzbDrone.Core.RemotePathMappings; using NzbDrone.Core.RemotePathMappings;
using NzbDrone.Core.Validation; using NzbDrone.Core.Validation;
using System.Text.RegularExpressions;
namespace NzbDrone.Core.Download.Clients.DownloadStation namespace NzbDrone.Core.Download.Clients.DownloadStation
{ {
@ -24,12 +25,14 @@ namespace NzbDrone.Core.Download.Clients.DownloadStation
protected readonly ISharedFolderResolver _sharedFolderResolver; protected readonly ISharedFolderResolver _sharedFolderResolver;
protected readonly ISerialNumberProvider _serialNumberProvider; protected readonly ISerialNumberProvider _serialNumberProvider;
protected readonly IFileStationProxy _fileStationProxy; protected readonly IFileStationProxy _fileStationProxy;
protected readonly IDSMInfoProxy _dsmInfoProxy;
public TorrentDownloadStation(ISharedFolderResolver sharedFolderResolver, public TorrentDownloadStation(ISharedFolderResolver sharedFolderResolver,
ISerialNumberProvider serialNumberProvider, ISerialNumberProvider serialNumberProvider,
IFileStationProxy fileStationProxy, IFileStationProxy fileStationProxy,
IDownloadStationInfoProxy dsInfoProxy, IDownloadStationInfoProxy dsInfoProxy,
IDownloadStationTaskProxy dsTaskProxy, IDownloadStationTaskProxy dsTaskProxy,
IDSMInfoProxy dsmInfoProxy,
ITorrentFileInfoReader torrentFileInfoReader, ITorrentFileInfoReader torrentFileInfoReader,
IHttpClient httpClient, IHttpClient httpClient,
IConfigService configService, IConfigService configService,
@ -43,6 +46,7 @@ namespace NzbDrone.Core.Download.Clients.DownloadStation
_fileStationProxy = fileStationProxy; _fileStationProxy = fileStationProxy;
_sharedFolderResolver = sharedFolderResolver; _sharedFolderResolver = sharedFolderResolver;
_serialNumberProvider = serialNumberProvider; _serialNumberProvider = serialNumberProvider;
_dsmInfoProxy = dsmInfoProxy;
} }
public override string Name => "Download Station"; public override string Name => "Download Station";
@ -191,6 +195,7 @@ namespace NzbDrone.Core.Download.Clients.DownloadStation
{ {
failures.AddIfNotNull(TestConnection()); failures.AddIfNotNull(TestConnection());
if (failures.Any()) return; if (failures.Any()) return;
failures.AddIfNotNull(TestDSMVersion());
failures.AddIfNotNull(TestOutputPath()); failures.AddIfNotNull(TestOutputPath());
failures.AddIfNotNull(TestGetTorrents()); failures.AddIfNotNull(TestGetTorrents());
} }
@ -334,7 +339,7 @@ namespace NzbDrone.Core.Download.Clients.DownloadStation
{ {
try try
{ {
return ValidateVersion(); return ValidateProxiesVersion();
} }
catch (DownloadClientAuthenticationException ex) catch (DownloadClientAuthenticationException ex)
{ {
@ -364,15 +369,38 @@ namespace NzbDrone.Core.Download.Clients.DownloadStation
} }
} }
protected ValidationFailure ValidateVersion() protected ValidationFailure TestDSMVersion()
{ {
var info = _dsTaskProxy.GetApiInfo(Settings); var info = _dsmInfoProxy.GetInfo(Settings);
_logger.Debug("Download Station api version information: Min {0} - Max {1}", info.MinVersion, info.MaxVersion); Regex regex = new Regex(@"(\bDSM\b (?<version>[\d.-]*)){1}");
if (info.MinVersion > 2 || info.MaxVersion < 2) var dsmVersion = regex.Match(info.Version).Groups["version"].Value;
var version = new Version(dsmVersion);
return version < new Version(6, 0, 0) ? new ValidationFailure(string.Empty, $"DSM Version {version} not supported. It should be 6 or above.") : null;
}
protected ValidationFailure ValidateProxiesVersion()
{ {
return new ValidationFailure(string.Empty, $"Download Station API version not supported, should be at least 2. It supports from {info.MinVersion} to {info.MaxVersion}"); var expectedVersions = new List<ExpectedVersion>()
{
new ExpectedVersion { Version = 2, Proxy = _dsTaskProxy },
new ExpectedVersion { Version = 2, Proxy = _fileStationProxy },
new ExpectedVersion { Version = 1, Proxy = _dsInfoProxy }
};
foreach (var expectedVersion in expectedVersions)
{
DiskStationApiInfo apiInfo = expectedVersion.Proxy.GetApiInfo(Settings);
_logger.Debug("{1} api version information: Min {1} - Max {2} - Expected {3}", apiInfo.Name, apiInfo.MinVersion, apiInfo.MaxVersion, expectedVersion.Version);
if (apiInfo.MinVersion > expectedVersion.Version || apiInfo.MaxVersion < expectedVersion.Version)
{
return new ValidationFailure(string.Empty, $"{apiInfo.Name} API version not supported, should be at least {expectedVersion.Version}. It supports from {apiInfo.MinVersion} to {apiInfo.MaxVersion}");
}
} }
return null; return null;

View File

@ -372,6 +372,7 @@
<Compile Include="Download\Clients\DownloadClientUnavailableException.cs" /> <Compile Include="Download\Clients\DownloadClientUnavailableException.cs" />
<Compile Include="Download\Clients\DownloadClientException.cs" /> <Compile Include="Download\Clients\DownloadClientException.cs" />
<Compile Include="Download\Clients\DownloadStation\Proxies\DownloadStationInfoProxy.cs" /> <Compile Include="Download\Clients\DownloadStation\Proxies\DownloadStationInfoProxy.cs" />
<Compile Include="Download\Clients\DownloadStation\Proxies\ExpectedVersion.cs" />
<Compile Include="Download\Clients\DownloadStation\TorrentDownloadStation.cs" /> <Compile Include="Download\Clients\DownloadStation\TorrentDownloadStation.cs" />
<Compile Include="Download\Clients\DownloadStation\Proxies\DownloadStationTaskProxy.cs" /> <Compile Include="Download\Clients\DownloadStation\Proxies\DownloadStationTaskProxy.cs" />
<Compile Include="Download\Clients\DownloadStation\DownloadStationSettings.cs" /> <Compile Include="Download\Clients\DownloadStation\DownloadStationSettings.cs" />