mirror of
https://github.com/Ombi-app/Ombi.git
synced 2025-07-08 06:00:50 -07:00
Added user logout method and unit tests to cover it
This commit is contained in:
parent
55560611e8
commit
759540c837
4 changed files with 51 additions and 6 deletions
|
@ -330,5 +330,29 @@ namespace PlexRequests.UI.Tests
|
||||||
PlexMock.Verify(x => x.SignIn(It.IsAny<string>(), It.IsAny<string>()), Times.Never);
|
PlexMock.Verify(x => x.SignIn(It.IsAny<string>(), It.IsAny<string>()), Times.Never);
|
||||||
PlexMock.Verify(x => x.GetUsers(It.IsAny<string>()), Times.Never);
|
PlexMock.Verify(x => x.GetUsers(It.IsAny<string>()), Times.Never);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void Logout()
|
||||||
|
{
|
||||||
|
var bootstrapper = new ConfigurableBootstrapper(with =>
|
||||||
|
{
|
||||||
|
with.Module<UserLoginModule>();
|
||||||
|
with.Dependency(AuthMock.Object);
|
||||||
|
with.Dependency(PlexMock.Object);
|
||||||
|
with.RootPathProvider<TestRootPathProvider>();
|
||||||
|
});
|
||||||
|
|
||||||
|
bootstrapper.WithSession(new Dictionary<string, object> { {SessionKeys.UsernameKey, "abc"} });
|
||||||
|
|
||||||
|
var browser = new Browser(bootstrapper);
|
||||||
|
var result = browser.Get("/userlogin/logout", with =>
|
||||||
|
{
|
||||||
|
with.HttpRequest();
|
||||||
|
with.Header("Accept", "application/json");
|
||||||
|
});
|
||||||
|
|
||||||
|
Assert.That(HttpStatusCode.SeeOther, Is.EqualTo(result.StatusCode));
|
||||||
|
Assert.That(result.Context.Request.Session[SessionKeys.UsernameKey], Is.Null);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -32,10 +32,11 @@ using Nancy.Authentication.Forms;
|
||||||
using Nancy.Extensions;
|
using Nancy.Extensions;
|
||||||
|
|
||||||
using PlexRequests.Core;
|
using PlexRequests.Core;
|
||||||
|
using PlexRequests.UI.Models;
|
||||||
|
|
||||||
namespace PlexRequests.UI.Modules
|
namespace PlexRequests.UI.Modules
|
||||||
{
|
{
|
||||||
public class LoginModule : BaseModule
|
public class LoginModule : NancyModule
|
||||||
{
|
{
|
||||||
public LoginModule()
|
public LoginModule()
|
||||||
{
|
{
|
||||||
|
@ -54,17 +55,21 @@ namespace PlexRequests.UI.Modules
|
||||||
|
|
||||||
Post["/login"] = x =>
|
Post["/login"] = x =>
|
||||||
{
|
{
|
||||||
var userId = UserMapper.ValidateUser((string)Request.Form.Username, (string)Request.Form.Password);
|
var username = (string)Request.Form.Username;
|
||||||
|
var password = (string)Request.Form.Password;
|
||||||
|
|
||||||
|
var userId = UserMapper.ValidateUser(username, password);
|
||||||
|
|
||||||
if (userId == null)
|
if (userId == null)
|
||||||
{
|
{
|
||||||
return Context.GetRedirect("~/login?error=true&username=" + (string)Request.Form.Username);
|
return Context.GetRedirect("~/login?error=true&username=" + username);
|
||||||
}
|
}
|
||||||
DateTime? expiry = null;
|
DateTime? expiry = null;
|
||||||
if (Request.Form.RememberMe.HasValue)
|
if (Request.Form.RememberMe.HasValue)
|
||||||
{
|
{
|
||||||
expiry = DateTime.Now.AddDays(7);
|
expiry = DateTime.Now.AddDays(7);
|
||||||
}
|
}
|
||||||
|
Session[SessionKeys.UsernameKey] = username;
|
||||||
return this.LoginAndRedirect(userId.Value, expiry);
|
return this.LoginAndRedirect(userId.Value, expiry);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -27,6 +27,7 @@
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
|
|
||||||
using Nancy;
|
using Nancy;
|
||||||
|
using Nancy.Extensions;
|
||||||
using Nancy.Responses.Negotiation;
|
using Nancy.Responses.Negotiation;
|
||||||
|
|
||||||
using PlexRequests.Api.Interfaces;
|
using PlexRequests.Api.Interfaces;
|
||||||
|
@ -46,6 +47,7 @@ namespace PlexRequests.UI.Modules
|
||||||
Api = api;
|
Api = api;
|
||||||
Get["/"] = _ => Index();
|
Get["/"] = _ => Index();
|
||||||
Post["/"] = x => LoginUser();
|
Post["/"] = x => LoginUser();
|
||||||
|
Get["/logout"] = x => Logout();
|
||||||
}
|
}
|
||||||
|
|
||||||
private ISettingsService<AuthenticationSettings> AuthService { get; }
|
private ISettingsService<AuthenticationSettings> AuthService { get; }
|
||||||
|
@ -104,6 +106,15 @@ namespace PlexRequests.UI.Modules
|
||||||
: new JsonResponseModel { Result = false, Message = "Incorrect User or Password"});
|
: new JsonResponseModel { Result = false, Message = "Incorrect User or Password"});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private Response Logout()
|
||||||
|
{
|
||||||
|
if (Session[SessionKeys.UsernameKey] != null)
|
||||||
|
{
|
||||||
|
Session.Delete(SessionKeys.UsernameKey);
|
||||||
|
}
|
||||||
|
return Context.GetRedirect("~/userlogin");
|
||||||
|
}
|
||||||
|
|
||||||
private bool CheckIfUserIsInPlexFriends(string username, string authToken)
|
private bool CheckIfUserIsInPlexFriends(string username, string authToken)
|
||||||
{
|
{
|
||||||
var users = Api.GetUsers(authToken);
|
var users = Api.GetUsers(authToken);
|
||||||
|
|
|
@ -1,4 +1,6 @@
|
||||||
@using Nancy.Security
|
@using Nancy.Security
|
||||||
|
@using Nancy.Session
|
||||||
|
@using PlexRequests.UI.Models
|
||||||
@inherits Nancy.ViewEngines.Razor.NancyRazorViewBase
|
@inherits Nancy.ViewEngines.Razor.NancyRazorViewBase
|
||||||
<html>
|
<html>
|
||||||
<head>
|
<head>
|
||||||
|
@ -42,13 +44,16 @@
|
||||||
<ul class="nav navbar-nav navbar-right">
|
<ul class="nav navbar-nav navbar-right">
|
||||||
@if (!Context.CurrentUser.IsAuthenticated())
|
@if (!Context.CurrentUser.IsAuthenticated())
|
||||||
{
|
{
|
||||||
<li><a href="/login">Login</a></li>
|
<li><a href="/login">Admin</a></li>
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
<li><a href="/logout">Logout</a></li>
|
<li><a href="/logout">Admin Logout</a></li>
|
||||||
|
}
|
||||||
|
@if (Context.Request.Session[SessionKeys.UsernameKey] != null)
|
||||||
|
{
|
||||||
|
<li><a href="/userlogin/logout">Logout</a></li>
|
||||||
}
|
}
|
||||||
|
|
||||||
</ul>
|
</ul>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue