Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OSOE-893: Fixing SMTP settings management #400

Merged
merged 1 commit into from
Aug 9, 2024
Merged
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
7 changes: 2 additions & 5 deletions Lombiq.Tests.UI.Samples/Tests/EmailTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,6 @@ public Task SendingTestEmailShouldWork() =>
// A shortcut to sign in without going through (and thus testing) the login screen.
await context.SignInDirectlyAsync();

// Set up the SMTP port. This is a dynamic value unique to each UI test so it can't come from a recipe.
await context.ConfigureSmtpPortAsync();

// Let's go to the "Test settings" option of the e-mail admin page and send a basic e-mail. The default
// sender is configured in the test recipe so we can use the test feature.
await context.GoToEmailTestAsync();
Expand All @@ -39,8 +36,8 @@ public Task SendingTestEmailShouldWork() =>
// If the e-mail we've sent exists then it's all good.
context.Exists(ByHelper.SmtpInboxRow("Test message"));
},
// UseSmtpService = true automatically enables the Email module too so you don't have to enable it in a
// recipe.
// UseSmtpService = true automatically enables the Email module so you don't have to enable it in a recipe,
// and it configures the module too.
configuration => configuration.UseSmtpService = true);
}

Expand Down
28 changes: 20 additions & 8 deletions Lombiq.Tests.UI/Extensions/EmailUITestContextExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,14 +92,25 @@ public static async Task FillEmailTestFormAsync(
public static Task FillEmailTestFormAsync(this UITestContext context, string subject) =>
context.FillEmailTestFormAsync("[email protected]", subject, "Hi, this is a test.");

[Obsolete("Use ConfigureSmtpSettingsAsync() instead.")]
public static Task ConfigureSmtpPortAsync(this UITestContext context, int? port = null, bool publish = true) =>
throw new NotSupportedException("Use ConfigureSmtpSettingsAsync() instead.");

/// <summary>
/// Goes to the Email settings and sets the SMTP port to the value of <paramref name="port"/>. If it's <see
/// langword="null"/> then the value in the current configuration (in <see
/// cref="OrchardCoreUITestExecutorConfiguration.SmtpServiceConfiguration"/>) is used instead.
/// The <c>OrchardCore.Email.Smtp</c> feature must be enabled, but if the SMTP provider is not turned on, this will
/// automatically do it as well.
/// Goes to the SMTP settings page and configures the provided settings. The <c>OrchardCore.Email.Smtp</c> feature
/// must be enabled, but if the SMTP provider is not turned on, this will automatically do it as well.
/// </summary>
public static async Task ConfigureSmtpPortAsync(this UITestContext context, int? port = null, bool publish = true)
/// <param name="host">The SMTP host to use.</param>
/// <param name="port">The SMTP port to use. If it's <see langword="null"/> then the value in the current
/// configuration (in <see cref="OrchardCoreUITestExecutorConfiguration.SmtpServiceConfiguration"/>) is used
/// instead.</param>
/// <param name="save">Whether to save the settings after configuring them.</param>
public static async Task ConfigureSmtpSettingsAsync(
this UITestContext context,
string defaultSender,
string host,
int? port = null,
bool save = true)
{
await context.GoToEmailSettingsAsync();
await context.ClickReliablyOnAsync(By.CssSelector("a[href='#tab-s-m-t-p']"));
Expand All @@ -117,12 +128,13 @@ public static async Task ConfigureSmtpPortAsync(this UITestContext context, int?
"The SMTP port configuration is missing. Did you forget to include \"configuration.UseSmtpService = true\"?");
}

await context.ClickAndFillInWithRetriesAsync(By.Id("ISite_SmtpSettings_Host"), "localhost");
await context.ClickAndFillInWithRetriesAsync(By.Id("ISite_SmtpSettings_DefaultSender"), defaultSender);
await context.ClickAndFillInWithRetriesAsync(By.Id("ISite_SmtpSettings_Host"), host);

var smtpPort = port.Value.ToTechnicalString();
await context.ClickAndFillInWithRetriesAsync(By.Id("ISite_SmtpSettings_Port"), smtpPort);

if (publish)
if (save)
{
await context.ClickReliablyOnAsync(By.ClassName("save"));
context.Get(By.ClassName("validation-summary-errors").Safely())?.Text?.Trim().ShouldBeNullOrEmpty();
Expand Down
11 changes: 3 additions & 8 deletions Lombiq.Tests.UI/Extensions/OrchardCoreBuilderExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection.Extensions;
using OrchardCore.Email;
using OrchardCore.Modules;
using System.Linq;

Expand Down Expand Up @@ -38,18 +37,14 @@ public static OrchardCoreBuilder ConfigureUITesting(

if (enableShortcutsDuringUITesting) builder.AddTenantFeatures("Lombiq.Tests.UI.Shortcuts");

var smtpPort = configuration.GetValue<string>("Lombiq_Tests_UI:SmtpSettings:Port");

if (!string.IsNullOrEmpty(smtpPort)) builder.AddTenantFeatures("OrchardCore.Email.Smtp");
var enableSmtp = configuration.GetValue<bool>("Lombiq_Tests_UI:EnableSmtpFeature");
if (enableSmtp) builder.AddTenantFeatures("OrchardCore.Email.Smtp");

if (configuration.GetValue<bool>("Lombiq_Tests_UI:UseAzureBlobStorage"))
{
builder.AddTenantFeatures("OrchardCore.Media.Azure.Storage");
}

return builder.ConfigureServices(
services => services
.PostConfigure<SmtpSettings>(settings =>
configuration.GetSection("Lombiq_Tests_UI:SmtpSettings").Bind(settings)));
return builder;
}
}
8 changes: 6 additions & 2 deletions Lombiq.Tests.UI/Services/UITestExecutionSession.cs
Original file line number Diff line number Diff line change
Expand Up @@ -747,8 +747,12 @@ Task SmtpServiceBeforeAppStartHandlerAsync(string contentRootPath, InstanceComma
{
_configuration.OrchardCoreConfiguration.BeforeAppStart -= SmtpServiceBeforeAppStartHandlerAsync;
arguments
.AddWithValue("Lombiq_Tests_UI:SmtpSettings:Port", value: smtpContext.Port)
.AddWithValue("Lombiq_Tests_UI:SmtpSettings:Host", value: "localhost");
.AddWithValue("Lombiq_Tests_UI:EnableSmtpFeature", value: true)
.AddWithValue("OrchardCore:OrchardCore_Email_Smtp:EnableSmtp", value: true)
.AddWithValue("OrchardCore:OrchardCore_Email_Smtp:Host", value: "localhost")
.AddWithValue("OrchardCore:OrchardCore_Email_Smtp:RequireCredentials", value: false)
.AddWithValue("OrchardCore:OrchardCore_Email_Smtp:Port", value: smtpContext.Port)
.AddWithValue("OrchardCore:OrchardCore_Email_Smtp:DefaultSender", value: "[email protected]");
return Task.CompletedTask;
}

Expand Down