Repository navigation
Expand file tree
/
Copy pathBaseCustomerControllerTests.cs
More file actions
672 lines (563 loc) · 32.5 KB
/
Copy pathBaseCustomerControllerTests.cs
File metadata and controls
672 lines (563 loc) · 32.5 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
using Grand.Business.Core.Interfaces.Common.Addresses;
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.Customers;
using Grand.Business.Core.Interfaces.Marketing.Customers;
using Grand.Domain.Customers;
using Grand.Infrastructure;
using Grand.Infrastructure.Configuration;
using Grand.Web.AdminShared.Controllers;
using Grand.Web.AdminShared.Interfaces;
using Grand.Web.AdminShared.Models.Common;
using Grand.Web.AdminShared.Models.Customers;
using Grand.Web.Common.DataSource;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.AspNetCore.Mvc.Routing;
using Microsoft.AspNetCore.Mvc.ViewFeatures;
using Microsoft.Extensions.Logging;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
namespace Grand.Web.Admin.Tests.Controllers;
[TestClass]
public class BaseCustomerControllerTests
{
// BaseCustomerController is abstract; minimal subclass so actions can be invoked directly.
// The full 17-dependency constructor is declared once here; every later task's tests reuse
// this same TestCustomerController and Setup().
protected class TestCustomerController(
ICustomerService customerService,
ICustomerViewModelService customerViewModelService,
ICustomerManagerService customerManagerService,
ICustomerProductService customerProductService,
Grand.Business.Core.Interfaces.Catalog.Products.IProductReviewService productReviewService,
Grand.Web.AdminShared.Interfaces.IProductReviewViewModelService productReviewViewModelService,
Grand.Web.AdminShared.Interfaces.IProductViewModelService productViewModelService,
ICustomerAttributeParser customerAttributeParser,
ICustomerAttributeService customerAttributeService,
IAddressAttributeParser addressAttributeParser,
IAddressAttributeService addressAttributeService,
Grand.Business.Core.Interfaces.Messages.IMessageProviderService messageProviderService,
IGroupService groupService,
ITranslationService translationService,
IContextAccessor contextAccessor,
CustomerSettings customerSettings,
IAdminDataScope<Customer> scope)
: BaseCustomerController(customerService, customerViewModelService, customerManagerService,
customerProductService, productReviewService, productReviewViewModelService,
productViewModelService, customerAttributeParser, customerAttributeService,
addressAttributeParser, addressAttributeService, messageProviderService, groupService,
translationService, contextAccessor, customerSettings, scope)
{
public Task<(Customer, IActionResult)> LoadAuthorizedCustomerPublic(string id) => LoadAuthorizedCustomer(id);
}
protected TestCustomerController Controller;
protected Mock<ICustomerService> CustomerServiceMock;
protected Mock<ICustomerViewModelService> CustomerViewModelServiceMock;
protected Mock<ICustomerManagerService> CustomerManagerServiceMock;
protected Mock<IGroupService> GroupServiceMock;
protected Mock<IAdminDataScope<Customer>> ScopeMock;
protected Mock<ITranslationService> TranslationServiceMock;
protected Mock<IAddressAttributeService> AddressAttributeServiceMock;
protected Mock<Grand.Business.Core.Interfaces.Catalog.Products.IProductReviewService> ProductReviewServiceMock;
protected Mock<ICustomerProductService> CustomerProductServiceMock;
[TestInitialize]
public void Setup()
{
CustomerServiceMock = new Mock<ICustomerService>();
CustomerViewModelServiceMock = new Mock<ICustomerViewModelService>();
CustomerManagerServiceMock = new Mock<ICustomerManagerService>();
GroupServiceMock = new Mock<IGroupService>();
ScopeMock = new Mock<IAdminDataScope<Customer>>();
ScopeMock.Setup(s => s.DefaultStoreId).Returns((string)null);
TranslationServiceMock = new Mock<ITranslationService>();
TranslationServiceMock.Setup(t => t.GetResource(It.IsAny<string>())).Returns("resource");
AddressAttributeServiceMock = new Mock<IAddressAttributeService>();
AddressAttributeServiceMock.Setup(a => a.GetAllAddressAttributes())
.ReturnsAsync(new List<Grand.Domain.Common.AddressAttribute>());
ProductReviewServiceMock = new Mock<Grand.Business.Core.Interfaces.Catalog.Products.IProductReviewService>();
CustomerProductServiceMock = new Mock<ICustomerProductService>();
var contextAccessorMock = new Mock<IContextAccessor>();
Controller = new TestCustomerController(
CustomerServiceMock.Object,
CustomerViewModelServiceMock.Object,
CustomerManagerServiceMock.Object,
CustomerProductServiceMock.Object,
ProductReviewServiceMock.Object,
new Mock<Grand.Web.AdminShared.Interfaces.IProductReviewViewModelService>().Object,
new Mock<Grand.Web.AdminShared.Interfaces.IProductViewModelService>().Object,
new Mock<ICustomerAttributeParser>().Object,
new Mock<ICustomerAttributeService>().Object,
new Mock<IAddressAttributeParser>().Object,
AddressAttributeServiceMock.Object,
new Mock<Grand.Business.Core.Interfaces.Messages.IMessageProviderService>().Object,
GroupServiceMock.Object,
TranslationServiceMock.Object,
contextAccessorMock.Object,
new CustomerSettings(),
ScopeMock.Object);
var httpContext = new DefaultHttpContext();
var loggerFactoryMock = new Mock<ILoggerFactory>();
loggerFactoryMock.Setup(l => l.CreateLogger(It.IsAny<string>())).Returns(new Mock<ILogger>().Object);
var urlHelperFactoryMock = new Mock<IUrlHelperFactory>();
urlHelperFactoryMock.Setup(f => f.GetUrlHelper(It.IsAny<ActionContext>())).Returns(new Mock<IUrlHelper>().Object);
var requestServicesMock = new Mock<IServiceProvider>();
requestServicesMock.Setup(s => s.GetService(typeof(ILoggerFactory))).Returns(loggerFactoryMock.Object);
requestServicesMock.Setup(s => s.GetService(typeof(IUrlHelperFactory))).Returns(urlHelperFactoryMock.Object);
httpContext.RequestServices = requestServicesMock.Object;
Controller.ControllerContext = new ControllerContext { HttpContext = httpContext };
Controller.TempData = new TempDataDictionary(httpContext, new Mock<ITempDataProvider>().Object);
}
[TestMethod]
public async Task List_CallsPrepareCustomerListModel_ForBothHosts()
{
// The fix: both Admin and Store must call the shared, fully-populated
// PrepareCustomerListModel() — Store's original bypassed it entirely (`new CustomerListModel()`),
// leaving CustomerSettings-driven column-visibility flags always false regardless of config.
CustomerViewModelServiceMock.Setup(v => v.PrepareCustomerListModel())
.ReturnsAsync(new CustomerListModel { UsernamesEnabled = true, CompanyEnabled = true });
var result = await Controller.List();
var view = result as ViewResult;
Assert.IsNotNull(view);
var model = view.Model as CustomerListModel;
Assert.IsTrue(model.UsernamesEnabled);
Assert.IsTrue(model.CompanyEnabled);
CustomerViewModelServiceMock.Verify(v => v.PrepareCustomerListModel(), Times.Once);
}
[TestMethod]
public async Task CustomerList_GlobalScope_UsesSubmittedGroupIds()
{
ScopeMock.Setup(s => s.DefaultStoreId).Returns((string)null);
CustomerViewModelServiceMock
.Setup(v => v.PrepareCustomerList(It.IsAny<CustomerListModel>(),
new[] { "group-1" }, null, 1, 10, ""))
.ReturnsAsync((Enumerable.Empty<CustomerModel>(), 0));
await Controller.CustomerList(new DataSourceRequest { Page = 1, PageSize = 10 },
new CustomerListModel(), new[] { "group-1" }, null);
CustomerViewModelServiceMock.Verify(v => v.PrepareCustomerList(It.IsAny<CustomerListModel>(),
new[] { "group-1" }, null, 1, 10, ""), Times.Once);
}
[TestMethod]
public async Task CustomerList_StoreScope_ForcesRegisteredGroupAndStoreId()
{
ScopeMock.Setup(s => s.DefaultStoreId).Returns("store-1");
var registered = new CustomerGroup { Id = "registered-id" };
GroupServiceMock.Setup(g => g.GetCustomerGroupBySystemName(SystemCustomerGroupNames.Registered))
.ReturnsAsync(registered);
CustomerViewModelServiceMock
.Setup(v => v.PrepareCustomerList(It.IsAny<CustomerListModel>(),
new[] { "registered-id" }, null, 1, 10, "store-1"))
.ReturnsAsync((Enumerable.Empty<CustomerModel>(), 0));
// Attacker/UI submits an arbitrary group filter — must be ignored under Store scope.
await Controller.CustomerList(new DataSourceRequest { Page = 1, PageSize = 10 },
new CustomerListModel(), new[] { "attacker-submitted-group" }, null);
CustomerViewModelServiceMock.Verify(v => v.PrepareCustomerList(It.IsAny<CustomerListModel>(),
new[] { "registered-id" }, null, 1, 10, "store-1"), Times.Once);
}
[TestMethod]
public async Task CreateGet_GlobalScope_DoesNotCallApplyPostConstraints_ModelUntouched()
{
CustomerViewModelServiceMock.Setup(v => v.PrepareCustomerModel(It.IsAny<CustomerModel>(), null, false))
.Returns(Task.CompletedTask);
var result = await Controller.Create();
var view = result as ViewResult;
var model = view.Model as CustomerModel;
Assert.IsTrue(model.Active);
Assert.AreEqual("", model.StoreId ?? ""); // never forced for Admin (default no-op hook)
}
[TestMethod]
public async Task CreateGet_StoreScope_ClearsAvailableStores()
{
// Regression test for the cross-store leak flagged in code review: PrepareCustomerModel
// always populates every store's id/name onto AvailableStores (used for the
// StaffStoreId/StoreId pickers), but Store's own view never renders either select and the
// model still reaches Store's store_customer_details_* widget zones via
// additional-data="Model" - other stores' names/ids must not leak there.
ScopeMock.Setup(s => s.DefaultStoreId).Returns("store-1");
CustomerViewModelServiceMock
.Setup(v => v.PrepareCustomerModel(It.IsAny<CustomerModel>(), null, false))
.Callback<CustomerModel, Customer, bool>((m, _, _) =>
m.AvailableStores.Add(new SelectListItem { Value = "store-1", Text = "Store 1" }))
.Returns(Task.CompletedTask);
var result = await Controller.Create();
var model = (CustomerModel)((ViewResult)result).Model;
Assert.AreEqual(0, model.AvailableStores.Count);
}
[TestMethod]
public async Task CreateGet_GlobalScope_KeepsAvailableStores()
{
ScopeMock.Setup(s => s.DefaultStoreId).Returns((string)null);
CustomerViewModelServiceMock
.Setup(v => v.PrepareCustomerModel(It.IsAny<CustomerModel>(), null, false))
.Callback<CustomerModel, Customer, bool>((m, _, _) =>
m.AvailableStores.Add(new SelectListItem { Value = "store-1", Text = "Store 1" }))
.Returns(Task.CompletedTask);
var result = await Controller.Create();
var model = (CustomerModel)((ViewResult)result).Model;
Assert.AreEqual(1, model.AvailableStores.Count);
}
[TestMethod]
public async Task EditGet_StoreScope_ClearsAvailableStores()
{
var customer = new Customer { Id = "c1" };
CustomerServiceMock.Setup(s => s.GetCustomerById("c1")).ReturnsAsync(customer);
ScopeMock.Setup(s => s.HasAccess(customer)).ReturnsAsync(true);
ScopeMock.Setup(s => s.DefaultStoreId).Returns("store-1");
CustomerViewModelServiceMock
.Setup(v => v.PrepareCustomerModel(It.IsAny<CustomerModel>(), customer, false))
.Callback<CustomerModel, Customer, bool>((m, _, _) =>
m.AvailableStores.Add(new SelectListItem { Value = "store-1", Text = "Store 1" }))
.Returns(Task.CompletedTask);
var result = await Controller.Edit("c1");
var model = (CustomerModel)((ViewResult)result).Model;
Assert.AreEqual(0, model.AvailableStores.Count);
}
[TestMethod]
public async Task EditGet_GlobalScope_KeepsAvailableStores()
{
var customer = new Customer { Id = "c1" };
CustomerServiceMock.Setup(s => s.GetCustomerById("c1")).ReturnsAsync(customer);
ScopeMock.Setup(s => s.HasAccess(customer)).ReturnsAsync(true);
ScopeMock.Setup(s => s.DefaultStoreId).Returns((string)null);
CustomerViewModelServiceMock
.Setup(v => v.PrepareCustomerModel(It.IsAny<CustomerModel>(), customer, false))
.Callback<CustomerModel, Customer, bool>((m, _, _) =>
m.AvailableStores.Add(new SelectListItem { Value = "store-1", Text = "Store 1" }))
.Returns(Task.CompletedTask);
var result = await Controller.Edit("c1");
var model = (CustomerModel)((ViewResult)result).Model;
Assert.AreEqual(1, model.AvailableStores.Count);
}
[TestMethod]
public async Task CreatePost_Invalid_TwoFactorWarningHookNotCalledOnBase()
{
// BaseCustomerController's own CheckTwoFactorEnabledWarning is a no-op — verified indirectly:
// no exception, no TempData warning entry set, for a base-level (non-Management) controller.
var model = new CustomerModel { TwoFactorEnabled = true };
Controller.ModelState.AddModelError("x", "invalid");
CustomerViewModelServiceMock.Setup(v => v.PrepareCustomerModel(model, null, true))
.Returns(Task.CompletedTask);
var result = await Controller.Create(model, false);
Assert.IsInstanceOfType(result, typeof(ViewResult));
Assert.IsFalse(Controller.TempData.ContainsKey("grand.notifications.Warning"));
}
[TestMethod]
public async Task LoadAuthorizedCustomer_NotFound_ReturnsRedirectToList()
{
CustomerServiceMock.Setup(s => s.GetCustomerById("missing")).ReturnsAsync((Customer)null);
var (customer, denied) = await Controller.LoadAuthorizedCustomerPublic("missing");
Assert.IsNull(customer);
var redirect = denied as RedirectToActionResult;
Assert.IsNotNull(redirect);
Assert.AreEqual("List", redirect.ActionName);
ScopeMock.Verify(s => s.HasAccess(It.IsAny<Customer>()), Times.Never);
}
[TestMethod]
public async Task LoadAuthorizedCustomer_ScopeDenies_ReturnsRedirectToList()
{
var customer = new Customer { Id = "c1" };
CustomerServiceMock.Setup(s => s.GetCustomerById("c1")).ReturnsAsync(customer);
ScopeMock.Setup(s => s.HasAccess(customer)).ReturnsAsync(false);
var (result, denied) = await Controller.LoadAuthorizedCustomerPublic("c1");
Assert.IsNull(result);
var redirect = denied as RedirectToActionResult;
Assert.IsNotNull(redirect);
Assert.AreEqual("List", redirect.ActionName);
}
[TestMethod]
public async Task LoadAuthorizedCustomer_ScopeAllows_ReturnsCustomerNoDenial()
{
var customer = new Customer { Id = "c1" };
CustomerServiceMock.Setup(s => s.GetCustomerById("c1")).ReturnsAsync(customer);
ScopeMock.Setup(s => s.HasAccess(customer)).ReturnsAsync(true);
var (result, denied) = await Controller.LoadAuthorizedCustomerPublic("c1");
Assert.AreSame(customer, result);
Assert.IsNull(denied);
}
[TestMethod]
public async Task EditGet_Authorized_ReturnsViewAndCallsPrepareCustomerModel()
{
var customer = new Customer { Id = "c1" };
CustomerServiceMock.Setup(s => s.GetCustomerById("c1")).ReturnsAsync(customer);
ScopeMock.Setup(s => s.HasAccess(customer)).ReturnsAsync(true);
var result = await Controller.Edit("c1");
Assert.IsInstanceOfType(result, typeof(ViewResult));
CustomerViewModelServiceMock.Verify(v => v.PrepareCustomerModel(It.IsAny<CustomerModel>(), customer, false), Times.Once);
}
[TestMethod]
public async Task EditGet_Denied_RedirectsToList()
{
CustomerServiceMock.Setup(s => s.GetCustomerById("missing")).ReturnsAsync((Customer)null);
var result = await Controller.Edit("missing");
var redirect = result as RedirectToActionResult;
Assert.IsNotNull(redirect);
Assert.AreEqual("List", redirect.ActionName);
}
[TestMethod]
public async Task Delete_SelfDelete_ErrorsAndRedirectsWithoutDeleting()
{
var self = new Customer { Id = "self" };
var contextAccessorMock = new Mock<IContextAccessor>();
var workContextMock = new Mock<IWorkContext>();
workContextMock.Setup(w => w.CurrentCustomer).Returns(self);
contextAccessorMock.Setup(c => c.WorkContext).Returns(workContextMock.Object);
// Rebuild controller with a context accessor whose CurrentCustomer matches the target id.
var controller = new TestCustomerController(CustomerServiceMock.Object, CustomerViewModelServiceMock.Object,
CustomerManagerServiceMock.Object, new Mock<ICustomerProductService>().Object,
new Mock<Grand.Business.Core.Interfaces.Catalog.Products.IProductReviewService>().Object,
new Mock<Grand.Web.AdminShared.Interfaces.IProductReviewViewModelService>().Object,
new Mock<Grand.Web.AdminShared.Interfaces.IProductViewModelService>().Object,
new Mock<ICustomerAttributeParser>().Object, new Mock<ICustomerAttributeService>().Object,
new Mock<IAddressAttributeParser>().Object, new Mock<IAddressAttributeService>().Object,
new Mock<Grand.Business.Core.Interfaces.Messages.IMessageProviderService>().Object,
GroupServiceMock.Object, TranslationServiceMock.Object, contextAccessorMock.Object,
new CustomerSettings(), ScopeMock.Object)
{
ControllerContext = Controller.ControllerContext,
TempData = Controller.TempData
};
CustomerServiceMock.Setup(s => s.GetCustomerById("self")).ReturnsAsync(self);
ScopeMock.Setup(s => s.HasAccess(self)).ReturnsAsync(true);
var result = await controller.Delete("self");
var redirect = result as RedirectToActionResult;
Assert.IsNotNull(redirect);
Assert.AreEqual("List", redirect.ActionName);
CustomerViewModelServiceMock.Verify(v => v.DeleteCustomer(It.IsAny<Customer>()), Times.Never);
}
[TestMethod]
public async Task LoyaltyPointsHistoryAdd_StoreScope_ForcesStaffStoreId_IgnoringSubmittedStoreId()
{
var customer = new Customer { Id = "c1" };
CustomerServiceMock.Setup(s => s.GetCustomerById("c1")).ReturnsAsync(customer);
ScopeMock.Setup(s => s.HasAccess(customer)).ReturnsAsync(true);
ScopeMock.Setup(s => s.DefaultStoreId).Returns("store-1");
CustomerViewModelServiceMock
.Setup(v => v.InsertLoyaltyPointsHistory(customer, "store-1", 10, "msg"))
.ReturnsAsync(new Grand.Domain.Orders.LoyaltyPointsHistory());
// Caller submits a different, attacker-controlled storeId — must be ignored under Store scope.
await Controller.LoyaltyPointsHistoryAdd("c1", "attacker-store", 10, "msg");
CustomerViewModelServiceMock.Verify(v => v.InsertLoyaltyPointsHistory(customer, "store-1", 10, "msg"), Times.Once);
}
[TestMethod]
public async Task LoyaltyPointsHistoryAdd_GlobalScope_UsesSubmittedStoreId()
{
var customer = new Customer { Id = "c1" };
CustomerServiceMock.Setup(s => s.GetCustomerById("c1")).ReturnsAsync(customer);
ScopeMock.Setup(s => s.HasAccess(customer)).ReturnsAsync(true);
ScopeMock.Setup(s => s.DefaultStoreId).Returns((string)null);
CustomerViewModelServiceMock
.Setup(v => v.InsertLoyaltyPointsHistory(customer, "admin-submitted-store", 10, "msg"))
.ReturnsAsync(new Grand.Domain.Orders.LoyaltyPointsHistory());
await Controller.LoyaltyPointsHistoryAdd("c1", "admin-submitted-store", 10, "msg");
CustomerViewModelServiceMock.Verify(v => v.InsertLoyaltyPointsHistory(customer, "admin-submitted-store", 10, "msg"), Times.Once);
}
[TestMethod]
public async Task AddressesSelect_Denied_ThrowsArgumentException()
{
CustomerServiceMock.Setup(s => s.GetCustomerById("missing")).ReturnsAsync((Customer)null);
await Assert.ThrowsAsync<ArgumentException>(() =>
Controller.AddressesSelect("missing", new DataSourceRequest()));
}
[TestMethod]
public async Task AddressCreatePost_Authorized_InsertsAndRedirects()
{
var customer = new Customer { Id = "c1" };
CustomerServiceMock.Setup(s => s.GetCustomerById("c1")).ReturnsAsync(customer);
ScopeMock.Setup(s => s.HasAccess(customer)).ReturnsAsync(true);
var model = new CustomerAddressModel { CustomerId = "c1", Address = new AddressModel() };
var inserted = new Grand.Domain.Common.Address { Id = "a1" };
CustomerViewModelServiceMock
.Setup(v => v.InsertAddressModel(customer, model, It.IsAny<List<Grand.Domain.Common.CustomAttribute>>()))
.ReturnsAsync(inserted);
var result = await Controller.AddressCreate(model);
var redirect = result as RedirectToActionResult;
Assert.IsNotNull(redirect);
Assert.AreEqual("AddressEdit", redirect.ActionName);
}
[TestMethod]
public async Task OrderDetails_StoreScope_MismatchedStore_ReturnsEmptyNotException()
{
ScopeMock.Setup(s => s.DefaultStoreId).Returns("store-1");
var orderServiceMock = new Mock<Grand.Business.Core.Interfaces.Checkout.Orders.IOrderService>();
orderServiceMock.Setup(s => s.GetOrderById("o1"))
.ReturnsAsync(new Grand.Domain.Orders.Order { Id = "o1", StoreId = "store-2" });
var result = await Controller.OrderDetails("o1", orderServiceMock.Object,
new Mock<Grand.Web.AdminShared.Interfaces.IOrderViewModelService>().Object,
new Mock<Grand.Business.Core.Interfaces.Common.Security.IPermissionService>().Object);
var json = result as JsonResult;
var data = json.Value as DataSourceResult;
Assert.AreEqual(0, data.Total);
}
[TestMethod]
public async Task OrderDetails_GlobalScope_NotFound_ThrowsArgumentException()
{
ScopeMock.Setup(s => s.DefaultStoreId).Returns((string)null);
var permissionServiceMock = new Mock<Grand.Business.Core.Interfaces.Common.Security.IPermissionService>();
permissionServiceMock.Setup(p => p.Authorize(Grand.Domain.Permissions.StandardPermission.ManageOrders)).ReturnsAsync(true);
var orderServiceMock = new Mock<Grand.Business.Core.Interfaces.Checkout.Orders.IOrderService>();
orderServiceMock.Setup(s => s.GetOrderById("missing")).ReturnsAsync((Grand.Domain.Orders.Order)null);
await Assert.ThrowsAsync<ArgumentException>(() =>
Controller.OrderDetails("missing", orderServiceMock.Object,
new Mock<Grand.Web.AdminShared.Interfaces.IOrderViewModelService>().Object, permissionServiceMock.Object));
}
[TestMethod]
public async Task GetCartList_GlobalScope_NoOwnershipCheck_CallsServiceDirectly()
{
ScopeMock.Setup(s => s.DefaultStoreId).Returns((string)null);
CustomerViewModelServiceMock.Setup(v => v.PrepareShoppingCartItemModel("any-customer-id", 1))
.ReturnsAsync(new List<Grand.Web.AdminShared.Models.ShoppingCart.ShoppingCartItemModel>());
var result = await Controller.GetCartList("any-customer-id", 1);
Assert.IsInstanceOfType(result, typeof(JsonResult));
CustomerServiceMock.Verify(s => s.GetCustomerById(It.IsAny<string>()), Times.Never);
}
[TestMethod]
public async Task GetCartList_StoreScope_DeniedOwnership_ReturnsEmpty()
{
ScopeMock.Setup(s => s.DefaultStoreId).Returns("store-1");
CustomerServiceMock.Setup(s => s.GetCustomerById("c1")).ReturnsAsync((Customer)null);
var result = await Controller.GetCartList("c1", 1);
var json = result as JsonResult;
var data = json.Value as DataSourceResult;
Assert.AreEqual(0, data.Total);
CustomerViewModelServiceMock.Verify(v => v.PrepareShoppingCartItemModel(It.IsAny<string>(), It.IsAny<int>()), Times.Never);
}
[TestMethod]
public async Task ReviewDelete_StoreScope_MismatchedStore_ThrowsArgumentException()
{
ScopeMock.Setup(s => s.DefaultStoreId).Returns("store-1");
var review = new Grand.Domain.Catalog.ProductReview { Id = "r1", StoreId = "store-2" };
ProductReviewServiceMock.Setup(s => s.GetProductReviewById("r1")).ReturnsAsync(review);
await Assert.ThrowsAsync<ArgumentException>(() => Controller.ReviewDelete("r1"));
}
[TestMethod]
public async Task DeleteProductPrice_GlobalScope_NoOwnershipCheck_CallsServiceDirectly()
{
ScopeMock.Setup(s => s.DefaultStoreId).Returns((string)null);
CustomerViewModelServiceMock.Setup(v => v.DeleteProductPrice("pp1")).Returns(Task.CompletedTask);
var result = await Controller.DeleteProductPrice("pp1");
Assert.IsInstanceOfType(result, typeof(JsonResult));
CustomerViewModelServiceMock.Verify(v => v.DeleteProductPrice("pp1"), Times.Once);
}
[TestMethod]
public async Task DeleteProductPrice_StoreScope_PriceNotFound_NoOpsSilently()
{
ScopeMock.Setup(s => s.DefaultStoreId).Returns("store-1");
CustomerProductServiceMock.Setup(s => s.GetCustomerProductPriceById("pp1"))
.ReturnsAsync((Grand.Domain.Customers.CustomerProductPrice)null);
var result = await Controller.DeleteProductPrice("pp1");
Assert.IsInstanceOfType(result, typeof(JsonResult));
CustomerViewModelServiceMock.Verify(v => v.DeleteProductPrice(It.IsAny<string>()), Times.Never);
CustomerProductServiceMock.Verify(s => s.DeleteCustomerProductPrice(It.IsAny<Grand.Domain.Customers.CustomerProductPrice>()), Times.Never);
}
[TestMethod]
public async Task DeleteProductPrice_StoreScope_DeniedOwnership_NoOpsSilently()
{
// Price exists but belongs to a customer outside the caller's store — exercises the
// ownership-denial half of the guard, distinct from the not-found half above.
ScopeMock.Setup(s => s.DefaultStoreId).Returns("store-1");
var price = new Grand.Domain.Customers.CustomerProductPrice { Id = "pp1", CustomerId = "c1" };
CustomerProductServiceMock.Setup(s => s.GetCustomerProductPriceById("pp1")).ReturnsAsync(price);
var foreignCustomer = new Customer { Id = "c1" };
CustomerServiceMock.Setup(s => s.GetCustomerById("c1")).ReturnsAsync(foreignCustomer);
ScopeMock.Setup(s => s.HasAccess(foreignCustomer)).ReturnsAsync(false);
var result = await Controller.DeleteProductPrice("pp1");
Assert.IsInstanceOfType(result, typeof(JsonResult));
CustomerViewModelServiceMock.Verify(v => v.DeleteProductPrice(It.IsAny<string>()), Times.Never);
CustomerProductServiceMock.Verify(s => s.DeleteCustomerProductPrice(It.IsAny<Grand.Domain.Customers.CustomerProductPrice>()), Times.Never);
}
[TestMethod]
public async Task ProductAddPopup_Get_StoreScope_Denied_RedirectsToList()
{
ScopeMock.Setup(s => s.DefaultStoreId).Returns("store-1");
CustomerServiceMock.Setup(s => s.GetCustomerById("c1")).ReturnsAsync((Customer)null);
var result = await Controller.ProductAddPopup("c1");
var redirect = result as RedirectToActionResult;
Assert.IsNotNull(redirect);
Assert.AreEqual("List", redirect.ActionName);
}
[TestMethod]
public async Task ProductsPrice_StoreScope_DeniedOwnership_ReturnsEmpty()
{
ScopeMock.Setup(s => s.DefaultStoreId).Returns("store-1");
CustomerServiceMock.Setup(s => s.GetCustomerById("c1")).ReturnsAsync((Customer)null);
var result = await Controller.ProductsPrice(new DataSourceRequest { Page = 1, PageSize = 10 }, "c1");
var json = result as JsonResult;
var data = json.Value as DataSourceResult;
Assert.AreEqual(0, data.Total);
CustomerViewModelServiceMock.Verify(
v => v.PrepareProductPriceModel(It.IsAny<string>(), It.IsAny<int>(), It.IsAny<int>()), Times.Never);
}
[TestMethod]
public async Task ProductsPrice_GlobalScope_NoOwnershipCheck_CallsServiceDirectly()
{
ScopeMock.Setup(s => s.DefaultStoreId).Returns((string)null);
CustomerViewModelServiceMock.Setup(v => v.PrepareProductPriceModel("c1", 1, 10))
.ReturnsAsync((Enumerable.Empty<CustomerModel.ProductPriceModel>(), 0));
var result = await Controller.ProductsPrice(new DataSourceRequest { Page = 1, PageSize = 10 }, "c1");
Assert.IsInstanceOfType(result, typeof(JsonResult));
CustomerServiceMock.Verify(s => s.GetCustomerById(It.IsAny<string>()), Times.Never);
CustomerViewModelServiceMock.Verify(v => v.PrepareProductPriceModel("c1", 1, 10), Times.Once);
}
[TestMethod]
public async Task PersonalizedProducts_StoreScope_DeniedOwnership_ReturnsEmpty()
{
ScopeMock.Setup(s => s.DefaultStoreId).Returns("store-1");
CustomerServiceMock.Setup(s => s.GetCustomerById("c1")).ReturnsAsync((Customer)null);
var result = await Controller.PersonalizedProducts(new DataSourceRequest { Page = 1, PageSize = 10 }, "c1");
var json = result as JsonResult;
var data = json.Value as DataSourceResult;
Assert.AreEqual(0, data.Total);
CustomerViewModelServiceMock.Verify(
v => v.PreparePersonalizedProducts(It.IsAny<string>(), It.IsAny<int>(), It.IsAny<int>()), Times.Never);
}
[TestMethod]
public async Task PersonalizedProducts_GlobalScope_NoOwnershipCheck_CallsServiceDirectly()
{
ScopeMock.Setup(s => s.DefaultStoreId).Returns((string)null);
CustomerViewModelServiceMock.Setup(v => v.PreparePersonalizedProducts("c1", 1, 10))
.ReturnsAsync((Enumerable.Empty<CustomerModel.ProductModel>(), 0));
var result = await Controller.PersonalizedProducts(new DataSourceRequest { Page = 1, PageSize = 10 }, "c1");
Assert.IsInstanceOfType(result, typeof(JsonResult));
CustomerServiceMock.Verify(s => s.GetCustomerById(It.IsAny<string>()), Times.Never);
CustomerViewModelServiceMock.Verify(v => v.PreparePersonalizedProducts("c1", 1, 10), Times.Once);
}
[TestMethod]
public async Task ReviewList_StoreScope_DeniedOwnership_ReturnsEmpty()
{
ScopeMock.Setup(s => s.DefaultStoreId).Returns("store-1");
CustomerServiceMock.Setup(s => s.GetCustomerById("c1")).ReturnsAsync((Customer)null);
var result = await Controller.ReviewList("c1", new DataSourceRequest { Page = 1, PageSize = 10 });
var json = result as JsonResult;
var data = json.Value as DataSourceResult;
Assert.AreEqual(0, data.Total);
ProductReviewServiceMock.Verify(s => s.GetAllProductReviews(
It.IsAny<string>(), It.IsAny<bool?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(),
It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<int>(), It.IsAny<int>()), Times.Never);
}
[TestMethod]
public async Task ReviewList_GlobalScope_NoOwnershipCheck_CallsServiceDirectly()
{
ScopeMock.Setup(s => s.DefaultStoreId).Returns((string)null);
ProductReviewServiceMock.Setup(s => s.GetAllProductReviews(
"c1", null, null, null, "", null, "", 0, 10))
.ReturnsAsync(new Grand.Domain.PagedList<Grand.Domain.Catalog.ProductReview>(
new List<Grand.Domain.Catalog.ProductReview>(), 0, 10, 0));
var result = await Controller.ReviewList("c1", new DataSourceRequest { Page = 1, PageSize = 10 });
Assert.IsInstanceOfType(result, typeof(JsonResult));
CustomerServiceMock.Verify(s => s.GetCustomerById(It.IsAny<string>()), Times.Never);
}
[TestMethod]
public async Task OutOfStockSubscriptionList_GlobalScope_NoOwnershipCheck()
{
ScopeMock.Setup(s => s.DefaultStoreId).Returns((string)null);
CustomerViewModelServiceMock
.Setup(v => v.PrepareOutOfStockSubscriptionModel("c1", 1, 10))
.ReturnsAsync((Enumerable.Empty<CustomerModel.OutOfStockSubscriptionModel>(), 0));
var result = await Controller.OutOfStockSubscriptionList(
new DataSourceRequest { Page = 1, PageSize = 10 }, "c1");
Assert.IsInstanceOfType(result, typeof(JsonResult));
CustomerServiceMock.Verify(s => s.GetCustomerById(It.IsAny<string>()), Times.Never);
}
}