This repository has been archived by the owner on Jun 25, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(back-end): Add UsersController tests
- Loading branch information
1 parent
465db76
commit 77649c4
Showing
4 changed files
with
245 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} | ||
} |
77649c4
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
#119