Skip to content

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.

Task 11 fix round 1: close review findings (attribute regression tests, EditWarningCheck coverage, unused usings)

KrzysztofPajak6787f53fc0c1
diff --git a/src/Tests/Grand.Web.Admin.Tests/Controllers/ProductControllerAttributesTests.cs b/src/Tests/Grand.Web.Admin.Tests/Controllers/ProductControllerAttributesTests.cs
new file mode 100644
index 000000000..1c6e16d51
--- /dev/null
+++ b/src/Tests/Grand.Web.Admin.Tests/Controllers/ProductControllerAttributesTests.cs
@@ -0,0 +1,39 @@
+using Grand.Domain.Permissions;
+using Grand.Web.Admin.Controllers;
+using Grand.Web.Common.Filters;
+using Grand.Web.Common.Security.Authorization;
+using Microsoft.AspNetCore.Mvc;
+using Microsoft.VisualStudio.TestTools.UnitTesting;
+
+namespace Grand.Web.Admin.Tests.Controllers;
+
+// Regression lock for the missing-authorization-attribute bug caught during ARCH-001 Phase 1 Task 11:
+// the plan's own inline example code for the thin ProductController subclass omitted
+// [AuthorizeAdmin]/[AutoValidateAntiforgeryToken]/[AuthorizeMenu] entirely, because BaseProductController
+// can't inherit any single host's base controller and so those attributes no longer arrive
+// transitively. Following the plan literally would have shipped Admin's product management with no
+// CSRF protection and no authentication/authorization filter. This test makes that class of regression
+// fail loudly instead of silently the next time this controller (or one like it) is touched.
+[TestClass]
+public class ProductControllerAttributesTests
+{
+ [TestMethod]
+ public void ProductController_CarriesRequiredAuthorizationAndCsrfAttributes()
+ {
+ var type = typeof(ProductController);
+
+ Assert.IsTrue(type.IsDefined(typeof(AuthorizeAdminAttribute), true), "Missing [AuthorizeAdmin].");
+ Assert.IsTrue(type.IsDefined(typeof(AutoValidateAntiforgeryTokenAttribute), true),
+ "Missing [AutoValidateAntiforgeryToken] - CSRF protection would be lost.");
+ Assert.IsTrue(type.IsDefined(typeof(AreaAttribute), true), "Missing [Area].");
+ Assert.IsTrue(type.IsDefined(typeof(AuthorizeMenuAttribute), true), "Missing [AuthorizeMenu].");
+
+ // Inherited from BaseProductController - PermissionAuthorizeAttribute has no
+ // AttributeUsage(Inherited = false), so MVC's attribute discovery (inherit: true) picks it up
+ // from the base class without the subclass needing to restate it.
+ var permissionAttr = (PermissionAuthorizeAttribute)Attribute.GetCustomAttribute(type,
+ typeof(PermissionAuthorizeAttribute), true);
+ Assert.IsNotNull(permissionAttr, "Missing [PermissionAuthorize] (expected via inheritance from BaseProductController).");
+ Assert.AreEqual(PermissionSystemName.Products, permissionAttr.Permission);
+ }
+}
diff --git a/src/Tests/Grand.Web.Store.Tests/Controllers/ProductControllerAttributesTests.cs b/src/Tests/Grand.Web.Store.Tests/Controllers/ProductControllerAttributesTests.cs
new file mode 100644
index 000000000..657d4e508
--- /dev/null
+++ b/src/Tests/Grand.Web.Store.Tests/Controllers/ProductControllerAttributesTests.cs
@@ -0,0 +1,39 @@
+using Grand.Domain.Permissions;
+using Grand.Web.Common.Filters;
+using Grand.Web.Common.Security.Authorization;
+using Grand.Web.Store.Controllers;
+using Microsoft.AspNetCore.Mvc;
+using Microsoft.VisualStudio.TestTools.UnitTesting;
+
+namespace Grand.Web.Store.Tests.Controllers;
+
+// Regression lock for the missing-authorization-attribute bug caught during ARCH-001 Phase 1 Task 11:
+// the plan's own inline example code for the thin ProductController subclass omitted
+// [AuthorizeStore]/[AutoValidateAntiforgeryToken]/[AuthorizeMenu] entirely, because BaseProductController
+// can't inherit any single host's base controller and so those attributes no longer arrive
+// transitively. Following the plan literally would have shipped Store's product management with no
+// CSRF protection and no authentication/authorization filter. This test makes that class of regression
+// fail loudly instead of silently the next time this controller (or one like it) is touched.
+[TestClass]
+public class ProductControllerAttributesTests
+{
+ [TestMethod]
+ public void ProductController_CarriesRequiredAuthorizationAndCsrfAttributes()
+ {
+ var type = typeof(ProductController);
+
+ Assert.IsTrue(type.IsDefined(typeof(AuthorizeStoreAttribute), true), "Missing [AuthorizeStore].");
+ Assert.IsTrue(type.IsDefined(typeof(AutoValidateAntiforgeryTokenAttribute), true),
+ "Missing [AutoValidateAntiforgeryToken] - CSRF protection would be lost.");
+ Assert.IsTrue(type.IsDefined(typeof(AreaAttribute), true), "Missing [Area].");
+ Assert.IsTrue(type.IsDefined(typeof(AuthorizeMenuAttribute), true), "Missing [AuthorizeMenu].");
+
+ // Inherited from BaseProductController - PermissionAuthorizeAttribute has no
+ // AttributeUsage(Inherited = false), so MVC's attribute discovery (inherit: true) picks it up
+ // from the base class without the subclass needing to restate it.
+ var permissionAttr = (PermissionAuthorizeAttribute)Attribute.GetCustomAttribute(type,
+ typeof(PermissionAuthorizeAttribute), true);
+ Assert.IsNotNull(permissionAttr, "Missing [PermissionAuthorize] (expected via inheritance from BaseProductController).");
+ Assert.AreEqual(PermissionSystemName.Products, permissionAttr.Permission);
+ }
+}
diff --git a/src/Tests/Grand.Web.Store.Tests/Controllers/ProductControllerTests.cs b/src/Tests/Grand.Web.Store.Tests/Controllers/ProductControllerTests.cs
new file mode 100644
index 000000000..9d3b20680
--- /dev/null
+++ b/src/Tests/Grand.Web.Store.Tests/Controllers/ProductControllerTests.cs
@@ -0,0 +1,112 @@
+using Grand.Business.Core.Interfaces.Catalog.Products;
+using Grand.Business.Core.Interfaces.Common.Directory;
+using Grand.Business.Core.Interfaces.Common.Localization;
+using Grand.Business.Core.Interfaces.Common.Security;
+using Grand.Domain.Catalog;
+using Grand.Domain.Customers;
+using Grand.Infrastructure;
+using Grand.Web.AdminShared.Interfaces;
+using Grand.Web.Common.Localization;
+using Grand.Web.Store.Controllers;
+using Microsoft.AspNetCore.Http;
+using Microsoft.AspNetCore.Mvc.ViewFeatures;
+using Microsoft.VisualStudio.TestTools.UnitTesting;
+using Moq;
+
+namespace Grand.Web.Store.Tests.Controllers;
+
+// Characterization test for ProductController.EditWarningCheck (ARCH-001 Phase 1 Task 11). This is the
+// one piece of behavior newly hand-written for this task (re-derived from the original pre-migration
+// Edit(GET) action's if/else, not copied) - everything else this controller does lives in, and is
+// covered by, BaseProductControllerTests.cs (see that file's own header comment on the Task 13
+// consolidation this replaces). The condition is unusual (warns when NOT limited to stores at all, or
+// when limited AND the staff member's store is one of several) and easy to get backwards, per the
+// plan's own warning - this test exists so a future regression here fails loudly instead of silently.
+[TestClass]
+public class ProductControllerTests
+{
+ private const string StaffStoreId = "store-1";
+ private const string OtherStoreId = "store-2";
+
+ private ProductController _controller;
+ private Mock<IProductService> _productServiceMock;
+ private Mock<IAdminDataScope<Product>> _scopeMock;
+
+ [TestInitialize]
+ public void Setup()
+ {
+ _productServiceMock = new Mock<IProductService>();
+ var productViewModelServiceMock = new Mock<IProductViewModelService>();
+ var translationServiceMock = new Mock<ITranslationService>();
+ translationServiceMock.Setup(t => t.GetResource(It.IsAny<string>())).Returns("resource");
+
+ var workContextMock = new Mock<IWorkContext>();
+ workContextMock.Setup(w => w.CurrentCustomer).Returns(new Customer { StaffStoreId = StaffStoreId });
+ var contextAccessorMock = new Mock<IContextAccessor>();
+ contextAccessorMock.Setup(c => c.WorkContext).Returns(workContextMock.Object);
+
+ _scopeMock = new Mock<IAdminDataScope<Product>>();
+ _scopeMock.Setup(s => s.DefaultStoreId).Returns(StaffStoreId);
+ _scopeMock.Setup(s => s.CanView(It.IsAny<Product>())).ReturnsAsync(true);
+
+ var languageServiceMock = new Mock<ILanguageService>();
+ languageServiceMock.Setup(l => l.GetAllLanguages(true, It.IsAny<string>()))
+ .ReturnsAsync(new List<Domain.Localization.Language>());
+
+ _controller = new ProductController(
+ productViewModelServiceMock.Object,
+ _productServiceMock.Object,
+ new Mock<IInventoryManageService>().Object,
+ languageServiceMock.Object,
+ translationServiceMock.Object,
+ new Mock<IProductReservationService>().Object,
+ new Mock<IAuctionService>().Object,
+ new Mock<IDateTimeService>().Object,
+ new Mock<IPermissionService>().Object,
+ new Mock<IEnumTranslationService>().Object,
+ _scopeMock.Object);
+
+ var httpContext = new DefaultHttpContext();
+ _controller.ControllerContext = new Microsoft.AspNetCore.Mvc.ControllerContext { HttpContext = httpContext };
+ _controller.TempData = new TempDataDictionary(httpContext, new Mock<ITempDataProvider>().Object);
+ }
+
+ private bool WarningWasRaised()
+ => _controller.TempData["grand.notifications.Warning"] is List<string> { Count: > 0 };
+
+ [TestMethod]
+ public async Task EditGet_ProductNotLimitedToAnyStore_RaisesWarning()
+ {
+ var product = new Product { Id = "p1", LimitedToStores = false };
+ _productServiceMock.Setup(p => p.GetProductById("p1", true)).ReturnsAsync(product);
+
+ await _controller.Edit("p1");
+
+ Assert.IsTrue(WarningWasRaised(), "A product visible to every store must warn a store-scoped editor.");
+ }
+
+ [TestMethod]
+ public async Task EditGet_ProductLimitedToStaffStoreAndAnotherStore_RaisesWarning()
+ {
+ var product = new Product
+ { Id = "p1", LimitedToStores = true, Stores = [StaffStoreId, OtherStoreId] };
+ _productServiceMock.Setup(p => p.GetProductById("p1", true)).ReturnsAsync(product);
+
+ await _controller.Edit("p1");
+
+ Assert.IsTrue(WarningWasRaised(),
+ "A product shared with another store beyond the staff member's own must still warn.");
+ }
+
+ [TestMethod]
+ public async Task EditGet_ProductLimitedToStaffStoreOnly_NoWarning()
+ {
+ var product = new Product { Id = "p1", LimitedToStores = true, Stores = [StaffStoreId] };
+ _productServiceMock.Setup(p => p.GetProductById("p1", true)).ReturnsAsync(product);
+
+ await _controller.Edit("p1");
+
+ Assert.IsFalse(WarningWasRaised(),
+ "A product exclusive to the staff member's own store needs no cross-store warning.");
+ }
+}
diff --git a/src/Web/Grand.Web.Admin/Controllers/ProductController.cs b/src/Web/Grand.Web.Admin/Controllers/ProductController.cs
index d01a1ef2d..aacd760d9 100644
--- a/src/Web/Grand.Web.Admin/Controllers/ProductController.cs
+++ b/src/Web/Grand.Web.Admin/Controllers/ProductController.cs
@@ -2,8 +2,6 @@ using Grand.Business.Core.Interfaces.Catalog.Products;
using Grand.Business.Core.Interfaces.Common.Directory;
using Grand.Business.Core.Interfaces.Common.Localization;
using Grand.Business.Core.Interfaces.Common.Security;
-using Grand.Business.Core.Interfaces.ExportImport;
-using Grand.Business.Core.Interfaces.Storage;
using Grand.Domain.Catalog;
using Grand.Web.Admin.Extensions;
using Grand.Web.AdminShared.Controllers;