Skip to content

Commit 5c832be

Browse files
Merge remote-tracking branch 'origin/develop' into arch001/phase19-messagetemplate-consolidation
# Conflicts: # src/Web/Grand.Web.AdminShared/Startup/StartupApplication.cs
2 parents fbb0533 + a86ab29 commit 5c832be

22 files changed

Lines changed: 964 additions & 470 deletions

src/Tests/Grand.Web.Admin.Tests/Controllers/BaseProductReviewControllerTests.cs

Lines changed: 391 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
using System.Linq;
2+
using Grand.Web.Admin.Controllers;
3+
using Grand.Web.AdminShared.Controllers;
4+
using Grand.Web.Common.Filters;
5+
using Grand.Web.Common.Security.Authorization;
6+
using Microsoft.AspNetCore.Mvc;
7+
using Microsoft.VisualStudio.TestTools.UnitTesting;
8+
9+
namespace Grand.Web.Admin.Tests.Controllers;
10+
11+
[TestClass]
12+
public class ProductReviewControllerAttributeTests
13+
{
14+
[TestMethod]
15+
public void IsThinSubclassOfBaseProductReviewController()
16+
{
17+
Assert.IsTrue(typeof(BaseProductReviewController).IsAssignableFrom(typeof(ProductReviewController)));
18+
Assert.AreEqual(typeof(BaseProductReviewController), typeof(ProductReviewController).BaseType);
19+
}
20+
21+
[TestMethod]
22+
public void HasAuthorizeAdminAttribute()
23+
{
24+
var attr = typeof(ProductReviewController).GetCustomAttributes(typeof(AuthorizeAdminAttribute), inherit: false);
25+
Assert.AreEqual(1, attr.Length);
26+
}
27+
28+
[TestMethod]
29+
public void HasAreaAdminAttribute()
30+
{
31+
var attr = typeof(ProductReviewController)
32+
.GetCustomAttributes(typeof(AreaAttribute), inherit: false)
33+
.Cast<AreaAttribute>().Single();
34+
Assert.AreEqual("Admin", attr.RouteValue);
35+
}
36+
37+
[TestMethod]
38+
public void HasAutoValidateAntiforgeryTokenAttribute()
39+
{
40+
var attr = typeof(ProductReviewController)
41+
.GetCustomAttributes(typeof(AutoValidateAntiforgeryTokenAttribute), inherit: true);
42+
Assert.AreEqual(1, attr.Length);
43+
}
44+
45+
[TestMethod]
46+
public void HasAuthorizeMenuAttribute()
47+
{
48+
var attr = typeof(ProductReviewController).GetCustomAttributes(typeof(AuthorizeMenuAttribute), inherit: false);
49+
Assert.AreEqual(1, attr.Length);
50+
}
51+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
using Grand.Domain.Catalog;
2+
using Grand.Web.AdminShared.Services;
3+
using Microsoft.AspNetCore.Http;
4+
using Microsoft.AspNetCore.Routing;
5+
using Microsoft.VisualStudio.TestTools.UnitTesting;
6+
using Moq;
7+
8+
namespace Grand.Web.Admin.Tests.Controllers;
9+
10+
[TestClass]
11+
public class RoutedProductReviewDataScopeTests
12+
{
13+
private static RoutedProductReviewDataScope CreateScope(string area)
14+
{
15+
var httpContext = new DefaultHttpContext();
16+
httpContext.Request.RouteValues = new RouteValueDictionary { ["area"] = area };
17+
var httpContextAccessor = new Mock<IHttpContextAccessor>();
18+
httpContextAccessor.Setup(a => a.HttpContext).Returns(httpContext);
19+
20+
var contextAccessor = new Mock<Grand.Infrastructure.IContextAccessor>();
21+
var workContext = new Mock<Grand.Infrastructure.IWorkContext>();
22+
workContext.Setup(w => w.CurrentCustomer).Returns(new Grand.Domain.Customers.Customer { StaffStoreId = "store-1" });
23+
contextAccessor.Setup(c => c.WorkContext).Returns(workContext.Object);
24+
25+
var storeScope = new StoreProductReviewDataScope(contextAccessor.Object);
26+
return new RoutedProductReviewDataScope(httpContextAccessor.Object,
27+
new GlobalAdminDataScope<ProductReview>(), storeScope);
28+
}
29+
30+
[TestMethod]
31+
public async Task AdminArea_ResolvesToGlobalScope_HasAccessAlwaysTrue()
32+
{
33+
var scope = CreateScope("Admin");
34+
var result = await scope.HasAccess(new ProductReview { StoreId = "any-other-store" });
35+
Assert.IsTrue(result);
36+
}
37+
38+
[TestMethod]
39+
public void AdminArea_DefaultStoreId_IsNull()
40+
{
41+
var scope = CreateScope("Admin");
42+
Assert.IsNull(scope.DefaultStoreId);
43+
}
44+
45+
[TestMethod]
46+
public async Task StoreArea_ResolvesToStoreScope_HasAccessMatchesOwnership()
47+
{
48+
var scope = CreateScope("Store");
49+
var owned = await scope.HasAccess(new ProductReview { StoreId = "store-1" });
50+
var other = await scope.HasAccess(new ProductReview { StoreId = "store-2" });
51+
Assert.IsTrue(owned);
52+
Assert.IsFalse(other);
53+
}
54+
55+
[TestMethod]
56+
public void StoreArea_DefaultStoreId_IsStaffStoreId()
57+
{
58+
var scope = CreateScope("Store");
59+
Assert.AreEqual("store-1", scope.DefaultStoreId);
60+
}
61+
62+
[TestMethod]
63+
public async Task UnrecognizedArea_ThrowsInvalidOperationException()
64+
{
65+
var scope = CreateScope("Vendor");
66+
await Assert.ThrowsAsync<InvalidOperationException>(
67+
() => scope.HasAccess(new ProductReview { StoreId = "store-1" }));
68+
}
69+
70+
[TestMethod]
71+
public async Task MissingArea_ThrowsInvalidOperationException()
72+
{
73+
var scope = CreateScope(null);
74+
await Assert.ThrowsAsync<InvalidOperationException>(
75+
() => scope.HasAccess(new ProductReview { StoreId = "store-1" }));
76+
}
77+
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
using Grand.Domain.Catalog;
2+
using Grand.Domain.Customers;
3+
using Grand.Infrastructure;
4+
using Grand.Web.AdminShared.Services;
5+
using Microsoft.VisualStudio.TestTools.UnitTesting;
6+
using Moq;
7+
8+
namespace Grand.Web.Admin.Tests.Controllers;
9+
10+
[TestClass]
11+
public class StoreProductReviewDataScopeTests
12+
{
13+
private static StoreProductReviewDataScope CreateScope(string staffStoreId)
14+
{
15+
var customer = new Customer { StaffStoreId = staffStoreId };
16+
var workContext = new Mock<IWorkContext>();
17+
workContext.Setup(w => w.CurrentCustomer).Returns(customer);
18+
var contextAccessor = new Mock<IContextAccessor>();
19+
contextAccessor.Setup(c => c.WorkContext).Returns(workContext.Object);
20+
return new StoreProductReviewDataScope(contextAccessor.Object);
21+
}
22+
23+
[TestMethod]
24+
public async Task HasAccess_OwnStore_ReturnsTrue()
25+
{
26+
var scope = CreateScope("store-1");
27+
var result = await scope.HasAccess(new ProductReview { StoreId = "store-1" });
28+
Assert.IsTrue(result);
29+
}
30+
31+
[TestMethod]
32+
public async Task HasAccess_OtherStore_ReturnsFalse()
33+
{
34+
var scope = CreateScope("store-1");
35+
var result = await scope.HasAccess(new ProductReview { StoreId = "store-2" });
36+
Assert.IsFalse(result);
37+
}
38+
39+
[TestMethod]
40+
public async Task HasAccess_NullEntity_ReturnsFalse()
41+
{
42+
var scope = CreateScope("store-1");
43+
var result = await scope.HasAccess(null);
44+
Assert.IsFalse(result);
45+
}
46+
47+
[TestMethod]
48+
public async Task CanView_MatchesHasAccess_OwnStore()
49+
{
50+
var scope = CreateScope("store-1");
51+
var result = await scope.CanView(new ProductReview { StoreId = "store-1" });
52+
Assert.IsTrue(result);
53+
}
54+
55+
[TestMethod]
56+
public async Task CanView_MatchesHasAccess_OtherStore()
57+
{
58+
var scope = CreateScope("store-1");
59+
var result = await scope.CanView(new ProductReview { StoreId = "store-2" });
60+
Assert.IsFalse(result);
61+
}
62+
63+
[TestMethod]
64+
public void DefaultStoreId_ReturnsStaffStoreId()
65+
{
66+
var scope = CreateScope("store-1");
67+
Assert.AreEqual("store-1", scope.DefaultStoreId);
68+
}
69+
70+
[TestMethod]
71+
public void ShowStoreSelector_IsFalse()
72+
{
73+
var scope = CreateScope("store-1");
74+
Assert.IsFalse(scope.ShowStoreSelector);
75+
}
76+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
using System.Linq;
2+
using Grand.Web.AdminShared.Controllers;
3+
using Grand.Web.Common.Filters;
4+
using Grand.Web.Common.Security.Authorization;
5+
using Grand.Web.Store.Controllers;
6+
using Microsoft.AspNetCore.Mvc;
7+
using Microsoft.VisualStudio.TestTools.UnitTesting;
8+
9+
namespace Grand.Web.Store.Tests.Controllers;
10+
11+
[TestClass]
12+
public class ProductReviewControllerAttributeTests
13+
{
14+
[TestMethod]
15+
public void IsThinSubclassOfBaseProductReviewController()
16+
{
17+
Assert.IsTrue(typeof(BaseProductReviewController).IsAssignableFrom(typeof(ProductReviewController)));
18+
Assert.AreEqual(typeof(BaseProductReviewController), typeof(ProductReviewController).BaseType);
19+
}
20+
21+
[TestMethod]
22+
public void HasAuthorizeStoreAttribute()
23+
{
24+
var attr = typeof(ProductReviewController).GetCustomAttributes(typeof(AuthorizeStoreAttribute), inherit: false);
25+
Assert.AreEqual(1, attr.Length);
26+
}
27+
28+
[TestMethod]
29+
public void HasAreaStoreAttribute()
30+
{
31+
var attr = typeof(ProductReviewController)
32+
.GetCustomAttributes(typeof(AreaAttribute), inherit: false)
33+
.Cast<AreaAttribute>().Single();
34+
Assert.AreEqual("Store", attr.RouteValue);
35+
}
36+
37+
[TestMethod]
38+
public void HasAutoValidateAntiforgeryTokenAttribute()
39+
{
40+
var attr = typeof(ProductReviewController)
41+
.GetCustomAttributes(typeof(AutoValidateAntiforgeryTokenAttribute), inherit: true);
42+
Assert.AreEqual(1, attr.Length);
43+
}
44+
45+
[TestMethod]
46+
public void HasAuthorizeMenuAttribute()
47+
{
48+
var attr = typeof(ProductReviewController).GetCustomAttributes(typeof(AuthorizeMenuAttribute), inherit: false);
49+
Assert.AreEqual(1, attr.Length);
50+
}
51+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
@model ProductReviewModel
2+
<vc:admin-widget widget-zone="product_review_details_bottom" additional-data="Model"/>
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
@model ProductReviewModel
2+
<vc:admin-widget widget-zone="product_review_details_buttons" additional-data="Model"/>
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
@model ProductReviewModel
2+
<vc:admin-widget widget-zone="product_review_details_top" additional-data="Model"/>

0 commit comments

Comments
 (0)