Files
server/test/Core.Test/Utilities/ValidateSequenceAttributeTests.cs
Rui Tomé 2a52362d33 [PM-34387] Add organization invite link creation endpoint (#7477)
* Add ConflictError type

* Add generic Handle<T> and extract MapError on BaseAdminConsoleController

* Initialize Code property with a new GUID in OrganizationInviteLink class

* Add ICreateOrganizationInviteLinkCommand interface

* Add CreateOrganizationInviteLinkRequest record for invite link creation

* Add OrganizationInviteLink request and response models for invite link management

* Refactor ICreateOrganizationInviteLinkCommand interface to use CreateOrganizationInviteLinkRequest for invite link creation

* Add CreateOrganizationInviteLinkCommand class to handle invite link creation logic, including domain sanitization and validation checks.

* Add error handling for invite link creation with specific conflict and validation errors

* Add OrganizationInviteLink service commands to OrganizationServiceCollectionExtensions

* Add OrganizationInviteLinksController to manage invite link creation for organizations

* Add integration tests for OrganizationInviteLinksController and CreateOrganizationInviteLinkCommand to validate invite link creation logic, including success and error scenarios.

* Remove unnecessary blank line in OrganizationInviteLinksControllerTests class

* Refactor CreateOrganizationInviteLinkRequestModel to use required properties for AllowedDomains and EncryptedInviteKey

* Update CreateOrganizationInviteLinkCommand to validate allowed domains by using DomainNameValidator

* Add encryption validation attributes to CreateOrganizationInviteLinkRequestModel and implement unit tests for model validation

* Refactor OrganizationInviteLink to encapsulate AllowedDomains serialization logic within methods. Update OrganizationInviteLinkResponseModel to utilize new GetAllowedDomains method for improved clarity and maintainability.

* Enhance domain sanitization in CreateOrganizationInviteLinkCommand by converting domains to lowercase during trimming for improved consistency.

* Update OrganizationInviteLinksControllerTests to use a valid encrypted invite key constant for consistency in test cases.

* Add ability check for organization invite links in CreateOrganizationInviteLinkCommand

- Introduced a new method to verify if an organization can use invite links based on its ability.
- Added a new error type for cases where invite links are not available due to organizational plan restrictions.
- Updated tests to cover scenarios where the organization lacks the ability to create invite links.

* Add documentation for Code property in OrganizationInviteLink class

- Added XML summary comments to the Code property to clarify its purpose and generation method.
- Explained the choice of using Guid.NewGuid for the Code to avoid predictability and ensure uniqueness.

* Implement domain validation in CreateOrganizationInviteLinkRequestModel

- Added IValidatableObject implementation to CreateOrganizationInviteLinkRequestModel for domain validation.
- Introduced Validate method to check the format of allowed domains and return appropriate validation results.
- Updated tests to cover scenarios for invalid domain formats and mixed valid/invalid domains.
- Removed redundant domain validation logic from CreateOrganizationInviteLinkCommand.

* Remove outdated tests from CreateOrganizationInviteLinkRequestModelTests

- Deleted tests for validating EncryptedInviteKey and EncryptedOrgKey as they are no longer relevant.
- Cleaned up the test class to focus on current validation logic for allowed domains.

* Refactor GetAllowedDomains method in OrganizationInviteLink class

- Updated the GetAllowedDomains method to return an empty array instead of throwing a JsonException when deserialization fails.
- This change improves the method's resilience by providing a default value for invalid or missing allowed domains.

* Remove unused InviteLinkInvalidDomains error type from Errors.cs

- Deleted the InviteLinkInvalidDomains record as it is no longer needed.
- This cleanup aligns with recent changes in domain validation logic and improves code maintainability.

* Update OrganizationServiceCollectionExtensions to use TryAddScoped for command registration

- Changed the registration of ICreateOrganizationInviteLinkCommand to use TryAddScoped instead of AddScoped.

* Mock organization ability retrieval in OrganizationInviteLinksControllerTests

* Add ValidateSequenceAttribute for collection validation and corresponding unit tests

* Refactor CreateOrganizationInviteLinkRequestModel to use ValidateSequenceAttribute for domain validation and update unit tests for improved error handling.

* Enhance ValidateSequenceAttribute to handle null values and improve error messaging format

* Add empty line

* Refactor ValidateSequenceAttribute to support IEnumerable interface for improved type handling

* Refactor ValidateSequenceAttribute to improve validation logic and error handling for IEnumerable types

* Remove unused using directive for Microsoft.AspNetCore.Http.HttpResults in BaseAdminConsoleController.cs

* Add MinLength validation to AllowedDomains in CreateOrganizationInviteLinkRequestModel and implement unit test for empty AllowedDomains scenario

* Refactor CreateOrganizationInviteLinkCommandTests to move SetupAbility method for better organization and readability

* Add error handling methods in BaseAdminConsoleController for improved response management

* Update CreateOrganizationInviteLinkRequestModelTests to use array initialization syntax for AllowedDomains so that MinLength attribute works

* Refactor OrganizationInviteLinkResponseModel constructor for improved readability
2026-05-01 14:23:56 +01:00

56 lines
1.7 KiB
C#

using System.ComponentModel.DataAnnotations;
using Bit.Core.Utilities;
using Xunit;
namespace Bit.Core.Test.Utilities;
public class ValidateSequenceAttributeTests
{
[Fact]
public void IsValid_WithEmptyCollection_ReturnsSuccess()
{
var result = Validate([]);
Assert.Equal(ValidationResult.Success, result);
}
[Fact]
public void IsValid_WithNullCollection_ReturnsSuccess()
{
var result = Validate(null);
Assert.Equal(ValidationResult.Success, result);
}
[Fact]
public void IsValid_WithSomeInvalidItems_ReturnsErrorMessage()
{
var result = Validate(["bad", "also-bad", "ok"]);
Assert.NotNull(result);
Assert.Contains("'bad'", result!.ErrorMessage);
Assert.Contains("'also-bad'", result.ErrorMessage);
Assert.DoesNotContain("ok", result.ErrorMessage);
}
/// <summary>
/// Invokes <see cref="ValidateSequenceAttribute{TValidator}"/> directly so edge cases
/// (null/empty collection) can be tested in isolation, independent of any model's
/// <c>[Required]</c> attribute which would intercept those cases first.
/// </summary>
private static ValidationResult? Validate(IEnumerable<string>? value)
{
var attr = new ValidateSequenceAttribute<OnlyAcceptsOkValidator>();
return attr.GetValidationResult(value, new ValidationContext(new object()));
}
/// <summary>
/// Accepts only the literal string "ok" so tests are not coupled to
/// <see cref="DomainNameValidatorAttribute"/> regex rules.
/// </summary>
private class OnlyAcceptsOkValidator : ValidationAttribute
{
public override bool IsValid(object? value) => value?.ToString() == "ok";
}
}