From 219ea364a93df0a25c5d1aece83653c5cf484f05 Mon Sep 17 00:00:00 2001 From: Mark McDowall Date: Tue, 21 Feb 2012 16:53:17 -0800 Subject: [PATCH] More unit tests for Plex notifications. --- .../ProviderTests/PlexProviderTest.cs | 89 +++++++++++++++++++ NzbDrone.Core/Providers/PlexProvider.cs | 4 +- 2 files changed, 91 insertions(+), 2 deletions(-) diff --git a/NzbDrone.Core.Test/ProviderTests/PlexProviderTest.cs b/NzbDrone.Core.Test/ProviderTests/PlexProviderTest.cs index 905af8cf2..3621c7e1b 100644 --- a/NzbDrone.Core.Test/ProviderTests/PlexProviderTest.cs +++ b/NzbDrone.Core.Test/ProviderTests/PlexProviderTest.cs @@ -3,6 +3,7 @@ using System; using System.IO; using System.Linq; +using System.Net; using System.Text; using FizzWare.NBuilder; using FluentAssertions; @@ -23,6 +24,27 @@ namespace NzbDrone.Core.Test.ProviderTests // ReSharper disable InconsistentNaming public class PlexProviderTest : CoreTest { + private void WithSingleClient() + { + Mocker.GetMock().SetupGet(s => s.PlexClientHosts) + .Returns("localhost:3000"); + } + + private void WithMultipleClients() + { + Mocker.GetMock().SetupGet(s => s.PlexClientHosts) + .Returns("localhost:3000, 192.168.0.10:3000"); + } + + public void WithClientCredentials() + { + Mocker.GetMock().SetupGet(s => s.PlexUsername) + .Returns("plex"); + + Mocker.GetMock().SetupGet(s => s.PlexPassword) + .Returns("plex"); + } + [Test] public void GetSectionKeys_should_return_single_section_key_when_only_one_show_section() { @@ -98,5 +120,72 @@ namespace NzbDrone.Core.Test.ProviderTests //Assert } + + [Test] + public void Notify_should_send_update_for_single_client_when_only_one_is_configured() + { + //Setup + WithSingleClient(); + + 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); + + var fakeHttp = Mocker.GetMock(); + fakeHttp.Setup(s => s.DownloadString(expectedUrl)) + .Returns("ok"); + + //Act + Mocker.Resolve().Notify(header, message); + + //Assert + fakeHttp.Verify(v => v.DownloadString(expectedUrl), Times.Once()); + } + + [Test] + public void Notify_should_send_update_to_all_configured_clients() + { + //Setup + WithMultipleClients(); + + 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); + + var fakeHttp = Mocker.GetMock(); + fakeHttp.Setup(s => s.DownloadString(It.IsAny())) + .Returns("ok"); + + //Act + Mocker.Resolve().Notify(header, message); + + //Assert + fakeHttp.Verify(v => v.DownloadString(It.IsAny()), Times.Exactly(2)); + } + + [Test] + public void Notify_should_send_notification_with_credentials_when_configured() + { + //Setup + WithSingleClient(); + 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); + + var fakeHttp = Mocker.GetMock(); + fakeHttp.Setup(s => s.DownloadString(expectedUrl, "plex", "plex")) + .Returns("ok"); + + //Act + Mocker.Resolve().Notify(header, message); + + //Assert + fakeHttp.Verify(v => v.DownloadString(expectedUrl, "plex", "plex"), Times.Once()); + } } } \ No newline at end of file diff --git a/NzbDrone.Core/Providers/PlexProvider.cs b/NzbDrone.Core/Providers/PlexProvider.cs index 621b1ea07..c08cdfb69 100644 --- a/NzbDrone.Core/Providers/PlexProvider.cs +++ b/NzbDrone.Core/Providers/PlexProvider.cs @@ -37,11 +37,11 @@ namespace NzbDrone.Core.Providers try { var command = String.Format("ExecBuiltIn(Notification({0}, {1}))", header, message); - SendCommand(host, command, _configProvider.PlexUsername, _configProvider.PlexPassword); + SendCommand(host.Trim(), command, _configProvider.PlexUsername, _configProvider.PlexPassword); } catch(Exception ex) { - logger.WarnException("Failed to send notification to Plex Client: " + host, ex); + logger.WarnException("Failed to send notification to Plex Client: " + host.Trim(), ex); } } }