-
-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Clean up verification email sending in HangfireBackgroundService
- Loading branch information
1 parent
e9d0707
commit 753caae
Showing
7 changed files
with
193 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/* | ||
* Copyright (C) 2023 Crypter File Transfer | ||
* | ||
* This file is part of the Crypter file transfer project. | ||
* | ||
* Crypter is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* The Crypter source code is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Affero General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
* | ||
* You can be released from the requirements of the aforementioned license | ||
* by purchasing a commercial license. Buying such a license is mandatory | ||
* as soon as you develop commercial activities involving the Crypter source | ||
* code without disclosing the source code of your own applications. | ||
* | ||
* Contact the current copyright holder to discuss commercial license options. | ||
*/ | ||
|
||
using System; | ||
|
||
namespace Crypter.Core.Exceptions; | ||
|
||
public class HangfireJobException : Exception | ||
{ | ||
public HangfireJobException(string message) : base(message) | ||
{ } | ||
} |
87 changes: 87 additions & 0 deletions
87
Crypter.Core/Features/UserEmailVerification/Commands/SendVerificationEmailCommand.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
/* | ||
* Copyright (C) 2023 Crypter File Transfer | ||
* | ||
* This file is part of the Crypter file transfer project. | ||
* | ||
* Crypter is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* The Crypter source code is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Affero General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
* | ||
* You can be released from the requirements of the aforementioned license | ||
* by purchasing a commercial license. Buying such a license is mandatory | ||
* as soon as you develop commercial activities involving the Crypter source | ||
* code without disclosing the source code of your own applications. | ||
* | ||
* Contact the current copyright holder to discuss commercial license options. | ||
*/ | ||
|
||
using System; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Crypter.Core.Models; | ||
using Crypter.Core.Services; | ||
using Crypter.Crypto.Common; | ||
using Crypter.DataAccess; | ||
using Crypter.DataAccess.Entities; | ||
using EasyMonads; | ||
using MediatR; | ||
|
||
namespace Crypter.Core.Features.UserEmailVerification.Commands; | ||
|
||
/// <summary> | ||
/// A command that will attempt to send a verification email to the provided | ||
/// user. | ||
/// | ||
/// Returns 'true' if the email was successfully sent or if the user's current | ||
/// state is such that they should not receive a verification email. | ||
/// | ||
/// Returns 'false' if the email should be sent, but failed to send. | ||
/// </summary> | ||
/// <param name="UserId"></param> | ||
public sealed record SendVerificationEmailCommand(Guid UserId) : IRequest<bool>; | ||
|
||
internal sealed class SendVerificationEmailCommandHandler | ||
: IRequestHandler<SendVerificationEmailCommand, bool> | ||
{ | ||
private readonly ICryptoProvider _cryptoProvider; | ||
private readonly DataContext _dataContext; | ||
private readonly IEmailService _emailService; | ||
|
||
public SendVerificationEmailCommandHandler( | ||
ICryptoProvider cryptoProvider, | ||
DataContext dataContext, | ||
IEmailService emailService) | ||
{ | ||
_cryptoProvider = cryptoProvider; | ||
_dataContext = dataContext; | ||
_emailService = emailService; | ||
} | ||
|
||
public async Task<bool> Handle(SendVerificationEmailCommand request, CancellationToken cancellationToken) | ||
{ | ||
return await UserEmailVerificationQueries | ||
.GenerateVerificationParametersAsync(_dataContext, _cryptoProvider, request.UserId) | ||
.BindAsync<UserEmailAddressVerificationParameters, bool>(async parameters => | ||
{ | ||
bool deliverySuccess = await _emailService.SendEmailVerificationAsync(parameters); | ||
if (deliverySuccess) | ||
{ | ||
UserEmailVerificationEntity newEntity = new UserEmailVerificationEntity(parameters.UserId, | ||
parameters.VerificationCode, parameters.VerificationKey, DateTime.UtcNow); | ||
_dataContext.UserEmailVerifications.Add(newEntity); | ||
await _dataContext.SaveChangesAsync(CancellationToken.None); | ||
} | ||
|
||
return deliverySuccess; | ||
}).SomeOrDefaultAsync(true); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,6 +28,8 @@ | |
using Crypter.Common.Primitives; | ||
using Crypter.Core.Services; | ||
using Crypter.Core.Settings; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Logging; | ||
using Microsoft.Extensions.Options; | ||
using NUnit.Framework; | ||
|
||
|
@@ -37,18 +39,25 @@ namespace Crypter.Test.Core_Tests.Services_Tests; | |
public class EmailService_Tests | ||
{ | ||
private IOptions<EmailSettings> _defaultEmailSettings; | ||
private ILogger<EmailService> _logger; | ||
|
||
[OneTimeSetUp] | ||
public void OneTimeSetup() | ||
[SetUp] | ||
public void Setup() | ||
{ | ||
EmailSettings settings = new EmailSettings(); | ||
_defaultEmailSettings = Options.Create(settings); | ||
|
||
var serviceProvider = new ServiceCollection() | ||
.AddLogging() | ||
.BuildServiceProvider(); | ||
var factory = serviceProvider.GetService<ILoggerFactory>(); | ||
_logger = factory.CreateLogger<EmailService>(); | ||
} | ||
|
||
[Test] | ||
public async Task ServiceDisabled_SendAsync_ReturnsTrue() | ||
{ | ||
var sut = new EmailService(_defaultEmailSettings); | ||
var sut = new EmailService(_defaultEmailSettings, _logger); | ||
var emailAddress = EmailAddress.From("[email protected]"); | ||
var result = await sut.SendAsync("foo", "bar", emailAddress); | ||
Assert.IsTrue(result); | ||
|
Oops, something went wrong.