Skip to content
This repository has been archived by the owner on Jun 25, 2022. It is now read-only.

Commit

Permalink
test(back-end): Write and add new test methods for the user controller
Browse files Browse the repository at this point in the history
See also: #119
  • Loading branch information
CarlosPavajeau committed Feb 10, 2021
1 parent 3a947ba commit 2d0ef10
Showing 1 changed file with 259 additions and 63 deletions.
322 changes: 259 additions & 63 deletions Kaizen.Test/Controllers/UsersControllerTest.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
using System.Linq;
using System.Threading.Tasks;
using AutoMapper;
using Kaizen.Controllers;
using Kaizen.Core.Security;
using Kaizen.Domain.Entities;
using Kaizen.Domain.Repositories;
using Kaizen.Extensions;
using Kaizen.Infrastructure.Identity;
using Kaizen.Models.ApplicationUser;
using Kaizen.Test.Helpers;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Routing;
using Microsoft.Extensions.DependencyInjection;
using Moq;
using NUnit.Framework;
Expand All @@ -19,25 +24,109 @@ public class UsersControllerTest : BaseControllerTest
{
private UsersController _usersController;
private Mock<IApplicationUserRepository> _applicationUserRepository;
private readonly SpanishIdentityErrorDescriber _spanishIdentityErrorDescriber = new();

[SetUp]
public void Setup()
{
_applicationUserRepository = new Mock<IApplicationUserRepository>();
_usersController = new UsersController(_applicationUserRepository.Object,
ServiceProvider.GetService<IMapper>(), ServiceProvider.GetService<ITokenGenerator>());

SetUpApplicationUserRepository();
}

[Test]
public async Task GetUser()
private void SetUpApplicationUserRepository()
{
_applicationUserRepository.Setup(r => r.FindByIdAsync("333-444-555")).Returns(Task.FromResult(
_applicationUserRepository.Setup(r => r.FindByIdAsync("333-444-555")).ReturnsAsync(
new ApplicationUser
{
Id = "333-444-555",
UserName = "admin",
Email = "admin@admin.com"
});

_applicationUserRepository.Setup(r => r.FindByIdAsync("123-456-789")).ReturnsAsync((ApplicationUser) null);

_applicationUserRepository.Setup(r => r.FindByNameOrEmailAsync("admin")).ReturnsAsync(
new ApplicationUser
{
Id = "333-444-555",
UserName = "admin",
Email = "admin@admin.com"
});

_applicationUserRepository.Setup(r => r.FindByNameOrEmailAsync("roots2"))
.ReturnsAsync((ApplicationUser) null);

_applicationUserRepository.Setup(r => r.FindByNameOrEmailAsync("admin@admin.com")).ReturnsAsync(
new ApplicationUser
{
Id = "333-444-555",
UserName = "admin",
Email = "admin@admin.com"
});

_applicationUserRepository.Setup(r => r.FindByNameOrEmailAsync("roots2@roots2.com"))
.ReturnsAsync((ApplicationUser) null);

_applicationUserRepository.Setup(r => r.GetAll()).Returns(new TestAsyncEnumerable<ApplicationUser>(new[]
{
new ApplicationUser
{
Id = "333-444-555",
UserName = "admin"
}
}).AsQueryable());

_applicationUserRepository.Setup(r =>
r.ChangePasswordAsync(It.IsAny<ApplicationUser>(), "WrongOldPassword", It.IsAny<string>()))
.Returns(Task.FromResult(IdentityResult.Failed(_spanishIdentityErrorDescriber.PasswordMismatch())));

_applicationUserRepository.Setup(r =>
r.ChangePasswordAsync(It.IsAny<ApplicationUser>(), "CorrectOldPassword", It.IsAny<string>()))
.Returns(Task.FromResult(IdentityResult.Success));

_applicationUserRepository.Setup(r =>
r.ResetPasswordAsync(It.IsAny<ApplicationUser>(), "WrongToken", It.IsAny<string>()))
.Returns(Task.FromResult(IdentityResult.Failed(_spanishIdentityErrorDescriber.InvalidToken())));

_applicationUserRepository.Setup(r =>
r.ResetPasswordAsync(It.IsAny<ApplicationUser>(), "ValidToken", It.IsAny<string>()))
.Returns(Task.FromResult(IdentityResult.Success));

_applicationUserRepository.Setup(r => r.GetUserRoleAsync(It.IsAny<ApplicationUser>()))
.Returns(Task.FromResult("Administrator"));

_applicationUserRepository
.Setup(r => r.Login(It.IsAny<ApplicationUser>(), "WrongPassword", It.IsAny<bool>()))
.ReturnsAsync(Microsoft.AspNetCore.Identity.SignInResult.Failed);

_applicationUserRepository
.Setup(r => r.Login(It.IsAny<ApplicationUser>(), "CorrectPassword", It.IsAny<bool>()))
.Returns(Task.FromResult(Microsoft.AspNetCore.Identity.SignInResult.Success));

_applicationUserRepository.Setup(r => r.ConfirmEmailAsync(It.IsAny<ApplicationUser>(), It.IsAny<string>()))
.Returns(Task.FromResult(new ApplicationUser
{
Id = "333-444-555",
UserName = "admin",
EmailConfirmed = true
}));

_applicationUserRepository.Setup(r => r.Logout()).Returns(Task.CompletedTask);

_applicationUserRepository.Setup(r => r.GeneratePasswordResetTokenAsync(It.IsAny<ApplicationUser>()))
.ReturnsAsync("PasswordResetToken");

_applicationUserRepository
.Setup(r => r.SendPasswordResetTokenAsync(It.IsAny<ApplicationUser>(), It.IsAny<string>()))
.ReturnsAsync(true);
}

[Test]
public async Task Get_Existing_User()
{
ActionResult<ApplicationUserViewModel> result = await _usersController.GetUser("333-444-555");
ApplicationUserViewModel userView = result.Value;

Expand All @@ -47,99 +136,206 @@ public async Task GetUser()
}

[Test]
public async Task ChangePassword()
public async Task Get_Non_Existent_User()
{
_applicationUserRepository.Setup(r => r.FindByIdAsync("333-444-555")).Returns(Task.FromResult(
new ApplicationUser
{
Id = "333-444-555",
UserName = "admin"
}));
var result = await _usersController.GetUser("123-456-789");

_applicationUserRepository.Setup(r =>
r.ChangePasswordAsync(It.IsAny<ApplicationUser>(), It.IsAny<string>(), It.IsAny<string>())).Returns(Task.FromResult(IdentityResult.Success));
Assert.IsNotNull(result);
Assert.IsNull(result.Value);
Assert.IsInstanceOf<NotFoundObjectResult>(result.Result);
}

ActionResult<ApplicationUserViewModel> result = await _usersController.ChangePassword("333-444-555",
new ChangePasswordModel { NewPassword = "newPassword", OldPassword = "oldPassword" });
[Test]
public async Task Check_User_Exists()
{
var result = await _usersController.CheckExists("admin");

Assert.NotNull(result);
Assert.NotNull(result.Value);
Assert.IsNotNull(result);
Assert.IsTrue(result.Value);
}

[Test]
public async Task ResetPassword()
public async Task Change_Password_Of_Non_Existent_User()
{
_applicationUserRepository.Setup(r => r.FindByNameOrEmailAsync(It.IsAny<string>())).Returns(Task.FromResult(
new ApplicationUser
var result = await _usersController.ChangePassword("123-456-789",
new ChangePasswordModel {NewPassword = "newPassword", OldPassword = "oldPassword"});

Assert.IsNotNull(result);
Assert.IsNull(result.Value);
Assert.IsInstanceOf<BadRequestObjectResult>(result.Result);
}

[Test]
public async Task Change_Password_Of_An_Existing_User_With_Wrong_Old_Password()
{
var result = await _usersController.ChangePassword("333-444-555",
new ChangePasswordModel {NewPassword = "NewPassword", OldPassword = "WrongOldPassword"});

Assert.IsNotNull(result);
Assert.IsNull(result.Value);
Assert.IsInstanceOf<BadRequestObjectResult>(result.Result);
}

[Test]
public async Task Change_Password_Of_An_Existing_User_With_Correct_Old_Password()
{
var result = await _usersController.ChangePassword("333-444-555",
new ChangePasswordModel {NewPassword = "NewPassword", OldPassword = "CorrectOldPassword"});

Assert.IsNotNull(result);
Assert.IsNotNull(result.Value);
}

[Test]
public async Task Reset_Password_Of_Non_Existent_User()
{
ActionResult<ApplicationUserViewModel> result = await _usersController.ResetPassword("roots2",
new ResetPasswordModel
{
Id = "333-444-555",
UserName = "admin"
}));
Token = "ResetPasswordToken".Base64ForUrlEncode(),
NewPassword = "ThisIsMyNewPassword"
});

_applicationUserRepository.Setup(r =>
r.ResetPasswordAsync(It.IsAny<ApplicationUser>(), It.IsAny<string>(), It.IsAny<string>())).Returns(Task.FromResult(IdentityResult.Success));
Assert.IsNotNull(result);
Assert.IsNull(result.Value);
Assert.IsInstanceOf<BadRequestObjectResult>(result.Result);
}

ActionResult<ApplicationUserViewModel> result = await _usersController.ResetPassword("admin", new ResetPasswordModel
[Test]
public async Task Reset_Password_Of_An_Existing_User_With_Wrong_Token()
{
var result = await _usersController.ResetPassword("admin", new ResetPasswordModel
{
Token = "ResetPasswordToken".Base64ForUrlEncode(),
NewPassword = "ThisIsMyNewPassword"
Token = "WrongToken".Base64ForUrlEncode(),
NewPassword = "NewPassword"
});

Assert.NotNull(result);
Assert.NotNull(result.Value);
Assert.IsNotNull(result);
Assert.IsNull(result.Value);
Assert.IsInstanceOf<BadRequestObjectResult>(result.Result);
}

[Test]
public async Task Reset_Password_Of_An_Existing_User_With_Valid_Token()
{
var result = await _usersController.ResetPassword("admin", new ResetPasswordModel
{
Token = "ValidToken".Base64ForUrlEncode(),
NewPassword = "NewPassword"
});

Assert.IsNotNull(result);
Assert.IsNotNull(result.Value);
}

[Test]
public async Task Login()
public async Task Login_Of_Non_Existent_User()
{
_applicationUserRepository.Setup(r => r.FindByNameOrEmailAsync(It.IsAny<string>())).Returns(Task.FromResult(
new ApplicationUser
{
Id = "333-444-555",
UserName = "admin"
}));
var result = await _usersController.Login(new LoginRequest
{
UsernameOrEmail = "roots2",
Password = "ThisIsAPassword"
});

_applicationUserRepository.Setup(r => r.GetUserRoleAsync(It.IsAny<ApplicationUser>()))
.Returns(Task.FromResult("Administrator"));
Assert.IsNotNull(result);
Assert.IsNull(result.Value);
}

_applicationUserRepository.Setup(r => r.Login(It.IsAny<ApplicationUser>(), It.IsAny<string>(), It.IsAny<bool>()))
.Returns(Task.FromResult(Microsoft.AspNetCore.Identity.SignInResult.Success));
[Test]
public async Task Login_Of_Non_Existing_User_With_Wrong_Password()
{
var result = await _usersController.Login(new LoginRequest
{
UsernameOrEmail = "admin",
Password = "WrongPassword"
});

ActionResult<ApplicationUserViewModel> result = await _usersController.Login(new LoginRequest
Assert.IsNotNull(result);
Assert.IsNull(result.Value);
Assert.IsInstanceOf<BadRequestObjectResult>(result.Result);
}

[Test]
public async Task Login_Of_Non_Existing_User_With_Correct_Password()
{
var result = await _usersController.Login(new LoginRequest
{
UsernameOrEmail = "Admin",
Password = "ThisIsAPassword"
UsernameOrEmail = "admin",
Password = "CorrectPassword"
});

Assert.NotNull(result);
Assert.NotNull(result.Value);
Assert.IsNotNull(result);
Assert.IsNotNull(result.Value);
}

[Test]
public async Task ConfirmEmail()
public async Task Logout()
{
_applicationUserRepository.Setup(r => r.FindByNameOrEmailAsync(It.IsAny<string>())).Returns(Task.FromResult(
new ApplicationUser
{
Id = "333-444-555",
UserName = "admin",
EmailConfirmed = false
}));
var result = await _usersController.Logout();

_applicationUserRepository.Setup(r => r.ConfirmEmailAsync(It.IsAny<ApplicationUser>(), It.IsAny<string>()))
.Returns(Task.FromResult(new ApplicationUser
{
Id = "333-444-555",
UserName = "admin",
EmailConfirmed = true
}));
Assert.IsNotNull(result);
Assert.IsTrue(result.Value);
}

ActionResult<ApplicationUserViewModel> result =
[Test]
public async Task Confirm_Email_Of_Non_Existent_User()
{
var result =
await _usersController.ConfirmEmail("ConfirmEmailToken".Base64ForUrlEncode(), "roots2@roots2.com");

Assert.IsNotNull(result);
Assert.IsNull(result.Value);
Assert.IsInstanceOf<NotFoundObjectResult>(result.Result);
}

[Test]
public async Task Confirm_Email_Of_An_Existing_User()
{
var result =
await _usersController.ConfirmEmail("ConfirmEmailToken".Base64ForUrlEncode(), "admin@admin.com");

Assert.NotNull(result);
Assert.NotNull(result.Value);
Assert.IsNotNull(result);
Assert.IsNotNull(result.Value);
}

[Test]
public async Task Forgotten_Password_Of_Non_Existent_User()
{
var result = await _usersController.ForgottenPassword("roots2");

Assert.IsNotNull(result);
Assert.IsFalse(result.Value);
Assert.IsInstanceOf<BadRequestObjectResult>(result.Result);
}

[Test]
public async Task Forgotten_Password_Of_An_Existing_User()
{
MockHttpContext();

var result = await _usersController.ForgottenPassword("admin");

Assert.IsNotNull(result);
Assert.IsTrue(result.Value);
}

private void MockHttpContext()
{
var urlHelper = new Mock<IUrlHelper>();
var httpContext = new Mock<HttpContext>();
var httpRequest = new Mock<HttpRequest>();

urlHelper.Setup(r => r.Action(It.IsAny<UrlActionContext>())).Returns("callbackUrl").Verifiable();

httpRequest.SetupGet(r => r.Scheme).Returns("https");

httpContext.SetupGet(r => r.Request).Returns(httpRequest.Object);

_usersController.Url = urlHelper.Object;
_usersController.ControllerContext = new ControllerContext
{
HttpContext = httpContext.Object
};
}
}
}

0 comments on commit 2d0ef10

Please sign in to comment.