laatzen/LaaProductionWeb/LaaProductionWeb.API.UnitTests/Models/WildcardInputModelTests.cs
2023-05-03 09:56:49 +02:00

146 lines
4.4 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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;
}
}