Ver código fonte

added tests for helper classes and modified previous tests

pull/148/head
meris.ahmatovic 3 anos atrás
pai
commit
f094919ba9

+ 3
- 0
Diligent.WebAPI.Business/Helper/StringGenerator.cs Ver arquivo

RequireUppercase = true RequireUppercase = true
}; };


if(opts.RequiredLength < 4)
opts.RequiredLength = 4;

string[] randomChars = new[] { string[] randomChars = new[] {
"ABCDEFGHJKLMNOPQRSTUVWXYZ", // uppercase "ABCDEFGHJKLMNOPQRSTUVWXYZ", // uppercase
"abcdefghijkmnopqrstuvwxyz", // lowercase "abcdefghijkmnopqrstuvwxyz", // lowercase

+ 9
- 8
Diligent.WebAPI.Tests/Controllers/UsersControllerTest.cs Ver arquivo

using Diligent.WebAPI.Business.MappingProfiles; using Diligent.WebAPI.Business.MappingProfiles;
using Diligent.WebAPI.Contracts.DTOs; using Diligent.WebAPI.Contracts.DTOs;
using Diligent.WebAPI.Contracts.DTOs.User; using Diligent.WebAPI.Contracts.DTOs.User;
using Diligent.WebAPI.Contracts.Exceptions;
using Diligent.WebAPI.Data.Entities; using Diligent.WebAPI.Data.Entities;


namespace Diligent.WebAPI.Tests.Controllers namespace Diligent.WebAPI.Tests.Controllers
[Fact] [Fact]
public async Task GetUserById_ShouldReturn404_WhenUserDoesNotExist() public async Task GetUserById_ShouldReturn404_WhenUserDoesNotExist()
{ {
_userService.GetById(Arg.Any<int>()).Returns<User>(x => null);
UsersController usersController = new(_userService, _mapper);
_userService.When(x => x.GetById(Arg.Any<int>()))
.Do(x => { throw new EntityNotFoundException(); });


var result = await usersController.GetUser(1);
UsersController usersController = new(_userService, _mapper);


(result as ObjectResult).StatusCode.Should().Be(400);
await Assert.ThrowsAsync<EntityNotFoundException>(() => usersController.GetUser(15));
} }


[Fact] [Fact]
[Fact] [Fact]
public async Task DeleteUser_ShouldReturn400_WhenUserDoesNotExist() public async Task DeleteUser_ShouldReturn400_WhenUserDoesNotExist()
{ {
_userService.GetById(Arg.Any<int>()).Returns<User>(x => null);
UsersController usersController = new(_userService, _mapper);
_userService.When(x => x.GetById(Arg.Any<int>()))
.Do(x => { throw new EntityNotFoundException(); });


var result = await usersController.DeleteUser(1);
UsersController usersController = new(_userService, _mapper);


(result as ObjectResult).StatusCode.Should().Be(400);
await Assert.ThrowsAsync<EntityNotFoundException>(() => usersController.DeleteUser(15));
} }
} }
} }

+ 34
- 0
Diligent.WebAPI.Tests/Helpers/HTMLHelperTests.cs Ver arquivo

using Diligent.WebAPI.Business.Helper;

namespace Diligent.WebAPI.Tests.Helpers
{
public class HTMLHelperTests
{
[Fact]
public void RenderForgotPasswordPage_ShouldReturnRawHTML_WithMailAsHref()
{
string url = "test.user@dilig.net";
Assert.Contains($"href=\"{url}\">", HTMLHelper.RenderForgotPasswordPage(url));
}

[Fact]
public void RenderRegisterPage_ShouldReturnRawHTML_WithMailAsHref()
{
string url = "test.user@dilig.net";
Assert.Contains($"href=\"{url}\">", HTMLHelper.RenderRegisterPage(url));
}

[Fact]
public void RenderTagPage_ShouldReturnRawHTML_WithMailAsHref()
{
string url = "test.user@dilig.net";
Assert.Contains($"href=\"{url}\">", HTMLHelper.RenderTagPage(url));
}

[Fact]
public void SuccessfulStep_ShouldReturnRawHTML_WithMailAsHref()
{
Assert.Contains($"<p style=\"color: #017397;\">message date</p>" , HTMLHelper.SuccessfulStep("message", "pattern", "date"));
}
}
}

+ 92
- 0
Diligent.WebAPI.Tests/Helpers/StringGeneratorTests.cs Ver arquivo

using Diligent.WebAPI.Business.Helper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Diligent.WebAPI.Tests.Helpers
{
public class StringGeneratorTests
{
[Fact]
public void GeneratePassword_ShouldReturn_8_Characters_IfOptionsAreNull()
{
Assert.Equal(8, StringGenerator.GenerateRandomPassword().Length);
}

[Fact]
public void GeneratePassword_ShouldReturn_4_Characters_IfOptionsDefinedLess()
{
Assert.Equal(4, StringGenerator.GenerateRandomPassword(new Microsoft.AspNetCore.Identity.PasswordOptions
{
RequiredLength = 3,
RequiredUniqueChars = 3,
RequireDigit = true,
RequireLowercase = true,
RequireNonAlphanumeric = true,
RequireUppercase = true
}).Length);
}

[Fact]
public void GeneratePassword_ShouldReturn_15_Characters_IfOptionsHaveDefinedMinLength()
{
Assert.Equal(15, StringGenerator.GenerateRandomPassword(new Microsoft.AspNetCore.Identity.PasswordOptions
{
RequiredLength = 15,
RequiredUniqueChars = 4,
RequireDigit = true,
RequireLowercase = true,
RequireNonAlphanumeric = true,
RequireUppercase = true
}).Length);
}

[Fact]
public void GeneratePassword_ShouldContainUppercase_IfOptionsAreNull()
{
Assert.Contains(StringGenerator.GenerateRandomPassword(), char.IsUpper);
}

[Fact]
public void GeneratePassword_ShoulContainNumber_IfOptionsAreNull()
{
Assert.Contains(StringGenerator.GenerateRandomPassword(), char.IsDigit);
}

[Fact]
public void GeneratePassword_ShouldNotContainNumber_AfterAddingRandomChars()
{
Assert.Contains(StringGenerator.GenerateRandomPassword(new Microsoft.AspNetCore.Identity.PasswordOptions
{
RequiredLength = 12,
RequiredUniqueChars = 4,
RequireDigit = false,
RequireLowercase = true,
RequireNonAlphanumeric = true,
RequireUppercase = false
}), char.IsDigit);
}

[Fact]
public void GeneratePassword_ShoulContainSpecialChar_IfOptionsAreNull()
{
Assert.False(StringGenerator.GenerateRandomPassword().All(char.IsLetterOrDigit));
}

[Fact]
public void GeneratePassword_ShouldContainSpecialChar_IfOptionsAreDefinedToAddRandomChars()
{
Assert.False(StringGenerator.GenerateRandomPassword(new Microsoft.AspNetCore.Identity.PasswordOptions
{
RequiredLength = 12,
RequiredUniqueChars = 1,
RequireDigit = true,
RequireLowercase = true,
RequireNonAlphanumeric = false,
RequireUppercase = true
}).All(char.IsLetterOrDigit));
}
}
}

+ 3
- 28
Diligent.WebAPI.Tests/Services/UserServiceTests.cs Ver arquivo

using Diligent.WebAPI.Business.Settings; using Diligent.WebAPI.Business.Settings;
using Diligent.WebAPI.Contracts.DTOs; using Diligent.WebAPI.Contracts.DTOs;
using Diligent.WebAPI.Contracts.DTOs.User; using Diligent.WebAPI.Contracts.DTOs.User;
using Diligent.WebAPI.Contracts.Exceptions;
using Diligent.WebAPI.Data; using Diligent.WebAPI.Data;
using Diligent.WebAPI.Data.Entities; using Diligent.WebAPI.Data.Entities;
using Microsoft.AspNetCore.Identity; using Microsoft.AspNetCore.Identity;
} }


[Fact] [Fact]
public async Task RemoveUser_ShouldDeleteUser()
public async Task RemoveUser_ShouldReturnError()
{ {
var databaseContext = await Helpers<User>.GetDatabaseContext(_users); var databaseContext = await Helpers<User>.GetDatabaseContext(_users);


var service = new UserService(frontSettings, _mockUserManager, _mapper, databaseContext, mailer, _logger); var service = new UserService(frontSettings, _mockUserManager, _mapper, databaseContext, mailer, _logger);
await service.RemoveUser(_user); await service.RemoveUser(_user);


var result = await service.GetById(2);
result.Should().Be(null);
await Assert.ThrowsAsync<EntityNotFoundException>(async () => await service.GetById(2));
} }


[Fact] [Fact]


Assert.Equal(true, result.IsError); Assert.Equal(true, result.IsError);
} }

//[Fact] To be debuged later
//public async Task Invite_ShouldPassIfUserDoesNotExist()
//{
// _mockUserManager.FindByEmailAsync(Arg.Any<string>()).Returns<User>(x => null);
// var databaseContext = await Helpers<User>.GetDatabaseContext(_users);
// var frontSettings = Substitute.For<IOptions<FrontEndSettings>>();

// var mailer = Substitute.For<IEmailer>();
// mailer.When(x => x.SendEmailAndWriteToDbAsync(Arg.Any<string>(), Arg.Any<string>(), Arg.Any<string>(), Arg.Any<bool>())).Do(x => { });

// var service = new UserService(frontSettings, _mockUserManager, _mapper, databaseContext, mailer);

// var result = await service.SendRegistrationLink(new InviteDTO
// {
// Email = "string@dilig.net",
// FirstName = "string",
// LastName = "string",
// });

// result.Data.Should().BeEquivalentTo(new
// {
// Message = "Link has been sent!"
// });
//}
} }
} }

Carregando…
Cancelar
Salvar