Added user logout method and unit tests to cover it

pull/13/head
tidusjar 8 years ago
parent 55560611e8
commit 759540c837

@ -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.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 PlexRequests.Core;
using PlexRequests.UI.Models;
namespace PlexRequests.UI.Modules
{
public class LoginModule : BaseModule
public class LoginModule : NancyModule
{
public LoginModule()
{
@ -54,17 +55,21 @@ namespace PlexRequests.UI.Modules
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)
{
return Context.GetRedirect("~/login?error=true&username=" + (string)Request.Form.Username);
return Context.GetRedirect("~/login?error=true&username=" + username);
}
DateTime? expiry = null;
if (Request.Form.RememberMe.HasValue)
{
expiry = DateTime.Now.AddDays(7);
}
Session[SessionKeys.UsernameKey] = username;
return this.LoginAndRedirect(userId.Value, expiry);
};

@ -27,6 +27,7 @@
using System.Linq;
using Nancy;
using Nancy.Extensions;
using Nancy.Responses.Negotiation;
using PlexRequests.Api.Interfaces;
@ -46,6 +47,7 @@ namespace PlexRequests.UI.Modules
Api = api;
Get["/"] = _ => Index();
Post["/"] = x => LoginUser();
Get["/logout"] = x => Logout();
}
private ISettingsService<AuthenticationSettings> AuthService { get; }
@ -104,6 +106,15 @@ namespace PlexRequests.UI.Modules
: 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)
{
var users = Api.GetUsers(authToken);

@ -1,4 +1,6 @@
@using Nancy.Security
@using Nancy.Session
@using PlexRequests.UI.Models
@inherits Nancy.ViewEngines.Razor.NancyRazorViewBase
<html>
<head>
@ -42,13 +44,16 @@
<ul class="nav navbar-nav navbar-right">
@if (!Context.CurrentUser.IsAuthenticated())
{
<li><a href="/login">Login</a></li>
<li><a href="/login">Admin</a></li>
}
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>
</div>
</div>

Loading…
Cancel
Save