2013-05-20 02:17:32 +03:00
|
|
|
using System;
|
2012-02-22 02:10:42 +03:00
|
|
|
using System.IO;
|
|
|
|
using System.Linq;
|
|
|
|
using System.Text;
|
|
|
|
using FluentAssertions;
|
|
|
|
using Moq;
|
|
|
|
using NUnit.Framework;
|
|
|
|
using NzbDrone.Common;
|
2013-05-20 02:17:32 +03:00
|
|
|
using NzbDrone.Core.Notifications.Plex;
|
2012-02-22 02:10:42 +03:00
|
|
|
using NzbDrone.Core.Test.Framework;
|
|
|
|
|
2013-06-13 04:37:05 +03:00
|
|
|
namespace NzbDrone.Core.Test.NotificationTests
|
2012-02-22 02:10:42 +03:00
|
|
|
{
|
|
|
|
[TestFixture]
|
2013-03-29 01:07:09 +03:00
|
|
|
|
2013-02-17 08:44:06 +03:00
|
|
|
public class PlexProviderTest : CoreTest
|
2012-02-22 02:10:42 +03:00
|
|
|
{
|
2013-05-20 02:17:32 +03:00
|
|
|
private PlexClientSettings _clientSettings;
|
2012-02-22 03:53:17 +03:00
|
|
|
|
2013-05-20 02:17:32 +03:00
|
|
|
public void WithClientCredentials()
|
2012-02-22 03:53:17 +03:00
|
|
|
{
|
2013-05-20 02:17:32 +03:00
|
|
|
_clientSettings.Username = "plex";
|
|
|
|
_clientSettings.Password = "plex";
|
2012-02-22 03:53:17 +03:00
|
|
|
}
|
|
|
|
|
2013-05-20 02:17:32 +03:00
|
|
|
[SetUp]
|
|
|
|
public void Setup()
|
2012-02-22 03:53:17 +03:00
|
|
|
{
|
2013-05-20 02:17:32 +03:00
|
|
|
_clientSettings = new PlexClientSettings
|
|
|
|
{
|
|
|
|
Host = "localhost",
|
|
|
|
Port = 3000
|
|
|
|
};
|
2012-02-22 03:53:17 +03:00
|
|
|
}
|
|
|
|
|
2012-02-22 02:10:42 +03:00
|
|
|
[Test]
|
|
|
|
public void GetSectionKeys_should_return_single_section_key_when_only_one_show_section()
|
2013-05-20 02:17:32 +03:00
|
|
|
{
|
2012-02-22 02:10:42 +03:00
|
|
|
var response = "<MediaContainer size=\"1\" mediaTagPrefix=\"/system/bundle/media/flags/\" mediaTagVersion=\"1329809559\" title1=\"Plex Library\" identifier=\"com.plexapp.plugins.library\"><Directory refreshing=\"0\" key=\"5\" type=\"show\" title=\"TV Shows\" art=\"/:/resources/show-fanart.jpg\" agent=\"com.plexapp.agents.thetvdb\" scanner=\"Plex Series Scanner\" language=\"en\" updatedAt=\"1329810350\"><Location path=\"C:/Test/TV\"/></Directory></MediaContainer>";
|
|
|
|
Stream stream = new MemoryStream(ASCIIEncoding.Default.GetBytes(response));
|
|
|
|
|
2013-04-11 02:41:45 +03:00
|
|
|
Mocker.GetMock<IHttpProvider>().Setup(s => s.DownloadStream("http://localhost:32400/library/sections", null))
|
2012-02-22 02:10:42 +03:00
|
|
|
.Returns(stream);
|
|
|
|
|
2013-06-13 09:41:26 +03:00
|
|
|
var result = Mocker.Resolve<PlexService>().GetSectionKeys(new PlexServerSettings { Host = "localhost", Port = 32400 });
|
2012-02-22 02:10:42 +03:00
|
|
|
|
2013-03-29 01:07:09 +03:00
|
|
|
|
2012-02-22 02:10:42 +03:00
|
|
|
result.Should().HaveCount(1);
|
|
|
|
result.First().Should().Be(5);
|
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
public void GetSectionKeys_should_return_single_section_key_when_only_one_show_section_with_other_sections()
|
|
|
|
{
|
2013-03-29 01:07:09 +03:00
|
|
|
|
2012-02-22 02:10:42 +03:00
|
|
|
|
|
|
|
var response = "<MediaContainer size=\"1\" mediaTagPrefix=\"/system/bundle/media/flags/\" mediaTagVersion=\"1329809559\" title1=\"Plex Library\" identifier=\"com.plexapp.plugins.library\"><Directory refreshing=\"0\" key=\"5\" type=\"show\" title=\"TV Shows\" art=\"/:/resources/show-fanart.jpg\" agent=\"com.plexapp.agents.thetvdb\" scanner=\"Plex Series Scanner\" language=\"en\" updatedAt=\"1329810350\"><Location path=\"C:/Test/TV\"/></Directory><Directory refreshing=\"0\" key=\"7\" type=\"movie\" title=\"TV Shows\" art=\"/:/resources/show-fanart.jpg\" agent=\"com.plexapp.agents.thetvdb\" scanner=\"Plex Series Scanner\" language=\"en\" updatedAt=\"1329810350\"><Location path=\"C:/Test/TV\"/></Directory></MediaContainer>";
|
|
|
|
Stream stream = new MemoryStream(ASCIIEncoding.Default.GetBytes(response));
|
|
|
|
|
2013-04-11 02:41:45 +03:00
|
|
|
Mocker.GetMock<IHttpProvider>().Setup(s => s.DownloadStream("http://localhost:32400/library/sections", null))
|
2012-02-22 02:10:42 +03:00
|
|
|
.Returns(stream);
|
|
|
|
|
2013-03-29 01:07:09 +03:00
|
|
|
|
2013-06-13 09:41:26 +03:00
|
|
|
var result = Mocker.Resolve<PlexService>().GetSectionKeys(new PlexServerSettings { Host = "localhost", Port = 32400 });
|
2012-02-22 02:10:42 +03:00
|
|
|
|
2013-03-29 01:07:09 +03:00
|
|
|
|
2012-02-22 02:10:42 +03:00
|
|
|
result.Should().HaveCount(1);
|
|
|
|
result.First().Should().Be(5);
|
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
public void GetSectionKeys_should_return_multiple_section_keys_when_there_are_multiple_show_sections()
|
|
|
|
{
|
2013-03-29 01:07:09 +03:00
|
|
|
|
2012-02-22 02:10:42 +03:00
|
|
|
|
|
|
|
var response = "<MediaContainer size=\"1\" mediaTagPrefix=\"/system/bundle/media/flags/\" mediaTagVersion=\"1329809559\" title1=\"Plex Library\" identifier=\"com.plexapp.plugins.library\"><Directory refreshing=\"0\" key=\"5\" type=\"show\" title=\"TV Shows\" art=\"/:/resources/show-fanart.jpg\" agent=\"com.plexapp.agents.thetvdb\" scanner=\"Plex Series Scanner\" language=\"en\" updatedAt=\"1329810350\"><Location path=\"C:/Test/TV\"/></Directory><Directory refreshing=\"0\" key=\"6\" type=\"show\" title=\"TV Shows\" art=\"/:/resources/show-fanart.jpg\" agent=\"com.plexapp.agents.thetvdb\" scanner=\"Plex Series Scanner\" language=\"en\" updatedAt=\"1329810350\"><Location path=\"C:/Test/TV\"/></Directory></MediaContainer>";
|
|
|
|
Stream stream = new MemoryStream(ASCIIEncoding.Default.GetBytes(response));
|
|
|
|
|
2013-04-11 02:41:45 +03:00
|
|
|
Mocker.GetMock<IHttpProvider>().Setup(s => s.DownloadStream("http://localhost:32400/library/sections", null))
|
2012-02-22 02:10:42 +03:00
|
|
|
.Returns(stream);
|
|
|
|
|
2013-06-13 09:41:26 +03:00
|
|
|
|
|
|
|
var result = Mocker.Resolve<PlexService>().GetSectionKeys(new PlexServerSettings { Host = "localhost", Port = 32400 });
|
2012-02-22 02:10:42 +03:00
|
|
|
|
2013-03-29 01:07:09 +03:00
|
|
|
|
2012-02-22 02:10:42 +03:00
|
|
|
result.Should().HaveCount(2);
|
|
|
|
result.First().Should().Be(5);
|
|
|
|
result.Last().Should().Be(6);
|
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
public void UpdateSection_should_update_section()
|
|
|
|
{
|
2013-03-29 01:07:09 +03:00
|
|
|
|
2012-02-22 02:10:42 +03:00
|
|
|
|
|
|
|
var response = "";
|
|
|
|
Stream stream = new MemoryStream(ASCIIEncoding.Default.GetBytes(response));
|
|
|
|
|
2013-04-11 02:41:45 +03:00
|
|
|
Mocker.GetMock<IHttpProvider>().Setup(s => s.DownloadString("http://localhost:32400/library/sections/5/refresh"))
|
2012-02-22 02:10:42 +03:00
|
|
|
.Returns(response);
|
|
|
|
|
2013-06-13 09:41:26 +03:00
|
|
|
|
|
|
|
Mocker.Resolve<PlexService>().UpdateSection(new PlexServerSettings { Host = "localhost", Port = 32400 }, 5);
|
2012-02-22 02:10:42 +03:00
|
|
|
|
2013-03-29 01:07:09 +03:00
|
|
|
|
2012-02-22 02:10:42 +03:00
|
|
|
|
|
|
|
}
|
2012-02-22 03:53:17 +03:00
|
|
|
|
|
|
|
[Test]
|
2013-05-20 02:17:32 +03:00
|
|
|
public void Notify_should_send_notification()
|
2012-02-22 03:53:17 +03:00
|
|
|
{
|
|
|
|
|
|
|
|
const string header = "Test Header";
|
|
|
|
const string message = "Test Message";
|
|
|
|
|
|
|
|
var expectedUrl = String.Format("http://localhost:3000/xbmcCmds/xbmcHttp?command=ExecBuiltIn(Notification({0}, {1}))", header, message);
|
|
|
|
|
2013-04-11 02:41:45 +03:00
|
|
|
var fakeHttp = Mocker.GetMock<IHttpProvider>();
|
2012-02-22 03:53:17 +03:00
|
|
|
fakeHttp.Setup(s => s.DownloadString(expectedUrl))
|
|
|
|
.Returns("ok");
|
|
|
|
|
2013-03-29 01:07:09 +03:00
|
|
|
|
2013-06-13 09:41:26 +03:00
|
|
|
Mocker.Resolve<PlexService>().Notify(_clientSettings, header, message);
|
2012-02-22 03:53:17 +03:00
|
|
|
|
2013-03-29 01:07:09 +03:00
|
|
|
|
2012-02-22 03:53:17 +03:00
|
|
|
fakeHttp.Verify(v => v.DownloadString(expectedUrl), Times.Once());
|
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
public void Notify_should_send_notification_with_credentials_when_configured()
|
|
|
|
{
|
|
|
|
WithClientCredentials();
|
|
|
|
|
|
|
|
const string header = "Test Header";
|
|
|
|
const string message = "Test Message";
|
|
|
|
|
|
|
|
var expectedUrl = String.Format("http://localhost:3000/xbmcCmds/xbmcHttp?command=ExecBuiltIn(Notification({0}, {1}))", header, message);
|
|
|
|
|
2013-04-11 02:41:45 +03:00
|
|
|
var fakeHttp = Mocker.GetMock<IHttpProvider>();
|
2012-02-22 03:53:17 +03:00
|
|
|
fakeHttp.Setup(s => s.DownloadString(expectedUrl, "plex", "plex"))
|
|
|
|
.Returns("ok");
|
|
|
|
|
2013-03-29 01:07:09 +03:00
|
|
|
|
2013-06-13 09:41:26 +03:00
|
|
|
Mocker.Resolve<PlexService>().Notify(_clientSettings, header, message);
|
2012-02-22 03:53:17 +03:00
|
|
|
|
2013-03-29 01:07:09 +03:00
|
|
|
|
2012-02-22 03:53:17 +03:00
|
|
|
fakeHttp.Verify(v => v.DownloadString(expectedUrl, "plex", "plex"), Times.Once());
|
|
|
|
}
|
2012-02-22 02:10:42 +03:00
|
|
|
}
|
|
|
|
}
|