diff --git a/modules/cms-kit/src/Volo.CmsKit.Public.Application.Contracts/Volo/CmsKit/Public/Comments/CreateCommentInput.cs b/modules/cms-kit/src/Volo.CmsKit.Public.Application.Contracts/Volo/CmsKit/Public/Comments/CreateCommentInput.cs index cc5ee3efec..d17e8c9deb 100644 --- a/modules/cms-kit/src/Volo.CmsKit.Public.Application.Contracts/Volo/CmsKit/Public/Comments/CreateCommentInput.cs +++ b/modules/cms-kit/src/Volo.CmsKit.Public.Application.Contracts/Volo/CmsKit/Public/Comments/CreateCommentInput.cs @@ -21,5 +21,6 @@ public class CreateCommentInput : ExtensibleObject public string Url { get; set; } + [Required] public string IdempotencyToken { get; set; } } diff --git a/modules/cms-kit/src/Volo.CmsKit.Public.Application.Contracts/Volo/CmsKit/Public/Comments/CreateCommentWithParametersInput.cs b/modules/cms-kit/src/Volo.CmsKit.Public.Application.Contracts/Volo/CmsKit/Public/Comments/CreateCommentWithParametersInput.cs index ec7aad1398..fda4856ffb 100644 --- a/modules/cms-kit/src/Volo.CmsKit.Public.Application.Contracts/Volo/CmsKit/Public/Comments/CreateCommentWithParametersInput.cs +++ b/modules/cms-kit/src/Volo.CmsKit.Public.Application.Contracts/Volo/CmsKit/Public/Comments/CreateCommentWithParametersInput.cs @@ -26,5 +26,6 @@ public class CreateCommentWithParametersInput public string Url { get; set; } + [Required] public string IdempotencyToken { get; set; } } diff --git a/modules/cms-kit/test/Volo.CmsKit.Application.Tests/Comments/CommentPublicAppService_Tests.cs b/modules/cms-kit/test/Volo.CmsKit.Application.Tests/Comments/CommentPublicAppService_Tests.cs index cbb165c1cd..d01fc295cf 100644 --- a/modules/cms-kit/test/Volo.CmsKit.Application.Tests/Comments/CommentPublicAppService_Tests.cs +++ b/modules/cms-kit/test/Volo.CmsKit.Application.Tests/Comments/CommentPublicAppService_Tests.cs @@ -1,4 +1,5 @@ -using System.Linq; +using System; +using System.Linq; using System.Threading.Tasks; using Microsoft.Extensions.DependencyInjection; using NSubstitute; @@ -48,7 +49,8 @@ public class CommentPublicAppService_Tests : CmsKitApplicationTestBase new CreateCommentInput { RepliedCommentId = null, - Text = "newComment" + Text = "newComment", + IdempotencyToken = Guid.NewGuid().ToString("N") } ); @@ -75,7 +77,8 @@ public class CommentPublicAppService_Tests : CmsKitApplicationTestBase new CreateCommentInput { RepliedCommentId = null, - Text = text + Text = text, + IdempotencyToken = Guid.NewGuid().ToString("N") } ); } @@ -95,6 +98,25 @@ public class CommentPublicAppService_Tests : CmsKitApplicationTestBase { RepliedCommentId = null, Text = text, //not allowed URL + IdempotencyToken = Guid.NewGuid().ToString("N") + } + )); + } + + [Fact] + public async Task CreateAsync_ShouldThrowUserFriendlyException_If_IdempotencyToken_Not_Unique() + { + _currentUser.Id.Returns(_cmsKitTestData.User2Id); + + await Should.ThrowAsync(async () => + await _commentAppService.CreateAsync( + _cmsKitTestData.EntityType1, + _cmsKitTestData.EntityId1, + new CreateCommentInput + { + RepliedCommentId = null, + Text = "", + IdempotencyToken = _cmsKitTestData.IdempotencyToken_1 } )); } diff --git a/modules/cms-kit/test/Volo.CmsKit.TestBase/CmsKitDataSeedContributor.cs b/modules/cms-kit/test/Volo.CmsKit.TestBase/CmsKitDataSeedContributor.cs index af5ee0754a..f17a95ddba 100644 --- a/modules/cms-kit/test/Volo.CmsKit.TestBase/CmsKitDataSeedContributor.cs +++ b/modules/cms-kit/test/Volo.CmsKit.TestBase/CmsKitDataSeedContributor.cs @@ -194,7 +194,7 @@ public class CmsKitDataSeedContributor : IDataSeedContributor, ITransientDepende "comment", null, _cmsKitTestData.User1Id - )); + ){ IdempotencyToken = _cmsKitTestData.IdempotencyToken_1 }); await _commentRepository.InsertAsync(new Comment(_guidGenerator.Create(), _cmsKitTestData.EntityType1, diff --git a/modules/cms-kit/test/Volo.CmsKit.TestBase/CmsKitTestData.cs b/modules/cms-kit/test/Volo.CmsKit.TestBase/CmsKitTestData.cs index cfa54d2e55..8c4c07e283 100644 --- a/modules/cms-kit/test/Volo.CmsKit.TestBase/CmsKitTestData.cs +++ b/modules/cms-kit/test/Volo.CmsKit.TestBase/CmsKitTestData.cs @@ -131,4 +131,6 @@ public class CmsKitTestData : ISingletonDependency public string PollName { get; } = "Poll"; public string WidgetName { get; } = "CmsPollByCode"; + + public string IdempotencyToken_1 { get; } = Guid.NewGuid().ToString("N"); }