Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: or-2514 add validator for voeg lidmaatschap toe request #979

Merged
merged 1 commit into from
Oct 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
113 changes: 79 additions & 34 deletions src/AssociationRegistry.Admin.Api/ValidationMessages.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions src/AssociationRegistry.Admin.Api/ValidationMessages.resx
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,7 @@
<data name="Status404NotFound" xml:space="preserve">
<value>De gevraagde vereniging werd niet gevonden.</value>
</data>
<data name="DatumTotMoetLaterZijnDanDatumVan" xml:space="preserve">
<value>Een einddatum in een periode moet altijd later zijn dan de startdatum.</value>
</data>
</root>
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ public VoegLidmaatschapToeRequest GetExamples()
{
AndereVereniging = "V0001001",
Beschrijving = "De beschrijving van het lidmaatschap.",
DatumVan = new DateOnly(2024, 10, 12),
DatumTot = new DateOnly(2024, 10, 10),
Van = new DateOnly(2024, 10, 12),
Tot = new DateOnly(2024, 10, 10),
Identificatie = "0012",
};
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
namespace AssociationRegistry.Admin.Api.Verenigingen.Lidmaatschap.RequestModels;

using AssociationRegistry.Acties.VoegLidmaatschapToe;
using AssociationRegistry.Vereniging;
using Acties.VoegLidmaatschapToe;
using System.Runtime.Serialization;
using Vereniging;

[DataContract]
public class VoegLidmaatschapToeRequest
Expand All @@ -17,30 +17,30 @@ public class VoegLidmaatschapToeRequest
/// De datum waarop de relatie actief wordt
/// </summary>
[DataMember]
public DateOnly? DatumVan { get; set; }
public DateOnly? Van { get; set; }

/// <summary>
/// De datum waarop de relatie niet meer actief wordt
/// </summary>
[DataMember]
public DateOnly? DatumTot { get; set; }
public DateOnly? Tot { get; set; }

/// <summary>
/// De externe identificatie voor de relatie
/// De externe identificatie voor het lidmaatschap
/// </summary>
[DataMember]
public string Identificatie { get; set; }

/// <summary>
/// De externe beschrijving van de relatie
/// De externe beschrijving van het lidmaatschap
/// </summary>
[DataMember]
public string Beschrijving { get; set; }

public VoegLidmaatschapToeCommand ToCommand(string vCode) => new(
VCode: VCode.Create(vCode),
Lidmaatschap.Create(VCode.Create(AndereVereniging),
new Geldigheidsperiode(new GeldigVan(DatumVan), new GeldigTot(DatumTot)),
new Geldigheidsperiode(new GeldigVan(Van), new GeldigTot(Tot)),
Identificatie,
Beschrijving
)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
namespace AssociationRegistry.Admin.Api.Verenigingen.Lidmaatschap.RequestModels;

using FluentValidation;
using Infrastructure.Validation;

public class VoegLidmaatschapToeRequestValidator : AbstractValidator<VoegLidmaatschapToeRequest>
{
public VoegLidmaatschapToeRequestValidator()
{
RuleFor(r => r.AndereVereniging)
.Must(andereVereniging => !string.IsNullOrEmpty(andereVereniging))
.WithMessage(ValidationMessages.VeldIsVerplicht);

RuleFor(r => r.Tot)
.GreaterThanOrEqualTo(x => x.Van)
.When(x => x.Van.HasValue)
.WithMessage(ValidationMessages.DatumTotMoetLaterZijnDanDatumVan);

RuleFor(r => r.Identificatie).MustNotContainHtml();
RuleFor(r => r.Beschrijving).MustNotContainHtml();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ public void Then_We_Get_A_Correct_Command()

command.VCode.Should().Be(vCode);
command.Lidmaatschap.AndereVereniging.Should().Be(VCode.Create(request.AndereVereniging));
command.Lidmaatschap.Geldigheidsperiode.Van.Should().Be(new GeldigVan(request.DatumVan));
command.Lidmaatschap.Geldigheidsperiode.Tot.Should().Be(new GeldigTot(request.DatumTot));
command.Lidmaatschap.Geldigheidsperiode.Van.Should().Be(new GeldigVan(request.Van));
command.Lidmaatschap.Geldigheidsperiode.Tot.Should().Be(new GeldigTot(request.Tot));
command.Lidmaatschap.Identificatie.Should().Be(request.Identificatie);
command.Lidmaatschap.Beschrijving.Should().Be(request.Beschrijving);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
namespace AssociationRegistry.Test.Admin.Api.Commands.FeitelijkeVereniging.When_Adding_Lidmaatschap.RequestValidating;

using AssociationRegistry.Admin.Api;
using AssociationRegistry.Admin.Api.Verenigingen.Lidmaatschap.RequestModels;
using AutoFixture;
using Common.AutoFixture;
using FluentValidation.TestHelper;
using Microsoft.AspNetCore.Http;
using Resources;
using Xunit;
using Xunit.Categories;
using ValidatorTest = Framework.ValidatorTest;

[UnitTest]
public class A_Invalid_Request : ValidatorTest
{
private readonly Fixture _fixture;
private readonly VoegLidmaatschapToeRequestValidator _validator;

public A_Invalid_Request()
{
_fixture = new Fixture().CustomizeAdminApi();
_validator = new VoegLidmaatschapToeRequestValidator();
}

[Fact]
public void Has_validation_errors_for_andereVereniging_when_empty()
{
var request = _fixture.Create<VoegLidmaatschapToeRequest>();
request.AndereVereniging = "";

var result = _validator.TestValidate(request);

result.ShouldHaveValidationErrorFor(x => x.AndereVereniging)
.WithErrorMessage(ValidationMessages.VeldIsVerplicht);
}

[Fact]
public void Has_validation_errors_for_andereVereniging_when_null()
{
var request = _fixture.Create<VoegLidmaatschapToeRequest>();
request.AndereVereniging = null;

var result = _validator.TestValidate(request);

result.ShouldHaveValidationErrorFor(x => x.AndereVereniging)
.WithErrorMessage(ValidationMessages.VeldIsVerplicht);
}

[Fact]
public void Has_validation_errors_when_tot_after_van()
{
var request = _fixture.Create<VoegLidmaatschapToeRequest>();
request.Van = DateOnly.FromDateTime(DateTime.Now);
request.Tot = DateOnly.FromDateTime(DateTime.Now.AddDays(-1));

var result = _validator.TestValidate(request);
result.ShouldHaveValidationErrorFor(x => x.Tot)
.WithErrorMessage(ValidationMessages.DatumTotMoetLaterZijnDanDatumVan);
}

[Fact]
public void Has_no_validation_errors_when_tot_not_after_van()
{
var request = _fixture.Create<VoegLidmaatschapToeRequest>();
request.Van = DateOnly.FromDateTime(DateTime.Now);
request.Tot = DateOnly.FromDateTime(DateTime.Now.AddDays(1));

var result = _validator.TestValidate(request);
result.ShouldNotHaveAnyValidationErrors();
}

[Fact]
public void Has_validation_errors_when_html_detected_in_identificatie()
{
var request = _fixture.Create<VoegLidmaatschapToeRequest>();
request.Identificatie = "<p>Something something</p>";

var result = _validator.TestValidate(request);
result.ShouldHaveValidationErrorFor(r => r.Identificatie)
.WithErrorCode(StatusCodes.Status400BadRequest.ToString())
.WithErrorMessage(ExceptionMessages.UnsupportedContent);
}

[Fact]
public void Has_validation_errors_when_html_detected_in_beschrijving()
{
var request = _fixture.Create<VoegLidmaatschapToeRequest>();
request.Beschrijving = "<p>Something something</p>";

var result = _validator.TestValidate(request);
result.ShouldHaveValidationErrorFor(r => r.Beschrijving)
.WithErrorCode(StatusCodes.Status400BadRequest.ToString())
.WithErrorMessage(ExceptionMessages.UnsupportedContent);
}
}
Loading
Loading