From c976362e753f3cfec905d9e93c17947a5ac743ba Mon Sep 17 00:00:00 2001 From: Carlos Pavajeau Date: Sun, 17 Jan 2021 11:19:34 -0500 Subject: [PATCH] test(back-end): Rewrite Clients controller tests See also: #119 --- .../Controllers/ClientsControllerTest.cs | 181 +++++++++++++----- 1 file changed, 133 insertions(+), 48 deletions(-) diff --git a/Kaizen.Test/Controllers/ClientsControllerTest.cs b/Kaizen.Test/Controllers/ClientsControllerTest.cs index d1e2752f..83b66f33 100644 --- a/Kaizen.Test/Controllers/ClientsControllerTest.cs +++ b/Kaizen.Test/Controllers/ClientsControllerTest.cs @@ -5,11 +5,13 @@ using Kaizen.Controllers; using Kaizen.Domain.Entities; using Kaizen.Domain.Repositories; +using Kaizen.Infrastructure.Identity; using Kaizen.Models.ApplicationUser; using Kaizen.Models.Client; using Kaizen.Test.Helpers; using Microsoft.AspNetCore.Identity; using Microsoft.AspNetCore.Mvc; +using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.DependencyInjection; using Moq; using NUnit.Framework; @@ -33,10 +35,13 @@ public void SetUp() _clientsController = new ClientsController(_clientsRepository.Object, _applicationUserRepository.Object, _unitWork.Object, ServiceProvider.GetService()); + + SetUpClientsRepository(); + SetUpApplicationUserRepository(); + SetUpUnitWork(); } - [Test] - public async Task GetClients() + private void SetUpClientsRepository() { _clientsRepository.Setup(r => r.GetAll()).Returns(new TestAsyncEnumerable(new[] { @@ -52,19 +57,6 @@ public async Task GetClients() } }).AsQueryable()); - OkObjectResult result = (await _clientsController.GetClients()).Result as OkObjectResult; - Assert.NotNull(result); - Assert.NotNull(result.Value); - Assert.IsInstanceOf>(result.Value); - - IEnumerable value = result.Value as IEnumerable; - Assert.NotNull(value); - Assert.AreEqual(2, value.Count()); - } - - [Test] - public async Task GetClientsRequests() - { _clientsRepository.Setup(r => r.GetClientRequestsAsync()).ReturnsAsync(new[] { new Client @@ -81,67 +73,116 @@ public async Task GetClientsRequests() } }); - OkObjectResult result = (await _clientsController.Requests()).Result as OkObjectResult; - Assert.NotNull(result); - Assert.NotNull(result.Value); + _clientsRepository.Setup(r => r.FindByIdAsync("1007870945")).ReturnsAsync(new Client + { + Id = "1007870945", + FirstName = "Manolo", + LastName = "Perez" + }); + _clientsRepository.Setup(r => r.FindByIdAsync("1007870944")).ReturnsAsync((Client)null); + + _clientsRepository.Setup(r => r.Update(It.IsAny())).Verifiable(); + _clientsRepository.Setup(r => r.Insert(It.IsAny())).Verifiable(); + } + + private void SetUpApplicationUserRepository() + { + _applicationUserRepository.Setup(r => r.CreateAsync(It.Is(a => a.UserName == "manolos"), + It.IsAny())) + .ReturnsAsync(IdentityResult.Success); + + _applicationUserRepository.Setup(r => r.CreateAsync(It.Is(a => a.UserName == "admin"), + It.IsAny())) + .ReturnsAsync(IdentityResult.Failed(new SpanishIdentityErrorDescriber().DuplicateUserName("admin"))); + + _applicationUserRepository.Setup(r => r.AddToRoleAsync(It.IsAny(), "Client")) + .ReturnsAsync(IdentityResult.Success); + } + + private void SetUpUnitWork() + { + _unitWork.Setup(u => u.SaveAsync()).Returns(Task.CompletedTask); + } + + [Test] + public async Task Get_All_Clients() + { + OkObjectResult result = (await _clientsController.GetClients()).Result as OkObjectResult; + + Assert.IsNotNull(result); + Assert.IsNotNull(result.Value); Assert.IsInstanceOf>(result.Value); IEnumerable value = result.Value as IEnumerable; - Assert.NotNull(value); + Assert.IsNotNull(value); Assert.AreEqual(2, value.Count()); } [Test] - public async Task GetClient() + public async Task Get_All_Clients_Requests() { - _clientsRepository.Setup(r => r.FindByIdAsync("1007870945")).ReturnsAsync(new Client - { - Id = "1007870945", - FirstName = "Manolo", - LastName = "Perez" - }); + OkObjectResult result = (await _clientsController.Requests()).Result as OkObjectResult; + Assert.IsNotNull(result); + Assert.IsNotNull(result.Value); + Assert.IsInstanceOf>(result.Value); + + IEnumerable value = result.Value as IEnumerable; + Assert.IsNotNull(value); + Assert.AreEqual(2, value.Count()); + } + + [Test] + public async Task Get_Existing_tClient() + { ActionResult result = await _clientsController.GetClient("1007870945"); - Assert.NotNull(result); - Assert.NotNull(result.Value); + Assert.IsNotNull(result); + Assert.IsNotNull(result.Value); Assert.AreEqual("1007870945", result.Value.Id); } [Test] - public async Task PutClient() + public async Task Get_Non_Existent_Client() { - _clientsRepository.Setup(r => r.FindByIdAsync(It.IsAny())).ReturnsAsync(new Client - { - Id = "1007870945", - FirstName = "Manolo", - LastName = "Perez" - }); - _clientsRepository.Setup(r => r.Update(It.IsAny())).Verifiable(); - _unitWork.Setup(u => u.SaveAsync()).Returns(Task.CompletedTask); + ActionResult result = await _clientsController.GetClient("1007870944"); + + Assert.IsNotNull(result); + Assert.IsNull(result.Value); + Assert.IsInstanceOf(result.Result); + } + [Test] + public async Task Update_Existing_Client() + { ActionResult result = await _clientsController.PutClient("1007870945", new ClientEditModel { FirstName = "Pedro", LastName = "Lopez" }); - Assert.NotNull(result); - Assert.NotNull(result.Value); + Assert.IsNotNull(result); + Assert.IsNotNull(result.Value); Assert.AreEqual("Pedro", result.Value.FirstName); } [Test] - public async Task PostClient() + public async Task Update_Non_Existent_Client() { - _clientsRepository.Setup(r => r.Insert(It.IsAny())).Verifiable(); - _applicationUserRepository.Setup(r => r.CreateAsync(It.IsAny(), It.IsAny())) - .ReturnsAsync(IdentityResult.Success); - _applicationUserRepository.Setup(r => r.AddToRoleAsync(It.IsAny(), "Client")) - .ReturnsAsync(IdentityResult.Success); + ActionResult result = await _clientsController.PutClient("1007870944", new ClientEditModel + { + FirstName = "Pedro", + LastName = "Lopez" + }); - _unitWork.Setup(u => u.SaveAsync()).Returns(Task.CompletedTask); + Assert.IsNotNull(result); + Assert.IsNull(result.Value); + Assert.IsInstanceOf(result.Result); + } + [Test] + public async Task Save_New_Client_With_New_Username() + { ActionResult result = await _clientsController.PostClient(new ClientInputModel { Id = "1007870945", @@ -155,9 +196,53 @@ public async Task PostClient() } }); - Assert.NotNull(result); - Assert.NotNull(result.Value); + Assert.IsNotNull(result); + Assert.IsNotNull(result.Value); Assert.AreEqual("1007870945", result.Value.Id); } + + [Test] + public async Task Save_New_Client_With_An_Existing_Username() + { + ActionResult result = await _clientsController.PostClient(new ClientInputModel + { + Id = "1007870945", + FirstName = "Manolo", + LastName = "Perez", + User = new ApplicationUserInputModel + { + Username = "admin", + Password = "ThisIsASecurePassword", + Email = "client@client.com" + } + }); + + Assert.IsNotNull(result); + Assert.IsNull(result.Value); + Assert.IsInstanceOf(result.Result); + } + + [Test] + public async Task Save_Existing_Client() + { + _unitWork.Setup(r => r.SaveAsync()).Throws(new DbUpdateException()); + + ActionResult result = await _clientsController.PostClient(new ClientInputModel + { + Id = "1007870946", + FirstName = "Manolo", + LastName = "Perez", + User = new ApplicationUserInputModel + { + Username = "manolos", + Password = "ThisIsASecurePassword", + Email = "client@client.com" + } + }); + + Assert.IsNotNull(result); + Assert.IsNull(result.Value); + Assert.IsInstanceOf(result.Result); + } } }