diff --git a/PlexRequests.UI.Tests/IssuesModuleTests.cs b/PlexRequests.UI.Tests/IssuesModuleTests.cs new file mode 100644 index 000000000..411cebbf2 --- /dev/null +++ b/PlexRequests.UI.Tests/IssuesModuleTests.cs @@ -0,0 +1,149 @@ +#region Copyright +// /************************************************************************ +// Copyright (c) 2016 Jamie Rees +// File: IssuesModuleTests.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 System.Threading.Tasks; + +using Moq; + +using Nancy; +using Nancy.Testing; + +using Newtonsoft.Json; + +using NUnit.Framework; + +using PlexRequests.Core; +using PlexRequests.Core.Models; +using PlexRequests.Core.SettingModels; +using PlexRequests.Helpers; +using PlexRequests.Services.Interfaces; +using PlexRequests.UI.Models; +using PlexRequests.UI.Modules; + +using Ploeh.AutoFixture; + +namespace PlexRequests.UI.Tests +{ + [TestFixture] + public class IssuesModuleTests + { + [SetUp] + public void Setup() + { + var f = new Fixture(); + ModelList = f.CreateMany(); + PlexRequestMock = new Mock>(); + PlexRequestMock.Setup(x => x.GetSettings()).Returns(new PlexRequestSettings()); + PlexRequestMock.Setup(x => x.GetSettingsAsync()).Returns(Task.FromResult(new PlexRequestSettings())); + IssueServiceMock = new Mock(); + RequestServiceMock = new Mock(); + NotificationServiceMock = new Mock(); + IssueServiceMock.Setup(x => x.GetAllAsync()).Returns(Task.FromResult(ModelList)); + + Bootstrapper = new ConfigurableBootstrapper( + with => + { + with.Module(); + with.Dependency(PlexRequestMock.Object); + with.Dependency(IssueServiceMock.Object); + with.Dependency(RequestServiceMock.Object); + with.Dependency(NotificationServiceMock.Object); + with.RootPathProvider(); + }); + + Bootstrapper.WithSession(new Dictionary { { SessionKeys.UsernameKey, "abc" } }); + } + + private Mock> PlexRequestMock { get; set; } + private Mock IssueServiceMock { get; set; } + private Mock RequestServiceMock { get; set; } + private Mock NotificationServiceMock { get; set; } + private ConfigurableBootstrapper Bootstrapper { get; set; } + private IEnumerable ModelList { get; set; } + + private IEnumerable NonResolvedModel => ModelList.Where(x => x.IssueStatus != IssueStatus.ResolvedIssue); + + [Test] + public void GetIssuesNonAdminButAllCanSee() + { + var browser = new Browser(Bootstrapper); + var result = browser.Get( + "/issues/pending", + with => + { + with.HttpRequest(); + with.Header("Accept", "application/json"); + }); + + Assert.That(HttpStatusCode.OK, Is.EqualTo(result.StatusCode)); + Assert.That(result.Context.Request.Session[SessionKeys.UsernameKey], Is.EqualTo("abc")); + + var body = JsonConvert.DeserializeObject>(result.Body.AsString()); + + Assert.That(body.Count, Is.EqualTo(NonResolvedModel.Count())); + Assert.That(body[0].Title, Is.Not.Empty); + } + + [Test] + public void GetIssuesForAdmin() + { + Bootstrapper = new ConfigurableBootstrapper( + with => + { + with.Module(); + with.Dependency(PlexRequestMock.Object); + with.Dependency(IssueServiceMock.Object); + with.Dependency(RequestServiceMock.Object); + with.Dependency(NotificationServiceMock.Object); + with.RootPathProvider(); + with.RequestStartup( + (container, pipelines, context) => + { + context.CurrentUser = new UserIdentity() { Claims = new[] { UserClaims.Admin } }; + }); + }); + + Bootstrapper.WithSession(new Dictionary { { SessionKeys.UsernameKey, "abc" } }); + var browser = new Browser(Bootstrapper); + var result = browser.Get( + "/issues/pending", + with => + { + with.HttpRequest(); + with.Header("Accept", "application/json"); + }); + + Assert.That(HttpStatusCode.OK, Is.EqualTo(result.StatusCode)); + Assert.That(result.Context.Request.Session[SessionKeys.UsernameKey], Is.EqualTo("abc")); + + var body = JsonConvert.DeserializeObject>(result.Body.AsString()); + Assert.That(body.Count, Is.EqualTo(NonResolvedModel.Count())); + Assert.That(body[0].Title, Is.Not.Empty); + } + } +} \ 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 5ec37a74a..95ac4be3a 100644 --- a/PlexRequests.UI.Tests/PlexRequests.UI.Tests.csproj +++ b/PlexRequests.UI.Tests/PlexRequests.UI.Tests.csproj @@ -105,6 +105,7 @@ + diff --git a/PlexRequests.UI/Modules/IssuesModule.cs b/PlexRequests.UI/Modules/IssuesModule.cs index 82cb1d2f0..9e10ac156 100644 --- a/PlexRequests.UI/Modules/IssuesModule.cs +++ b/PlexRequests.UI/Modules/IssuesModule.cs @@ -317,6 +317,8 @@ namespace PlexRequests.UI.Modules /// Filters the issues. Checks to see if we have set UsersCanViewOnlyOwnIssues in the database and filters upon the user logged in and that setting. /// /// The issues. + /// if set to true [show resolved]. + /// private async Task> FilterIssuesAsync(IEnumerable issues, bool showResolved = false) { var settings = await PlexRequestSettings.GetSettingsAsync();