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

fix(webapi): Only allow transmissionId on TransmissionOpened activities #1631

Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -434,11 +434,11 @@ public CreateDialogDialogActivityDtoValidator(
.When(x => x.Type != DialogActivityType.Values.Information);
RuleFor(x => x.TransmissionId)
.Null()
.WithMessage($"A {nameof(DialogActivityType.Values.DialogOpened)} activity cannot reference a transmission.")
.When(x => x.Type == DialogActivityType.Values.DialogOpened);
.WithMessage($"Only activities of type {nameof(DialogActivityType.Values.TransmissionOpened)} can reference a transmission.")
.When(x => x.Type != DialogActivityType.Values.TransmissionOpened);
RuleFor(x => x.TransmissionId)
.NotEmpty()
.WithMessage($"A {nameof(DialogActivityType.Values.TransmissionOpened)} needs to reference a transmission.")
.WithMessage($"An activity of type {nameof(DialogActivityType.Values.TransmissionOpened)} needs to reference a transmission.")
.When(x => x.Type == DialogActivityType.Values.TransmissionOpened);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -400,11 +400,11 @@ public UpdateDialogDialogActivityDtoValidator(
.When(x => x.Type != DialogActivityType.Values.Information);
RuleFor(x => x.TransmissionId)
.Null()
.WithMessage($"A {nameof(DialogActivityType.Values.DialogOpened)} activity cannot reference a transmission.")
.When(x => x.Type == DialogActivityType.Values.DialogOpened);
.WithMessage($"Only activities of type {nameof(DialogActivityType.Values.TransmissionOpened)} can reference a transmission.")
.When(x => x.Type != DialogActivityType.Values.TransmissionOpened);
RuleFor(x => x.TransmissionId)
.NotEmpty()
.WithMessage($"A {nameof(DialogActivityType.Values.TransmissionOpened)} needs to reference a transmission.")
.WithMessage($"An activity of type {nameof(DialogActivityType.Values.TransmissionOpened)} needs to reference a transmission.")
.When(x => x.Type == DialogActivityType.Values.TransmissionOpened);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
using AutoMapper;
using Digdir.Domain.Dialogporten.Application.Features.V1.Common.Localizations;
using Digdir.Domain.Dialogporten.Application.Features.V1.ServiceOwner.Common.Actors;
using Digdir.Domain.Dialogporten.Application.Features.V1.ServiceOwner.Dialogs.Commands.Create;
using Digdir.Domain.Dialogporten.Application.Features.V1.ServiceOwner.Dialogs.Commands.Update;
using Digdir.Domain.Dialogporten.Domain.Dialogs.Entities.Activities;
using Digdir.Library.Entity.Abstractions.Features.Identifiable;
using Digdir.Tool.Dialogporten.GenerateFakeData;
using FluentAssertions;

using UpdateActivityDto =
Digdir.Domain.Dialogporten.Application.Features.V1.ServiceOwner.Dialogs.Commands.Update.ActivityDto;
using CreateActivityDto =
Digdir.Domain.Dialogporten.Application.Features.V1.ServiceOwner.Dialogs.Commands.Create.ActivityDto;

namespace Digdir.Domain.Dialogporten.Application.Unit.Tests.Features.V1.ServiceOwner.Activities;

public class ActivityValidatorTests
{
public static IEnumerable<object[]> ActivityTypes() =>
from DialogActivityType.Values activityType in Enum.GetValues(typeof(DialogActivityType.Values))
select new object[] { activityType, };

[Theory, MemberData(nameof(ActivityTypes))]
public void Only_TransmissionOpened_Is_Allowed_To_Set_TransmissionId(
DialogActivityType.Values activityType)
{
// Arrange
var mapper = new MapperConfiguration(cfg => cfg.CreateMap<CreateActivityDto, UpdateActivityDto>()).CreateMapper();

var activity = DialogGenerator.GenerateFakeDialogActivity(type: activityType);
activity.TransmissionId = IdentifiableExtensions.CreateVersion7();

var localizationValidator = new LocalizationDtosValidator();
var actorValidator = new ActorValidator();

var createValidator = new CreateDialogDialogActivityDtoValidator(localizationValidator, actorValidator);
var updateValidator = new UpdateDialogDialogActivityDtoValidator(localizationValidator, actorValidator);

// Act
var createValidation = createValidator.Validate(activity);
var updateValidation = updateValidator.Validate(mapper.Map<UpdateActivityDto>(activity));

// Assert
if (activityType == DialogActivityType.Values.TransmissionOpened)
{
createValidation.IsValid.Should().BeTrue();
updateValidation.IsValid.Should().BeTrue();
}
else
{
createValidation.IsValid.Should().BeFalse();
updateValidation.IsValid.Should().BeFalse();

createValidation.Errors.Should().ContainSingle();
updateValidation.Errors.Should().ContainSingle();

createValidation.Errors.First().ErrorMessage.Should().Contain("TransmissionOpened");
updateValidation.Errors.First().ErrorMessage.Should().Contain("TransmissionOpened");
}
}
}
Loading