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): Add UsersController tests
Browse files Browse the repository at this point in the history
  • Loading branch information
CarlosPavajeau committed Dec 20, 2020
1 parent 465db76 commit 77649c4
Show file tree
Hide file tree
Showing 4 changed files with 245 additions and 1 deletion.
57 changes: 57 additions & 0 deletions Kaizen.Test/Controllers/BaseControllerTest.cs
Original file line number Diff line number Diff line change
@@ -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<Data>(c =>
{
c.Provider = (DataProvider)Enum.Parse(typeof(DataProvider), configuration["Data:Provider"]);
});
services.Configure<ConnectionStrings>(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<IHostEnvironment> mockHostingEnvironment = new Mock<IHostEnvironment>();
services.AddSingleton(mockHostingEnvironment.Object);

ServiceProvider = services.BuildServiceProvider();
}
}
}
145 changes: 145 additions & 0 deletions Kaizen.Test/Controllers/UsersControllerTest.cs
Original file line number Diff line number Diff line change
@@ -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<IApplicationUserRepository> _applicationUserRepository;

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

[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<ApplicationUserViewModel> 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<ApplicationUser>(), It.IsAny<string>(), It.IsAny<string>())).Returns(Task.FromResult(IdentityResult.Success));

ActionResult<ApplicationUserViewModel> 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<string>())).Returns(Task.FromResult(
new ApplicationUser
{
Id = "333-444-555",
UserName = "admin"
}));

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

ActionResult<ApplicationUserViewModel> 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<string>())).Returns(Task.FromResult(
new ApplicationUser
{
Id = "333-444-555",
UserName = "admin"
}));

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

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

ActionResult<ApplicationUserViewModel> 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<string>())).Returns(Task.FromResult(
new ApplicationUser
{
Id = "333-444-555",
UserName = "admin",
EmailConfirmed = false
}));

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

ActionResult<ApplicationUserViewModel> result =
await _usersController.ConfirmEmail("ConfirmEmailToken".Base64ForUrlEncode(), "admin@admin.com");

Assert.NotNull(result);
Assert.NotNull(result.Value);
}
}
}
14 changes: 13 additions & 1 deletion Kaizen.Test/Kaizen.Test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,26 @@
<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<None Remove="appsettings.json" />
</ItemGroup>

<ItemGroup>
<Content Include="appsettings.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<ExcludeFromSingleFile>true</ExcludeFromSingleFile>
<CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
</Content>
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Configuration" Version="5.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="5.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="5.0.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="5.0.1" />
<PackageReference Include="Microsoft.Extensions.Options" Version="5.0.0" />
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="5.0.0" />
<PackageReference Include="Moq" Version="4.15.2" />
<PackageReference Include="nunit" Version="3.12.0" />
<PackageReference Include="NUnit3TestAdapter" Version="3.17.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.8.3" />
Expand All @@ -23,7 +36,6 @@
</ItemGroup>

<ItemGroup>
<Folder Include="Controllers\" />
<Folder Include="Mappers\" />
<Folder Include="Models\" />
<Folder Include="DomainEvents\" />
Expand Down
30 changes: 30 additions & 0 deletions Kaizen.Test/appsettings.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
}

1 comment on commit 77649c4

@CarlosPavajeau
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.