Files
server/test/Api.Test/AdminConsole/Models/Request/Organizations/UpdateOrganizationInviteLinkRequestModelTests.cs
Rui Tomé 5289a5fd8c [PM-34388] Add organization invite link update endpoint (#7560)
* Add InviteLinkDomainSanitizer utility for domain normalization

- Introduced InviteLinkDomainSanitizer class to normalize invite link domains by converting them to lowercase and removing blank entries.
- The SanitizeDomains method processes a list of domains, ensuring clean and valid entries for further use.

* Refactor CreateOrganizationInviteLinkCommand to use InviteLinkDomainSanitizer

* Add UpdateOrganizationInviteLinkRequest record for managing invite link updates

- Introduced a new record, UpdateOrganizationInviteLinkRequest, to encapsulate the data required for updating organization invite links.
- The record includes properties for OrganizationId and AllowedDomains, ensuring structured data handling for invite link modifications.

* Add UpdateOrganizationInviteLinkRequestModel for invite link updates

- Introduced UpdateOrganizationInviteLinkRequestModel class to facilitate the update of organization invite links.
- The model includes a required property for AllowedDomains, ensuring validation and structured data handling for invite link modifications.

* Add tests for UpdateOrganizationInviteLinkRequestModel validation

* Add unit tests for UpdateOrganizationInviteLinkCommand

- Introduced comprehensive tests for the UpdateOrganizationInviteLinkCommand, covering scenarios such as successful updates, handling of non-existing links, and validation of allowed domains.
- Ensured that the command behaves correctly under various conditions, including ability checks and input validation.

* Add UpdateOrganizationInviteLinkCommand and IUpdateOrganizationInviteLinkCommand interface

- Implemented UpdateOrganizationInviteLinkCommand to handle updates for organization invite links, including validation of allowed domains and organization abilities.
- Created IUpdateOrganizationInviteLinkCommand interface to define the contract for updating invite links, ensuring structured handling of update requests.

* Add IUpdateOrganizationInviteLinkCommand registration to service collection

- Registered the IUpdateOrganizationInviteLinkCommand interface with the service collection, enabling dependency injection for the UpdateOrganizationInviteLinkCommand functionality.

* Add Update endpoint to OrganizationInviteLinksController

- Implemented the Update method in OrganizationInviteLinksController to handle updates for organization invite links.
- The method utilizes the IUpdateOrganizationInviteLinkCommand to process update requests and returns the appropriate response model.
- Enhanced the controller's functionality to support invite link modifications, ensuring better management of organization invite links.

* Add unit tests for Update method in OrganizationInviteLinksController

- Implemented multiple test cases for the Update method, covering scenarios such as successful updates, handling of non-existing invite links, and validation errors for allowed domains.
- Ensured comprehensive coverage of the Update functionality to validate correct behavior and response models in various conditions.

* Add integration test for updating organization invite links

* fix(invite-link): add [MinLength(1)] to Update request model and matching test
2026-05-04 16:46:40 +01:00

77 lines
2.2 KiB
C#

using System.ComponentModel.DataAnnotations;
using Bit.Api.AdminConsole.Models.Request.Organizations;
using Xunit;
namespace Bit.Api.Test.AdminConsole.Models.Request.Organizations;
public class UpdateOrganizationInviteLinkRequestModelTests
{
[Fact]
public void Validate_WithValidModel_ReturnsNoErrors()
{
var model = new UpdateOrganizationInviteLinkRequestModel
{
AllowedDomains = ["acme.com"],
};
var results = Validate(model);
Assert.Empty(results);
}
[Theory]
[InlineData("not a domain")]
[InlineData("<script>alert(1)</script>")]
[InlineData("double..dot.com")]
[InlineData("-starts-with-hyphen.com")]
[InlineData(" acme.com ")]
public void Validate_WithInvalidDomainFormat_ReturnsError(string invalidDomain)
{
var model = new UpdateOrganizationInviteLinkRequestModel
{
AllowedDomains = [invalidDomain],
};
var results = Validate(model);
Assert.Single(results);
Assert.Contains(results, r => r.MemberNames.Contains(nameof(model.AllowedDomains)));
}
[Fact]
public void Validate_WithEmptyAllowedDomains_ReturnsError()
{
var model = new UpdateOrganizationInviteLinkRequestModel
{
AllowedDomains = [],
};
var results = Validate(model);
Assert.Single(results);
Assert.Contains(results, r => r.MemberNames.Contains(nameof(model.AllowedDomains)));
}
[Fact]
public void Validate_WithMixedValidAndInvalidDomains_ReturnsError()
{
var model = new UpdateOrganizationInviteLinkRequestModel
{
AllowedDomains = ["acme.com", "not a domain", "<script>"],
};
var results = Validate(model);
var error = Assert.Single(results);
Assert.Contains("'not a domain'", error.ErrorMessage);
Assert.Contains("'<script>'", error.ErrorMessage);
}
private static List<ValidationResult> Validate(UpdateOrganizationInviteLinkRequestModel model)
{
var results = new List<ValidationResult>();
Validator.TryValidateObject(model, new ValidationContext(model), results, true);
return results;
}
}