1
0
mirror of https://github.com/Sonarr/Sonarr.git synced 2024-10-30 07:22:35 +01:00

IHttpController/HttpController Added (So we can Mock SABnzbd requests)

More unit tests for SabController
This commit is contained in:
markus101 2010-09-27 17:49:34 -07:00
parent 50f97e824e
commit a2967f4658
9 changed files with 407 additions and 23 deletions

View File

@ -0,0 +1,115 @@
<?xml version="1.0" encoding="UTF-8" ?>
<queue><active_lang>us-en</active_lang>
<session>5c770e3197e4fe763423ee7c392c25d1</session>
<slots><slot><status>Queued</status>
<index>0</index>
<eta>unknown</eta>
<timeleft>0:00:00</timeleft>
<avg_age>11h</avg_age>
<script>None</script>
<msgid></msgid>
<verbosity></verbosity>
<mb>770.96</mb>
<sizeleft>770.96 MB</sizeleft>
<filename>Ubuntu Test</filename>
<priority>Normal</priority>
<cat>None</cat>
<mbleft>770.96</mbleft>
<percentage>0</percentage>
<nzo_id>SABnzbd_nzo_xyr5ak</nzo_id>
<unpackopts>3</unpackopts>
<size>770.96 MB</size>
</slot>
</slots>
<speed>0 </speed>
<size>770.96 MB</size>
<limit>0</limit>
<start>0</start>
<diskspacetotal2>259.45</diskspacetotal2>
<darwin>False</darwin>
<last_warning></last_warning>
<have_warnings>0</have_warnings>
<noofslots>1</noofslots>
<pause_int>0</pause_int>
<categories><category>None</category>
<category>anime</category>
<category>apps</category>
<category>books</category>
<category>consoles</category>
<category>ds-games</category>
<category>emulation</category>
<category>games</category>
<category>misc</category>
<category>movies</category>
<category>music</category>
<category>pda</category>
<category>resources</category>
<category>test</category>
<category>tv</category>
<category>tv-dvd</category>
<category>unknown</category>
<category>wii-games</category>
<category>xbox-dlc</category>
<category>xbox-xbla</category>
</categories>
<diskspacetotal1>259.45</diskspacetotal1>
<mb>770.96</mb>
<loadavg></loadavg>
<cache_max>157286400</cache_max>
<speedlimit></speedlimit>
<webdir>C:\Program Files\SABnzbd\interfaces\Plush\templates</webdir>
<paused>True</paused>
<isverbose>False</isverbose>
<restart_req>False</restart_req>
<power_options>True</power_options>
<helpuri>http://wiki.sabnzbd.org/</helpuri>
<uptime>4d</uptime>
<refresh_rate></refresh_rate>
<version>0.6.x</version>
<color_scheme></color_scheme>
<new_release></new_release>
<nt>True</nt>
<status>Paused</status>
<finish>0</finish>
<cache_art>0</cache_art>
<paused_all>False</paused_all>
<finishaction></finishaction>
<sizeleft>770.96 MB</sizeleft>
<cache_size>0B</cache_size>
<mbleft>770.96</mbleft>
<diskspace2>1177.64</diskspace2>
<diskspace1>1177.64</diskspace1>
<scripts><script>None</script>
<script>Alias.config</script>
<script>ConsoleCategories.config</script>
<script>exiso.exe</script>
<script>Movies.exe</script>
<script>Movies.exe.config</script>
<script>Movies.pdb</script>
<script>PreQueue.exe</script>
<script>PreQueue.exe.config</script>
<script>PreQueue.pdb</script>
<script>SABTest.exe</script>
<script>TVMove.exe</script>
<script>TVMove.exe.config</script>
<script>TVMove_0.4.1.exe</script>
<script>XBox360.exe</script>
<script>XBox360.exe.config</script>
</scripts>
<timeleft>0:00:00</timeleft>
<nzb_quota></nzb_quota>
<eta>unknown</eta>
<kbpersec>0.00</kbpersec>
<new_rel_url></new_rel_url>
<queue_details>0</queue_details>
</queue>

View File

@ -0,0 +1,94 @@
<?xml version="1.0" encoding="UTF-8" ?>
<queue><active_lang>us-en</active_lang>
<session>5c770e3197e4fe763423ee7c392c25d1</session>
<slots></slots>
<speed>0 </speed>
<size>0B</size>
<limit>0</limit>
<start>0</start>
<diskspacetotal2>259.45</diskspacetotal2>
<darwin>False</darwin>
<last_warning></last_warning>
<have_warnings>0</have_warnings>
<noofslots>0</noofslots>
<pause_int>0</pause_int>
<categories><category>None</category>
<category>anime</category>
<category>apps</category>
<category>books</category>
<category>consoles</category>
<category>ds-games</category>
<category>emulation</category>
<category>games</category>
<category>misc</category>
<category>movies</category>
<category>music</category>
<category>pda</category>
<category>resources</category>
<category>test</category>
<category>tv</category>
<category>tv-dvd</category>
<category>unknown</category>
<category>wii-games</category>
<category>xbox-dlc</category>
<category>xbox-xbla</category>
</categories>
<diskspacetotal1>259.45</diskspacetotal1>
<mb>0.00</mb>
<loadavg></loadavg>
<cache_max>157286400</cache_max>
<speedlimit></speedlimit>
<webdir>C:\Program Files\SABnzbd\interfaces\Plush\templates</webdir>
<paused>False</paused>
<isverbose>False</isverbose>
<restart_req>False</restart_req>
<power_options>True</power_options>
<helpuri>http://wiki.sabnzbd.org/</helpuri>
<uptime>4d</uptime>
<refresh_rate></refresh_rate>
<version>0.6.x</version>
<color_scheme></color_scheme>
<new_release></new_release>
<nt>True</nt>
<status>Idle</status>
<finish>0</finish>
<cache_art>0</cache_art>
<paused_all>False</paused_all>
<finishaction></finishaction>
<sizeleft>0B</sizeleft>
<cache_size>0B</cache_size>
<mbleft>0.00</mbleft>
<diskspace2>1177.64</diskspace2>
<diskspace1>1177.64</diskspace1>
<scripts><script>None</script>
<script>Alias.config</script>
<script>ConsoleCategories.config</script>
<script>exiso.exe</script>
<script>Movies.exe</script>
<script>Movies.exe.config</script>
<script>Movies.pdb</script>
<script>PreQueue.exe</script>
<script>PreQueue.exe.config</script>
<script>PreQueue.pdb</script>
<script>SABTest.exe</script>
<script>TVMove.exe</script>
<script>TVMove.exe.config</script>
<script>TVMove_0.4.1.exe</script>
<script>XBox360.exe</script>
<script>XBox360.exe.config</script>
</scripts>
<timeleft>0:00:00</timeleft>
<nzb_quota></nzb_quota>
<eta>unknown</eta>
<kbpersec>0.00</kbpersec>
<new_rel_url></new_rel_url>
<queue_details>0</queue_details>
</queue>

View File

@ -0,0 +1,4 @@
<?xml version="1.0" encoding="UTF-8" ?>
<result><status>False</status>
<error>API Key Incorrect</error>
</result>

View File

@ -70,9 +70,19 @@
</ProjectReference> </ProjectReference>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Content Include="Files\Queue.xml">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="Files\QueueEmpty.xml">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="Files\QueueError.txt">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="Moq\Moq.dll" /> <Content Include="Moq\Moq.dll" />
<Content Include="Moq\Moq.xml" /> <Content Include="Moq\Moq.xml" />
</ItemGroup> </ItemGroup>
<ItemGroup />
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it. <!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets. Other similar extension points exist, see Microsoft.Common.targets.

View File

@ -1,5 +1,6 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO;
using System.Text; using System.Text;
using Gallio.Framework; using Gallio.Framework;
using log4net; using log4net;
@ -16,7 +17,7 @@ namespace NzbDrone.Core.Test
public class SabControllerTest public class SabControllerTest
{ {
[Test] [Test]
public void AddByUrl() public void AddByUrlSuccess()
{ {
//Setup //Setup
string sabnzbdInfo = "192.168.5.55:2222"; string sabnzbdInfo = "192.168.5.55:2222";
@ -32,7 +33,10 @@ namespace NzbDrone.Core.Test
config.Setup(c => c.GetValue("Password", String.Empty, false)).Returns(password); config.Setup(c => c.GetValue("Password", String.Empty, false)).Returns(password);
config.Setup(c => c.GetValue("Priority", String.Empty, false)).Returns(priority); config.Setup(c => c.GetValue("Priority", String.Empty, false)).Returns(priority);
var target = new SabController(config.Object, new Mock<ILog>().Object); var http = new Mock<IHttpController>();
http.Setup(s => s.GetRequest("http://192.168.5.55:2222/sabnzbd/api?mode=addurl&name=http://www.nzbclub.com/nzb_download.aspx?mid=1950232&priority=0&cat=tv&nzbname=This+is+an+Nzb&apikey=5c770e3197e4fe763423ee7c392c25d1&ma_username=admin&ma_password=pass")).Returns("ok");
var target = new SabController(config.Object, new Mock<ILog>().Object, http.Object);
ItemInfo nzb = new ItemInfo(); ItemInfo nzb = new ItemInfo();
nzb.Link = new Uri("http://www.nzbclub.com/nzb_download.aspx?mid=1950232"); nzb.Link = new Uri("http://www.nzbclub.com/nzb_download.aspx?mid=1950232");
@ -46,7 +50,7 @@ namespace NzbDrone.Core.Test
} }
[Test] [Test]
public void IsInQueue() public void AddByUrlError()
{ {
//Setup //Setup
string sabnzbdInfo = "192.168.5.55:2222"; string sabnzbdInfo = "192.168.5.55:2222";
@ -62,11 +66,127 @@ namespace NzbDrone.Core.Test
config.Setup(c => c.GetValue("Password", String.Empty, false)).Returns(password); config.Setup(c => c.GetValue("Password", String.Empty, false)).Returns(password);
config.Setup(c => c.GetValue("Priority", String.Empty, false)).Returns(priority); config.Setup(c => c.GetValue("Priority", String.Empty, false)).Returns(priority);
var target = new SabController(config.Object, new Mock<ILog>().Object); var http = new Mock<IHttpController>();
http.Setup(s => s.GetRequest("http://192.168.5.55:2222/sabnzbd/api?mode=addurl&name=http://www.nzbclub.com/nzb_download.aspx?mid=1950232&priority=0&cat=tv&nzbname=This+is+an+Nzb&apikey=5c770e3197e4fe763423ee7c392c25d1&ma_username=admin&ma_password=pass")).Returns("error");
var target = new SabController(config.Object, new Mock<ILog>().Object, http.Object);
ItemInfo nzb = new ItemInfo();
nzb.Link = new Uri("http://www.nzbclub.com/nzb_download.aspx?mid=1950232");
nzb.Title = "This is an Nzb";
//Act
bool result = target.AddByUrl(nzb);
//Assert
Assert.AreEqual(false, result);
}
[Test]
public void IsInQueue_True()
{
//Setup
string sabnzbdInfo = "192.168.5.55:2222";
string apikey = "5c770e3197e4fe763423ee7c392c25d1";
string username = "admin";
string password = "pass";
string priority = "0";
var config = new Mock<IConfigController>();
config.Setup(c => c.GetValue("SabnzbdInfo", String.Empty, false)).Returns(sabnzbdInfo);
config.Setup(c => c.GetValue("ApiKey", String.Empty, false)).Returns(apikey);
config.Setup(c => c.GetValue("Username", String.Empty, false)).Returns(username);
config.Setup(c => c.GetValue("Password", String.Empty, false)).Returns(password);
config.Setup(c => c.GetValue("Priority", String.Empty, false)).Returns(priority);
var http = new Mock<IHttpController>();
http.Setup(
s =>
s.GetRequest(
"http://192.168.5.55:2222/sabnzbd/api?mode=queue&output=xml&apikey=5c770e3197e4fe763423ee7c392c25d1&ma_username=admin&ma_password=pass"))
.Returns(new StreamReader(@".\Files\Queue.xml").ReadToEnd());
var target = new SabController(config.Object, new Mock<ILog>().Object, http.Object);
Episode episode = new Episode(); Episode episode = new Episode();
FeedItem item = new FeedItem(); FeedItem item = new FeedItem();
item.TitleFix = "This is my fixed title"; item.TitleFix = "Ubuntu Test";
episode.FeedItem = item;
//Act
bool result = target.IsInQueue(episode);
//Assert
Assert.AreEqual(true, result);
}
[Test]
public void IsInQueue_False_Empty()
{
//Setup
string sabnzbdInfo = "192.168.5.55:2222";
string apikey = "5c770e3197e4fe763423ee7c392c25d1";
string username = "admin";
string password = "pass";
string priority = "0";
var config = new Mock<IConfigController>();
config.Setup(c => c.GetValue("SabnzbdInfo", String.Empty, false)).Returns(sabnzbdInfo);
config.Setup(c => c.GetValue("ApiKey", String.Empty, false)).Returns(apikey);
config.Setup(c => c.GetValue("Username", String.Empty, false)).Returns(username);
config.Setup(c => c.GetValue("Password", String.Empty, false)).Returns(password);
config.Setup(c => c.GetValue("Priority", String.Empty, false)).Returns(priority);
var http = new Mock<IHttpController>();
http.Setup(
s =>
s.GetRequest(
"http://192.168.5.55:2222/sabnzbd/api?mode=queue&output=xml&apikey=5c770e3197e4fe763423ee7c392c25d1&ma_username=admin&ma_password=pass"))
.Returns(new StreamReader(@".\Files\QueueEmpty.xml").ReadToEnd());
var target = new SabController(config.Object, new Mock<ILog>().Object, http.Object);
Episode episode = new Episode();
FeedItem item = new FeedItem();
item.TitleFix = "Ubuntu Test";
episode.FeedItem = item;
//Act
bool result = target.IsInQueue(episode);
//Assert
Assert.AreEqual(false, result);
}
[Test]
public void IsInQueue_False_Error()
{
//Setup
string sabnzbdInfo = "192.168.5.55:2222";
string apikey = "5c770e3197e4fe763423ee7c392c25d1";
string username = "admin";
string password = "pass";
string priority = "0";
var config = new Mock<IConfigController>();
config.Setup(c => c.GetValue("SabnzbdInfo", String.Empty, false)).Returns(sabnzbdInfo);
config.Setup(c => c.GetValue("ApiKey", String.Empty, false)).Returns(apikey);
config.Setup(c => c.GetValue("Username", String.Empty, false)).Returns(username);
config.Setup(c => c.GetValue("Password", String.Empty, false)).Returns(password);
config.Setup(c => c.GetValue("Priority", String.Empty, false)).Returns(priority);
var http = new Mock<IHttpController>();
http.Setup(
s =>
s.GetRequest(
"http://192.168.5.55:2222/sabnzbd/api?mode=queue&output=xml&apikey=5c770e3197e4fe763423ee7c392c25d1&ma_username=admin&ma_password=pass"))
.Returns(new StreamReader(@".\Files\QueueError.xml").ReadToEnd());
var target = new SabController(config.Object, new Mock<ILog>().Object, http.Object);
Episode episode = new Episode();
FeedItem item = new FeedItem();
item.TitleFix = "Ubuntu Test";
episode.FeedItem = item; episode.FeedItem = item;
//Act //Act

View File

@ -0,0 +1,27 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
namespace NzbDrone.Core.Controllers
{
class HttpController : IHttpController
{
public string GetRequest(string request)
{
//Get the request and return as String Array
try
{
var webClient = new WebClient();
return webClient.DownloadString(request);
}
catch (Exception ex)
{
Console.WriteLine(ex);
return String.Empty;
}
}
}
}

View File

@ -0,0 +1,12 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace NzbDrone.Core.Controllers
{
public interface IHttpController
{
string GetRequest(string request);
}
}

View File

@ -1,5 +1,6 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO;
using System.Linq; using System.Linq;
using System.Net; using System.Net;
using System.Text; using System.Text;
@ -15,11 +16,13 @@ namespace NzbDrone.Core.Controllers
{ {
private readonly IConfigController _config; private readonly IConfigController _config;
private readonly ILog _logger; private readonly ILog _logger;
private readonly IHttpController _http;
public SabController(IConfigController config, ILog logger) public SabController(IConfigController config, ILog logger, IHttpController http)
{ {
_config = config; _config = config;
_logger = logger; _logger = logger;
_http = http;
} }
#region IDownloadClientController Members #region IDownloadClientController Members
@ -28,15 +31,19 @@ namespace NzbDrone.Core.Controllers
{ {
const string mode = "addurl"; const string mode = "addurl";
const string cat = "tv"; const string cat = "tv";
string priority = _config.GetValue("Priority", String.Empty, false);
string name = nzb.Link.ToString().Replace("&", "%26"); string name = nzb.Link.ToString().Replace("&", "%26");
string nzbName = HttpUtility.UrlEncode(nzb.Title); string nzbName = HttpUtility.UrlEncode(nzb.Title);
string action = string.Format("mode={0}&name={1}&cat={2}&nzbname={3}", mode, name, cat, nzbName); string action = string.Format("mode={0}&name={1}&priority={2}&cat={3}&nzbname={4}", mode, name, priority, cat, nzbName);
string request = GetSabRequest(action); string request = GetSabRequest(action);
_logger.DebugFormat("Adding report [{0}] to the queue.", nzbName); _logger.DebugFormat("Adding report [{0}] to the queue.", nzbName);
if (SendRequest(request) == "ok") string response = _http.GetRequest(request).Replace("\n", String.Empty);
_logger.DebugFormat("Queue Repsonse: [{0}]", response);
if (response == "ok")
return true; return true;
return false; return false;
@ -45,18 +52,20 @@ namespace NzbDrone.Core.Controllers
public bool IsInQueue(Episode epsiode) public bool IsInQueue(Episode epsiode)
{ {
string action = "mode=queue&output=xml"; string action = "mode=queue&output=xml";
string request = GetSabRequest(action);
string response = _http.GetRequest(request);
XDocument xDoc = XDocument.Load(GetSabRequest(action)); XDocument xDoc = XDocument.Parse(response);
//If an Error Occurred, retuyrn //If an Error Occurred, retuyrn)
if (xDoc.Descendants("error").Count() != 0) if (xDoc.Descendants("error").Count() != 0)
return false; return false;
if (xDoc.Descendants("queue").Count() == 0) if (xDoc.Descendants("queue").Count() == 0)
return false; return false;
//Get the Count of Items in Queue where 'filename' is Equal to goodName, if not zero, return true (isInQueue) //Get the Count of Items in Queue where 'filename' is Equal to goodName, if not zero, return true (isInQueue)))
if ((from s in xDoc.Descendants("slot") where s.Element("filename").ToString().Equals(epsiode.FeedItem.TitleFix, StringComparison.InvariantCultureIgnoreCase) select s).Count() != 0) if ((from s in xDoc.Descendants("slot") where s.Element("filename").Value.Equals(epsiode.FeedItem.TitleFix, StringComparison.InvariantCultureIgnoreCase) select s).Count() != 0)
{ {
_logger.DebugFormat("Episode in queue - '{0}'", epsiode.FeedItem.TitleFix); _logger.DebugFormat("Episode in queue - '{0}'", epsiode.FeedItem.TitleFix);
@ -74,19 +83,10 @@ namespace NzbDrone.Core.Controllers
string username = _config.GetValue("Username", String.Empty, false); string username = _config.GetValue("Username", String.Empty, false);
string password = _config.GetValue("Password", String.Empty, false); string password = _config.GetValue("Password", String.Empty, false);
string apiKey = _config.GetValue("ApiKey", String.Empty, false); string apiKey = _config.GetValue("ApiKey", String.Empty, false);
string priority = _config.GetValue("Priority", String.Empty, false);
return string.Format( return string.Format(
@"http://{0}/sabnzbd/api?$Action&priority={1}&apikey={2}&ma_username={3}&ma_password={4}", @"http://{0}/sabnzbd/api?$Action&apikey={1}&ma_username={2}&ma_password={3}",
sabnzbdInfo, priority, apiKey, username, password).Replace("$Action", action); sabnzbdInfo, apiKey, username, password).Replace("$Action", action);
}
private string SendRequest(string request)
{
var webClient = new WebClient();
string response = webClient.DownloadString(request).Replace("\n", String.Empty);
_logger.DebugFormat("Queue Repsonse: [{0}]", response);
return response;
} }
} }
} }

View File

@ -136,7 +136,9 @@
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Compile Include="Controllers\DbConfigController.cs" /> <Compile Include="Controllers\DbConfigController.cs" />
<Compile Include="Controllers\HttpController.cs" />
<Compile Include="Controllers\IDownloadClientController.cs" /> <Compile Include="Controllers\IDownloadClientController.cs" />
<Compile Include="Controllers\IHttpController.cs" />
<Compile Include="Controllers\ISeriesController.cs" /> <Compile Include="Controllers\ISeriesController.cs" />
<Compile Include="Controllers\ITvDbController.cs" /> <Compile Include="Controllers\ITvDbController.cs" />
<Compile Include="Controllers\SabController.cs" /> <Compile Include="Controllers\SabController.cs" />