Repository navigation
Expand file tree
/
Copy pathRoutedEmailAccountDataScopeTests.cs
More file actions
86 lines (75 loc) · 3.12 KB
/
Copy pathRoutedEmailAccountDataScopeTests.cs
File metadata and controls
86 lines (75 loc) · 3.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
using Grand.Domain.Customers;
using Grand.Domain.Messages;
using Grand.Infrastructure;
using Grand.Web.AdminShared.Services;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Routing;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
namespace Grand.Web.Admin.Tests.Controllers;
[TestClass]
public class RoutedEmailAccountDataScopeTests
{
private static IHttpContextAccessor CreateHttpContextAccessor(string? area)
{
var httpContext = new DefaultHttpContext();
if (area is not null)
httpContext.Request.RouteValues["area"] = area;
var accessor = new Mock<IHttpContextAccessor>();
accessor.Setup(a => a.HttpContext).Returns(httpContext);
return accessor.Object;
}
private static StoreEmailAccountDataScope CreateStoreScope(string staffStoreId)
{
var customer = new Customer { StaffStoreId = staffStoreId };
var workContext = new Mock<IWorkContext>();
workContext.Setup(w => w.CurrentCustomer).Returns(customer);
var contextAccessor = new Mock<IContextAccessor>();
contextAccessor.Setup(c => c.WorkContext).Returns(workContext.Object);
return new StoreEmailAccountDataScope(contextAccessor.Object);
}
[TestMethod]
public async Task AdminArea_ResolvesToGlobalScope_HasAccessAlwaysTrue()
{
var routed = new RoutedEmailAccountDataScope(
CreateHttpContextAccessor("Admin"),
new GlobalAdminDataScope<EmailAccount>(),
CreateStoreScope("store-1"));
var account = new EmailAccount { StoreId = "store-2" };
Assert.IsTrue(await routed.HasAccess(account));
Assert.IsNull(routed.DefaultStoreId);
}
[TestMethod]
public async Task StoreArea_ResolvesToStoreScope_HasAccessScoped()
{
var routed = new RoutedEmailAccountDataScope(
CreateHttpContextAccessor("Store"),
new GlobalAdminDataScope<EmailAccount>(),
CreateStoreScope("store-1"));
var owned = new EmailAccount { StoreId = "store-1" };
var notOwned = new EmailAccount { StoreId = "store-2" };
Assert.IsTrue(await routed.HasAccess(owned));
Assert.IsFalse(await routed.HasAccess(notOwned));
Assert.AreEqual("store-1", routed.DefaultStoreId);
}
[TestMethod]
public async Task UnrecognizedArea_ThrowsInvalidOperationException()
{
var routed = new RoutedEmailAccountDataScope(
CreateHttpContextAccessor("Vendor"),
new GlobalAdminDataScope<EmailAccount>(),
CreateStoreScope("store-1"));
await Assert.ThrowsAsync<InvalidOperationException>(
() => routed.HasAccess(new EmailAccount { StoreId = "store-1" }));
}
[TestMethod]
public async Task MissingArea_ThrowsInvalidOperationException()
{
var routed = new RoutedEmailAccountDataScope(
CreateHttpContextAccessor(null),
new GlobalAdminDataScope<EmailAccount>(),
CreateStoreScope("store-1"));
await Assert.ThrowsAsync<InvalidOperationException>(
() => routed.HasAccess(new EmailAccount { StoreId = "store-1" }));
}
}