Open-source eCommerce platform for C# and ASP.NET Core 10 with MongoDB. Build multi-store shops and multi-vendor marketplaces with B2B/B2C, REST API, plugins, Vue 3 and Docker.
C# aspnet-corecsharpdockerdotnetecommerceecommerce-platformmongodbmulti-storemulti-vendornosqlonline-shopshoppingcart
Make RoutedProductDataScope fail closed on an unknown area
KrzysztofPajakd337a12c7bb0
diff --git a/src/Tests/Grand.Web.Admin.Tests/Services/RoutedProductDataScopeTests.cs b/src/Tests/Grand.Web.Admin.Tests/Services/RoutedProductDataScopeTests.csnew file mode 100644index 000000000..aac5d0ee0--- /dev/null+++ b/src/Tests/Grand.Web.Admin.Tests/Services/RoutedProductDataScopeTests.cs@@ -0,0 +1,101 @@+#nullable enable++using Grand.Domain.Catalog;+using Grand.Domain.Customers;+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.Services;++/// <summary>+/// Covers the routing decision itself - which concrete IAdminDataScope<Product> the resolver+/// delegates to for each area, and that an unrecognized or missing area fails closed rather than+/// falling back to the unscoped global scope.+/// </summary>+[TestClass]+public class RoutedProductDataScopeTests+{+ private const string StaffStoreId = "store-1";+ private const string VendorId = "vendor-1";++ private GlobalAdminDataScope<Product> _globalScope = null!;+ private StoreAdminDataScope<Product> _storeScope = null!;+ private VendorProductDataScope _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);++ _globalScope = new GlobalAdminDataScope<Product>();+ _storeScope = new StoreAdminDataScope<Product>(contextAccessor.Object);+ _vendorScope = new VendorProductDataScope(contextAccessor.Object);+ }++ private RoutedProductDataScope 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 RoutedProductDataScope(httpContextAccessor.Object, _globalScope, _storeScope, _vendorScope);+ }++ [TestMethod]+ public void AdminArea_ResolvesToGlobalScope()+ {+ var resolver = ResolverForArea("Admin");+ Assert.AreEqual("Admin", resolver.ResourceKeyPrefix);+ Assert.IsNull(resolver.DefaultStoreId);+ Assert.IsNull(resolver.DefaultVendorId);+ }++ [TestMethod]+ public void StoreArea_ResolvesToStoreScope()+ {+ var resolver = ResolverForArea("Store");+ Assert.AreEqual("Admin", resolver.ResourceKeyPrefix);+ 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 MissingArea_Throws()+ {+ var resolver = ResolverForArea(null);+ Assert.Throws<InvalidOperationException>(() => _ = resolver.ResourceKeyPrefix);+ }++ [TestMethod]+ public void UnrecognizedArea_Throws()+ {+ var resolver = ResolverForArea("Something");+ Assert.Throws<InvalidOperationException>(() => _ = resolver.ResourceKeyPrefix);+ }++ [TestMethod]+ public void NoHttpContext_Throws()+ {+ var httpContextAccessor = new Mock<IHttpContextAccessor>();+ httpContextAccessor.Setup(x => x.HttpContext).Returns((HttpContext)null!);+ var resolver = new RoutedProductDataScope(httpContextAccessor.Object, _globalScope, _storeScope, _vendorScope);+ Assert.Throws<InvalidOperationException>(() => _ = resolver.ResourceKeyPrefix);+ }+}diff --git a/src/Web/Grand.Web.AdminShared/Services/RoutedProductDataScope.cs b/src/Web/Grand.Web.AdminShared/Services/RoutedProductDataScope.csindex 0e9131504..7e17968ec 100644--- a/src/Web/Grand.Web.AdminShared/Services/RoutedProductDataScope.cs+++ b/src/Web/Grand.Web.AdminShared/Services/RoutedProductDataScope.cs@@ -38,11 +38,13 @@ public class RoutedProductDataScope({var area = httpContextAccessor.HttpContext?.Request.RouteValues["area"] as string;return area switch {- "Vendor" => vendorScope,+ "Admin" => globalScope,"Store" => storeScope,- //covers "Admin" and the (unexpected) case of no area route value at all - same- //fallback GlobalAdminDataScope always represented before this resolver existed- _ => globalScope+ "Vendor" => vendorScope,+ //fail closed: this object fronts vendor/store tenant isolation, so an unrecognized+ //or missing area must never silently resolve to the unscoped global scope+ _ => throw new InvalidOperationException(+ $"RoutedProductDataScope: unrecognized or missing area '{area}'.")};}}