parent
6842c561ea
commit
a8b6f70be1
@ -0,0 +1,92 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using FluentAssertions;
|
||||
using NUnit.Framework;
|
||||
using NzbDrone.Common.Serializer;
|
||||
using NzbDrone.Core.Datastore.Migration;
|
||||
using NzbDrone.Core.Test.Framework;
|
||||
|
||||
namespace NzbDrone.Core.Test.Datastore.Migration
|
||||
{
|
||||
[TestFixture]
|
||||
public class email_multiple_addressesFixture : MigrationTest<email_multiple_addresses>
|
||||
{
|
||||
[Test]
|
||||
public void should_convert_to_list_on_email_lists()
|
||||
{
|
||||
var db = WithMigrationTestDb(c =>
|
||||
{
|
||||
c.Insert.IntoTable("Notifications").Row(new
|
||||
{
|
||||
OnGrab = true,
|
||||
OnReleaseImport = true,
|
||||
OnUpgrade = true,
|
||||
OnDownloadFailure = true,
|
||||
OnImportFailure = true,
|
||||
OnTrackRetag = true,
|
||||
OnHealthIssue = true,
|
||||
IncludeHealthWarnings = true,
|
||||
OnRename = true,
|
||||
Name = "Gmail Lidarr",
|
||||
Implementation = "Email",
|
||||
Tags = "[]",
|
||||
Settings = new EmailSettings48
|
||||
{
|
||||
Server = "smtp.gmail.com",
|
||||
Port = 563,
|
||||
To = "lidarr@lidarr.audio"
|
||||
}.ToJson(),
|
||||
ConfigContract = "EmailSettings"
|
||||
});
|
||||
});
|
||||
|
||||
var items = db.Query<ProviderDefinition166>("SELECT * FROM Notifications");
|
||||
|
||||
items.Should().HaveCount(1);
|
||||
items.First().Implementation.Should().Be("Email");
|
||||
items.First().ConfigContract.Should().Be("EmailSettings");
|
||||
items.First().Settings.To.Count().Should().Be(1);
|
||||
}
|
||||
}
|
||||
|
||||
public class ProviderDefinition166
|
||||
{
|
||||
public int Id { get; set; }
|
||||
public string Implementation { get; set; }
|
||||
public string ConfigContract { get; set; }
|
||||
public EmailSettings49 Settings { get; set; }
|
||||
public string Name { get; set; }
|
||||
public bool OnGrab { get; set; }
|
||||
public bool OnReleaseImport { get; set; }
|
||||
public bool OnUpgrade { get; set; }
|
||||
public bool OnRename { get; set; }
|
||||
public bool OnDownloadFailure { get; set; }
|
||||
public bool OnImportFailure { get; set; }
|
||||
public bool OnTrackRetag { get; set; }
|
||||
public bool OnHealthIssue { get; set; }
|
||||
public bool IncludeHealthWarnings { get; set; }
|
||||
public List<int> Tags { get; set; }
|
||||
}
|
||||
|
||||
public class EmailSettings48
|
||||
{
|
||||
public string Server { get; set; }
|
||||
public int Port { get; set; }
|
||||
public bool Ssl { get; set; }
|
||||
public string Username { get; set; }
|
||||
public string Password { get; set; }
|
||||
public string From { get; set; }
|
||||
public string To { get; set; }
|
||||
}
|
||||
|
||||
public class EmailSettings49
|
||||
{
|
||||
public string Server { get; set; }
|
||||
public int Port { get; set; }
|
||||
public bool Ssl { get; set; }
|
||||
public string Username { get; set; }
|
||||
public string Password { get; set; }
|
||||
public string From { get; set; }
|
||||
public IEnumerable<string> To { get; set; }
|
||||
}
|
||||
}
|
@ -0,0 +1,103 @@
|
||||
using System;
|
||||
using FizzWare.NBuilder;
|
||||
using FluentAssertions;
|
||||
using NUnit.Framework;
|
||||
using NzbDrone.Core.Notifications.Email;
|
||||
using NzbDrone.Core.Test.Framework;
|
||||
using NzbDrone.Test.Common;
|
||||
|
||||
namespace NzbDrone.Core.Test.NotificationTests.EmailTests
|
||||
{
|
||||
[TestFixture]
|
||||
public class EmailSettingsValidatorFixture : CoreTest<EmailSettingsValidator>
|
||||
{
|
||||
private EmailSettings _emailSettings;
|
||||
private TestValidator<EmailSettings> _validator;
|
||||
|
||||
[SetUp]
|
||||
public void Setup()
|
||||
{
|
||||
_validator = new TestValidator<EmailSettings>
|
||||
{
|
||||
v => v.RuleFor(s => s).SetValidator(Subject)
|
||||
};
|
||||
|
||||
_emailSettings = Builder<EmailSettings>.CreateNew()
|
||||
.With(s => s.Server = "someserver")
|
||||
.With(s => s.Port = 567)
|
||||
.With(s => s.From = "lidarr@lidarr.audio")
|
||||
.With(s => s.To = new string[] { "lidarr@lidarr.audio" })
|
||||
.Build();
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void should_be_valid_if_all_settings_valid()
|
||||
{
|
||||
_validator.Validate(_emailSettings).IsValid.Should().BeTrue();
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void should_not_be_valid_if_port_is_out_of_range()
|
||||
{
|
||||
_emailSettings.Port = 900000;
|
||||
|
||||
_validator.Validate(_emailSettings).IsValid.Should().BeFalse();
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void should_not_be_valid_if_server_is_empty()
|
||||
{
|
||||
_emailSettings.Server = "";
|
||||
|
||||
_validator.Validate(_emailSettings).IsValid.Should().BeFalse();
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void should_not_be_valid_if_from_is_empty()
|
||||
{
|
||||
_emailSettings.From = "";
|
||||
|
||||
_validator.Validate(_emailSettings).IsValid.Should().BeFalse();
|
||||
}
|
||||
|
||||
[TestCase("lidarr")]
|
||||
[TestCase("lidarr@lidarr")]
|
||||
[TestCase("lidarr.audio")]
|
||||
public void should_not_be_valid_if_to_is_invalid(string email)
|
||||
{
|
||||
_emailSettings.To = new string[] { email };
|
||||
|
||||
_validator.Validate(_emailSettings).IsValid.Should().BeFalse();
|
||||
}
|
||||
|
||||
[TestCase("lidarr")]
|
||||
[TestCase("lidarr@lidarr")]
|
||||
[TestCase("lidarr.audio")]
|
||||
public void should_not_be_valid_if_cc_is_invalid(string email)
|
||||
{
|
||||
_emailSettings.CC = new string[] { email };
|
||||
|
||||
_validator.Validate(_emailSettings).IsValid.Should().BeFalse();
|
||||
}
|
||||
|
||||
[TestCase("lidarr")]
|
||||
[TestCase("lidarr@lidarr")]
|
||||
[TestCase("lidarr.audio")]
|
||||
public void should_not_be_valid_if_bcc_is_invalid(string email)
|
||||
{
|
||||
_emailSettings.Bcc = new string[] { email };
|
||||
|
||||
_validator.Validate(_emailSettings).IsValid.Should().BeFalse();
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void should_not_be_valid_if_to_bcc_cc_are_all_empty()
|
||||
{
|
||||
_emailSettings.To = Array.Empty<string>();
|
||||
_emailSettings.CC = Array.Empty<string>();
|
||||
_emailSettings.Bcc = Array.Empty<string>();
|
||||
|
||||
_validator.Validate(_emailSettings).IsValid.Should().BeFalse();
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,97 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data;
|
||||
using System.Text.Json;
|
||||
using Dapper;
|
||||
using FluentMigrator;
|
||||
using NzbDrone.Core.Datastore.Migration.Framework;
|
||||
|
||||
namespace NzbDrone.Core.Datastore.Migration
|
||||
{
|
||||
[Migration(049)]
|
||||
public class email_multiple_addresses : NzbDroneMigrationBase
|
||||
{
|
||||
private readonly JsonSerializerOptions _serializerSettings;
|
||||
|
||||
public email_multiple_addresses()
|
||||
{
|
||||
_serializerSettings = new JsonSerializerOptions
|
||||
{
|
||||
AllowTrailingCommas = true,
|
||||
IgnoreNullValues = false,
|
||||
PropertyNameCaseInsensitive = true,
|
||||
DictionaryKeyPolicy = JsonNamingPolicy.CamelCase,
|
||||
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
|
||||
WriteIndented = true
|
||||
};
|
||||
}
|
||||
|
||||
protected override void MainDbUpgrade()
|
||||
{
|
||||
Execute.WithConnection(ChangeEmailAddressType);
|
||||
}
|
||||
|
||||
private void ChangeEmailAddressType(IDbConnection conn, IDbTransaction tran)
|
||||
{
|
||||
var rows = conn.Query<ProviderDefinition166>($"SELECT Id, Settings FROM Notifications WHERE Implementation = 'Email'");
|
||||
|
||||
var corrected = new List<ProviderDefinition166>();
|
||||
|
||||
foreach (var row in rows)
|
||||
{
|
||||
var settings = JsonSerializer.Deserialize<EmailSettings48>(row.Settings, _serializerSettings);
|
||||
|
||||
var newSettings = new EmailSettings49
|
||||
{
|
||||
Server = settings.Server,
|
||||
Port = settings.Port,
|
||||
Ssl = settings.Ssl,
|
||||
Username = settings.Username,
|
||||
Password = settings.Password,
|
||||
From = settings.From,
|
||||
To = new string[] { settings.To },
|
||||
CC = Array.Empty<string>(),
|
||||
Bcc = Array.Empty<string>()
|
||||
};
|
||||
|
||||
corrected.Add(new ProviderDefinition166
|
||||
{
|
||||
Id = row.Id,
|
||||
Settings = JsonSerializer.Serialize(newSettings, _serializerSettings)
|
||||
});
|
||||
}
|
||||
|
||||
var updateSql = "UPDATE Notifications SET Settings = @Settings WHERE Id = @Id";
|
||||
conn.Execute(updateSql, corrected, transaction: tran);
|
||||
}
|
||||
|
||||
private class ProviderDefinition166 : ModelBase
|
||||
{
|
||||
public string Settings { get; set; }
|
||||
}
|
||||
|
||||
private class EmailSettings48
|
||||
{
|
||||
public string Server { get; set; }
|
||||
public int Port { get; set; }
|
||||
public bool Ssl { get; set; }
|
||||
public string Username { get; set; }
|
||||
public string Password { get; set; }
|
||||
public string From { get; set; }
|
||||
public string To { get; set; }
|
||||
}
|
||||
|
||||
private class EmailSettings49
|
||||
{
|
||||
public string Server { get; set; }
|
||||
public int Port { get; set; }
|
||||
public bool Ssl { get; set; }
|
||||
public string Username { get; set; }
|
||||
public string Password { get; set; }
|
||||
public string From { get; set; }
|
||||
public string[] To { get; set; }
|
||||
public string[] CC { get; set; }
|
||||
public string[] Bcc { get; set; }
|
||||
}
|
||||
}
|
||||
}
|
@ -1,98 +0,0 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
using FluentValidation.Results;
|
||||
using MailKit.Net.Smtp;
|
||||
using MailKit.Security;
|
||||
using MimeKit;
|
||||
using NLog;
|
||||
|
||||
namespace NzbDrone.Core.Notifications.Email
|
||||
{
|
||||
public interface IEmailService
|
||||
{
|
||||
void SendEmail(EmailSettings settings, string subject, string body, bool htmlBody = false);
|
||||
ValidationFailure Test(EmailSettings settings);
|
||||
}
|
||||
|
||||
public class EmailService : IEmailService
|
||||
{
|
||||
private readonly Logger _logger;
|
||||
|
||||
public EmailService(Logger logger)
|
||||
{
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public void SendEmail(EmailSettings settings, string subject, string body, bool htmlBody = false)
|
||||
{
|
||||
var email = new MimeMessage();
|
||||
email.From.Add(MailboxAddress.Parse(settings.From));
|
||||
|
||||
email.To.Add(MailboxAddress.Parse(settings.To));
|
||||
|
||||
email.Subject = subject;
|
||||
email.Body = new TextPart(htmlBody ? "html" : "plain")
|
||||
{
|
||||
Text = body
|
||||
};
|
||||
|
||||
try
|
||||
{
|
||||
Send(email, settings);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.Error("Error sending email. Subject: {0}", email.Subject);
|
||||
_logger.Debug(ex, ex.Message);
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
private void Send(MimeMessage email, EmailSettings settings)
|
||||
{
|
||||
using (var client = new SmtpClient())
|
||||
{
|
||||
var serverOption = SecureSocketOptions.Auto;
|
||||
|
||||
if (settings.RequireEncryption)
|
||||
{
|
||||
if (settings.Port == 465)
|
||||
{
|
||||
serverOption = SecureSocketOptions.SslOnConnect;
|
||||
}
|
||||
else
|
||||
{
|
||||
serverOption = SecureSocketOptions.StartTls;
|
||||
}
|
||||
}
|
||||
|
||||
client.Connect(settings.Server, settings.Port, serverOption);
|
||||
|
||||
if (!string.IsNullOrWhiteSpace(settings.Username))
|
||||
{
|
||||
client.Authenticate(settings.Username, settings.Password);
|
||||
}
|
||||
|
||||
client.Send(email);
|
||||
client.Disconnect(true);
|
||||
}
|
||||
}
|
||||
|
||||
public ValidationFailure Test(EmailSettings settings)
|
||||
{
|
||||
const string body = "Success! You have properly configured your email notification settings";
|
||||
|
||||
try
|
||||
{
|
||||
SendEmail(settings, "Lidarr - Test Notification", body);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.Error(ex, "Unable to send test email");
|
||||
return new ValidationFailure("Server", "Unable to send test email");
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in new issue