Removed the service locator from the base classes and added in some Api tests

added all the tests back in!
This commit is contained in:
tidusjar 2016-05-19 17:02:46 +01:00
commit 84dc4515fd
21 changed files with 251 additions and 176 deletions

View file

@ -101,7 +101,7 @@ namespace PlexRequests.UI.Modules
INotificationService notify,
ISettingsService<HeadphonesSettings> headphones,
ISettingsService<LogSettings> 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();

View file

@ -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<PlexRequestSettings> pr) : base("apidocs", pr)
{
Get["/"] = x => Documentation();
}

View file

@ -54,7 +54,6 @@ namespace PlexRequests.UI.Modules
with.Notes("This returns a single request");
with.QueryParam<string>("apikey", "The Api Key found in the settings", true);
//with.QueryParam<int>("id", "The request id to return", true);
with.PathParam<int>("id");
with.Model<ApiModel<List<RequestedModel>>>();
});
@ -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<ApiModel<bool>>();
with.BodyParam<int>("The request ID to delete", true);
with.PathParam<int>("id");
with.QueryParam<string>("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.");
});

View file

@ -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<PlexRequestSettings> 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<int>();
var id = (int)x.id;
var apiModel = new ApiModel<bool>();
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";

View file

@ -44,7 +44,7 @@ namespace PlexRequests.UI.Modules
{
public ApplicationTesterModule(ICouchPotatoApi cpApi, ISonarrApi sonarrApi, IPlexApi plexApi,
ISettingsService<AuthenticationSettings> authSettings, ISickRageApi srApi, IHeadphonesApi hpApi) : base("test")
ISettingsService<AuthenticationSettings> authSettings, ISickRageApi srApi, IHeadphonesApi hpApi, ISettingsService<PlexRequestSettings> pr) : base("test", pr)
{
this.RequiresAuthentication();

View file

@ -48,7 +48,7 @@ namespace PlexRequests.UI.Modules
public ApprovalModule(IRequestService service, ISettingsService<CouchPotatoSettings> cpService, ICouchPotatoApi cpApi, ISonarrApi sonarrApi,
ISettingsService<SonarrSettings> sonarrSettings, ISickRageApi srApi, ISettingsService<SickRageSettings> srSettings,
ISettingsService<HeadphonesSettings> hpSettings, IHeadphonesApi hpApi) : base("approval")
ISettingsService<HeadphonesSettings> hpSettings, IHeadphonesApi hpApi, ISettingsService<PlexRequestSettings> pr) : base("approval", pr)
{
this.RequiresClaims(UserClaims.Admin, UserClaims.PowerUser);

View file

@ -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<PlexRequestSettings> s) : base(s)
{
Settings = s;
Before += (ctx) => CheckAuth();
}
protected BaseApiModule(string modulePath) : base(modulePath)
protected BaseApiModule(string modulePath, ISettingsService<PlexRequestSettings> s) : base(modulePath, s)
{
Settings = s;
Before += (ctx) => CheckAuth();
}
private ISettingsService<PlexRequestSettings> Settings { get; }
protected Response ReturnReponse(object result)
{
var queryString = (DynamicDictionary)Context.Request.Query;

View file

@ -75,20 +75,23 @@ namespace PlexRequests.UI.Modules
}
}
protected BaseAuthModule()
protected BaseAuthModule(ISettingsService<PlexRequestSettings> pr) : base(pr)
{
Service = pr;
Before += (ctx) => CheckAuth();
}
protected BaseAuthModule(string modulePath) : base(modulePath)
protected BaseAuthModule(string modulePath, ISettingsService<PlexRequestSettings> pr) : base(modulePath, pr)
{
Service = pr;
Before += (ctx) => CheckAuth();
}
private ISettingsService<PlexRequestSettings> Service { get; }
private Response CheckAuth()
{
var settings = Locator.Resolve<ISettingsService<PlexRequestSettings>>().GetSettings();
var settings = Service.GetSettings();
var baseUrl = settings.BaseUrl;
var redirectPath = string.IsNullOrEmpty(baseUrl) ? "~/userlogin" : $"~/{baseUrl}/userlogin";

View file

@ -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<PlexRequestSettings> Settings => Locator.Resolve<ISettingsService<PlexRequestSettings>>();
protected string BaseUrl { get; set; }
protected BaseModule()
protected BaseModule(ISettingsService<PlexRequestSettings> 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<PlexRequestSettings> settingsService)
{
var settings = Settings.GetSettings();
var settings = settingsService.GetSettings();
var baseUrl = settings.BaseUrl;
BaseUrl = baseUrl;

View file

@ -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<PlexRequestSettings> pr) : base(pr)
{
Get["/"] = parameters => Context.GetRedirect(!string.IsNullOrEmpty(BaseUrl) ? $"~/{BaseUrl}/search" : "~/search");

View file

@ -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<PlexRequestSettings> pr) : base(pr)
{
Get["/login"] = _ =>
{

View file

@ -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;

View file

@ -62,7 +62,7 @@ namespace PlexRequests.UI.Modules
ISettingsService<SickRageSettings> sickRageService, ICouchPotatoApi cpApi, ISickRageApi srApi,
INotificationService notify, IMusicBrainzApi mbApi, IHeadphonesApi hpApi, ISettingsService<HeadphonesSettings> hpService,
ICouchPotatoCacher cpCacher, ISonarrCacher sonarrCacher, ISickRageCacher sickRageCacher, IPlexApi plexApi,
ISettingsService<PlexSettings> plexService, ISettingsService<AuthenticationSettings> auth) : base("search")
ISettingsService<PlexSettings> plexService, ISettingsService<AuthenticationSettings> auth) : base("search", prSettings)
{
Auth = auth;
PlexService = plexService;

View file

@ -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<PlexRequestSettings> pr) : base("updatechecker", pr)
{
Cache = provider;

View file

@ -46,7 +46,7 @@ namespace PlexRequests.UI.Modules
{
public class UserLoginModule : BaseModule
{
public UserLoginModule(ISettingsService<AuthenticationSettings> auth, IPlexApi api) : base("userlogin")
public UserLoginModule(ISettingsService<AuthenticationSettings> auth, IPlexApi api, ISettingsService<PlexRequestSettings> pr) : base("userlogin", pr)
{
AuthService = auth;
Api = api;

View file

@ -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<PlexRequestSettings> pr) : base("usermanagement",pr)
{
this.RequiresClaims(UserClaims.Admin);
Get["/"] = x => Load();