From eede03d12850bdbe77f6b5171e68e083a18ae7af Mon Sep 17 00:00:00 2001 From: support Date: Mon, 23 Nov 2026 07:10:32 +0100 Subject: [PATCH 1/2] Check the stored customer group, not the posted flag, before an API update --- .../Controllers/CustomerGroupController.cs | 16 ++- .../CustomerGroupControllerTests.cs | 109 ++++++++++++++++++ 2 files changed, 119 insertions(+), 6 deletions(-) create mode 100644 src/Tests/Grand.Module.Api.Tests/Controllers/CustomerGroupControllerTests.cs diff --git a/src/Modules/Grand.Module.Api/Controllers/CustomerGroupController.cs b/src/Modules/Grand.Module.Api/Controllers/CustomerGroupController.cs index da5cf06a7..e4bfd1f61 100644 --- a/src/Modules/Grand.Module.Api/Controllers/CustomerGroupController.cs +++ b/src/Modules/Grand.Module.Api/Controllers/CustomerGroupController.cs @@ -73,17 +73,21 @@ public async Task Post([FromBody] CustomerGroupDto model) [ProducesResponseType(StatusCodes.Status403Forbidden)] [ProducesResponseType(StatusCodes.Status200OK, Type = typeof(CustomerGroupDto))] [ProducesResponseType(StatusCodes.Status400BadRequest)] + [ProducesResponseType(StatusCodes.Status404NotFound)] public async Task Put([FromBody] CustomerGroupDto model) { + if (string.IsNullOrEmpty(model.Id)) + return BadRequest("Key is null or empty"); + if (!await _permissionService.Authorize(PermissionSystemName.Customers, CurrentCustomer)) return Forbid(); - if (!model.IsSystem) - { - model = await _mediator.Send(new UpdateCustomerGroupCommand { Model = model }); - return Ok(model); - } + var customerGroup = await _mediator.Send(new GetGenericQuery(model.Id)); + if (!customerGroup.Any()) return NotFound(); + + if (customerGroup.FirstOrDefault()!.IsSystem) return BadRequest(ModelState); - return BadRequest(ModelState); + model = await _mediator.Send(new UpdateCustomerGroupCommand { Model = model }); + return Ok(model); } [EndpointDescription("Partially update entity in CustomerGroup")] diff --git a/src/Tests/Grand.Module.Api.Tests/Controllers/CustomerGroupControllerTests.cs b/src/Tests/Grand.Module.Api.Tests/Controllers/CustomerGroupControllerTests.cs new file mode 100644 index 000000000..e52c17f83 --- /dev/null +++ b/src/Tests/Grand.Module.Api.Tests/Controllers/CustomerGroupControllerTests.cs @@ -0,0 +1,109 @@ +using Grand.Business.Core.Interfaces.Common.Security; +using Grand.Domain.Customers; +using Grand.Domain.Permissions; +using Grand.Infrastructure; +using Grand.Mediator; +using Grand.Module.Api.Commands.Models.Customers; +using Grand.Module.Api.Controllers; +using Grand.Module.Api.DTOs.Customers; +using Grand.Module.Api.Queries.Models.Common; +using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.JsonPatch.SystemTextJson; +using Microsoft.AspNetCore.Mvc; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using Moq; + +namespace Grand.Module.Api.Tests.Controllers; + +[TestClass] +public class CustomerGroupControllerTests +{ + private Mock _mediatorMock; + private CustomerGroupController _controller; + + [TestInitialize] + public void Init() + { + _mediatorMock = new Mock(); + var customer = new Customer { Id = "admin" }; + var permissionService = new Mock(); + permissionService.Setup(p => p.Authorize(PermissionSystemName.Customers, customer)).ReturnsAsync(true); + + var workContext = new Mock(); + workContext.Setup(x => x.CurrentCustomer).Returns(customer); + var contextAccessor = new Mock(); + contextAccessor.Setup(x => x.WorkContext).Returns(workContext.Object); + var services = new ServiceCollection().AddSingleton(contextAccessor.Object).BuildServiceProvider(); + + _controller = new CustomerGroupController(_mediatorMock.Object, permissionService.Object) { + ControllerContext = new ControllerContext + { HttpContext = new DefaultHttpContext { RequestServices = services } } + }; + } + + private void SetupStoredGroup(CustomerGroupDto group) + { + _mediatorMock.Setup(m => m.Send(It.IsAny>(), + It.IsAny())) + .ReturnsAsync((group is null ? [] : new[] { group }).AsQueryable()); + } + + [TestMethod] + public async Task Put_StoredSystemGroup_ReturnsBadRequestWithoutUpdating() + { + SetupStoredGroup(new CustomerGroupDto { Id = "cg-1", Name = "Administrators", IsSystem = true }); + + var result = await _controller.Put(new CustomerGroupDto { Id = "cg-1", Name = "Renamed", IsSystem = false }); + + Assert.IsInstanceOfType(result, typeof(BadRequestObjectResult)); + _mediatorMock.Verify(m => m.Send(It.IsAny(), It.IsAny()), + Times.Never); + } + + [TestMethod] + public async Task Put_StoredGroupIsNotSystem_Updates() + { + SetupStoredGroup(new CustomerGroupDto { Id = "cg-2", Name = "VIP" }); + + var result = await _controller.Put(new CustomerGroupDto { Id = "cg-2", Name = "VIP Customers" }); + + Assert.IsInstanceOfType(result, typeof(OkObjectResult)); + _mediatorMock.Verify(m => m.Send(It.Is(c => c.Model.Id == "cg-2"), + It.IsAny()), Times.Once); + } + + [TestMethod] + public async Task Put_UnknownGroup_ReturnsNotFound() + { + SetupStoredGroup(null); + + var result = await _controller.Put(new CustomerGroupDto { Id = "cg-missing", Name = "VIP" }); + + Assert.IsInstanceOfType(result, typeof(NotFoundResult)); + } + + [TestMethod] + public async Task Put_WithoutId_ReturnsBadRequest() + { + var result = await _controller.Put(new CustomerGroupDto { Name = "VIP" }); + + Assert.IsInstanceOfType(result, typeof(BadRequestObjectResult)); + _mediatorMock.Verify(m => m.Send(It.IsAny(), It.IsAny()), + Times.Never); + } + + [TestMethod] + public async Task Patch_StoredSystemGroup_ReturnsBadRequestWithoutUpdating() + { + SetupStoredGroup(new CustomerGroupDto { Id = "cg-1", Name = "Administrators", IsSystem = true }); + var patch = new JsonPatchDocument(); + patch.Replace(x => x.IsSystem, false); + + var result = await _controller.Patch("cg-1", patch); + + Assert.IsInstanceOfType(result, typeof(BadRequestObjectResult)); + _mediatorMock.Verify(m => m.Send(It.IsAny(), It.IsAny()), + Times.Never); + } +} From 57d64913646822c1b37d5109b07b785342b7291c Mon Sep 17 00:00:00 2001 From: support Date: Mon, 23 Nov 2026 07:10:33 +0100 Subject: [PATCH 2/2] Drop the unused product id from the product review edit links --- .../Grand.Web.Admin/Areas/Admin/Views/ProductReview/List.cshtml | 2 +- .../Grand.Web.Store/Areas/Store/Views/ProductReview/List.cshtml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Web/Grand.Web.Admin/Areas/Admin/Views/ProductReview/List.cshtml b/src/Web/Grand.Web.Admin/Areas/Admin/Views/ProductReview/List.cshtml index a8eb3d54a..a01b13b82 100644 --- a/src/Web/Grand.Web.Admin/Areas/Admin/Views/ProductReview/List.cshtml +++ b/src/Web/Grand.Web.Admin/Areas/Admin/Views/ProductReview/List.cshtml @@ -75,7 +75,7 @@ - {{ Title }} + {{ Title }} diff --git a/src/Web/Grand.Web.Store/Areas/Store/Views/ProductReview/List.cshtml b/src/Web/Grand.Web.Store/Areas/Store/Views/ProductReview/List.cshtml index 129fc88ca..6e50c220b 100644 --- a/src/Web/Grand.Web.Store/Areas/Store/Views/ProductReview/List.cshtml +++ b/src/Web/Grand.Web.Store/Areas/Store/Views/ProductReview/List.cshtml @@ -74,7 +74,7 @@ - {{ Title }} + {{ Title }}