From c301f1838b8ba715f7afed45740a9bd2ba713780 Mon Sep 17 00:00:00 2001 From: support Date: Wed, 25 Nov 2026 19:33:15 +0100 Subject: [PATCH] Admin API: GetCollectionById returns the collection, not a list of it The endpoint is documented to return a CollectionDto, as GetBrandById and GetCategoryById do, but it returned the whole query, which serialized as a JSON array of one element. It now returns the collection itself; 404 for an unknown key is unchanged. --- .../Controllers/CollectionController.cs | 2 +- .../Controllers/CollectionControllerTests.cs | 72 +++++++++++++++++++ 2 files changed, 73 insertions(+), 1 deletion(-) create mode 100644 src/Tests/Grand.Module.Api.Tests/Controllers/CollectionControllerTests.cs diff --git a/src/Modules/Grand.Module.Api/Controllers/CollectionController.cs b/src/Modules/Grand.Module.Api/Controllers/CollectionController.cs index a5731aec9..4abc6205b 100644 --- a/src/Modules/Grand.Module.Api/Controllers/CollectionController.cs +++ b/src/Modules/Grand.Module.Api/Controllers/CollectionController.cs @@ -37,7 +37,7 @@ public async Task Get([FromRoute] string key) var collection = await _mediator.Send(new GetGenericQuery(key)); if (!collection.Any()) return NotFound(); - return Ok(collection); + return Ok(collection.FirstOrDefault()); } [EndpointDescription("Get entities from Collection")] diff --git a/src/Tests/Grand.Module.Api.Tests/Controllers/CollectionControllerTests.cs b/src/Tests/Grand.Module.Api.Tests/Controllers/CollectionControllerTests.cs new file mode 100644 index 000000000..172f80cea --- /dev/null +++ b/src/Tests/Grand.Module.Api.Tests/Controllers/CollectionControllerTests.cs @@ -0,0 +1,72 @@ +using Grand.Business.Core.Interfaces.Common.Security; +using Grand.Domain.Catalog; +using Grand.Domain.Customers; +using Grand.Domain.Permissions; +using Grand.Infrastructure; +using Grand.Mediator; +using Grand.Module.Api.Controllers; +using Grand.Module.Api.DTOs.Catalog; +using Grand.Module.Api.Queries.Models.Common; +using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Mvc; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using Moq; + +namespace Grand.Module.Api.Tests.Controllers; + +[TestClass] +public class CollectionControllerTests +{ + private Mock _mediatorMock; + private CollectionController _controller; + + [TestInitialize] + public void Init() + { + _mediatorMock = new Mock(); + var customer = new Customer { Id = "admin" }; + var permissionService = new Mock(); + permissionService.Setup(p => p.Authorize(PermissionSystemName.Collections, 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 CollectionController(_mediatorMock.Object, permissionService.Object) { + ControllerContext = new ControllerContext + { HttpContext = new DefaultHttpContext { RequestServices = services } } + }; + } + + private void SetupStoredCollection(CollectionDto collection) + { + _mediatorMock.Setup(m => m.Send(It.IsAny>(), + It.IsAny())) + .ReturnsAsync((collection is null ? [] : new[] { collection }).AsQueryable()); + } + + [TestMethod] + public async Task Get_StoredCollection_ReturnsTheCollectionItself() + { + var stored = new CollectionDto { Id = "c-1", Name = "Summer" }; + SetupStoredCollection(stored); + + var result = await _controller.Get("c-1"); + + Assert.IsInstanceOfType(result, typeof(OkObjectResult)); + Assert.AreSame(stored, ((OkObjectResult)result).Value); + } + + [TestMethod] + public async Task Get_UnknownCollection_ReturnsNotFound() + { + SetupStoredCollection(null); + + var result = await _controller.Get("missing"); + + Assert.IsInstanceOfType(result, typeof(NotFoundResult)); + } +}