Add test to make sure validation works in regular case. Add whitespace and * to validation regex

This commit is contained in:
Jill Bender 2019-01-17 11:32:37 -08:00
Родитель 5f0f68d5e4
Коммит dbf7f09553
2 изменённых файлов: 38 добавлений и 6 удалений

Просмотреть файл

@ -457,6 +457,21 @@ namespace Services.Test
await Assert.ThrowsAsync<InvalidInputException>(async () => await this.rules.UpsertIfNotDeletedAsync(rule)); await Assert.ThrowsAsync<InvalidInputException>(async () => await this.rules.UpsertIfNotDeletedAsync(rule));
} }
[Fact, Trait(Constants.TYPE, Constants.UNIT_TEST)]
public void InputValidationPassesWithValidRule()
{
// Arrange
this.ThereAreSomeRulesInStorage();
List<Rule> rulesList = this.GetSampleRulesList();
// Act & Assert
foreach (var rule in rulesList)
{
rule.Validate();
}
}
private void ThereAreNoRulessInStorage() private void ThereAreNoRulessInStorage()
{ {
this.rulesMock.Setup(x => x.GetListAsync(null, 0, LIMIT, null, false)) this.rulesMock.Setup(x => x.GetListAsync(null, 0, LIMIT, null, false))
@ -464,6 +479,14 @@ namespace Services.Test
} }
private void ThereAreSomeRulesInStorage() private void ThereAreSomeRulesInStorage()
{
var sampleRules = this.GetSampleRulesList();
this.rulesMock.Setup(x => x.GetListAsync(null, 0, LIMIT, null, false))
.ReturnsAsync(sampleRules);
}
private List<Rule> GetSampleRulesList()
{ {
var sampleConditions = new List<Condition> var sampleConditions = new List<Condition>
{ {
@ -507,11 +530,22 @@ namespace Services.Test
Severity = SeverityType.Warning, Severity = SeverityType.Warning,
Conditions = sampleConditions, Conditions = sampleConditions,
Actions = sampleActions Actions = sampleActions
},
new Rule()
{
ETag = "*",
Name = "Sample 3",
Enabled = true,
Calculation = CalculationType.Instant,
Description = "Sample description 2.",
GroupId = "Chillers",
Severity = SeverityType.Warning,
Conditions = sampleConditions,
Actions = sampleActions
} }
}; };
this.rulesMock.Setup(x => x.GetListAsync(null, 0, LIMIT, null, false)) return sampleRules;
.ReturnsAsync(sampleRules);
} }
/** /**

Просмотреть файл

@ -5,14 +5,12 @@ namespace Microsoft.Azure.IoTSolutions.DeviceTelemetry.Services.Helpers
{ {
public class InputValidator public class InputValidator
{ {
private const string INVALID_CHARACTER = @"[^A-Za-z0-9:;.,_\-*]"; private const string INVALID_CHARACTER = @"[^A-Za-z0-9:;.!,_\-* ]";
// Check illegal characters in input // Check illegal characters in input
public static void Validate(string input) public static void Validate(string input)
{ {
input = input.Trim(); if (Regex.IsMatch(input.Trim(), INVALID_CHARACTER))
if (Regex.IsMatch(input, INVALID_CHARACTER))
{ {
throw new InvalidInputException($"Input '{input}' contains invalid characters."); throw new InvalidInputException($"Input '{input}' contains invalid characters.");
} }