2012-02-07 05:08:07 +00:00
|
|
|
using System;
|
2012-10-20 20:03:18 +00:00
|
|
|
using System.Net;
|
2012-02-07 05:08:07 +00:00
|
|
|
using FluentAssertions;
|
|
|
|
using Moq;
|
|
|
|
using NUnit.Framework;
|
2012-02-11 00:48:20 +00:00
|
|
|
using NzbDrone.Common;
|
2013-02-24 06:48:52 +00:00
|
|
|
using NzbDrone.Core.Configuration;
|
2013-03-05 05:33:34 +00:00
|
|
|
using NzbDrone.Core.Download.Clients.Sabnzbd;
|
2013-07-08 03:15:15 +00:00
|
|
|
using NzbDrone.Core.Parser.Model;
|
2012-02-07 05:08:07 +00:00
|
|
|
using NzbDrone.Core.Test.Framework;
|
|
|
|
using NzbDrone.Test.Common;
|
|
|
|
|
2013-04-01 06:22:16 +00:00
|
|
|
namespace NzbDrone.Core.Test.Download.DownloadClientTests.SabProviderTests
|
2012-02-07 05:08:07 +00:00
|
|
|
{
|
|
|
|
[TestFixture]
|
2013-04-28 19:46:13 +00:00
|
|
|
|
|
|
|
public class SabProviderFixture : CoreTest<SabnzbdClient>
|
2012-02-07 05:08:07 +00:00
|
|
|
{
|
2013-04-28 19:46:13 +00:00
|
|
|
private const string URL = "http://www.nzbclub.com/nzb_download.aspx?mid=1950232";
|
|
|
|
private const string TITLE = "My Series Name - 5x2-5x3 - My title [Bluray720p] [Proper]";
|
2013-07-08 03:15:15 +00:00
|
|
|
private RemoteEpisode _remoteEpisode;
|
2012-02-07 05:08:07 +00:00
|
|
|
|
|
|
|
[SetUp]
|
|
|
|
public void Setup()
|
|
|
|
{
|
2013-02-24 19:39:31 +00:00
|
|
|
var fakeConfig = Mocker.GetMock<IConfigService>();
|
2012-02-07 05:08:07 +00:00
|
|
|
|
|
|
|
fakeConfig.SetupGet(c => c.SabHost).Returns("192.168.5.55");
|
|
|
|
fakeConfig.SetupGet(c => c.SabPort).Returns(2222);
|
|
|
|
fakeConfig.SetupGet(c => c.SabApiKey).Returns("5c770e3197e4fe763423ee7c392c25d1");
|
|
|
|
fakeConfig.SetupGet(c => c.SabUsername).Returns("admin");
|
|
|
|
fakeConfig.SetupGet(c => c.SabPassword).Returns("pass");
|
|
|
|
fakeConfig.SetupGet(c => c.SabTvCategory).Returns("tv");
|
2013-07-08 03:15:15 +00:00
|
|
|
|
|
|
|
_remoteEpisode = new RemoteEpisode();
|
|
|
|
_remoteEpisode.Report = new ReportInfo();
|
|
|
|
_remoteEpisode.Report.Title = TITLE;
|
|
|
|
_remoteEpisode.Report.NzbUrl = URL;
|
2012-02-07 05:08:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private void WithFailResponse()
|
|
|
|
{
|
2013-04-10 23:41:45 +00:00
|
|
|
Mocker.GetMock<IHttpProvider>()
|
2012-10-23 06:57:01 +00:00
|
|
|
.Setup(s => s.DownloadString(It.IsAny<String>())).Returns("{ \"status\": false, \"error\": \"API Key Required\" }");
|
2012-02-07 05:08:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
public void add_url_should_format_request_properly()
|
|
|
|
{
|
2013-04-10 23:41:45 +00:00
|
|
|
Mocker.GetMock<IHttpProvider>(MockBehavior.Strict)
|
2012-10-23 06:57:01 +00:00
|
|
|
.Setup(s => s.DownloadString("http://192.168.5.55:2222/api?mode=addurl&name=http://www.nzbclub.com/nzb_download.aspx?mid=1950232&priority=0&pp=3&cat=tv&nzbname=My+Series+Name+-+5x2-5x3+-+My+title+%5bBluray720p%5d+%5bProper%5d&output=json&apikey=5c770e3197e4fe763423ee7c392c25d1&ma_username=admin&ma_password=pass"))
|
|
|
|
.Returns("{ \"status\": true }");
|
2012-02-07 05:08:07 +00:00
|
|
|
|
2013-04-28 19:46:13 +00:00
|
|
|
|
2013-07-08 03:15:15 +00:00
|
|
|
Subject.DownloadNzb(_remoteEpisode).Should().BeTrue();
|
2012-02-07 05:08:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
public void add_by_url_should_detect_and_handle_sab_errors()
|
|
|
|
{
|
|
|
|
WithFailResponse();
|
2013-07-08 03:15:15 +00:00
|
|
|
Assert.Throws<ApplicationException>(() => Subject.DownloadNzb(_remoteEpisode).Should().BeFalse());
|
2012-02-07 05:08:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
public void should_be_able_to_get_categories_when_config_is_passed_in()
|
|
|
|
{
|
2013-04-28 19:46:13 +00:00
|
|
|
|
2012-02-07 05:08:07 +00:00
|
|
|
const string host = "192.168.5.22";
|
|
|
|
const int port = 1111;
|
|
|
|
const string apikey = "5c770e3197e4fe763423ee7c392c25d2";
|
|
|
|
const string username = "admin2";
|
|
|
|
const string password = "pass2";
|
|
|
|
|
2013-04-10 23:41:45 +00:00
|
|
|
Mocker.GetMock<IHttpProvider>(MockBehavior.Strict)
|
2012-02-07 05:08:07 +00:00
|
|
|
.Setup(s => s.DownloadString("http://192.168.5.22:1111/api?mode=get_cats&output=json&apikey=5c770e3197e4fe763423ee7c392c25d2&ma_username=admin2&ma_password=pass2"))
|
2013-04-28 19:46:13 +00:00
|
|
|
.Returns(ReadAllText("Files", "Categories_json.txt"));
|
|
|
|
|
|
|
|
var result = Subject.GetCategories(host, port, apikey, username, password);
|
2012-02-07 05:08:07 +00:00
|
|
|
|
|
|
|
|
|
|
|
result.Should().NotBeNull();
|
|
|
|
result.categories.Should().NotBeEmpty();
|
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
public void should_be_able_to_get_categories_using_config()
|
|
|
|
{
|
2013-04-10 23:41:45 +00:00
|
|
|
Mocker.GetMock<IHttpProvider>(MockBehavior.Strict)
|
2012-02-07 05:08:07 +00:00
|
|
|
.Setup(s => s.DownloadString("http://192.168.5.55:2222/api?mode=get_cats&output=json&apikey=5c770e3197e4fe763423ee7c392c25d1&ma_username=admin&ma_password=pass"))
|
2013-04-28 19:46:13 +00:00
|
|
|
.Returns(ReadAllText("Files", "Categories_json.txt"));
|
|
|
|
|
|
|
|
|
|
|
|
var result = Subject.GetCategories();
|
2012-02-07 05:08:07 +00:00
|
|
|
|
|
|
|
|
|
|
|
result.Should().NotBeNull();
|
|
|
|
result.categories.Should().NotBeEmpty();
|
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
public void GetHistory_should_return_a_list_with_items_when_the_history_has_items()
|
|
|
|
{
|
2013-04-10 23:41:45 +00:00
|
|
|
Mocker.GetMock<IHttpProvider>()
|
2012-02-07 05:08:07 +00:00
|
|
|
.Setup(s => s.DownloadString("http://192.168.5.55:2222/api?mode=history&output=json&start=0&limit=0&apikey=5c770e3197e4fe763423ee7c392c25d1&ma_username=admin&ma_password=pass"))
|
2013-03-28 19:05:43 +00:00
|
|
|
.Returns(ReadAllText("Files", "History.txt"));
|
2012-02-07 05:08:07 +00:00
|
|
|
|
|
|
|
|
2013-04-28 19:46:13 +00:00
|
|
|
var result = Subject.GetHistory();
|
|
|
|
|
|
|
|
|
2012-02-07 05:08:07 +00:00
|
|
|
result.Should().HaveCount(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
public void GetHistory_should_return_an_empty_list_when_the_queue_is_empty()
|
|
|
|
{
|
2013-04-10 23:41:45 +00:00
|
|
|
Mocker.GetMock<IHttpProvider>()
|
2012-02-07 05:08:07 +00:00
|
|
|
.Setup(s => s.DownloadString("http://192.168.5.55:2222/api?mode=history&output=json&start=0&limit=0&apikey=5c770e3197e4fe763423ee7c392c25d1&ma_username=admin&ma_password=pass"))
|
2013-04-28 19:46:13 +00:00
|
|
|
.Returns(ReadAllText("Files", "HistoryEmpty.txt"));
|
|
|
|
|
|
|
|
|
|
|
|
var result = Subject.GetHistory();
|
2012-02-07 05:08:07 +00:00
|
|
|
|
|
|
|
|
|
|
|
result.Should().BeEmpty();
|
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
public void GetHistory_should_return_an_empty_list_when_there_is_an_error_getting_the_queue()
|
|
|
|
{
|
2013-04-10 23:41:45 +00:00
|
|
|
Mocker.GetMock<IHttpProvider>()
|
2012-02-07 05:08:07 +00:00
|
|
|
.Setup(s => s.DownloadString("http://192.168.5.55:2222/api?mode=history&output=json&start=0&limit=0&apikey=5c770e3197e4fe763423ee7c392c25d1&ma_username=admin&ma_password=pass"))
|
2013-04-28 19:46:13 +00:00
|
|
|
.Returns(ReadAllText("Files", "JsonError.txt"));
|
|
|
|
|
2012-02-07 05:08:07 +00:00
|
|
|
|
2013-04-28 19:46:13 +00:00
|
|
|
Assert.Throws<ApplicationException>(() => Subject.GetHistory(), "API Key Incorrect");
|
2012-02-07 05:08:07 +00:00
|
|
|
}
|
|
|
|
|
2012-03-18 04:14:32 +00:00
|
|
|
[Test]
|
|
|
|
public void GetVersion_should_return_the_version_using_passed_in_values()
|
|
|
|
{
|
|
|
|
var response = "{ \"version\": \"0.6.9\" }";
|
|
|
|
|
2013-04-10 23:41:45 +00:00
|
|
|
Mocker.GetMock<IHttpProvider>()
|
2012-03-18 04:14:32 +00:00
|
|
|
.Setup(s => s.DownloadString("http://192.168.5.55:2222/api?mode=version&output=json&apikey=5c770e3197e4fe763423ee7c392c25d1&ma_username=admin&ma_password=pass"))
|
|
|
|
.Returns(response);
|
|
|
|
|
|
|
|
|
2013-04-28 19:46:13 +00:00
|
|
|
var result = Subject.GetVersion("192.168.5.55", 2222, "5c770e3197e4fe763423ee7c392c25d1", "admin", "pass");
|
|
|
|
|
|
|
|
|
2012-03-18 04:14:32 +00:00
|
|
|
result.Should().NotBeNull();
|
|
|
|
result.Version.Should().Be("0.6.9");
|
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
public void GetVersion_should_return_the_version_using_saved_values()
|
|
|
|
{
|
|
|
|
var response = "{ \"version\": \"0.6.9\" }";
|
|
|
|
|
2013-04-10 23:41:45 +00:00
|
|
|
Mocker.GetMock<IHttpProvider>()
|
2012-03-18 04:14:32 +00:00
|
|
|
.Setup(s => s.DownloadString("http://192.168.5.55:2222/api?mode=version&output=json&apikey=5c770e3197e4fe763423ee7c392c25d1&ma_username=admin&ma_password=pass"))
|
|
|
|
.Returns(response);
|
|
|
|
|
|
|
|
|
2013-04-28 19:46:13 +00:00
|
|
|
var result = Subject.GetVersion();
|
|
|
|
|
|
|
|
|
2012-03-18 04:14:32 +00:00
|
|
|
result.Should().NotBeNull();
|
|
|
|
result.Version.Should().Be("0.6.9");
|
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
public void Test_should_return_version_as_a_string()
|
|
|
|
{
|
2013-04-28 19:46:13 +00:00
|
|
|
const string response = "{ \"version\": \"0.6.9\" }";
|
2012-03-18 04:14:32 +00:00
|
|
|
|
2013-04-10 23:41:45 +00:00
|
|
|
Mocker.GetMock<IHttpProvider>()
|
2012-03-18 04:14:32 +00:00
|
|
|
.Setup(s => s.DownloadString("http://192.168.5.55:2222/api?mode=version&output=json&apikey=5c770e3197e4fe763423ee7c392c25d1&ma_username=admin&ma_password=pass"))
|
|
|
|
.Returns(response);
|
|
|
|
|
|
|
|
|
2013-04-28 19:46:13 +00:00
|
|
|
var result = Subject.Test("192.168.5.55", 2222, "5c770e3197e4fe763423ee7c392c25d1", "admin", "pass");
|
|
|
|
|
|
|
|
|
2012-03-18 04:14:32 +00:00
|
|
|
result.Should().Be("0.6.9");
|
|
|
|
}
|
2012-10-20 20:03:18 +00:00
|
|
|
|
|
|
|
[Test]
|
|
|
|
public void should_return_false_when_WebException_is_thrown()
|
|
|
|
{
|
2013-04-10 23:41:45 +00:00
|
|
|
Mocker.GetMock<IHttpProvider>()
|
2012-10-20 20:03:18 +00:00
|
|
|
.Setup(s => s.DownloadString(It.IsAny<String>())).Throws(new WebException());
|
|
|
|
|
2013-07-08 03:15:15 +00:00
|
|
|
Subject.DownloadNzb(_remoteEpisode).Should().BeFalse();
|
2012-10-20 20:03:18 +00:00
|
|
|
ExceptionVerification.ExpectedErrors(1);
|
|
|
|
}
|
2012-11-23 02:56:27 +00:00
|
|
|
|
|
|
|
[Test]
|
|
|
|
public void downloadNzb_should_use_sabRecentTvPriority_when_recentEpisode_is_true()
|
|
|
|
{
|
2013-02-24 19:39:31 +00:00
|
|
|
Mocker.GetMock<IConfigService>()
|
2012-11-23 02:56:27 +00:00
|
|
|
.SetupGet(s => s.SabRecentTvPriority)
|
|
|
|
.Returns(SabPriorityType.High);
|
|
|
|
|
|
|
|
|
2013-04-10 23:41:45 +00:00
|
|
|
Mocker.GetMock<IHttpProvider>()
|
2012-11-23 02:56:27 +00:00
|
|
|
.Setup(s => s.DownloadString("http://192.168.5.55:2222/api?mode=addurl&name=http://www.nzbclub.com/nzb_download.aspx?mid=1950232&priority=1&pp=3&cat=tv&nzbname=My+Series+Name+-+5x2-5x3+-+My+title+%5bBluray720p%5d+%5bProper%5d&output=json&apikey=5c770e3197e4fe763423ee7c392c25d1&ma_username=admin&ma_password=pass"))
|
|
|
|
.Returns("{ \"status\": true }");
|
|
|
|
|
2013-04-28 19:46:13 +00:00
|
|
|
|
2013-07-08 03:15:15 +00:00
|
|
|
Subject.DownloadNzb(_remoteEpisode).Should().BeTrue();
|
2012-11-23 02:56:27 +00:00
|
|
|
|
2013-04-10 23:41:45 +00:00
|
|
|
Mocker.GetMock<IHttpProvider>()
|
2012-11-23 02:56:27 +00:00
|
|
|
.Verify(v => v.DownloadString("http://192.168.5.55:2222/api?mode=addurl&name=http://www.nzbclub.com/nzb_download.aspx?mid=1950232&priority=1&pp=3&cat=tv&nzbname=My+Series+Name+-+5x2-5x3+-+My+title+%5bBluray720p%5d+%5bProper%5d&output=json&apikey=5c770e3197e4fe763423ee7c392c25d1&ma_username=admin&ma_password=pass"), Times.Once());
|
|
|
|
}
|
2012-02-07 05:08:07 +00:00
|
|
|
}
|
2013-07-08 03:15:15 +00:00
|
|
|
}
|