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): Rewrite Clients controller tests
Browse files Browse the repository at this point in the history
See also: #119
  • Loading branch information
CarlosPavajeau committed Jan 17, 2021
1 parent f1614e7 commit c976362
Showing 1 changed file with 133 additions and 48 deletions.
181 changes: 133 additions & 48 deletions Kaizen.Test/Controllers/ClientsControllerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -33,10 +35,13 @@ public void SetUp()

_clientsController = new ClientsController(_clientsRepository.Object, _applicationUserRepository.Object,
_unitWork.Object, ServiceProvider.GetService<IMapper>());

SetUpClientsRepository();
SetUpApplicationUserRepository();
SetUpUnitWork();
}

[Test]
public async Task GetClients()
private void SetUpClientsRepository()
{
_clientsRepository.Setup(r => r.GetAll()).Returns(new TestAsyncEnumerable<Client>(new[]
{
Expand All @@ -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<IEnumerable<ClientViewModel>>(result.Value);

IEnumerable<ClientViewModel> value = result.Value as IEnumerable<ClientViewModel>;
Assert.NotNull(value);
Assert.AreEqual(2, value.Count());
}

[Test]
public async Task GetClientsRequests()
{
_clientsRepository.Setup(r => r.GetClientRequestsAsync()).ReturnsAsync(new[]
{
new Client
Expand All @@ -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<Client>())).Verifiable();
_clientsRepository.Setup(r => r.Insert(It.IsAny<Client>())).Verifiable();
}

private void SetUpApplicationUserRepository()
{
_applicationUserRepository.Setup(r => r.CreateAsync(It.Is<ApplicationUser>(a => a.UserName == "manolos"),
It.IsAny<string>()))
.ReturnsAsync(IdentityResult.Success);

_applicationUserRepository.Setup(r => r.CreateAsync(It.Is<ApplicationUser>(a => a.UserName == "admin"),
It.IsAny<string>()))
.ReturnsAsync(IdentityResult.Failed(new SpanishIdentityErrorDescriber().DuplicateUserName("admin")));

_applicationUserRepository.Setup(r => r.AddToRoleAsync(It.IsAny<ApplicationUser>(), "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<IEnumerable<ClientViewModel>>(result.Value);

IEnumerable<ClientViewModel> value = result.Value as IEnumerable<ClientViewModel>;
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<IEnumerable<ClientViewModel>>(result.Value);

IEnumerable<ClientViewModel> value = result.Value as IEnumerable<ClientViewModel>;
Assert.IsNotNull(value);
Assert.AreEqual(2, value.Count());
}

[Test]
public async Task Get_Existing_tClient()
{
ActionResult<ClientViewModel> 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<string>())).ReturnsAsync(new Client
{
Id = "1007870945",
FirstName = "Manolo",
LastName = "Perez"
});
_clientsRepository.Setup(r => r.Update(It.IsAny<Client>())).Verifiable();
_unitWork.Setup(u => u.SaveAsync()).Returns(Task.CompletedTask);
ActionResult<ClientViewModel> result = await _clientsController.GetClient("1007870944");

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

[Test]
public async Task Update_Existing_Client()
{
ActionResult<ClientViewModel> 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<Client>())).Verifiable();
_applicationUserRepository.Setup(r => r.CreateAsync(It.IsAny<ApplicationUser>(), It.IsAny<string>()))
.ReturnsAsync(IdentityResult.Success);
_applicationUserRepository.Setup(r => r.AddToRoleAsync(It.IsAny<ApplicationUser>(), "Client"))
.ReturnsAsync(IdentityResult.Success);
ActionResult<ClientViewModel> 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<BadRequestObjectResult>(result.Result);
}

[Test]
public async Task Save_New_Client_With_New_Username()
{
ActionResult<ClientViewModel> result = await _clientsController.PostClient(new ClientInputModel
{
Id = "1007870945",
Expand All @@ -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<ClientViewModel> 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<BadRequestObjectResult>(result.Result);
}

[Test]
public async Task Save_Existing_Client()
{
_unitWork.Setup(r => r.SaveAsync()).Throws(new DbUpdateException());

ActionResult<ClientViewModel> 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<ConflictObjectResult>(result.Result);
}
}
}

0 comments on commit c976362

Please sign in to comment.