From 77649c48cd8328070ef0d19fe1ef9a0e3573c25b Mon Sep 17 00:00:00 2001 From: Carlos Pavajeau Date: Sun, 20 Dec 2020 17:48:42 -0500 Subject: [PATCH] test(back-end): Add UsersController tests --- Kaizen.Test/Controllers/BaseControllerTest.cs | 57 +++++++ .../Controllers/UsersControllerTest.cs | 145 ++++++++++++++++++ Kaizen.Test/Kaizen.Test.csproj | 14 +- Kaizen.Test/appsettings.json | 30 ++++ 4 files changed, 245 insertions(+), 1 deletion(-) create mode 100644 Kaizen.Test/Controllers/BaseControllerTest.cs create mode 100644 Kaizen.Test/Controllers/UsersControllerTest.cs create mode 100644 Kaizen.Test/appsettings.json diff --git a/Kaizen.Test/Controllers/BaseControllerTest.cs b/Kaizen.Test/Controllers/BaseControllerTest.cs new file mode 100644 index 00000000..3dae20ed --- /dev/null +++ b/Kaizen.Test/Controllers/BaseControllerTest.cs @@ -0,0 +1,57 @@ +using System; +using AutoMapper; +using Kaizen.Domain.Data.Configuration; +using Kaizen.Domain.Extensions; +using Kaizen.Extensions; +using Kaizen.Infrastructure.Extensions; +using MediatR; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Hosting; +using Moq; +using NUnit.Framework; + +namespace Kaizen.Test.Controllers +{ + [TestFixture] + public class BaseControllerTest + { + protected ServiceProvider ServiceProvider { get; private set; } + + [OneTimeSetUp] + public void Init() + { + IConfiguration configuration = new ConfigurationBuilder().AddJsonFile("appsettings.json", false, true) + .AddEnvironmentVariables() + .Build(); + + ServiceCollection services = new ServiceCollection(); + services.AddOptions(); + services.AddSingleton(configuration); + services.Configure(c => + { + c.Provider = (DataProvider)Enum.Parse(typeof(DataProvider), configuration["Data:Provider"]); + }); + services.Configure(configuration.GetSection("ConnectionStrings")); + services.AddEntityFramework(configuration); + services.ConfigureRepositories(); + services.AddIdentityConfig(); + services.AddLogging(); + services.ConfigureApplicationServices(); + services.ConfigureMailTemplates(); + services.LoadMailSettings(configuration); + services.ConfigureTokenGenerator(); + + services.AddAutoMapper(typeof(Startup)); + services.AddMediatR(typeof(Startup)); + services.ConfigureDomainEventDispatcher(); + services.ConfigureGlobalFilters(); + services.ConfigureApplicationServices(); + + Mock mockHostingEnvironment = new Mock(); + services.AddSingleton(mockHostingEnvironment.Object); + + ServiceProvider = services.BuildServiceProvider(); + } + } +} diff --git a/Kaizen.Test/Controllers/UsersControllerTest.cs b/Kaizen.Test/Controllers/UsersControllerTest.cs new file mode 100644 index 00000000..280b1e7f --- /dev/null +++ b/Kaizen.Test/Controllers/UsersControllerTest.cs @@ -0,0 +1,145 @@ +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.Models.ApplicationUser; +using Microsoft.AspNetCore.Identity; +using Microsoft.AspNetCore.Mvc; +using Microsoft.Extensions.DependencyInjection; +using Moq; +using NUnit.Framework; + +namespace Kaizen.Test.Controllers +{ + [TestFixture] + public class UsersControllerTest : BaseControllerTest + { + private UsersController _usersController; + private Mock _applicationUserRepository; + + [SetUp] + public void Setup() + { + _applicationUserRepository = new Mock(); + _usersController = new UsersController(_applicationUserRepository.Object, + ServiceProvider.GetService(), ServiceProvider.GetService()); + } + + [Test] + public async Task GetUser() + { + _applicationUserRepository.Setup(r => r.FindByIdAsync("333-444-555")).Returns(Task.FromResult( + new ApplicationUser + { + Id = "333-444-555", + UserName = "admin" + })); + + ActionResult result = await _usersController.GetUser("333-444-555"); + ApplicationUserViewModel userView = result.Value; + + Assert.NotNull(result); + Assert.NotNull(userView); + Assert.AreEqual("333-444-555", userView.Id); + } + + [Test] + public async Task ChangePassword() + { + _applicationUserRepository.Setup(r => r.FindByIdAsync("333-444-555")).Returns(Task.FromResult( + new ApplicationUser + { + Id = "333-444-555", + UserName = "admin" + })); + + _applicationUserRepository.Setup(r => + r.ChangePassswordAsync(It.IsAny(), It.IsAny(), It.IsAny())).Returns(Task.FromResult(IdentityResult.Success)); + + ActionResult result = await _usersController.ChangePassword("333-444-555", + new ChangePasswordModel { NewPassword = "newPassword", OldPassword = "oldPassword" }); + + Assert.NotNull(result); + Assert.NotNull(result.Value); + } + + [Test] + public async Task ResetPassword() + { + _applicationUserRepository.Setup(r => r.FindByNameOrEmailAsync(It.IsAny())).Returns(Task.FromResult( + new ApplicationUser + { + Id = "333-444-555", + UserName = "admin" + })); + + _applicationUserRepository.Setup(r => + r.ResetPasswordAsync(It.IsAny(), It.IsAny(), It.IsAny())).Returns(Task.FromResult(IdentityResult.Success)); + + ActionResult result = await _usersController.ResetPassword("admin", new ResetPasswordModel + { + Token = "ResetPasswordToken".Base64ForUrlEncode(), + NewPassword = "ThisIsMyNewPassword" + }); + + Assert.NotNull(result); + Assert.NotNull(result.Value); + } + + + [Test] + public async Task Login() + { + _applicationUserRepository.Setup(r => r.FindByNameOrEmailAsync(It.IsAny())).Returns(Task.FromResult( + new ApplicationUser + { + Id = "333-444-555", + UserName = "admin" + })); + + _applicationUserRepository.Setup(r => r.GetUserRoleAsync(It.IsAny())) + .Returns(Task.FromResult("Administrator")); + + _applicationUserRepository.Setup(r => r.Login(It.IsAny(), It.IsAny())) + .Returns(Task.FromResult(Microsoft.AspNetCore.Identity.SignInResult.Success)); + + ActionResult result = await _usersController.Login(new LoginRequest + { + UsernameOrEmail = "Admin", + Password = "ThisIsAPassword" + }); + + Assert.NotNull(result); + Assert.NotNull(result.Value); + } + + [Test] + public async Task ConfirmEmail() + { + _applicationUserRepository.Setup(r => r.FindByNameOrEmailAsync(It.IsAny())).Returns(Task.FromResult( + new ApplicationUser + { + Id = "333-444-555", + UserName = "admin", + EmailConfirmed = false + })); + + _applicationUserRepository.Setup(r => r.ConfirmEmailAsync(It.IsAny(), It.IsAny())) + .Returns(Task.FromResult(new ApplicationUser + { + Id = "333-444-555", + UserName = "admin", + EmailConfirmed = true + })); + + ActionResult result = + await _usersController.ConfirmEmail("ConfirmEmailToken".Base64ForUrlEncode(), "admin@admin.com"); + + Assert.NotNull(result); + Assert.NotNull(result.Value); + } + } +} diff --git a/Kaizen.Test/Kaizen.Test.csproj b/Kaizen.Test/Kaizen.Test.csproj index 568b7ed5..9fa93041 100644 --- a/Kaizen.Test/Kaizen.Test.csproj +++ b/Kaizen.Test/Kaizen.Test.csproj @@ -6,6 +6,18 @@ false + + + + + + + PreserveNewest + true + PreserveNewest + + + @@ -13,6 +25,7 @@ + @@ -23,7 +36,6 @@ - diff --git a/Kaizen.Test/appsettings.json b/Kaizen.Test/appsettings.json new file mode 100644 index 00000000..b08acd19 --- /dev/null +++ b/Kaizen.Test/appsettings.json @@ -0,0 +1,30 @@ +{ + "Data": { + "Provider": "MySQL" + }, + "ConnectionStrings": { + "DefaultConnection": "Server=localhost;Port=3306;Database=kaizen_test;Uid=kaizen;Pwd=kaizen;" + }, + "AppSettings": { + "Key": "TEST-bzr!$yzrtu--z(5ir2^!4vwf5hof_lky^o!qz%2fnl_)(" + }, + "Mail": { + "Host": "smtp.gmail.com", + "Port": 587, + "User": "example@example.com", + "Password": "ThisIsAPassword" + }, + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft": "Warning", + "Microsoft.Hosting.Lifetime": "Information" + } + }, + "AllowedHosts": "*", + "Azure": { + "SignalR": { + "Enabled": "true" + } + } +}