Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
<PackageVersion Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.21.0" />
<PackageVersion Include="Microsoft.Win32.Primitives" Version="4.3.0" />
<PackageVersion Include="Minimalpi.Endpoint" Version="1.3.0" />
<PackageVersion Include="Moq" Version="4.20.72" />
<PackageVersion Include="NimblePros.SharedKernel" Version="2.1.1" />
<PackageVersion Include="NimblePros.Metronome" Version="0.4.1" />
<PackageVersion Include="NSubstitute" Version="5.3.0" />
Expand Down
26 changes: 16 additions & 10 deletions src/Web/Controllers/ManageController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,16 +107,7 @@ public async Task<IActionResult> SendVerificationEmail(IndexViewModel model)

var user = await GetCurrentUserAsync();

var code = await _userManager.GenerateEmailConfirmationTokenAsync(user);
var callbackUrl = Url.EmailConfirmationLink(user.Id, code, Request.Scheme);
Guard.Against.Null(callbackUrl, nameof(callbackUrl));
var email = user.Email;
if (email == null)
{
throw new ApplicationException($"No email associated with user {user.UserName}'.");
}

await _emailSender.SendEmailConfirmationAsync(email, callbackUrl);
await SendVerificationEmailInternalAsync(user);

StatusMessage = "Verification email sent. Please check your email.";
return RedirectToAction(nameof(MyAccount));
Expand Down Expand Up @@ -491,4 +482,19 @@ private async Task<ApplicationUser> GetCurrentUserAsync()
return user ?? throw new UserNotFoundException(_userManager.GetUserId(User) ?? string.Empty);
}

protected virtual async Task SendVerificationEmailInternalAsync(ApplicationUser user)
{
var email = user.Email;
if(email == null)
{
throw new ApplicationException($"No email associated with user {user.UserName}'.");
}

var code = await _userManager.GenerateEmailConfirmationTokenAsync(user);
var callbackUrl = Url.EmailConfirmationLink(user.Id, code, Request.Scheme);
Guard.Against.Null(callbackUrl, nameof(callbackUrl));

await _emailSender.SendEmailConfirmationAsync(email, callbackUrl);
}

}
1 change: 1 addition & 0 deletions tests/UnitTests/UnitTests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.NET.Test.Sdk" />
<PackageReference Include="Moq" />
<PackageReference Include="NSubstitute" />
<PackageReference Include="NSubstitute.Analyzers.CSharp">
<PrivateAssets>all</PrivateAssets>
Expand Down
131 changes: 131 additions & 0 deletions tests/UnitTests/Web/Controllers/ManageControllerTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
using System;
using System.Collections.Generic;
using System.Security.Claims;
using System.Text;
using System.Text.Encodings.Web;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using Microsoft.eShopWeb.ApplicationCore.Interfaces;
using Microsoft.eShopWeb.Infrastructure.Identity;
using Microsoft.eShopWeb.Web.Controllers;
using Microsoft.eShopWeb.Web.Services;
using Moq;
using Xunit;
using Microsoft.AspNetCore.Mvc.Routing;

namespace Microsoft.eShopWeb.UnitTests.Web.Controllers;
public class ManageControllerTests
{
[Fact]
public async Task SendVerificationEmail_sends_email()
{
// Arrange
var user = new ApplicationUser
{
Id = "user-id",
Email = "test@test.com",
UserName = "test"
};

var userManager = TestUserManager(user);
var signInManager = TestSignInManager(userManager);

var emailSender = new Mock<IEmailSender>();
emailSender.Setup(e => e.SendEmailAsync(
user.Email,
It.IsAny<string>(),
It.IsAny<string>()))
.Returns(Task.CompletedTask);

var logger = new Mock<IAppLogger<ManageController>>();
var urlEncoder = UrlEncoder.Default;

var controller = new TestableManageController(
userManager,
signInManager,
emailSender.Object,
logger.Object,
urlEncoder);

controller.ControllerContext = new ControllerContext
{
HttpContext = new DefaultHttpContext
{
User = new ClaimsPrincipal(new ClaimsIdentity(
new[] { new Claim(ClaimTypes.NameIdentifier, user.Id) }))
}
};

var urlHelper = new Mock<IUrlHelper>();
urlHelper.Setup(u => u.Action(It.IsAny<UrlActionContext>()))
.Returns("https://localhost/confirm-email");

controller.Url = urlHelper.Object;

// Act
await controller.InvokeSendVerificationEmail(user);

// Assert
emailSender.Verify(e => e.SendEmailAsync(
user.Email,
It.IsAny<string>(),
It.IsAny<string>()),
Times.Once);
}

#region Helpers
private static UserManager<ApplicationUser> TestUserManager(ApplicationUser user)
{
var store = new Mock<IUserStore<ApplicationUser>>();

var manager = new Mock<UserManager<ApplicationUser>>(
store.Object,
null,
null,
null,
null,
null,
null,
null,
null);

manager.Setup(m => m.GetUserAsync(It.IsAny<ClaimsPrincipal>()))
.ReturnsAsync(user);

manager.Setup(m => m.GenerateEmailConfirmationTokenAsync(user))
.ReturnsAsync("token");

return manager.Object;
}

private static SignInManager<ApplicationUser> TestSignInManager(
UserManager<ApplicationUser> userManager)
{
return new SignInManager<ApplicationUser>(
userManager,
new Mock<IHttpContextAccessor>().Object,
new Mock<IUserClaimsPrincipalFactory<ApplicationUser>>().Object,
null,
null,
null,
null);
}
#endregion


internal class TestableManageController : ManageController
{
public TestableManageController(
UserManager<ApplicationUser> userManager,
SignInManager<ApplicationUser> signInManager,
IEmailSender emailSender,
IAppLogger<ManageController> logger,
UrlEncoder urlEncoder)
: base(userManager, signInManager, emailSender, logger, urlEncoder)
{
}

public Task InvokeSendVerificationEmail(ApplicationUser user) => SendVerificationEmailInternalAsync(user);
}
}