mirror of
https://github.com/bitwarden/server
synced 2026-01-29 07:43:22 +00:00
Add unit tests for PolicyDataValidator to validate minLength and minComplexity rules
Implemented new test cases to verify the behavior of the ValidateAndSerialize method in PolicyDataValidator. Tests cover scenarios for minimum and maximum values, as well as edge cases for invalid inputs, ensuring robust validation for MasterPassword policy data.
This commit is contained in:
@@ -83,4 +83,100 @@ public class PolicyDataValidatorTests
|
||||
|
||||
Assert.Contains("Invalid data for MasterPassword policy", exception.Message);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ValidateAndSerialize_MinLengthAtMinimum_Succeeds()
|
||||
{
|
||||
var data = new Dictionary<string, object> { { "minLength", 12 } };
|
||||
|
||||
var result = PolicyDataValidator.ValidateAndSerialize(data, PolicyType.MasterPassword);
|
||||
|
||||
Assert.NotNull(result);
|
||||
Assert.Contains("\"minLength\":12", result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ValidateAndSerialize_MinLengthAtMaximum_Succeeds()
|
||||
{
|
||||
var data = new Dictionary<string, object> { { "minLength", 128 } };
|
||||
|
||||
var result = PolicyDataValidator.ValidateAndSerialize(data, PolicyType.MasterPassword);
|
||||
|
||||
Assert.NotNull(result);
|
||||
Assert.Contains("\"minLength\":128", result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ValidateAndSerialize_MinLengthBelowMinimum_ThrowsBadRequestException()
|
||||
{
|
||||
var data = new Dictionary<string, object> { { "minLength", 11 } };
|
||||
|
||||
var exception = Assert.Throws<BadRequestException>(() =>
|
||||
PolicyDataValidator.ValidateAndSerialize(data, PolicyType.MasterPassword));
|
||||
|
||||
Assert.Contains("Invalid data for MasterPassword policy", exception.Message);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ValidateAndSerialize_MinComplexityAtMinimum_Succeeds()
|
||||
{
|
||||
var data = new Dictionary<string, object> { { "minComplexity", 0 } };
|
||||
|
||||
var result = PolicyDataValidator.ValidateAndSerialize(data, PolicyType.MasterPassword);
|
||||
|
||||
Assert.NotNull(result);
|
||||
Assert.Contains("\"minComplexity\":0", result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ValidateAndSerialize_MinComplexityAtMaximum_Succeeds()
|
||||
{
|
||||
var data = new Dictionary<string, object> { { "minComplexity", 4 } };
|
||||
|
||||
var result = PolicyDataValidator.ValidateAndSerialize(data, PolicyType.MasterPassword);
|
||||
|
||||
Assert.NotNull(result);
|
||||
Assert.Contains("\"minComplexity\":4", result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ValidateAndSerialize_MinComplexityBelowMinimum_ThrowsBadRequestException()
|
||||
{
|
||||
var data = new Dictionary<string, object> { { "minComplexity", -1 } };
|
||||
|
||||
var exception = Assert.Throws<BadRequestException>(() =>
|
||||
PolicyDataValidator.ValidateAndSerialize(data, PolicyType.MasterPassword));
|
||||
|
||||
Assert.Contains("Invalid data for MasterPassword policy", exception.Message);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ValidateAndSerialize_NullMinLength_Succeeds()
|
||||
{
|
||||
var data = new Dictionary<string, object>
|
||||
{
|
||||
{ "minComplexity", 2 }
|
||||
// minLength is omitted, should be null
|
||||
};
|
||||
|
||||
var result = PolicyDataValidator.ValidateAndSerialize(data, PolicyType.MasterPassword);
|
||||
|
||||
Assert.NotNull(result);
|
||||
Assert.Contains("\"minComplexity\":2", result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ValidateAndSerialize_MultipleInvalidFields_ThrowsBadRequestException()
|
||||
{
|
||||
var data = new Dictionary<string, object>
|
||||
{
|
||||
{ "minLength", 200 },
|
||||
{ "minComplexity", 10 }
|
||||
};
|
||||
|
||||
var exception = Assert.Throws<BadRequestException>(() =>
|
||||
PolicyDataValidator.ValidateAndSerialize(data, PolicyType.MasterPassword));
|
||||
|
||||
Assert.Contains("Invalid data for MasterPassword policy", exception.Message);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user