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.

2.6 KB
Raw Blame
using Grand.Business.Core.Interfaces.Customers;
using Grand.Business.Core.Utilities.Customers;
using Grand.Domain.Customers;
using Grand.Web.Commands.Handler.Customers;
using Grand.Web.Commands.Models.Customers;
using Grand.Web.Models.Customer;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;

namespace Grand.Web.Tests.Commands.Customers;

[TestClass]
public class SubAccountEditCommandHandlerTests
{
    private Mock<ICustomerService> _customerServiceMock;
    private Mock<ICustomerManagerService> _customerManagerServiceMock;
    private SubAccountEditCommandHandler _handler;

    [TestInitialize]
    public void Init()
    {
        _customerServiceMock = new Mock<ICustomerService>();
        _customerManagerServiceMock = new Mock<ICustomerManagerService>();
        _handler = new SubAccountEditCommandHandler(_customerServiceMock.Object,
            _customerManagerServiceMock.Object, new CustomerSettings { AllowUsersToChangeEmail = true });
    }

    [TestMethod]
    public async Task Handle_WithPassword_ChangesPasswordScopedToCustomerStore()
    {
        var customer = new Customer { Id = "c1", Email = "[email protected]", StoreId = "store-9" };
        _customerServiceMock.Setup(c => c.GetCustomerById("c1")).ReturnsAsync(customer);

        var command = new SubAccountEditCommand {
            CurrentCustomer = new Customer { Id = "owner" },
            EditModel = new SubAccountEditModel {
                Id = "c1", Email = "[email protected]", Password = "newpass", Active = true,
                FirstName = "John", LastName = "Doe"
            }
        };

        var result = await _handler.Handle(command, default);

        Assert.IsTrue(result);
        //the sub-account's store must scope the password change (safe with or without per-store identity)
        _customerManagerServiceMock.Verify(m => m.ChangePassword(
            It.Is<ChangePasswordRequest>(r => r.Email == "[email protected]"), "store-9"), Times.Once);
    }

    [TestMethod]
    public async Task Handle_WithoutPassword_DoesNotChangePassword()
    {
        var customer = new Customer { Id = "c1", Email = "[email protected]", StoreId = "store-9" };
        _customerServiceMock.Setup(c => c.GetCustomerById("c1")).ReturnsAsync(customer);

        var command = new SubAccountEditCommand {
            CurrentCustomer = new Customer { Id = "owner" },
            EditModel = new SubAccountEditModel { Id = "c1", Email = "[email protected]", Active = true }
        };

        var result = await _handler.Handle(command, default);

        Assert.IsTrue(result);
        _customerManagerServiceMock.Verify(m => m.ChangePassword(
            It.IsAny<ChangePasswordRequest>(), It.IsAny<string>()), Times.Never);
    }
}