|
@@ -8,6 +8,7 @@ using Microsoft.AspNetCore.Mvc.Routing;
|
|
|
using Microsoft.AspNetCore.Mvc.ViewFeatures;
|
|
|
using NSubstitute;
|
|
|
using NSubstitute.ExceptionExtensions;
|
|
|
+using System.Security.Claims;
|
|
|
using Website.Controllers;
|
|
|
using Website.Data;
|
|
|
using Website.Models.Auth;
|
|
@@ -146,5 +147,61 @@ namespace Website.Tests.Controllers {
|
|
|
result.Should().BeOfType<ViewResult>();
|
|
|
(result as ViewResult)?.Model.Should().BeEquivalentTo(expected);
|
|
|
}
|
|
|
+
|
|
|
+ [Fact]
|
|
|
+ public async Task UpdatePassword_WithConfirmPasswordNotMatching_RedirectsToIndexWithFailure() {
|
|
|
+ var authenticationProvider = Substitute.For<IAuthenticationProvider>();
|
|
|
+ var claimsPrincipal = Substitute.For<ClaimsPrincipal>();
|
|
|
+ claimsPrincipal.FindFirst(ClaimTypes.Name).Returns(new Claim(ClaimTypes.Name, "valid"));
|
|
|
+ var controller = new AccountController(authenticationProvider) {
|
|
|
+ ControllerContext = new ControllerContext {HttpContext = new DefaultHttpContext {User = claimsPrincipal}},
|
|
|
+ TempData = new TempDataDictionary(new DefaultHttpContext(), Substitute.For<ITempDataProvider>()) // TempData needs to be set up in a unit test
|
|
|
+ };
|
|
|
+
|
|
|
+ var request = new UpdatePasswordRequest {Username = "valid", OldPassword = "correct", NewPassword = "new", ConfirmPassword = "no match"};
|
|
|
+
|
|
|
+ var result = await controller.UpdatePassword(request);
|
|
|
+ result.Should().BeOfType<RedirectToActionResult>();
|
|
|
+ (result as RedirectToActionResult).ActionName.Should().Be("Index");
|
|
|
+ controller.TempData["updatePassword"].Should().Be(false);
|
|
|
+ }
|
|
|
+
|
|
|
+ [Fact]
|
|
|
+ public async Task UpdatePassword_WithValidCredentials_RedirectsToIndexWithSuccess() {
|
|
|
+ var authenticationProvider = Substitute.For<IAuthenticationProvider>();
|
|
|
+ var claimsPrincipal = Substitute.For<ClaimsPrincipal>();
|
|
|
+ claimsPrincipal.FindFirst(ClaimTypes.Name).Returns(new Claim(ClaimTypes.Name, "valid"));
|
|
|
+ var controller = new AccountController(authenticationProvider) {
|
|
|
+ ControllerContext = new ControllerContext {HttpContext = new DefaultHttpContext {User = claimsPrincipal}},
|
|
|
+ TempData = new TempDataDictionary(new DefaultHttpContext(), Substitute.For<ITempDataProvider>()) // TempData needs to be set up in a unit test
|
|
|
+ };
|
|
|
+
|
|
|
+ var request = new UpdatePasswordRequest {Username = "valid", OldPassword = "correct", NewPassword = "new", ConfirmPassword = "new"};
|
|
|
+ authenticationProvider.UpdateUserPassword(request).Returns(true);
|
|
|
+
|
|
|
+ var result = await controller.UpdatePassword(request);
|
|
|
+ result.Should().BeOfType<RedirectToActionResult>();
|
|
|
+ (result as RedirectToActionResult).ActionName.Should().Be("Index");
|
|
|
+ controller.TempData["updatePassword"].Should().Be(true);
|
|
|
+ }
|
|
|
+
|
|
|
+ [Fact]
|
|
|
+ public async Task UpdatePassword_WithInvalidCredentials_RedirectsToIndexWithFailure() {
|
|
|
+ var authenticationProvider = Substitute.For<IAuthenticationProvider>();
|
|
|
+ var claimsPrincipal = Substitute.For<ClaimsPrincipal>();
|
|
|
+ claimsPrincipal.FindFirst(ClaimTypes.Name).Returns(new Claim(ClaimTypes.Name, "valid"));
|
|
|
+ var controller = new AccountController(authenticationProvider) {
|
|
|
+ ControllerContext = new ControllerContext {HttpContext = new DefaultHttpContext {User = claimsPrincipal}},
|
|
|
+ TempData = new TempDataDictionary(new DefaultHttpContext(), Substitute.For<ITempDataProvider>()) // TempData needs to be set up in a unit test
|
|
|
+ };
|
|
|
+
|
|
|
+ var request = new UpdatePasswordRequest {Username = "valid", OldPassword = "incorrect", NewPassword = "new", ConfirmPassword = "new"};
|
|
|
+ authenticationProvider.UpdateUserPassword(request).Returns(false);
|
|
|
+
|
|
|
+ var result = await controller.UpdatePassword(request);
|
|
|
+ result.Should().BeOfType<RedirectToActionResult>();
|
|
|
+ (result as RedirectToActionResult).ActionName.Should().Be("Index");
|
|
|
+ controller.TempData["updatePassword"].Should().Be(false);
|
|
|
+ }
|
|
|
}
|
|
|
}
|