Skip to content

AdminController: adding unit tests #610

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 13 commits into from
Mar 31, 2016

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,12 @@ private static void SetFakeHttpContextIfNotAlreadySet(Controller controller)
controller.SetFakeHttpContext();
}

public static Mock<IUrlHelper> SetFakeIUrlHelper(this Controller controller)
{
controller.Url = new Mock<IUrlHelper>().Object;
return Mock.Get(controller.Url);
}

private static HttpContext FakeHttpContext()
{
var mockHttpContext = new Mock<HttpContext>();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using System.Collections.Generic;
using System.Linq;
using Xunit;
using AllReady.Extensions;

namespace AllReady.UnitTest.Extensions
{
Expand Down
30 changes: 30 additions & 0 deletions AllReadyApp/Web-App/AllReady.UnitTest/SelectListItemComparer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using System.Collections.Generic;
using Microsoft.AspNet.Mvc.Rendering;

namespace AllReady.UnitTest
{
public class SelectListItemComparer : IEqualityComparer<SelectListItem>
{
public bool Equals(SelectListItem x, SelectListItem y)
{
if (ReferenceEquals(x, y))
return true;

if (ReferenceEquals(x, null) || ReferenceEquals(y, null))
return false;

return x.Text.Equals(y.Text) && x.Value.Equals(y.Value);
}

public int GetHashCode(SelectListItem obj)
{
if (ReferenceEquals(obj, null))
return 0;

var hashText = obj.Text?.GetHashCode() ?? 0;
var hashValue = obj.Value.GetHashCode();

return hashText ^ hashValue;
}
}
}
66 changes: 31 additions & 35 deletions AllReadyApp/Web-App/AllReady/Controllers/AccountController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ public IActionResult Login(string returnUrl = null)
public async Task<IActionResult> Login(LoginViewModel model, string returnUrl = null)
{
ViewData["ReturnUrl"] = returnUrl;

if (ModelState.IsValid)
{
// Require admin users to have a confirmed email before they can log on.
Expand All @@ -77,19 +78,20 @@ public async Task<IActionResult> Login(LoginViewModel model, string returnUrl =
{
return RedirectToLocal(returnUrl, user);
}

if (result.RequiresTwoFactor)
{
return RedirectToAction(nameof(AdminController.SendCode), new { ReturnUrl = returnUrl, RememberMe = model.RememberMe });
}

if (result.IsLockedOut)
{
return View("Lockout");
}
else
{
ModelState.AddModelError(string.Empty, "Invalid login attempt.");
return View(model);
}

ModelState.AddModelError(string.Empty, "Invalid login attempt.");

return View(model);
}

// If we got this far, something failed, redisplay form
Expand Down Expand Up @@ -123,10 +125,9 @@ public async Task<IActionResult> Register(RegisterViewModel model)
var result = await _userManager.CreateAsync(user, model.Password);
if (result.Succeeded)
{
// For more information on how to enable account confirmation and password reset please visit http://go.microsoft.com/fwlink/?LinkID=532713
// Send an email with this link
var code = await _userManager.GenerateEmailConfirmationTokenAsync(user);
var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: HttpContext.Request.Scheme);
var token = await _userManager.GenerateEmailConfirmationTokenAsync(user);
var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, token = token }, protocol: HttpContext.Request.Scheme);
await _emailSender.SendEmailAsync(model.Email, "Confirm your allReady account",
"Please confirm your allReady account by clicking this link: <a href=\"" + callbackUrl + "\">link</a>");
await _userManager.AddClaimAsync(user, new Claim(Security.ClaimTypes.ProfileIncomplete, "NewUser"));
Expand All @@ -153,27 +154,28 @@ public async Task<IActionResult> LogOff()
// GET: /Account/ConfirmEmail
[HttpGet]
[AllowAnonymous]
public async Task<IActionResult> ConfirmEmail(string userId, string code)
public async Task<IActionResult> ConfirmEmail(string userId, string token)
{
if (userId == null || code == null)
if (userId == null || token == null)
{
return View("Error");
}

var user = await _userManager.FindByIdAsync(userId);
if (user == null)
{
return View("Error");
}
var result = await _userManager.ConfirmEmailAsync(user, code);

var result = await _userManager.ConfirmEmailAsync(user, token);

if (result.Succeeded && user.IsProfileComplete())
{
await _mediator.SendAsync(new RemoveUserProfileIncompleteClaimCommand { UserId = user.Id });
if (User.IsSignedIn())
{
await _signInManager.RefreshSignInAsync(user);
}
}

return View(result.Succeeded ? "ConfirmEmail" : "Error");
}

Expand Down Expand Up @@ -235,18 +237,22 @@ public async Task<IActionResult> ResetPassword(ResetPasswordViewModel model)
{
return View(model);
}

var user = await _userManager.FindByNameAsync(model.Email);
if (user == null)
{
// Don't reveal that the user does not exist
return RedirectToAction(nameof(ResetPasswordConfirmation), "Account");
}

var result = await _userManager.ResetPasswordAsync(user, model.Code, model.Password);
if (result.Succeeded)
{
return RedirectToAction(nameof(ResetPasswordConfirmation), "Account");
}

AddErrors(result);

return View();
}

Expand All @@ -259,7 +265,6 @@ public IActionResult ResetPasswordConfirmation()
return View();
}


//
// POST: /Account/ExternalLogin
[HttpPost]
Expand Down Expand Up @@ -294,13 +299,12 @@ public async Task<IActionResult> ExternalLoginCallback(string returnUrl = null)
var user = await _mediator.SendAsync(new ApplicationUserQuery { UserName = email });
return RedirectToLocal(returnUrl, user);
}
else
{
// If the user does not have an account, then ask the user to create an account.
ViewData["ReturnUrl"] = returnUrl;
ViewData["LoginProvider"] = info.LoginProvider;
return View("ExternalLoginConfirmation", new ExternalLoginConfirmationViewModel { Email = email });
}

// If the user does not have an account, then ask the user to create an account.
ViewData["ReturnUrl"] = returnUrl;
ViewData["LoginProvider"] = info.LoginProvider;

return View("ExternalLoginConfirmation", new ExternalLoginConfirmationViewModel { Email = email });
}

//
Expand Down Expand Up @@ -351,8 +355,6 @@ public async Task<IActionResult> ExternalLoginConfirmation(ExternalLoginConfirma
return View(model);
}



#region Helpers

private void AddErrors(IdentityResult result)
Expand All @@ -363,11 +365,6 @@ private void AddErrors(IdentityResult result)
}
}

private async Task<ApplicationUser> GetCurrentUserAsync()
{
return await _userManager.FindByIdAsync(HttpContext.User.GetUserId());
}

private IActionResult RedirectToLocal(string returnUrl, ApplicationUser user)
{
if (Url.IsLocalUrl(returnUrl))
Expand All @@ -379,16 +376,15 @@ private IActionResult RedirectToLocal(string returnUrl, ApplicationUser user)
{
return RedirectToAction(nameof(SiteController.Index), "Site", new { area = "Admin" });
}
else if (user.IsUserType(UserType.OrgAdmin))
{
return base.RedirectToAction(nameof(Areas.Admin.Controllers.CampaignController.Index), "Campaign", new { area = "Admin" });
}
else

if (user.IsUserType(UserType.OrgAdmin))
{
return RedirectToAction(nameof(HomeController.Index), "Home");
return RedirectToAction(nameof(Areas.Admin.Controllers.CampaignController.Index), "Campaign", new { area = "Admin" });
}

return RedirectToAction(nameof(HomeController.Index), "Home");
}

#endregion
}
}
}
Loading