diff --git a/PlexRequests.UI.Tests/AdminModuleTests.cs b/PlexRequests.UI.Tests/AdminModuleTests.cs index 77d316deb..441e6fe53 100644 --- a/PlexRequests.UI.Tests/AdminModuleTests.cs +++ b/PlexRequests.UI.Tests/AdminModuleTests.cs @@ -25,6 +25,7 @@ // ************************************************************************/ #endregion using System.Collections.Generic; +using System.Linq; using Moq; @@ -32,6 +33,7 @@ using Nancy; using Nancy.Testing; using Newtonsoft.Json; +using Newtonsoft.Json.Linq; using NUnit.Framework; @@ -50,7 +52,6 @@ using PlexRequests.UI.Helpers; namespace PlexRequests.UI.Tests { [TestFixture] - [Ignore("Needs rework")] public class AdminModuleTests { private Mock> PlexRequestMock { get; set; } @@ -71,6 +72,7 @@ namespace PlexRequests.UI.Tests private Mock> LogRepo { get; set; } private Mock NotificationService { get; set; } private Mock Cache { get; set; } + private Mock> Log { get; set; } private ConfigurableBootstrapper Bootstrapper { get; set; } @@ -83,9 +85,10 @@ namespace PlexRequests.UI.Tests PlexMock = new Mock(); PlexMock.Setup(x => x.SignIn("Username1", "Password1")) - .Returns(new PlexAuthentication { user = new User { authentication_token = "abc", username = "Username1" } }); + .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(); @@ -101,6 +104,7 @@ namespace PlexRequests.UI.Tests NotificationService = new Mock(); HeadphonesSettings = new Mock>(); Cache = new Mock(); + Log = new Mock>(); Bootstrapper = new ConfigurableBootstrapper(with => { @@ -123,16 +127,11 @@ namespace PlexRequests.UI.Tests with.Dependency(NotificationService.Object); with.Dependency(HeadphonesSettings.Object); with.Dependency(Cache.Object); - with.ApplicationStartup( - (container, pipelines) => - { - var loc = ServiceLocator.Instance; - loc.SetContainer(container); - }); + with.Dependency(Log.Object); with.RootPathProvider(); with.RequestStartup((container, pipelines, context) => { - context.CurrentUser = new UserIdentity { UserName = "user" }; + context.CurrentUser = new UserIdentity { UserName = "user", Claims = new List {"Admin"} }; }); }); @@ -240,7 +239,7 @@ namespace PlexRequests.UI.Tests [Test] public void GetUsersSuccessfully() { - var users = new PlexFriends { User = new[] { new UserFriends { Username = "abc2" }, } }; + var users = new PlexFriends { User = new[] { new UserFriends { Title = "abc2" }, } }; PlexMock.Setup(x => x.GetUsers(It.IsAny())).Returns(users); var browser = new Browser(Bootstrapper); @@ -255,9 +254,11 @@ namespace PlexRequests.UI.Tests Assert.That(HttpStatusCode.OK, Is.EqualTo(result.StatusCode)); - var body = result.Body.AsString(); + + var body = JsonConvert.DeserializeObject(result.Body.AsString()); + var user = body["users"]; Assert.That(body, Is.Not.Null); - Assert.That(body, Contains.Substring("abc2")); + Assert.That(user.ToString().Contains("abc"), Is.True); PlexMock.Verify(x => x.GetUsers(It.IsAny()), Times.Once); AuthMock.Verify(x => x.GetSettings(), Times.Once); @@ -276,6 +277,7 @@ namespace PlexRequests.UI.Tests with.Header("Accept", "application/json"); with.FormValue("username", "Username1"); with.FormValue("password", "Password1"); + }); diff --git a/PlexRequests.UI.Tests/ApiModuleTests.cs b/PlexRequests.UI.Tests/ApiModuleTests.cs index 7208b8bfb..28ed4b52a 100644 --- a/PlexRequests.UI.Tests/ApiModuleTests.cs +++ b/PlexRequests.UI.Tests/ApiModuleTests.cs @@ -24,22 +24,37 @@ // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. // ************************************************************************/ #endregion +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.Linq; + +using FluentValidation; + using Moq; using Nancy; +using Nancy.Bootstrapper; using Nancy.Testing; +using Nancy.Validation; +using Nancy.Validation.FluentValidation; + +using Newtonsoft.Json; using NUnit.Framework; using PlexRequests.Core; using PlexRequests.Core.SettingModels; -using PlexRequests.UI.Helpers; +using PlexRequests.Store; +using PlexRequests.UI.Models; using PlexRequests.UI.Modules; +using PlexRequests.UI.Validators; + +using Ploeh.AutoFixture; namespace PlexRequests.UI.Tests { [TestFixture] - [Ignore("Locator :(")] public class ApiModuleTests { private ConfigurableBootstrapper Bootstrapper { get; set; } @@ -47,38 +62,147 @@ namespace PlexRequests.UI.Tests [SetUp] public void Setup() { + var requests = new Fixture().CreateMany(); var requestMock = new Mock(); var settingsMock = new Mock>(); + settingsMock.Setup(x => x.GetSettings()).Returns(new PlexRequestSettings {ApiKey = "api"}); + requestMock.Setup(x => x.GetAll()).Returns(requests); + requestMock.Setup(x => x.Get(1)).Returns(requests.FirstOrDefault()); + requestMock.Setup(x => x.Get(99)).Returns(new RequestedModel()); + requestMock.Setup(x => x.DeleteRequest(It.IsAny())); + Bootstrapper = new ConfigurableBootstrapper(with => { with.Module(); with.Dependency(requestMock.Object); with.Dependency(settingsMock.Object); - with.ApplicationStartup( - (c, a) => - { - var loc = ServiceLocator.Instance; - loc.SetContainer(c); - }); + with.RootPathProvider(); + with.ModelValidatorLocator( + new DefaultValidatorLocator( + new List() + { + new FluentValidationValidatorFactory( + new DefaultFluentAdapterFactory(new List()), + new List { new RequestedModelValidator() }) + })); }); - + } - [Test] - public void GetAllRequests() + private Action GetBrowser() { + return with => + { + with.HttpRequest(); + with.Header("Accept", "application/json"); + with.Query("apikey", "api"); + }; + } + [Test] + public void InvalidApiKey() + { var browser = new Browser(Bootstrapper); - var result = browser.Post("/api/requests", with => + var result = browser.Get("/api/requests", with => { with.HttpRequest(); with.Header("Accept", "application/json"); with.Query("apikey","a"); - }); Assert.That(HttpStatusCode.OK, Is.EqualTo(result.StatusCode)); + + var body = JsonConvert.DeserializeObject>>(result.Body.AsString()); + Assert.That(body.Error, Is.True); + Assert.That(body.ErrorMessage, Is.Not.Empty); + } + + [Test] + public void GetAllRequests() + { + var browser = new Browser(Bootstrapper); + + var result = browser.Get("/api/requests", GetBrowser()); + Assert.That(HttpStatusCode.OK, Is.EqualTo(result.StatusCode)); + + var body = JsonConvert.DeserializeObject>>(result.Body.AsString()); + Assert.That(body.Data, Is.Not.Null); + Assert.That(body.Data.Count, Is.GreaterThan(0)); + Assert.That(body.Error, Is.False); + Assert.That(body.ErrorMessage, Is.Null.Or.Empty); + } + + [Test] + public void GetSingleRequest() + { + var browser = new Browser(Bootstrapper); + + var result = browser.Get("/api/requests/1", GetBrowser()); + Assert.That(HttpStatusCode.OK, Is.EqualTo(result.StatusCode)); + + var body = JsonConvert.DeserializeObject>>(result.Body.AsString()); + Assert.That(body.Data, Is.Not.Null); + Assert.That(body.Data.Count, Is.EqualTo(1)); + Assert.That(body.Error, Is.False); + Assert.That(body.ErrorMessage, Is.Null.Or.Empty); + } + + [Test] + public void GetSingleRequestThatDoesntExist() + { + var browser = new Browser(Bootstrapper); + + var result = browser.Get("/api/requests/99", GetBrowser()); + Assert.That(HttpStatusCode.OK, Is.EqualTo(result.StatusCode)); + + var body = JsonConvert.DeserializeObject>>(result.Body.AsString()); + Assert.That(body.Data, Is.Not.Null); + Assert.That(body.Data.Count, Is.EqualTo(0)); + Assert.That(body.Error, Is.True); + Assert.That(body.ErrorMessage, Is.Not.Null.Or.Empty); + } + + [Test] + public void DeleteARequest() + { + var browser = new Browser(Bootstrapper); + + var result = browser.Delete("/api/requests/1", GetBrowser()); + Assert.That(HttpStatusCode.OK, Is.EqualTo(result.StatusCode)); + + var body = JsonConvert.DeserializeObject>(result.Body.AsString()); + Assert.That(body.Data, Is.True); + Assert.That(body.Error, Is.False); + Assert.That(body.ErrorMessage, Is.Null.Or.Empty); + } + + [Test] + public void DeleteARequestThatDoesNotExist() + { + var browser = new Browser(Bootstrapper); + + var result = browser.Delete("/api/requests/99", GetBrowser()); + Assert.That(HttpStatusCode.OK, Is.EqualTo(result.StatusCode)); + + var body = JsonConvert.DeserializeObject>(result.Body.AsString()); + Assert.That(body.Data, Is.False); + Assert.That(body.Error, Is.True); + Assert.That(body.ErrorMessage, Is.Not.Null.Or.Empty); + } + + [Test] + public void CreateAEmptyRequest() + { + var browser = new Browser(Bootstrapper); + + var result = browser.Post("/api/requests/", GetBrowser()); + Assert.That(HttpStatusCode.OK, Is.EqualTo(result.StatusCode)); + + var body = JsonConvert.DeserializeObject>(result.Body.AsString()); + Assert.That(body.Data, Is.Not.Null.Or.Empty); + Assert.That(body.Error, Is.True); + Assert.That(body.ErrorMessage, Is.Not.Null.Or.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 3c7db1543..083fd2286 100644 --- a/PlexRequests.UI.Tests/PlexRequests.UI.Tests.csproj +++ b/PlexRequests.UI.Tests/PlexRequests.UI.Tests.csproj @@ -46,6 +46,11 @@ ..\packages\FluentScheduler.3.1.46\lib\net40\FluentScheduler.dll True + + ..\packages\FluentValidation.6.2.1.0\lib\Net45\FluentValidation.dll + True + + ..\packages\Moq.4.2.1510.2205\lib\net40\Moq.dll True @@ -58,6 +63,10 @@ ..\packages\Nancy.Testing.1.4.1\lib\net40\Nancy.Testing.dll True + + ..\packages\Nancy.Validation.FluentValidation.1.4.1\lib\net40\Nancy.Validation.FluentValidation.dll + True + ..\packages\Nancy.Viewengines.Razor.1.4.3\lib\net40\Nancy.ViewEngines.Razor.dll True diff --git a/PlexRequests.UI.Tests/UserLoginModuleTests.cs b/PlexRequests.UI.Tests/UserLoginModuleTests.cs index d8e5b3e3a..f38f17ce0 100644 --- a/PlexRequests.UI.Tests/UserLoginModuleTests.cs +++ b/PlexRequests.UI.Tests/UserLoginModuleTests.cs @@ -47,11 +47,12 @@ using PlexRequests.UI.Modules; namespace PlexRequests.UI.Tests { [TestFixture] - [Ignore("Needs some work")] + //[Ignore("Needs some work")] public class UserLoginModuleTests { private Mock> AuthMock { get; set; } private Mock> PlexRequestMock { get; set; } + private ConfigurableBootstrapper Bootstrapper { get; set; } private Mock PlexMock { get; set; } [SetUp] @@ -60,6 +61,15 @@ namespace PlexRequests.UI.Tests AuthMock = new Mock>(); PlexMock = new Mock(); PlexRequestMock = new Mock>(); + PlexRequestMock.Setup(x => x.GetSettings()).Returns(new PlexRequestSettings()); + Bootstrapper = new ConfigurableBootstrapper(with => + { + with.Module(); + with.Dependency(PlexRequestMock.Object); + with.Dependency(AuthMock.Object); + with.Dependency(PlexMock.Object); + with.RootPathProvider(); + }); } [Test] @@ -68,21 +78,11 @@ namespace PlexRequests.UI.Tests var expectedSettings = new AuthenticationSettings { UserAuthentication = false, PlexAuthToken = "abc" }; AuthMock.Setup(x => x.GetSettings()).Returns(expectedSettings); - var bootstrapper = new ConfigurableBootstrapper(with => - { - with.Module(); - with.Dependency(AuthMock.Object); - with.Dependency(PlexMock.Object); - with.Dependency(PlexRequestMock.Object); - with.RootPathProvider(); - }); - var loc = ServiceLocator.Instance; - loc.SetContainer(TinyIoCContainer.Current); - bootstrapper.WithSession(new Dictionary()); + Bootstrapper.WithSession(new Dictionary()); - var browser = new Browser(bootstrapper); + var browser = new Browser(Bootstrapper); var result = browser.Post("/userlogin", with => { with.HttpRequest(); @@ -106,17 +106,10 @@ namespace PlexRequests.UI.Tests var expectedSettings = new AuthenticationSettings { UserAuthentication = false, PlexAuthToken = "abc" }; AuthMock.Setup(x => x.GetSettings()).Returns(expectedSettings); - var bootstrapper = new ConfigurableBootstrapper(with => - { - with.Module(); - with.Dependency(AuthMock.Object); - with.Dependency(PlexMock.Object); - with.RootPathProvider(); - }); - bootstrapper.WithSession(new Dictionary()); + Bootstrapper.WithSession(new Dictionary()); - var browser = new Browser(bootstrapper); + var browser = new Browser(Bootstrapper); var result = browser.Post("/userlogin", with => { with.HttpRequest(); @@ -143,7 +136,7 @@ namespace PlexRequests.UI.Tests { new UserFriends { - Username = "abc", + Title = "abc", }, } }; @@ -152,17 +145,9 @@ namespace PlexRequests.UI.Tests PlexMock.Setup(x => x.GetUsers(It.IsAny())).Returns(plexFriends); PlexMock.Setup(x => x.GetAccount(It.IsAny())).Returns(new PlexAccount()); - var bootstrapper = new ConfigurableBootstrapper(with => - { - with.Module(); - with.Dependency(AuthMock.Object); - with.Dependency(PlexMock.Object); - with.RootPathProvider(); - }); - - bootstrapper.WithSession(new Dictionary()); + Bootstrapper.WithSession(new Dictionary()); - var browser = new Browser(bootstrapper); + var browser = new Browser(Bootstrapper); var result = browser.Post("/userlogin", with => { with.HttpRequest(); @@ -199,17 +184,9 @@ namespace PlexRequests.UI.Tests PlexMock.Setup(x => x.GetUsers(It.IsAny())).Returns(plexFriends); PlexMock.Setup(x => x.GetAccount(It.IsAny())).Returns(new PlexAccount()); - var bootstrapper = new ConfigurableBootstrapper(with => - { - with.Module(); - with.Dependency(AuthMock.Object); - with.Dependency(PlexMock.Object); - with.RootPathProvider(); - }); - - bootstrapper.WithSession(new Dictionary()); + Bootstrapper.WithSession(new Dictionary()); - var browser = new Browser(bootstrapper); + var browser = new Browser(Bootstrapper); var result = browser.Post("/userlogin", with => { @@ -240,7 +217,7 @@ namespace PlexRequests.UI.Tests { new UserFriends { - Username = "abc", + Title = "abc", } } }; @@ -257,17 +234,9 @@ namespace PlexRequests.UI.Tests PlexMock.Setup(x => x.SignIn(It.IsAny(), It.IsAny())).Returns(plexAuth); PlexMock.Setup(x => x.GetAccount(It.IsAny())).Returns(new PlexAccount()); - var bootstrapper = new ConfigurableBootstrapper(with => - { - with.Module(); - with.Dependency(AuthMock.Object); - with.Dependency(PlexMock.Object); - with.RootPathProvider(); - }); - - bootstrapper.WithSession(new Dictionary()); + Bootstrapper.WithSession(new Dictionary()); - var browser = new Browser(bootstrapper); + var browser = new Browser(Bootstrapper); var result = browser.Post("/userlogin", with => { with.HttpRequest(); @@ -310,17 +279,10 @@ namespace PlexRequests.UI.Tests PlexMock.Setup(x => x.GetUsers(It.IsAny())).Returns(plexFriends); PlexMock.Setup(x => x.SignIn(It.IsAny(), It.IsAny())).Returns(plexAuth); - var bootstrapper = new ConfigurableBootstrapper(with => - { - with.Module(); - with.Dependency(AuthMock.Object); - with.Dependency(PlexMock.Object); - with.RootPathProvider(); - }); - - bootstrapper.WithSession(new Dictionary()); + + Bootstrapper.WithSession(new Dictionary()); - var browser = new Browser(bootstrapper); + var browser = new Browser(Bootstrapper); var result = browser.Post("/userlogin", with => { with.HttpRequest(); @@ -347,17 +309,9 @@ namespace PlexRequests.UI.Tests var expectedSettings = new AuthenticationSettings { UserAuthentication = false, DeniedUsers = "abc", PlexAuthToken = "abc" }; AuthMock.Setup(x => x.GetSettings()).Returns(expectedSettings); - var bootstrapper = new ConfigurableBootstrapper(with => - { - with.Module(); - with.Dependency(AuthMock.Object); - with.Dependency(PlexMock.Object); - with.RootPathProvider(); - }); - - bootstrapper.WithSession(new Dictionary()); + Bootstrapper.WithSession(new Dictionary()); - var browser = new Browser(bootstrapper); + var browser = new Browser(Bootstrapper); var result = browser.Post("/userlogin", with => { with.HttpRequest(); @@ -379,17 +333,9 @@ namespace PlexRequests.UI.Tests [Test] public void Logout() { - var bootstrapper = new ConfigurableBootstrapper(with => - { - with.Module(); - with.Dependency(AuthMock.Object); - with.Dependency(PlexMock.Object); - with.RootPathProvider(); - }); - - bootstrapper.WithSession(new Dictionary { { SessionKeys.UsernameKey, "abc" } }); + Bootstrapper.WithSession(new Dictionary { { SessionKeys.UsernameKey, "abc" } }); - var browser = new Browser(bootstrapper); + var browser = new Browser(Bootstrapper); var result = browser.Get("/userlogin/logout", with => { with.HttpRequest(); @@ -418,17 +364,9 @@ namespace PlexRequests.UI.Tests PlexMock.Setup(x => x.GetAccount(It.IsAny())).Returns(account); PlexMock.Setup(x => x.SignIn(It.IsAny(), It.IsAny())).Returns(new PlexAuthentication { user = new User { username = "Jamie" } }); - var bootstrapper = new ConfigurableBootstrapper(with => - { - with.Module(); - with.Dependency(AuthMock.Object); - with.Dependency(PlexMock.Object); - with.RootPathProvider(); - }); - - bootstrapper.WithSession(new Dictionary()); + Bootstrapper.WithSession(new Dictionary()); - var browser = new Browser(bootstrapper); + var browser = new Browser(Bootstrapper); var result = browser.Post("/userlogin", with => { with.HttpRequest(); @@ -473,17 +411,9 @@ namespace PlexRequests.UI.Tests PlexMock.Setup(x => x.SignIn(It.IsAny(), It.IsAny())).Returns(plexAuth); PlexMock.Setup(x => x.GetAccount(It.IsAny())).Returns(account); - var bootstrapper = new ConfigurableBootstrapper(with => - { - with.Module(); - with.Dependency(AuthMock.Object); - with.Dependency(PlexMock.Object); - with.RootPathProvider(); - }); - - bootstrapper.WithSession(new Dictionary()); + Bootstrapper.WithSession(new Dictionary()); - var browser = new Browser(bootstrapper); + var browser = new Browser(Bootstrapper); var result = browser.Post("/userlogin", with => { with.HttpRequest(); diff --git a/PlexRequests.UI.Tests/packages.config b/PlexRequests.UI.Tests/packages.config index 7de66863b..d4b45a263 100644 --- a/PlexRequests.UI.Tests/packages.config +++ b/PlexRequests.UI.Tests/packages.config @@ -3,10 +3,12 @@ + + diff --git a/PlexRequests.UI/Modules/AdminModule.cs b/PlexRequests.UI/Modules/AdminModule.cs index 38b830ead..56bc64df8 100644 --- a/PlexRequests.UI/Modules/AdminModule.cs +++ b/PlexRequests.UI/Modules/AdminModule.cs @@ -101,7 +101,7 @@ namespace PlexRequests.UI.Modules INotificationService notify, ISettingsService headphones, ISettingsService logs, - ICacheProvider cache) : base("admin") + ICacheProvider cache) : base("admin", prService) { PrService = prService; CpService = cpService; @@ -123,9 +123,8 @@ namespace PlexRequests.UI.Modules LogService = logs; Cache = cache; - #if DEBUG this.RequiresClaims(UserClaims.Admin); - #endif + Get["/"] = _ => Admin(); Get["/authentication"] = _ => Authentication(); diff --git a/PlexRequests.UI/Modules/ApiDocsModule.cs b/PlexRequests.UI/Modules/ApiDocsModule.cs index 61d4dde6f..94dbaeda5 100644 --- a/PlexRequests.UI/Modules/ApiDocsModule.cs +++ b/PlexRequests.UI/Modules/ApiDocsModule.cs @@ -27,11 +27,14 @@ using Nancy; using Nancy.Responses.Negotiation; +using PlexRequests.Core; +using PlexRequests.Core.SettingModels; + namespace PlexRequests.UI.Modules { public class ApiDocsModule : BaseModule { - public ApiDocsModule() : base("apidocs") + public ApiDocsModule(ISettingsService pr) : base("apidocs", pr) { Get["/"] = x => Documentation(); } diff --git a/PlexRequests.UI/Modules/ApiMetadataModule.cs b/PlexRequests.UI/Modules/ApiMetadataModule.cs index eb8ae6a52..6ea6ea8a5 100644 --- a/PlexRequests.UI/Modules/ApiMetadataModule.cs +++ b/PlexRequests.UI/Modules/ApiMetadataModule.cs @@ -54,7 +54,6 @@ namespace PlexRequests.UI.Modules with.Notes("This returns a single request"); with.QueryParam("apikey", "The Api Key found in the settings", true); - //with.QueryParam("id", "The request id to return", true); with.PathParam("id"); with.Model>>(); }); @@ -81,10 +80,10 @@ namespace PlexRequests.UI.Modules Describe["DeleteRequests"] = description => description.AsSwagger(with => { - with.ResourcePath("/requests"); + with.ResourcePath("/requests/{id}"); with.Summary("Deletes an existing request"); with.Model>(); - with.BodyParam("The request ID to delete", true); + with.PathParam("id"); with.QueryParam("apikey", "The Api Key found in the settings", true); with.Notes("Deletes an existing request. If the request doesn't exist we will return an error."); }); diff --git a/PlexRequests.UI/Modules/ApiModule.cs b/PlexRequests.UI/Modules/ApiModule.cs index eb4f34f84..e45981a9f 100644 --- a/PlexRequests.UI/Modules/ApiModule.cs +++ b/PlexRequests.UI/Modules/ApiModule.cs @@ -26,24 +26,26 @@ #endregion using System; using System.Collections.Generic; +using System.Diagnostics; using Nancy; using Nancy.ModelBinding; using PlexRequests.Core; +using PlexRequests.Core.SettingModels; using PlexRequests.Store; namespace PlexRequests.UI.Modules { public class ApiModule : BaseApiModule { - public ApiModule(IRequestService service) : base("api") + public ApiModule(IRequestService service, ISettingsService pr) : base("api", pr) { Get["GetRequests","/requests"] = x => GetRequests(); Get["GetRequest","/requests/{id}"] = x => GetSingleRequests(x); Post["PostRequests", "/requests"] = x => CreateRequest(); Put["PutRequests", "/requests"] = x => UpdateRequest(); - Delete["DeleteRequests", "/requests"] = x => DeleteRequest(); + Delete["DeleteRequests", "/requests/{id}"] = x => DeleteRequest(x); RequestService = service; } @@ -127,16 +129,15 @@ namespace PlexRequests.UI.Modules return ReturnReponse(apiModel); } - public Response DeleteRequest() + public Response DeleteRequest(dynamic x) { - var id = this.Bind(); - + var id = (int)x.id; var apiModel = new ApiModel(); try { var exisitingRequest = RequestService.Get(id); - if (exisitingRequest == null) + if (string.IsNullOrEmpty(exisitingRequest.Title)) { apiModel.Error = true; apiModel.ErrorMessage = $"The request id {id} does not exist"; diff --git a/PlexRequests.UI/Modules/ApplicationTesterModule.cs b/PlexRequests.UI/Modules/ApplicationTesterModule.cs index 55df3310f..69cf1575c 100644 --- a/PlexRequests.UI/Modules/ApplicationTesterModule.cs +++ b/PlexRequests.UI/Modules/ApplicationTesterModule.cs @@ -44,7 +44,7 @@ namespace PlexRequests.UI.Modules { public ApplicationTesterModule(ICouchPotatoApi cpApi, ISonarrApi sonarrApi, IPlexApi plexApi, - ISettingsService authSettings, ISickRageApi srApi, IHeadphonesApi hpApi) : base("test") + ISettingsService authSettings, ISickRageApi srApi, IHeadphonesApi hpApi, ISettingsService pr) : base("test", pr) { this.RequiresAuthentication(); diff --git a/PlexRequests.UI/Modules/ApprovalModule.cs b/PlexRequests.UI/Modules/ApprovalModule.cs index d57f31a1f..c0546a70c 100644 --- a/PlexRequests.UI/Modules/ApprovalModule.cs +++ b/PlexRequests.UI/Modules/ApprovalModule.cs @@ -48,7 +48,7 @@ namespace PlexRequests.UI.Modules public ApprovalModule(IRequestService service, ISettingsService cpService, ICouchPotatoApi cpApi, ISonarrApi sonarrApi, ISettingsService sonarrSettings, ISickRageApi srApi, ISettingsService srSettings, - ISettingsService hpSettings, IHeadphonesApi hpApi) : base("approval") + ISettingsService hpSettings, IHeadphonesApi hpApi, ISettingsService pr) : base("approval", pr) { this.RequiresClaims(UserClaims.Admin, UserClaims.PowerUser); diff --git a/PlexRequests.UI/Modules/BaseApiModule.cs b/PlexRequests.UI/Modules/BaseApiModule.cs index 9d2d023e3..4ed4b8031 100644 --- a/PlexRequests.UI/Modules/BaseApiModule.cs +++ b/PlexRequests.UI/Modules/BaseApiModule.cs @@ -30,6 +30,7 @@ using System.Linq; using Nancy; using Nancy.Validation; +using PlexRequests.Core; using PlexRequests.Core.SettingModels; using PlexRequests.Store; @@ -37,16 +38,20 @@ namespace PlexRequests.UI.Modules { public abstract class BaseApiModule : BaseModule { - protected BaseApiModule() + protected BaseApiModule(ISettingsService s) : base(s) { + Settings = s; Before += (ctx) => CheckAuth(); } - protected BaseApiModule(string modulePath) : base(modulePath) + protected BaseApiModule(string modulePath, ISettingsService s) : base(modulePath, s) { + Settings = s; Before += (ctx) => CheckAuth(); } + private ISettingsService Settings { get; } + protected Response ReturnReponse(object result) { var queryString = (DynamicDictionary)Context.Request.Query; diff --git a/PlexRequests.UI/Modules/BaseAuthModule.cs b/PlexRequests.UI/Modules/BaseAuthModule.cs index d640d4b21..0bbad4a02 100644 --- a/PlexRequests.UI/Modules/BaseAuthModule.cs +++ b/PlexRequests.UI/Modules/BaseAuthModule.cs @@ -75,20 +75,23 @@ namespace PlexRequests.UI.Modules } } - protected BaseAuthModule() + protected BaseAuthModule(ISettingsService pr) : base(pr) { + Service = pr; Before += (ctx) => CheckAuth(); } - protected BaseAuthModule(string modulePath) : base(modulePath) + protected BaseAuthModule(string modulePath, ISettingsService pr) : base(modulePath, pr) { + Service = pr; Before += (ctx) => CheckAuth(); } + private ISettingsService Service { get; } private Response CheckAuth() { - var settings = Locator.Resolve>().GetSettings(); + var settings = Service.GetSettings(); var baseUrl = settings.BaseUrl; var redirectPath = string.IsNullOrEmpty(baseUrl) ? "~/userlogin" : $"~/{baseUrl}/userlogin"; diff --git a/PlexRequests.UI/Modules/BaseModule.cs b/PlexRequests.UI/Modules/BaseModule.cs index 5d289a578..335fc16f5 100644 --- a/PlexRequests.UI/Modules/BaseModule.cs +++ b/PlexRequests.UI/Modules/BaseModule.cs @@ -28,19 +28,16 @@ using Nancy; using PlexRequests.Core; using PlexRequests.Core.SettingModels; -using PlexRequests.UI.Helpers; namespace PlexRequests.UI.Modules { public abstract class BaseModule : NancyModule { - protected ServiceLocator Locator => ServiceLocator.Instance; - protected ISettingsService Settings => Locator.Resolve>(); protected string BaseUrl { get; set; } - protected BaseModule() + protected BaseModule(ISettingsService settingsService) { - var settings = Settings.GetSettings(); + var settings = settingsService.GetSettings(); var baseUrl = settings.BaseUrl; BaseUrl = baseUrl; @@ -49,9 +46,9 @@ namespace PlexRequests.UI.Modules ModulePath = modulePath; } - protected BaseModule(string modulePath) + protected BaseModule(string modulePath, ISettingsService settingsService) { - var settings = Settings.GetSettings(); + var settings = settingsService.GetSettings(); var baseUrl = settings.BaseUrl; BaseUrl = baseUrl; diff --git a/PlexRequests.UI/Modules/IndexModule.cs b/PlexRequests.UI/Modules/IndexModule.cs index 46b54e098..81cdfa7e2 100644 --- a/PlexRequests.UI/Modules/IndexModule.cs +++ b/PlexRequests.UI/Modules/IndexModule.cs @@ -27,11 +27,14 @@ using Nancy; using Nancy.Extensions; +using PlexRequests.Core; +using PlexRequests.Core.SettingModels; + namespace PlexRequests.UI.Modules { public class IndexModule : BaseAuthModule { - public IndexModule() + public IndexModule(ISettingsService pr) : base(pr) { Get["/"] = parameters => Context.GetRedirect(!string.IsNullOrEmpty(BaseUrl) ? $"~/{BaseUrl}/search" : "~/search"); diff --git a/PlexRequests.UI/Modules/LoginModule.cs b/PlexRequests.UI/Modules/LoginModule.cs index 416291f48..14fbf49d0 100644 --- a/PlexRequests.UI/Modules/LoginModule.cs +++ b/PlexRequests.UI/Modules/LoginModule.cs @@ -34,13 +34,14 @@ using Nancy.Responses.Negotiation; using Nancy.Security; using PlexRequests.Core; +using PlexRequests.Core.SettingModels; using PlexRequests.UI.Models; namespace PlexRequests.UI.Modules { public class LoginModule : BaseModule { - public LoginModule() + public LoginModule(ISettingsService pr) : base(pr) { Get["/login"] = _ => { diff --git a/PlexRequests.UI/Modules/RequestsModule.cs b/PlexRequests.UI/Modules/RequestsModule.cs index 3886649e7..94e01a90b 100644 --- a/PlexRequests.UI/Modules/RequestsModule.cs +++ b/PlexRequests.UI/Modules/RequestsModule.cs @@ -59,7 +59,7 @@ namespace PlexRequests.UI.Modules ICouchPotatoApi cpApi, ISonarrApi sonarrApi, ISickRageApi sickRageApi, - ICacheProvider cache) : base("requests") + ICacheProvider cache) : base("requests", prSettings) { Service = service; PrSettings = prSettings; diff --git a/PlexRequests.UI/Modules/SearchModule.cs b/PlexRequests.UI/Modules/SearchModule.cs index 4ef08002b..62601aee8 100644 --- a/PlexRequests.UI/Modules/SearchModule.cs +++ b/PlexRequests.UI/Modules/SearchModule.cs @@ -62,7 +62,7 @@ namespace PlexRequests.UI.Modules ISettingsService sickRageService, ICouchPotatoApi cpApi, ISickRageApi srApi, INotificationService notify, IMusicBrainzApi mbApi, IHeadphonesApi hpApi, ISettingsService hpService, ICouchPotatoCacher cpCacher, ISonarrCacher sonarrCacher, ISickRageCacher sickRageCacher, IPlexApi plexApi, - ISettingsService plexService, ISettingsService auth) : base("search") + ISettingsService plexService, ISettingsService auth) : base("search", prSettings) { Auth = auth; PlexService = plexService; diff --git a/PlexRequests.UI/Modules/UpdateCheckerModule.cs b/PlexRequests.UI/Modules/UpdateCheckerModule.cs index 3d89ff2ff..f1b46a805 100644 --- a/PlexRequests.UI/Modules/UpdateCheckerModule.cs +++ b/PlexRequests.UI/Modules/UpdateCheckerModule.cs @@ -31,6 +31,7 @@ using Nancy; using NLog; using PlexRequests.Core; +using PlexRequests.Core.SettingModels; using PlexRequests.Helpers; using PlexRequests.UI.Models; @@ -38,7 +39,7 @@ namespace PlexRequests.UI.Modules { public class UpdateCheckerModule : BaseAuthModule { - public UpdateCheckerModule(ICacheProvider provider) : base("updatechecker") + public UpdateCheckerModule(ICacheProvider provider, ISettingsService pr) : base("updatechecker", pr) { Cache = provider; diff --git a/PlexRequests.UI/Modules/UserLoginModule.cs b/PlexRequests.UI/Modules/UserLoginModule.cs index a543287c1..d6acb5343 100644 --- a/PlexRequests.UI/Modules/UserLoginModule.cs +++ b/PlexRequests.UI/Modules/UserLoginModule.cs @@ -46,7 +46,7 @@ namespace PlexRequests.UI.Modules { public class UserLoginModule : BaseModule { - public UserLoginModule(ISettingsService auth, IPlexApi api) : base("userlogin") + public UserLoginModule(ISettingsService auth, IPlexApi api, ISettingsService pr) : base("userlogin", pr) { AuthService = auth; Api = api; diff --git a/PlexRequests.UI/Modules/UserManagementModule.cs b/PlexRequests.UI/Modules/UserManagementModule.cs index 2ea43e79e..b2d8a6a2d 100644 --- a/PlexRequests.UI/Modules/UserManagementModule.cs +++ b/PlexRequests.UI/Modules/UserManagementModule.cs @@ -1,23 +1,19 @@ -using System; +using System.Collections.Generic; using Nancy; -using Nancy.Authentication.Forms; -using Nancy.Extensions; using Nancy.Responses.Negotiation; using Nancy.Security; using PlexRequests.Core; -using PlexRequests.UI.Models; -using PlexRequests.UI.Modules; +using PlexRequests.Core.SettingModels; using PlexRequests.Helpers; -using System.Collections.Generic; - +using PlexRequests.UI.Models; -namespace PlexRequests.UI +namespace PlexRequests.UI.Modules { public class UserManagementModule : BaseModule { - public UserManagementModule() : base("usermanagement") + public UserManagementModule(ISettingsService pr) : base("usermanagement",pr) { this.RequiresClaims(UserClaims.Admin); Get["/"] = x => Load();