mirror of
https://github.com/bitwarden/server
synced 2026-01-29 07:43:22 +00:00
* Enhance MasterPasswordPolicyData with validation attributes Added data annotations for MinComplexity and MinLength properties to enforce validation rules. MinComplexity must be between 0 and 4, and MinLength must be between 12 and 128. * Implement model validation in PolicyDataValidator and enhance error handling Added a ValidateModel method to enforce validation rules for policy data. Updated error messages to provide clearer feedback on validation failures. Enhanced unit tests to cover new validation scenarios for MinLength and MinComplexity properties. * Update PoliciesControllerTests to reflect new validation rules for MinComplexity and MinLength Modified test cases to use updated values for MinComplexity (4) and MinLength (128). Added new tests to verify that excessive values for these properties return BadRequest responses. Ensured consistency across integration tests for both Admin and Public controllers. * Enhance MasterPasswordPolicyData with XML documentation for properties Added XML documentation comments for MinComplexity and MinLength properties to clarify their purpose and constraints. This improves code readability and provides better context for developers using the model. * 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.
183 lines
6.0 KiB
C#
183 lines
6.0 KiB
C#
using Bit.Core.AdminConsole.Enums;
|
|
using Bit.Core.AdminConsole.OrganizationFeatures.Policies.Models;
|
|
using Bit.Core.AdminConsole.Utilities;
|
|
using Bit.Core.Exceptions;
|
|
using Xunit;
|
|
|
|
namespace Bit.Core.Test.AdminConsole.Utilities;
|
|
|
|
public class PolicyDataValidatorTests
|
|
{
|
|
[Fact]
|
|
public void ValidateAndSerialize_NullData_ReturnsNull()
|
|
{
|
|
var result = PolicyDataValidator.ValidateAndSerialize(null, PolicyType.MasterPassword);
|
|
|
|
Assert.Null(result);
|
|
}
|
|
|
|
[Fact]
|
|
public void ValidateAndSerialize_ValidData_ReturnsSerializedJson()
|
|
{
|
|
var data = new Dictionary<string, object>
|
|
{
|
|
{ "minLength", 12 },
|
|
{ "minComplexity", 4 }
|
|
};
|
|
|
|
var result = PolicyDataValidator.ValidateAndSerialize(data, PolicyType.MasterPassword);
|
|
|
|
Assert.NotNull(result);
|
|
Assert.Contains("\"minLength\":12", result);
|
|
Assert.Contains("\"minComplexity\":4", result);
|
|
}
|
|
|
|
[Fact]
|
|
public void ValidateAndSerialize_InvalidDataType_ThrowsBadRequestException()
|
|
{
|
|
var data = new Dictionary<string, object> { { "minLength", "not a number" } };
|
|
|
|
var exception = Assert.Throws<BadRequestException>(() =>
|
|
PolicyDataValidator.ValidateAndSerialize(data, PolicyType.MasterPassword));
|
|
|
|
Assert.Contains("Invalid data for MasterPassword policy", exception.Message);
|
|
Assert.Contains("minLength", exception.Message);
|
|
}
|
|
|
|
[Fact]
|
|
public void ValidateAndDeserializeMetadata_NullMetadata_ReturnsEmptyMetadataModel()
|
|
{
|
|
var result = PolicyDataValidator.ValidateAndDeserializeMetadata(null, PolicyType.SingleOrg);
|
|
|
|
Assert.IsType<EmptyMetadataModel>(result);
|
|
}
|
|
|
|
[Fact]
|
|
public void ValidateAndDeserializeMetadata_ValidMetadata_ReturnsModel()
|
|
{
|
|
var metadata = new Dictionary<string, object> { { "defaultUserCollectionName", "collection name" } };
|
|
|
|
var result = PolicyDataValidator.ValidateAndDeserializeMetadata(metadata, PolicyType.OrganizationDataOwnership);
|
|
|
|
Assert.IsType<OrganizationModelOwnershipPolicyModel>(result);
|
|
}
|
|
|
|
[Fact]
|
|
public void ValidateAndSerialize_ExcessiveMinLength_ThrowsBadRequestException()
|
|
{
|
|
var data = new Dictionary<string, object> { { "minLength", 129 } };
|
|
|
|
var exception = Assert.Throws<BadRequestException>(() =>
|
|
PolicyDataValidator.ValidateAndSerialize(data, PolicyType.MasterPassword));
|
|
|
|
Assert.Contains("Invalid data for MasterPassword policy", exception.Message);
|
|
}
|
|
|
|
[Fact]
|
|
public void ValidateAndSerialize_ExcessiveMinComplexity_ThrowsBadRequestException()
|
|
{
|
|
var data = new Dictionary<string, object> { { "minComplexity", 5 } };
|
|
|
|
var exception = Assert.Throws<BadRequestException>(() =>
|
|
PolicyDataValidator.ValidateAndSerialize(data, PolicyType.MasterPassword));
|
|
|
|
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);
|
|
}
|
|
}
|