|
| 1 | +using Grand.Business.Core.Interfaces.Common.Directory; |
| 2 | +using Grand.Business.Core.Interfaces.Common.Security; |
| 3 | +using Grand.Business.Core.Interfaces.Customers; |
| 4 | +using Grand.Domain.Customers; |
| 5 | +using Grand.Infrastructure; |
| 6 | +using Grand.Mediator; |
| 7 | +using Grand.Module.Api.Controllers; |
| 8 | +using Grand.Module.Api.DTOs.Customers; |
| 9 | +using Microsoft.AspNetCore.Http; |
| 10 | +using Microsoft.AspNetCore.Mvc; |
| 11 | +using Microsoft.Extensions.DependencyInjection; |
| 12 | +using Microsoft.VisualStudio.TestTools.UnitTesting; |
| 13 | +using Moq; |
| 14 | + |
| 15 | +namespace Grand.Module.Api.Tests.Controllers; |
| 16 | + |
| 17 | +[TestClass] |
| 18 | +public class CustomerControllerSetPasswordTests |
| 19 | +{ |
| 20 | + private readonly Customer _current = new() { Id = "staff" }; |
| 21 | + private readonly Customer _target = new() { Id = "c1", Email = "c1@shop.test" }; |
| 22 | + private CustomerController _controller; |
| 23 | + private Mock<ICustomerManagerService> _customerManagerService; |
| 24 | + private Mock<IGroupService> _groupService; |
| 25 | + |
| 26 | + [TestInitialize] |
| 27 | + public void Init() |
| 28 | + { |
| 29 | + var permissionService = new Mock<IPermissionService>(); |
| 30 | + permissionService.Setup(x => x.Authorize(It.IsAny<string>(), _current)).ReturnsAsync(true); |
| 31 | + permissionService.Setup(x => x.AuthorizeAction(It.IsAny<string>(), It.IsAny<string>(), _current)) |
| 32 | + .ReturnsAsync(true); |
| 33 | + var customerService = new Mock<ICustomerService>(); |
| 34 | + customerService.Setup(x => x.GetCustomerByEmail(_target.Email, It.IsAny<string>())).ReturnsAsync(_target); |
| 35 | + _customerManagerService = new Mock<ICustomerManagerService>(); |
| 36 | + _groupService = new Mock<IGroupService>(); |
| 37 | + |
| 38 | + var contextAccessor = new Mock<IContextAccessor>(); |
| 39 | + contextAccessor.Setup(x => x.WorkContext.CurrentCustomer).Returns(_current); |
| 40 | + var services = new ServiceCollection().AddSingleton(contextAccessor.Object).BuildServiceProvider(); |
| 41 | + |
| 42 | + _controller = new CustomerController(Mock.Of<IMediator>(), permissionService.Object, |
| 43 | + _customerManagerService.Object, new CustomerSettings(), customerService.Object, _groupService.Object) { |
| 44 | + ControllerContext = new ControllerContext |
| 45 | + { HttpContext = new DefaultHttpContext { RequestServices = services } } |
| 46 | + }; |
| 47 | + } |
| 48 | + |
| 49 | + [TestMethod] |
| 50 | + public async Task NonAdministrator_SetsAnAdministratorsPassword_Forbidden() |
| 51 | + { |
| 52 | + _groupService.Setup(x => x.IsAdmin(_target)).ReturnsAsync(true); |
| 53 | + |
| 54 | + var result = await _controller.SetPassword(_target.Email, new PasswordDto { Password = "secret" }); |
| 55 | + |
| 56 | + Assert.IsInstanceOfType<ForbidResult>(result); |
| 57 | + _customerManagerService.Verify(x => x.ChangePassword(It.IsAny<Customer>(), It.IsAny<PasswordFormat>(), |
| 58 | + It.IsAny<string>()), Times.Never); |
| 59 | + } |
| 60 | + |
| 61 | + [TestMethod] |
| 62 | + public async Task Administrator_SetsAnAdministratorsPassword_Changed() |
| 63 | + { |
| 64 | + _groupService.Setup(x => x.IsAdmin(It.IsAny<Customer>())).ReturnsAsync(true); |
| 65 | + |
| 66 | + var result = await _controller.SetPassword(_target.Email, new PasswordDto { Password = "secret" }); |
| 67 | + |
| 68 | + Assert.IsInstanceOfType<OkObjectResult>(result); |
| 69 | + _customerManagerService.Verify(x => x.ChangePassword(_target, It.IsAny<PasswordFormat>(), "secret"), |
| 70 | + Times.Once); |
| 71 | + } |
| 72 | + |
| 73 | + [TestMethod] |
| 74 | + public async Task NonAdministrator_SetsACustomersPassword_Changed() |
| 75 | + { |
| 76 | + var result = await _controller.SetPassword(_target.Email, new PasswordDto { Password = "secret" }); |
| 77 | + |
| 78 | + Assert.IsInstanceOfType<OkObjectResult>(result); |
| 79 | + _customerManagerService.Verify(x => x.ChangePassword(_target, It.IsAny<PasswordFormat>(), "secret"), |
| 80 | + Times.Once); |
| 81 | + } |
| 82 | + |
| 83 | + [TestMethod] |
| 84 | + public async Task UnknownEmail_NotFound() |
| 85 | + { |
| 86 | + var result = await _controller.SetPassword("nobody@shop.test", new PasswordDto { Password = "secret" }); |
| 87 | + |
| 88 | + Assert.IsInstanceOfType<NotFoundResult>(result); |
| 89 | + } |
| 90 | +} |
0 commit comments