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

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
#nullable enable

using Grand.Domain.Customers;
using Grand.Domain.Shipping;
using Grand.Domain.Vendors;
using Grand.Infrastructure;
using Grand.Web.AdminShared.Services;
using Microsoft.AspNetCore.Http;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;

namespace Grand.Web.Admin.Tests.Controllers;

[TestClass]
public class RoutedShipmentDataScopeTests
{
private const string StaffStoreId = "store-1";
private const string VendorId = "vendor-1";

private GlobalAdminDataScope<Shipment> _adminScope = null!;
private StoreShipmentDataScope _storeScope = null!;
private VendorShipmentDataScope _vendorScope = null!;

[TestInitialize]
public void Setup()
{
var workContext = new Mock<IWorkContext>();
workContext.Setup(x => x.CurrentCustomer).Returns(new Customer { StaffStoreId = StaffStoreId });
workContext.Setup(x => x.CurrentVendor).Returns(new Vendor { Id = VendorId });
var contextAccessor = new Mock<IContextAccessor>();
contextAccessor.Setup(x => x.WorkContext).Returns(workContext.Object);

_adminScope = new GlobalAdminDataScope<Shipment>();
_storeScope = new StoreShipmentDataScope(contextAccessor.Object);
_vendorScope = new VendorShipmentDataScope(contextAccessor.Object);
}

private RoutedShipmentDataScope ResolverForArea(string? area)
{
var httpContext = new DefaultHttpContext();
if (area is not null) httpContext.Request.RouteValues["area"] = area;
var httpContextAccessor = new Mock<IHttpContextAccessor>();
httpContextAccessor.Setup(x => x.HttpContext).Returns(httpContext);
return new RoutedShipmentDataScope(httpContextAccessor.Object, _adminScope, _storeScope, _vendorScope);
}

[TestMethod]
public void AdminArea_ResolvesToAdminScope()
{
var resolver = ResolverForArea("Admin");
Assert.IsNull(resolver.DefaultStoreId);
Assert.IsNull(resolver.DefaultVendorId);
}

[TestMethod]
public void StoreArea_ResolvesToStoreScope()
{
var resolver = ResolverForArea("Store");
Assert.AreEqual(StaffStoreId, resolver.DefaultStoreId);
Assert.IsNull(resolver.DefaultVendorId);
}

[TestMethod]
public void VendorArea_ResolvesToVendorScope()
{
var resolver = ResolverForArea("Vendor");
Assert.AreEqual("Vendor", resolver.ResourceKeyPrefix);
Assert.AreEqual(VendorId, resolver.DefaultVendorId);
Assert.IsFalse(resolver.ShowStoreSelector);
}

[TestMethod]
public void UnrecognizedOrMissingArea_ThrowsFailClosed()
{
var resolver = ResolverForArea("Vue");
Assert.Throws<InvalidOperationException>(() => _ = resolver.ResourceKeyPrefix);

var resolverNoArea = ResolverForArea(null);
Assert.Throws<InvalidOperationException>(() => _ = resolverNoArea.ResourceKeyPrefix);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
using Grand.Domain.Customers;
using Grand.Domain.Shipping;
using Grand.Infrastructure;
using Grand.Web.AdminShared.Services;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;

namespace Grand.Web.Admin.Tests.Controllers;

[TestClass]
public class StoreShipmentDataScopeTests
{
private static StoreShipmentDataScope Build(string staffStoreId)
{
var customer = new Customer { StaffStoreId = staffStoreId };
var workContextMock = new Mock<IWorkContext>();
workContextMock.Setup(w => w.CurrentCustomer).Returns(customer);
var contextAccessorMock = new Mock<IContextAccessor>();
contextAccessorMock.Setup(c => c.WorkContext).Returns(workContextMock.Object);
return new StoreShipmentDataScope(contextAccessorMock.Object);
}

[TestMethod]
public async Task HasAccess_MatchingStoreId_True()
{
var scope = Build("store-1");
Assert.IsTrue(await scope.HasAccess(new Shipment { StoreId = "store-1" }));
}

[TestMethod]
public async Task HasAccess_MismatchedStoreId_False()
{
var scope = Build("store-1");
Assert.IsFalse(await scope.HasAccess(new Shipment { StoreId = "store-2" }));
}

[TestMethod]
public async Task HasAccess_NullEntity_False()
{
var scope = Build("store-1");
Assert.IsFalse(await scope.HasAccess(null));
}

[TestMethod]
public async Task HasAccess_EmptyStaffStoreIdAndEmptyEntityStoreId_False()
{
var scope = Build(string.Empty);
Assert.IsFalse(await scope.HasAccess(new Shipment { StoreId = string.Empty }));
}

[TestMethod]
public async Task HasAccess_NullStaffStoreIdAndNullEntityStoreId_False()
{
var scope = Build(null);
Assert.IsFalse(await scope.HasAccess(new Shipment { StoreId = null }));
}

[TestMethod]
public void DefaultStoreId_ReturnsStaffStoreId()
{
var scope = Build("store-1");
Assert.AreEqual("store-1", scope.DefaultStoreId);
Assert.IsNull(scope.DefaultVendorId);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
using Grand.Domain.Orders;
using Grand.Domain.Shipping;
using Grand.Domain.Vendors;
using Grand.Infrastructure;
using Grand.Web.AdminShared.Services;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;

namespace Grand.Web.Admin.Tests.Controllers;

[TestClass]
public class VendorShipmentDataScopeTests
{
private static VendorShipmentDataScope Build(string currentVendorId)
{
var workContextMock = new Mock<IWorkContext>();
workContextMock.Setup(w => w.CurrentVendor).Returns(new Vendor { Id = currentVendorId });
var contextAccessorMock = new Mock<IContextAccessor>();
contextAccessorMock.Setup(c => c.WorkContext).Returns(workContextMock.Object);
return new VendorShipmentDataScope(contextAccessorMock.Object);
}

[TestMethod]
public async Task HasAccess_MatchingVendorId_True()
{
var scope = Build("vendor-A");
Assert.IsTrue(await scope.HasAccess(new Shipment { VendorId = "vendor-A" }));
}

[TestMethod]
public async Task HasAccess_MismatchedVendorId_False()
{
var scope = Build("vendor-A");
Assert.IsFalse(await scope.HasAccess(new Shipment { VendorId = "vendor-B" }));
}

[TestMethod]
public async Task HasAccess_NullEntity_False()
{
var scope = Build("vendor-A");
Assert.IsFalse(await scope.HasAccess(null));
}

[TestMethod]
public async Task HasAccess_EmptyCurrentVendorIdAndEmptyEntityVendorId_False()
{
var scope = Build(string.Empty);
Assert.IsFalse(await scope.HasAccess(new Shipment { VendorId = string.Empty }));
}

[TestMethod]
public async Task HasAccess_NullCurrentVendorIdAndNullEntityVendorId_False()
{
var scope = Build(null);
Assert.IsFalse(await scope.HasAccess(new Shipment { VendorId = null }));
}

[TestMethod]
public void FilterOrderItems_MixedVendorOrder_ReturnsOnlyOwnItems()
{
var scope = Build("vendor-A");
var itemA1 = new OrderItem { Id = "i1", VendorId = "vendor-A" };
var itemB = new OrderItem { Id = "i2", VendorId = "vendor-B" };
var itemA2 = new OrderItem { Id = "i3", VendorId = "vendor-A" };

var filtered = scope.FilterOrderItems([itemA1, itemB, itemA2]).ToList();

CollectionAssert.AreEqual(new[] { itemA1, itemA2 }, filtered);
}

[TestMethod]
public void ScopeDefaults_VendorScoped()
{
var scope = Build("vendor-A");
Assert.IsNull(scope.DefaultStoreId);
Assert.AreEqual("vendor-A", scope.DefaultVendorId);
Assert.AreEqual("Vendor", scope.ResourceKeyPrefix);
Assert.IsFalse(scope.ShowStoreSelector);
Assert.IsFalse(scope.CanFeatureOnHomepage);
}
}
Loading
Loading