#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 Moq; using Nancy; using Nancy.Testing; using Newtonsoft.Json; using NUnit.Framework; using PlexRequests.Api.Interfaces; using PlexRequests.Api.Models.Plex; using PlexRequests.Core; using PlexRequests.Core.SettingModels; using PlexRequests.UI.Models; using PlexRequests.UI.Modules; 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> EmailMock { get; set; } private Mock> PushbulletSettings { get; set; } private Mock PlexMock { get; set; } private Mock SonarrApiMock { get; set; } private Mock PushbulletApi { 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", username = "Username1" } }); PlexRequestMock = new Mock>(); CpMock = new Mock>(); PlexSettingsMock = new Mock>(); SonarrApiMock = new Mock(); SonarrSettingsMock = new Mock>(); EmailMock = new Mock>(); PushbulletApi = new Mock(); PushbulletSettings = 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.RootPathProvider(); with.RequestStartup((container, pipelines, context) => { context.CurrentUser = new UserIdentity { UserName = "user" }; }); }); 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 { Username = "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 = result.Body.AsString(); Assert.That(body, Is.Not.Null); Assert.That(body, Contains.Substring("abc2")); 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()); Assert.That(body, Is.Not.Null); Assert.That(string.IsNullOrWhiteSpace(body), Is.True); PlexMock.Verify(x => x.GetUsers(It.IsAny()), Times.Never); AuthMock.Verify(x => x.GetSettings(), Times.Once); } } }