From 4d85000753b4853f718109ed91a4bb2542335335 Mon Sep 17 00:00:00 2001 From: tidusjar Date: Tue, 26 Jul 2016 20:50:38 +0100 Subject: [PATCH] Fixed the bug in #438 and added unit tests to make so we dont break it in the future --- PlexRequests.UI.Tests/AdminModuleTests.cs | 725 +++++++++--------- .../PlexRequests.UI.Tests.csproj | 1 + PlexRequests.UI.Tests/SearchModuleTests.cs | 164 ++++ PlexRequests.UI/Modules/BaseModule.cs | 4 +- PlexRequests.UI/Modules/SearchModule.cs | 4 +- 5 files changed, 529 insertions(+), 369 deletions(-) create mode 100644 PlexRequests.UI.Tests/SearchModuleTests.cs diff --git a/PlexRequests.UI.Tests/AdminModuleTests.cs b/PlexRequests.UI.Tests/AdminModuleTests.cs index 17b8f48f2..050871b82 100644 --- a/PlexRequests.UI.Tests/AdminModuleTests.cs +++ b/PlexRequests.UI.Tests/AdminModuleTests.cs @@ -1,365 +1,360 @@ -//#region Copyright -//// /************************************************************************ -//// Copyright (c) 2016 Jamie Rees -//// File: UserLoginModuleTests.cs -//// Created By: Jamie Rees -//// -//// Permission is hereby granted, free of charge, to any person obtaining -//// a copy of this software and associated documentation files (the -//// "Software"), to deal in the Software without restriction, including -//// without limitation the rights to use, copy, modify, merge, publish, -//// distribute, sublicense, and/or sell copies of the Software, and to -//// permit persons to whom the Software is furnished to do so, subject to -//// the following conditions: -//// -//// The above copyright notice and this permission notice shall be -//// included in all copies or substantial portions of the Software. -//// -//// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -//// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -//// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND -//// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE -//// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION -//// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION -//// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -//// ************************************************************************/ -//#endregion -//using System.Collections.Generic; -//using System.Linq; - -//using Moq; - -//using Nancy; -//using Nancy.Testing; - -//using Newtonsoft.Json; -//using Newtonsoft.Json.Linq; - -//using NUnit.Framework; - -//using PlexRequests.Api.Interfaces; -//using PlexRequests.Api.Models.Plex; -//using PlexRequests.Core; -//using PlexRequests.Core.SettingModels; -//using PlexRequests.Services.Interfaces; -//using PlexRequests.Store.Models; -//using PlexRequests.Store.Repository; -//using PlexRequests.UI.Models; -//using PlexRequests.UI.Modules; -//using PlexRequests.Helpers; -//using PlexRequests.Helpers.Analytics; -//using PlexRequests.UI.Helpers; - -//namespace PlexRequests.UI.Tests -//{ -// [TestFixture] -// public class AdminModuleTests -// { -// private Mock> PlexRequestMock { get; set; } -// private Mock> CpMock { get; set; } -// private Mock> AuthMock { get; set; } -// private Mock> PlexSettingsMock { get; set; } -// private Mock> SonarrSettingsMock { get; set; } -// private Mock> SickRageSettingsMock { get; set; } -// private Mock> ScheduledJobsSettingsMock { get; set; } -// private Mock> EmailMock { get; set; } -// private Mock> PushbulletSettings { get; set; } -// private Mock> PushoverSettings { get; set; } -// private Mock> HeadphonesSettings { get; set; } -// private Mock PlexMock { get; set; } -// private Mock SonarrApiMock { get; set; } -// private Mock PushbulletApi { get; set; } -// private Mock PushoverApi { get; set; } -// private Mock CpApi { get; set; } -// private Mock RecorderMock { get; set; } -// private Mock> LogRepo { get; set; } -// private Mock NotificationService { get; set; } -// private Mock Cache { get; set; } -// private Mock> Log { get; set; } -// private Mock> SlackSettings { get; set; } -// private Mock> LandingPageSettings { get; set; } -// private Mock SlackApi { get; set; } -// private Mock IAnalytics { get; set; } - -// private ConfigurableBootstrapper Bootstrapper { get; set; } - -// [SetUp] -// public void Setup() -// { -// AuthMock = new Mock>(); -// var expectedSettings = new AuthenticationSettings { UserAuthentication = false, PlexAuthToken = "abc" }; -// AuthMock.Setup(x => x.GetSettings()).Returns(expectedSettings); - -// PlexMock = new Mock(); -// PlexMock.Setup(x => x.SignIn("Username1", "Password1")) -// .Returns(new PlexAuthentication { user = new User { authentication_token = "abc", title = "Username1" } }); - -// PlexRequestMock = new Mock>(); -// PlexRequestMock.Setup(x => x.GetSettings()).Returns(new PlexRequestSettings()); -// CpMock = new Mock>(); -// PlexSettingsMock = new Mock>(); -// SonarrApiMock = new Mock(); -// SonarrSettingsMock = new Mock>(); -// EmailMock = new Mock>(); -// PushbulletApi = new Mock(); -// PushbulletSettings = new Mock>(); -// CpApi = new Mock(); -// SickRageSettingsMock = new Mock>(); -// LogRepo = new Mock>(); -// PushoverSettings = new Mock>(); -// PushoverApi = new Mock(); -// NotificationService = new Mock(); -// HeadphonesSettings = new Mock>(); -// Cache = new Mock(); -// Log = new Mock>(); -// SlackApi = new Mock(); -// SlackSettings = new Mock>(); -// LandingPageSettings = new Mock>(); -// ScheduledJobsSettingsMock = new Mock>(); -// RecorderMock = new Mock(); -// IAnalytics = new Mock(); - - -// Bootstrapper = new ConfigurableBootstrapper(with => -// { -// with.Module(); -// with.Dependency(AuthMock.Object); -// with.Dependency(PlexRequestMock.Object); -// with.Dependency(CpMock.Object); -// with.Dependency(PlexSettingsMock.Object); -// with.Dependency(SonarrApiMock.Object); -// with.Dependency(SonarrSettingsMock.Object); -// with.Dependency(PlexMock.Object); -// with.Dependency(EmailMock.Object); -// with.Dependency(PushbulletApi.Object); -// with.Dependency(PushbulletSettings.Object); -// with.Dependency(CpApi.Object); -// with.Dependency(SickRageSettingsMock.Object); -// with.Dependency(LogRepo.Object); -// with.Dependency(PushoverSettings.Object); -// with.Dependency(PushoverApi.Object); -// with.Dependency(NotificationService.Object); -// with.Dependency(IAnalytics.Object); -// with.Dependency(HeadphonesSettings.Object); -// with.Dependency(Cache.Object); -// with.Dependency(Log.Object); -// with.Dependency(SlackApi.Object); -// with.Dependency(LandingPageSettings.Object); -// with.Dependency(SlackSettings.Object); -// with.Dependency(ScheduledJobsSettingsMock.Object); -// with.Dependency(RecorderMock.Object); -// with.RootPathProvider(); -// with.RequestStartup((container, pipelines, context) => -// { -// context.CurrentUser = new UserIdentity { UserName = "user", Claims = new List {"Admin"} }; -// }); -// }); - -// Bootstrapper.WithSession(new Dictionary()); -// } - -// [Test] -// public void RequestAuthTokenTestNewSettings() -// { -// var browser = new Browser(Bootstrapper); - -// var result = browser.Post("/admin/requestauth", with => -// { -// with.HttpRequest(); -// with.Header("Accept", "application/json"); -// with.FormValue("username", "Username1"); -// with.FormValue("password", "Password1"); - -// }); - -// Assert.That(HttpStatusCode.OK, Is.EqualTo(result.StatusCode)); - -// var body = JsonConvert.DeserializeObject(result.Body.AsString()); -// Assert.That(body.Result, Is.EqualTo(true)); -// PlexMock.Verify(x => x.SignIn("Username1", "Password1"), Times.Once); -// AuthMock.Verify(x => x.GetSettings(), Times.Once); -// AuthMock.Verify(x => x.SaveSettings(It.IsAny()), Times.Once); -// } - -// [Test] -// public void RequestAuthTokenTestEmptyCredentials() -// { -// var browser = new Browser(Bootstrapper); - -// var result = browser.Post("/admin/requestauth", with => -// { -// with.HttpRequest(); -// with.Header("Accept", "application/json"); -// with.FormValue("username", string.Empty); -// with.FormValue("password", "Password1"); - -// }); - -// Assert.That(HttpStatusCode.OK, Is.EqualTo(result.StatusCode)); - -// var body = JsonConvert.DeserializeObject(result.Body.AsString()); -// Assert.That(body.Result, Is.EqualTo(false)); -// Assert.That(body.Message, Is.Not.Empty); - -// PlexMock.Verify(x => x.SignIn("Username1", "Password1"), Times.Never); -// AuthMock.Verify(x => x.GetSettings(), Times.Never); -// AuthMock.Verify(x => x.SaveSettings(It.IsAny()), Times.Never); -// } - -// [Test] -// public void RequestAuthTokenTesPlexSignInFail() -// { -// var browser = new Browser(Bootstrapper); - -// var result = browser.Post("/admin/requestauth", with => -// { -// with.HttpRequest(); -// with.Header("Accept", "application/json"); -// with.FormValue("username", "Badusername"); -// with.FormValue("password", "Password1"); - -// }); - -// Assert.That(HttpStatusCode.OK, Is.EqualTo(result.StatusCode)); - -// var body = JsonConvert.DeserializeObject(result.Body.AsString()); -// Assert.That(body.Result, Is.EqualTo(false)); -// Assert.That(body.Message, Is.Not.Empty); - -// PlexMock.Verify(x => x.SignIn("Badusername", "Password1"), Times.Once); -// AuthMock.Verify(x => x.GetSettings(), Times.Never); -// AuthMock.Verify(x => x.SaveSettings(It.IsAny()), Times.Never); -// } - -// [Test] -// public void RequestAuthTokenTestExistingSettings() -// { -// AuthMock.Setup(x => x.GetSettings()).Returns(() => null); -// var browser = new Browser(Bootstrapper); - -// var result = browser.Post("/admin/requestauth", with => -// { -// with.HttpRequest(); -// with.Header("Accept", "application/json"); -// with.FormValue("username", "Username1"); -// with.FormValue("password", "Password1"); - -// }); - -// Assert.That(HttpStatusCode.OK, Is.EqualTo(result.StatusCode)); - -// var body = JsonConvert.DeserializeObject(result.Body.AsString()); -// Assert.That(body.Result, Is.EqualTo(true)); - -// PlexMock.Verify(x => x.SignIn("Username1", "Password1"), Times.Once); -// AuthMock.Verify(x => x.GetSettings(), Times.Once); -// AuthMock.Verify(x => x.SaveSettings(It.IsAny()), Times.Once); -// } - -// [Test] -// public void GetUsersSuccessfully() -// { -// var users = new PlexFriends { User = new[] { new UserFriends { Title = "abc2" }, } }; -// PlexMock.Setup(x => x.GetUsers(It.IsAny())).Returns(users); -// var browser = new Browser(Bootstrapper); - -// var result = browser.Get("/admin/getusers", with => -// { -// with.HttpRequest(); -// with.Header("Accept", "application/json"); -// with.FormValue("username", "Username1"); -// with.FormValue("password", "Password1"); - -// }); - -// Assert.That(HttpStatusCode.OK, Is.EqualTo(result.StatusCode)); - - -// var body = JsonConvert.DeserializeObject(result.Body.AsString()); -// var user = body["users"]; -// Assert.That(body, Is.Not.Null); -// Assert.That(user.ToString().Contains("abc"), Is.True); - -// PlexMock.Verify(x => x.GetUsers(It.IsAny()), Times.Once); -// AuthMock.Verify(x => x.GetSettings(), Times.Once); -// } - -// [Test] -// public void GetUsersReturnsNoUsers() -// { -// var users = new PlexFriends(); -// PlexMock.Setup(x => x.GetUsers(It.IsAny())).Returns(users); -// var browser = new Browser(Bootstrapper); - -// var result = browser.Get("/admin/getusers", with => -// { -// with.HttpRequest(); -// with.Header("Accept", "application/json"); -// with.FormValue("username", "Username1"); -// with.FormValue("password", "Password1"); - - -// }); - -// Assert.That(HttpStatusCode.OK, Is.EqualTo(result.StatusCode)); - -// var body = JsonConvert.DeserializeObject(result.Body.AsString()); -// Assert.That(body, Is.Not.Null); -// Assert.That(string.IsNullOrWhiteSpace(body), Is.True); - -// PlexMock.Verify(x => x.GetUsers(It.IsAny()), Times.Once); -// AuthMock.Verify(x => x.GetSettings(), Times.Once); -// } - -// [Test] -// public void GetUsersReturnsNull() -// { -// PlexMock.Setup(x => x.GetUsers(It.IsAny())).Returns(() => null); -// var browser = new Browser(Bootstrapper); - -// var result = browser.Get("/admin/getusers", with => -// { -// with.HttpRequest(); -// with.Header("Accept", "application/json"); -// with.FormValue("username", "Username1"); -// with.FormValue("password", "Password1"); - -// }); - -// Assert.That(HttpStatusCode.OK, Is.EqualTo(result.StatusCode)); - -// var body = JsonConvert.DeserializeObject(result.Body.AsString()); -// Assert.That(body, Is.Not.Null); -// Assert.That(string.IsNullOrWhiteSpace(body), Is.True); - -// PlexMock.Verify(x => x.GetUsers(It.IsAny()), Times.Once); -// AuthMock.Verify(x => x.GetSettings(), Times.Once); -// } - -// [Test] -// public void GetUsersTokenIsNull() -// { -// AuthMock.Setup(x => x.GetSettings()).Returns(new AuthenticationSettings()); -// var browser = new Browser(Bootstrapper); - -// var result = browser.Get("/admin/getusers", with => -// { -// with.HttpRequest(); -// with.Header("Accept", "application/json"); -// with.FormValue("username", "Username1"); -// with.FormValue("password", "Password1"); - -// }); - -// Assert.That(HttpStatusCode.OK, Is.EqualTo(result.StatusCode)); - -// var body = JsonConvert.DeserializeObject(result.Body.AsString()); -// var user = (string)body["users"]; -// Assert.That(body, Is.Not.Null); -// Assert.That(string.IsNullOrWhiteSpace(user), Is.True); - -// PlexMock.Verify(x => x.GetUsers(It.IsAny()), Times.Never); -// AuthMock.Verify(x => x.GetSettings(), Times.Once); -// } -// } -//} \ No newline at end of file +#region Copyright +// /************************************************************************ +// Copyright (c) 2016 Jamie Rees +// File: UserLoginModuleTests.cs +// Created By: Jamie Rees +// +// Permission is hereby granted, free of charge, to any person obtaining +// a copy of this software and associated documentation files (the +// "Software"), to deal in the Software without restriction, including +// without limitation the rights to use, copy, modify, merge, publish, +// distribute, sublicense, and/or sell copies of the Software, and to +// permit persons to whom the Software is furnished to do so, subject to +// the following conditions: +// +// The above copyright notice and this permission notice shall be +// included in all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE +// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION +// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +// ************************************************************************/ +#endregion +using System.Collections.Generic; +using System.Linq; + +using Moq; + +using Nancy; +using Nancy.Testing; + +using Newtonsoft.Json; +using Newtonsoft.Json.Linq; + +using NUnit.Framework; + +using PlexRequests.Api.Interfaces; +using PlexRequests.Api.Models.Plex; +using PlexRequests.Core; +using PlexRequests.Core.SettingModels; +using PlexRequests.Services.Interfaces; +using PlexRequests.Store.Models; +using PlexRequests.Store.Repository; +using PlexRequests.UI.Models; +using PlexRequests.UI.Modules; +using PlexRequests.Helpers; +using PlexRequests.Helpers.Analytics; +using PlexRequests.UI.Helpers; + +namespace PlexRequests.UI.Tests +{ + [TestFixture] + public class AdminModuleTests + { + private Mock> PlexRequestMock { get; set; } + private Mock> CpMock { get; set; } + private Mock> AuthMock { get; set; } + private Mock> PlexSettingsMock { get; set; } + private Mock> SonarrSettingsMock { get; set; } + private Mock> SickRageSettingsMock { get; set; } + private Mock> ScheduledJobsSettingsMock { get; set; } + private Mock> EmailMock { get; set; } + private Mock> PushbulletSettings { get; set; } + private Mock> PushoverSettings { get; set; } + private Mock> HeadphonesSettings { get; set; } + private Mock PlexMock { get; set; } + private Mock SonarrApiMock { get; set; } + private Mock PushbulletApi { get; set; } + private Mock PushoverApi { get; set; } + private Mock CpApi { get; set; } + private Mock RecorderMock { get; set; } + private Mock> LogRepo { get; set; } + private Mock NotificationService { get; set; } + private Mock Cache { get; set; } + private Mock> Log { get; set; } + private Mock> SlackSettings { get; set; } + private Mock> LandingPageSettings { get; set; } + private Mock SlackApi { get; set; } + private Mock Analytics { get; set; } + + private ConfigurableBootstrapper Bootstrapper { get; set; } + + [SetUp] + public void Setup() + { + AuthMock = new Mock>(); + var expectedSettings = new AuthenticationSettings { UserAuthentication = false }; + AuthMock.Setup(x => x.GetSettings()).Returns(expectedSettings); + PlexSettingsMock = new Mock>(); + PlexSettingsMock.Setup(x => x.GetSettings()).Returns(new PlexSettings {PlexAuthToken = "abc"}); + + PlexMock = new Mock(); + PlexMock.Setup(x => x.SignIn("Username1", "Password1")) + .Returns(new PlexAuthentication { user = new User { authentication_token = "abc", title = "Username1" } }); + + PlexRequestMock = new Mock>(); + PlexRequestMock.Setup(x => x.GetSettings()).Returns(new PlexRequestSettings() ); + CpMock = new Mock>(); + + SonarrApiMock = new Mock(); + SonarrSettingsMock = new Mock>(); + EmailMock = new Mock>(); + PushbulletApi = new Mock(); + PushbulletSettings = new Mock>(); + CpApi = new Mock(); + SickRageSettingsMock = new Mock>(); + LogRepo = new Mock>(); + PushoverSettings = new Mock>(); + PushoverApi = new Mock(); + NotificationService = new Mock(); + HeadphonesSettings = new Mock>(); + Cache = new Mock(); + Log = new Mock>(); + SlackApi = new Mock(); + SlackSettings = new Mock>(); + LandingPageSettings = new Mock>(); + ScheduledJobsSettingsMock = new Mock>(); + RecorderMock = new Mock(); + Analytics = new Mock(); + + + Bootstrapper = new ConfigurableBootstrapper(with => + { + with.Module(); + with.Dependency(AuthMock.Object); + with.Dependency(PlexRequestMock.Object); + with.Dependency(CpMock.Object); + with.Dependency(PlexSettingsMock.Object); + with.Dependency(SonarrApiMock.Object); + with.Dependency(SonarrSettingsMock.Object); + with.Dependency(PlexMock.Object); + with.Dependency(EmailMock.Object); + with.Dependency(PushbulletApi.Object); + with.Dependency(PushbulletSettings.Object); + with.Dependency(CpApi.Object); + with.Dependency(SickRageSettingsMock.Object); + with.Dependency(LogRepo.Object); + with.Dependency(PushoverSettings.Object); + with.Dependency(PushoverApi.Object); + with.Dependency(NotificationService.Object); + with.Dependency(Analytics.Object); + with.Dependency(HeadphonesSettings.Object); + with.Dependency(Cache.Object); + with.Dependency(Log.Object); + with.Dependency(SlackApi.Object); + with.Dependency(LandingPageSettings.Object); + with.Dependency(SlackSettings.Object); + with.Dependency(ScheduledJobsSettingsMock.Object); + with.Dependency(RecorderMock.Object); + with.RootPathProvider(); + with.RequestStartup((container, pipelines, context) => + { + context.CurrentUser = new UserIdentity { UserName = "user", Claims = new List { "Admin" } }; + }); + }); + + Bootstrapper.WithSession(new Dictionary()); + } + + [Test] + public void RequestAuthTokenTestNewSettings() + { + var browser = new Browser(Bootstrapper); + + var result = browser.Post("/admin/requestauth", with => + { + with.HttpRequest(); + with.Header("Accept", "application/json"); + with.FormValue("username", "Username1"); + with.FormValue("password", "Password1"); + + }); + + Assert.That(HttpStatusCode.OK, Is.EqualTo(result.StatusCode)); + + var body = JsonConvert.DeserializeObject(result.Body.AsString()); + Assert.That(body.Result, Is.EqualTo(true)); + PlexMock.Verify(x => x.SignIn("Username1", "Password1"), Times.Once); + PlexSettingsMock.Verify(x => x.SaveSettings(It.IsAny()), Times.Once); + } + + [Test] + public void RequestAuthTokenTestEmptyCredentials() + { + var browser = new Browser(Bootstrapper); + + var result = browser.Post("/admin/requestauth", with => + { + with.HttpRequest(); + with.Header("Accept", "application/json"); + with.FormValue("username", string.Empty); + with.FormValue("password", "Password1"); + + }); + + Assert.That(HttpStatusCode.OK, Is.EqualTo(result.StatusCode)); + + var body = JsonConvert.DeserializeObject(result.Body.AsString()); + Assert.That(body.Result, Is.EqualTo(false)); + Assert.That(body.Message, Is.Not.Empty); + + PlexMock.Verify(x => x.SignIn("Username1", "Password1"), Times.Never); + AuthMock.Verify(x => x.GetSettings(), Times.Never); + AuthMock.Verify(x => x.SaveSettings(It.IsAny()), Times.Never); + } + + [Test] + public void RequestAuthTokenTesPlexSignInFail() + { + var browser = new Browser(Bootstrapper); + + var result = browser.Post("/admin/requestauth", with => + { + with.HttpRequest(); + with.Header("Accept", "application/json"); + with.FormValue("username", "Badusername"); + with.FormValue("password", "Password1"); + + }); + + Assert.That(HttpStatusCode.OK, Is.EqualTo(result.StatusCode)); + + var body = JsonConvert.DeserializeObject(result.Body.AsString()); + Assert.That(body.Result, Is.EqualTo(false)); + Assert.That(body.Message, Is.Not.Empty); + + PlexMock.Verify(x => x.SignIn("Badusername", "Password1"), Times.Once); + AuthMock.Verify(x => x.GetSettings(), Times.Never); + AuthMock.Verify(x => x.SaveSettings(It.IsAny()), Times.Never); + } + + [Test] + public void RequestAuthTokenTestExistingSettings() + { + AuthMock.Setup(x => x.GetSettings()).Returns(() => null); + var browser = new Browser(Bootstrapper); + + var result = browser.Post("/admin/requestauth", with => + { + with.HttpRequest(); + with.Header("Accept", "application/json"); + with.FormValue("username", "Username1"); + with.FormValue("password", "Password1"); + + }); + + Assert.That(HttpStatusCode.OK, Is.EqualTo(result.StatusCode)); + + var body = JsonConvert.DeserializeObject(result.Body.AsString()); + Assert.That(body.Result, Is.EqualTo(true)); + + PlexMock.Verify(x => x.SignIn("Username1", "Password1"), Times.Once); + } + + [Test] + public void GetUsersSuccessfully() + { + var users = new PlexFriends { User = new[] { new UserFriends { Title = "abc2" }, } }; + PlexMock.Setup(x => x.GetUsers(It.IsAny())).Returns(users); + var browser = new Browser(Bootstrapper); + + var result = browser.Get("/admin/getusers", with => + { + with.HttpRequest(); + with.Header("Accept", "application/json"); + with.FormValue("username", "Username1"); + with.FormValue("password", "Password1"); + + }); + + Assert.That(HttpStatusCode.OK, Is.EqualTo(result.StatusCode)); + + + var body = JsonConvert.DeserializeObject(result.Body.AsString()); + var user = body["users"]; + Assert.That(body, Is.Not.Null); + Assert.That(user.ToString().Contains("abc"), Is.True); + + PlexMock.Verify(x => x.GetUsers(It.IsAny()), Times.Once); + } + + [Test] + public void GetUsersReturnsNoUsers() + { + var users = new PlexFriends(); + PlexMock.Setup(x => x.GetUsers(It.IsAny())).Returns(users); + var browser = new Browser(Bootstrapper); + + var result = browser.Get("/admin/getusers", with => + { + with.HttpRequest(); + with.Header("Accept", "application/json"); + with.FormValue("username", "Username1"); + with.FormValue("password", "Password1"); + + + }); + + Assert.That(HttpStatusCode.OK, Is.EqualTo(result.StatusCode)); + + var body = JsonConvert.DeserializeObject(result.Body.AsString()); + Assert.That(body, Is.Not.Null); + Assert.That(string.IsNullOrWhiteSpace(body), Is.True); + + PlexMock.Verify(x => x.GetUsers(It.IsAny()), Times.Once); + } + + [Test] + public void GetUsersReturnsNull() + { + PlexMock.Setup(x => x.GetUsers(It.IsAny())).Returns(() => null); + var browser = new Browser(Bootstrapper); + + var result = browser.Get("/admin/getusers", with => + { + with.HttpRequest(); + with.Header("Accept", "application/json"); + with.FormValue("username", "Username1"); + with.FormValue("password", "Password1"); + + }); + + Assert.That(HttpStatusCode.OK, Is.EqualTo(result.StatusCode)); + + var body = JsonConvert.DeserializeObject(result.Body.AsString()); + Assert.That(body, Is.Not.Null); + Assert.That(string.IsNullOrWhiteSpace(body), Is.True); + + PlexMock.Verify(x => x.GetUsers(It.IsAny()), Times.Once); + } + + [Test] + public void GetUsersTokenIsNull() + { + PlexSettingsMock.Setup(x => x.GetSettings()).Returns(new PlexSettings()); + var browser = new Browser(Bootstrapper); + + var result = browser.Get("/admin/getusers", with => + { + with.HttpRequest(); + with.Header("Accept", "application/json"); + with.FormValue("username", "Username1"); + with.FormValue("password", "Password1"); + + }); + + Assert.That(HttpStatusCode.OK, Is.EqualTo(result.StatusCode)); + + var body = JsonConvert.DeserializeObject(result.Body.AsString()); + var user = (string)body["users"]; + Assert.That(body, Is.Not.Null); + Assert.That(string.IsNullOrWhiteSpace(user), Is.True); + + PlexMock.Verify(x => x.GetUsers(It.IsAny()), Times.Never); + } + } +} \ No newline at end of file diff --git a/PlexRequests.UI.Tests/PlexRequests.UI.Tests.csproj b/PlexRequests.UI.Tests/PlexRequests.UI.Tests.csproj index a84c00082..12ac1f175 100644 --- a/PlexRequests.UI.Tests/PlexRequests.UI.Tests.csproj +++ b/PlexRequests.UI.Tests/PlexRequests.UI.Tests.csproj @@ -107,6 +107,7 @@ + diff --git a/PlexRequests.UI.Tests/SearchModuleTests.cs b/PlexRequests.UI.Tests/SearchModuleTests.cs new file mode 100644 index 000000000..cffa9487d --- /dev/null +++ b/PlexRequests.UI.Tests/SearchModuleTests.cs @@ -0,0 +1,164 @@ +#region Copyright +// /************************************************************************ +// Copyright (c) 2016 Jamie Rees +// File: SearchModuleTests.cs +// Created By: Jamie Rees +// +// Permission is hereby granted, free of charge, to any person obtaining +// a copy of this software and associated documentation files (the +// "Software"), to deal in the Software without restriction, including +// without limitation the rights to use, copy, modify, merge, publish, +// distribute, sublicense, and/or sell copies of the Software, and to +// permit persons to whom the Software is furnished to do so, subject to +// the following conditions: +// +// The above copyright notice and this permission notice shall be +// included in all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE +// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION +// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +// ************************************************************************/ +#endregion + +using System; +using System.Collections.Generic; +using System.Linq; +using Moq; +using NUnit.Framework; +using PlexRequests.Api.Interfaces; +using PlexRequests.Api.Models.Plex; +using PlexRequests.Core; +using PlexRequests.Core.SettingModels; +using PlexRequests.Helpers; +using PlexRequests.Helpers.Analytics; +using PlexRequests.Services.Interfaces; +using PlexRequests.Services.Jobs; +using PlexRequests.Store; +using PlexRequests.Store.Models; +using PlexRequests.Store.Repository; +using PlexRequests.UI.Modules; +using Ploeh.AutoFixture; + +namespace PlexRequests.UI.Tests +{ + [TestFixture] + public class SearchModuleTests + { + private Mock> _headphonesSettings; + private Mock _notificationService; + private Mock> _sickRageSettingsMock; + private Mock _cpApi; + private Mock> _sonarrSettingsMock; + private Mock _sonarrApiMock; + private Mock> _plexSettingsMock; + private Mock> _cpMock; + private Mock> _plexRequestMock; + private Mock> _authMock; + private Mock _analytics; + private Mock _availabilityMock; + private Mock _rServiceMock; + private Mock _srApi; + private Mock _music; + private Mock _hpAPi; + private Mock _cpCache; + private Mock _sonarrCache; + private Mock _srCache; + private Mock _plexApi; + private Mock> _userRepo; + private Mock> _emailSettings; + private Mock _issueService; + private Mock _cache; + private Mock> RequestLimitRepo { get; set; } + private SearchModule Search { get; set; } + private readonly Fixture F = new Fixture(); + + [Test] + public void CheckNoRequestLimitTest() + { + var settings = new PlexRequestSettings { AlbumWeeklyRequestLimit = 0, MovieWeeklyRequestLimit = 2, TvWeeklyRequestLimit = 0 }; + var result = Search.CheckRequestLimit(settings, RequestType.Movie).Result; + + Assert.That(result, Is.True); + RequestLimitRepo.Verify(x => x.GetAllAsync(), Times.Once); + } + + [TestCaseSource(nameof(MovieLimitData))] + public bool CheckMovieLimitTest(int requestCount) + { + var users = F.CreateMany().ToList(); + users.Add(new RequestLimit { Username = "", RequestCount = requestCount, RequestType = RequestType.Movie}); + RequestLimitRepo.Setup(x => x.GetAllAsync()).ReturnsAsync(users); + var settings = new PlexRequestSettings { AlbumWeeklyRequestLimit = 0, MovieWeeklyRequestLimit = 5, TvWeeklyRequestLimit = 0 }; + var result = Search.CheckRequestLimit(settings, RequestType.Movie).Result; + + RequestLimitRepo.Verify(x => x.GetAllAsync(), Times.Once); + + return result; + } + + private static IEnumerable MovieLimitData + { + get + { + yield return new TestCaseData(1).Returns(true).SetName("1 Request of 5"); + yield return new TestCaseData(2).Returns(true).SetName("2 Request of 5"); + yield return new TestCaseData(3).Returns(true).SetName("3 Request of 5"); + yield return new TestCaseData(4).Returns(true).SetName("4 Request of 5"); + yield return new TestCaseData(5).Returns(false).SetName("5 Request of 5"); + yield return new TestCaseData(6).Returns(false).SetName("6 Request of 5"); + yield return new TestCaseData(0).Returns(true).SetName("0 Request of 5"); + } + } + + [SetUp] + public void Setup() + { + _authMock = new Mock>(); + _plexRequestMock = new Mock>(); + _plexRequestMock.Setup(x => x.GetSettings()).Returns(new PlexRequestSettings()); + _cpMock = new Mock>(); + _plexSettingsMock = new Mock>(); + _sonarrApiMock = new Mock(); + _sonarrSettingsMock = new Mock>(); + _cpApi = new Mock(); + _sickRageSettingsMock = new Mock>(); + _notificationService = new Mock(); + _headphonesSettings = new Mock>(); + _cache = new Mock(); + + _analytics = new Mock(); + _availabilityMock = new Mock(); + _rServiceMock = new Mock(); + _srApi = new Mock(); + _music = new Mock(); + _hpAPi = new Mock(); + _cpCache = new Mock(); + _sonarrCache = new Mock(); + _srCache = new Mock(); + _plexApi = new Mock(); + _userRepo = new Mock>(); + RequestLimitRepo = new Mock>(); + _emailSettings = new Mock>(); + _issueService = new Mock(); + CreateModule(); + } + + private void CreateModule() + { + Search = new SearchModule(_cache.Object, _cpMock.Object, _plexRequestMock.Object, _availabilityMock.Object, + _rServiceMock.Object, _sonarrApiMock.Object, _sonarrSettingsMock.Object, + _sickRageSettingsMock.Object, _cpApi.Object, _srApi.Object, _notificationService.Object, + _music.Object, _hpAPi.Object, _headphonesSettings.Object, _cpCache.Object, _sonarrCache.Object, + _srCache.Object, _plexApi.Object, _plexSettingsMock.Object, _authMock.Object, + _userRepo.Object, _emailSettings.Object, _issueService.Object, _analytics.Object, RequestLimitRepo.Object); + } + + + + } +} \ No newline at end of file diff --git a/PlexRequests.UI/Modules/BaseModule.cs b/PlexRequests.UI/Modules/BaseModule.cs index d0aa8006c..88e252daa 100644 --- a/PlexRequests.UI/Modules/BaseModule.cs +++ b/PlexRequests.UI/Modules/BaseModule.cs @@ -122,11 +122,11 @@ namespace PlexRequests.UI.Modules { get { - if (Context.CurrentUser == null) + if (Context?.CurrentUser == null) { return false; } - var claims = Context.CurrentUser.Claims.ToList(); + var claims = Context?.CurrentUser.Claims.ToList(); return claims.Contains(UserClaims.Admin) || claims.Contains(UserClaims.PowerUser); } } diff --git a/PlexRequests.UI/Modules/SearchModule.cs b/PlexRequests.UI/Modules/SearchModule.cs index 801f1ed01..d100e2897 100644 --- a/PlexRequests.UI/Modules/SearchModule.cs +++ b/PlexRequests.UI/Modules/SearchModule.cs @@ -873,7 +873,7 @@ namespace PlexRequests.UI.Modules return Response.AsJson(seasons); } - private async Task CheckRequestLimit(PlexRequestSettings s, RequestType type) + public async Task CheckRequestLimit(PlexRequestSettings s, RequestType type) { if (IsAdmin) return true; @@ -895,7 +895,7 @@ namespace PlexRequests.UI.Modules return true; } - return requestLimit >= usersLimit.RequestCount; + return requestLimit > usersLimit.RequestCount; } private int GetRequestLimitForType(RequestType type, PlexRequestSettings s)