Skip to content
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 @@ -37,7 +37,7 @@ public async Task<IActionResult> Get([FromRoute] string key)
var collection = await _mediator.Send(new GetGenericQuery<CollectionDto, Collection>(key));
if (!collection.Any()) return NotFound();

return Ok(collection);
return Ok(collection.FirstOrDefault());
}

[EndpointDescription("Get entities from Collection")]
Expand Down
Original file line number Diff line number Diff line change
@@ -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<IMediator> _mediatorMock;
private CollectionController _controller;

[TestInitialize]
public void Init()
{
_mediatorMock = new Mock<IMediator>();
var customer = new Customer { Id = "admin" };
var permissionService = new Mock<IPermissionService>();
permissionService.Setup(p => p.Authorize(PermissionSystemName.Collections, customer)).ReturnsAsync(true);

var workContext = new Mock<IWorkContext>();
workContext.Setup(x => x.CurrentCustomer).Returns(customer);
var contextAccessor = new Mock<IContextAccessor>();
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<GetGenericQuery<CollectionDto, Collection>>(),
It.IsAny<CancellationToken>()))
.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));
}
}
Loading