146 lines
4.4 KiB
C#
146 lines
4.4 KiB
C#
namespace LaaProductionWeb.Tests.Models
|
||
{
|
||
using LaaProductionWeb.API.Areas.Serach.Models;
|
||
|
||
using NUnit.Framework;
|
||
|
||
using System.Collections.Generic;
|
||
using System.ComponentModel.DataAnnotations;
|
||
using System.Linq;
|
||
using System.Reflection;
|
||
|
||
public class WildcardInputModelTests
|
||
{
|
||
private readonly WildcardInputModel model;
|
||
private readonly PropertyInfo tokenPropertyInfo;
|
||
|
||
public WildcardInputModelTests()
|
||
{
|
||
this.model = new WildcardInputModel();
|
||
this.tokenPropertyInfo = typeof(WildcardInputModel)
|
||
.GetProperty(nameof(WildcardInputModel.Token), BindingFlags.Instance | BindingFlags.Public);
|
||
}
|
||
|
||
[Test]
|
||
public void ModelValidation_ShouldNotValidate_WhenTokenIsNull()
|
||
{
|
||
// Arrange
|
||
var errorMessage = this.ValidateModelWhitValue(default(string));
|
||
|
||
// Act
|
||
var attributeMessage = this.GetAttributeMessage<RequiredAttribute>();
|
||
|
||
// Assert
|
||
Assert.AreEqual(errorMessage, attributeMessage);
|
||
}
|
||
|
||
[Test]
|
||
public void ModelValidation_ShouldNotValidate_WhenTokenIsUnder_1CharLength()
|
||
{
|
||
// Arrange
|
||
var errorMessage = this.ValidateModelWhitValue(string.Empty);
|
||
|
||
// Act
|
||
var attributeMessage = this.GetAttributeMessage<StringLengthAttribute>();
|
||
|
||
// Assert
|
||
Assert.AreEqual(errorMessage, attributeMessage);
|
||
}
|
||
|
||
[Test]
|
||
public void ModelValidation_ShouldNotValidate_WhenTokenIsOver_50CharsLength()
|
||
{
|
||
// Arrange
|
||
var errorMessage = this.ValidateModelWhitValue(new string('0', 51));
|
||
|
||
// Act
|
||
var attributeMessage = this.GetAttributeMessage<StringLengthAttribute>();
|
||
|
||
// Assert
|
||
Assert.AreEqual(errorMessage, attributeMessage);
|
||
}
|
||
|
||
[Test]
|
||
[TestCase("^")]
|
||
[TestCase("°")]
|
||
[TestCase("\"")]
|
||
[TestCase("§")]
|
||
[TestCase("$")]
|
||
[TestCase("%")]
|
||
[TestCase("&")]
|
||
[TestCase("/")]
|
||
[TestCase("(")]
|
||
[TestCase(")")]
|
||
[TestCase("=")]
|
||
[TestCase("?")]
|
||
[TestCase("`")]
|
||
[TestCase("´")]
|
||
[TestCase("'")]
|
||
[TestCase("#")]
|
||
[TestCase("*")]
|
||
[TestCase("+")]
|
||
[TestCase("|")]
|
||
[TestCase("\t")]
|
||
[TestCase("\n")]
|
||
[TestCase("<")]
|
||
[TestCase(">")]
|
||
[TestCase(".")]
|
||
[TestCase(";")]
|
||
[TestCase(":")]
|
||
[TestCase("_")]
|
||
[TestCase("ä")]
|
||
[TestCase("ö")]
|
||
[TestCase("ü")]
|
||
[TestCase("Ä")]
|
||
[TestCase("Ö")]
|
||
[TestCase("Ü")]
|
||
[TestCase("ß")]
|
||
public void ModelValidation_ShouldNotValidate_WhenTokenContains_NonWordCharachters(string token)
|
||
{
|
||
// Arrange
|
||
var errorMessage = this.ValidateModelWhitValue(token);
|
||
|
||
// Act
|
||
var attributeMessage = this.GetAttributeMessage<RegularExpressionAttribute>();
|
||
|
||
// Assert
|
||
Assert.AreEqual(errorMessage, attributeMessage);
|
||
}
|
||
|
||
[Test]
|
||
[TestCase("0123245 5575689-ARWQGVVJALR fasdkfawe irfoelvm")]
|
||
public void ModelValidation_ShouldValidate_WhenTokenIsValidNr(string token)
|
||
{
|
||
// Arrange
|
||
var nullMessage = default(string);
|
||
|
||
// Act
|
||
var errorMessage = this.ValidateModelWhitValue(token);
|
||
|
||
// Assert
|
||
Assert.AreEqual(errorMessage, nullMessage);
|
||
}
|
||
|
||
private string ValidateModelWhitValue(string value)
|
||
{
|
||
var validationResults = new List<ValidationResult>();
|
||
var validationContext = new ValidationContext(this.model);
|
||
|
||
if (Validator.TryValidateObject(this.model, validationContext, validationResults))
|
||
{
|
||
return validationResults
|
||
.Where(x => x.MemberNames.Contains(nameof(WildcardInputModel.Token)))
|
||
.FirstOrDefault(x => !string.IsNullOrWhiteSpace(x.ErrorMessage))
|
||
?.ErrorMessage ?? default(string);
|
||
}
|
||
|
||
return default(string);
|
||
}
|
||
|
||
private string GetAttributeMessage<T>() where T : ValidationAttribute
|
||
=> this.tokenPropertyInfo
|
||
.GetCustomAttribute<RequiredAttribute>()
|
||
.ErrorMessage;
|
||
}
|
||
}
|