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

feat: add mailpit as module #1134

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
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
16 changes: 15 additions & 1 deletion Testcontainers.sln
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Microsoft Visual Studio Solution File, Format Version 12.00
Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.0.31903.59
MinimumVisualStudioVersion = 10.0.40219.1
Expand Down Expand Up @@ -195,6 +195,10 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Testcontainers.Tests", "tes
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Testcontainers.WebDriver.Tests", "tests\Testcontainers.WebDriver.Tests\Testcontainers.WebDriver.Tests.csproj", "{EBA72C3B-57D5-43FF-A5B4-3D55B3B6D4C2}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Testcontainers.Mailpit", "src\Testcontainers.Mailpit\Testcontainers.Mailpit.csproj", "{2FD800AA-7015-48B2-8A31-CB28E229F3F6}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Testcontainers.Mailpit.Tests", "tests\Testcontainers.Mailpit.Tests\Testcontainers.Mailpit.Tests.csproj", "{A7F19A31-41A9-4934-8353-BF26446DA1FE}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -568,6 +572,14 @@ Global
{EBA72C3B-57D5-43FF-A5B4-3D55B3B6D4C2}.Debug|Any CPU.Build.0 = Debug|Any CPU
{EBA72C3B-57D5-43FF-A5B4-3D55B3B6D4C2}.Release|Any CPU.ActiveCfg = Release|Any CPU
{EBA72C3B-57D5-43FF-A5B4-3D55B3B6D4C2}.Release|Any CPU.Build.0 = Release|Any CPU
{2FD800AA-7015-48B2-8A31-CB28E229F3F6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{2FD800AA-7015-48B2-8A31-CB28E229F3F6}.Debug|Any CPU.Build.0 = Debug|Any CPU
{2FD800AA-7015-48B2-8A31-CB28E229F3F6}.Release|Any CPU.ActiveCfg = Release|Any CPU
{2FD800AA-7015-48B2-8A31-CB28E229F3F6}.Release|Any CPU.Build.0 = Release|Any CPU
{A7F19A31-41A9-4934-8353-BF26446DA1FE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{A7F19A31-41A9-4934-8353-BF26446DA1FE}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A7F19A31-41A9-4934-8353-BF26446DA1FE}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A7F19A31-41A9-4934-8353-BF26446DA1FE}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{5365F780-0E6C-41F0-B1B9-7DC34368F80C} = {673F23AE-7694-4BB9-ABD4-136D6C13634E}
Expand Down Expand Up @@ -661,5 +673,7 @@ Global
{1A1983E6-5297-435F-B467-E8E1F11277D6} = {7164F1FB-7F24-444A-ACD2-2C329C2B3CCF}
{27CDB869-A150-4593-958F-6F26E5391E7C} = {7164F1FB-7F24-444A-ACD2-2C329C2B3CCF}
{EBA72C3B-57D5-43FF-A5B4-3D55B3B6D4C2} = {7164F1FB-7F24-444A-ACD2-2C329C2B3CCF}
{2FD800AA-7015-48B2-8A31-CB28E229F3F6} = {673F23AE-7694-4BB9-ABD4-136D6C13634E}
{A7F19A31-41A9-4934-8353-BF26446DA1FE} = {7164F1FB-7F24-444A-ACD2-2C329C2B3CCF}
EndGlobalSection
EndGlobal
1 change: 1 addition & 0 deletions docs/modules/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ await moduleNameContainer.StartAsync();
| Keycloak | `quay.io/keycloak/keycloak:21.1` | [NuGet](https://www.nuget.org/packages/Testcontainers.Keycloak) | [Source](https://github.com/testcontainers/testcontainers-dotnet/tree/develop/src/Testcontainers.Keycloak) |
| Kusto emulator | `mcr.microsoft.com/azuredataexplorer/kustainer-linux:latest` | [NuGet](https://www.nuget.org/packages/Testcontainers.Kusto) | [Source](https://github.com/testcontainers/testcontainers-dotnet/tree/develop/src/Testcontainers.Kusto) |
| LocalStack | `localstack/localstack:2.0` | [NuGet](https://www.nuget.org/packages/Testcontainers.LocalStack) | [Source](https://github.com/testcontainers/testcontainers-dotnet/tree/develop/src/Testcontainers.LocalStack) |
| Mailpit | `axllent/mailpit` | [NuGet](https://www.nuget.org/packages/Testcontainers.Mailpit) | [Source](https://github.com/testcontainers/testcontainers-dotnet/tree/develop/src/Testcontainers.Mailpit) |
| MariaDB | `mariadb:10.10` | [NuGet](https://www.nuget.org/packages/Testcontainers.MariaDb) | [Source](https://github.com/testcontainers/testcontainers-dotnet/tree/develop/src/Testcontainers.MariaDb) |
| MinIO | `minio/minio:RELEASE.2023-01-31T02-24-19Z` | [NuGet](https://www.nuget.org/packages/Testcontainers.Minio) | [Source](https://github.com/testcontainers/testcontainers-dotnet/tree/develop/src/Testcontainers.Minio) |
| MongoDB | `mongo:6.0` | [NuGet](https://www.nuget.org/packages/Testcontainers.MongoDb) | [Source](https://github.com/testcontainers/testcontainers-dotnet/tree/develop/src/Testcontainers.MongoDb) |
Expand Down
1 change: 1 addition & 0 deletions src/Testcontainers.Mailpit/.editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
root = true
12 changes: 12 additions & 0 deletions src/Testcontainers.Mailpit/Testcontainers.Mailpit.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>netstandard2.0;netstandard2.1</TargetFrameworks>
<LangVersion>latest</LangVersion>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="JetBrains.Annotations" VersionOverride="2023.3.0" PrivateAssets="All"/>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="../Testcontainers/Testcontainers.csproj"/>
</ItemGroup>
</Project>
148 changes: 148 additions & 0 deletions src/Testcontainers.Mailpit/Testcontainers.MailpitBuilder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
using System.Net;

namespace Testcontainers.Mailpit;

/// <inheritdoc cref="ContainerBuilder{TBuilderEntity, TContainerEntity, TConfigurationEntity}" />
[PublicAPI]
public sealed class MailpitBuilder
: ContainerBuilder<MailpitBuilder, MailpitContainer, MailpitConfiguration>
{
public const string MAILPIT_IMAGE = "axllent/mailpit";
public const ushort MAILPIT_SMTP_PORT = 1025;
public const ushort MAILPIT_API_PORT = 8025;

/// <summary>
/// Initializes a new instance of the <see cref="MailpitBuilder" /> class.
/// </summary>
public MailpitBuilder()
: this(new MailpitConfiguration())
{
DockerResourceConfiguration = Init().DockerResourceConfiguration;
}

/// <summary>
/// Initializes a new instance of the <see cref="MailpitBuilder" /> class.
/// </summary>
/// <param name="resourceConfiguration">The Docker resource configuration.</param>
private MailpitBuilder(MailpitConfiguration resourceConfiguration)
: base(resourceConfiguration)
{
DockerResourceConfiguration = resourceConfiguration;
}

/// <inheritdoc />
protected override MailpitConfiguration DockerResourceConfiguration { get; }

/// <summary>
/// Sets the Mailpit MP_SMTP_AUTH config. Also sets MP_SMTP_AUTH_ALLOW_INSECURE to true.
/// </summary>
/// <param name="authCredentials">List of credentials to be used in SMTP authentication</param>
/// <returns>A configured instance of <see cref="Testcontainers.MailpitBuilder" />.</returns>
public MailpitBuilder WithSmtpAuthCredentials(
List<MailpitConfiguration.AuthCredentials> authCredentials
)
{
return Merge(
DockerResourceConfiguration,
new MailpitConfiguration(
smtpAuthCredentials: authCredentials,
smtpAuthAllowInsecure: true
)
)
.WithEnvironment(
"MP_SMTP_AUTH",
string.Join(" ", authCredentials.Select(e => $"{e.Username}:{e.Password}"))
)
.WithEnvironment("MP_SMTP_AUTH_ALLOW_INSECURE", "1");
}

/// <summary>
/// Sets the Mailpit MP_SMTP_AUTH_ALLOW_INSECURE config.
/// Typically STARTTLS is enforced for all SMTP authentication. This option allows insecure PLAIN & LOGIN SMTP authentication.
/// </summary>
/// <param name="allowInsecure">Whether or not to allow PLAIN & LOGIN SMTP authentication</param>
/// <returns>A configured instance of <see cref="Testcontainers.MailpitBuilder" />.</returns>
public MailpitBuilder WithSmtpAuthAllowInsecure(bool allowInsecure)
{
return Merge(
DockerResourceConfiguration,
new MailpitConfiguration(smtpAuthAllowInsecure: allowInsecure)
)
.WithEnvironment("MP_SMTP_AUTH_ALLOW_INSECURE", allowInsecure ? "1" : "0");
}

/// <summary>
/// Sets the Mailpit MP_MAX_MESSAGES config.
/// Maximum number of messages to store. Mailpit will periodically delete the oldest messages if greater than this. Set to 0 to disable auto-deletion.
/// </summary>
/// <param name="maxMessages">Maximum number to set</param>
/// <returns>A configured instance of <see cref="Testcontainers.MailpitBuilder" />.</returns>
public MailpitBuilder WithMaxMessages(uint maxMessages)
{
return Merge(
DockerResourceConfiguration,
new MailpitConfiguration(maxMessages: maxMessages)
)
.WithEnvironment("MP_MAX_MESSAGES", maxMessages.ToString());
}

/// <inheritdoc />
public override MailpitContainer Build()
{
Validate();
return new MailpitContainer(DockerResourceConfiguration, TestcontainersSettings.Logger);
}

/// <inheritdoc />
protected override MailpitBuilder Init()
{
return base.Init()
.WithImage(MAILPIT_IMAGE)
.WithPortBinding(MAILPIT_SMTP_PORT, true)
.WithPortBinding(MAILPIT_API_PORT, true)
.WithWaitStrategy(
Wait.ForUnixContainer()
.UntilHttpRequestIsSucceeded(r =>
r.ForPort(MAILPIT_API_PORT)
.ForPath("/api/v1/info")
.ForStatusCode(HttpStatusCode.OK)
)
);
}

/// <inheritdoc />
protected override void Validate()
{
base.Validate();

_ = Guard
.Argument(
DockerResourceConfiguration.SmtpAuthCredentials,
nameof(DockerResourceConfiguration.SmtpAuthCredentials)
)
.NotNull();
}

/// <inheritdoc />
protected override MailpitBuilder Clone(
IResourceConfiguration<CreateContainerParameters> resourceConfiguration
)
{
return Merge(DockerResourceConfiguration, new MailpitConfiguration(resourceConfiguration));
}

/// <inheritdoc />
protected override MailpitBuilder Clone(IContainerConfiguration resourceConfiguration)
{
return Merge(DockerResourceConfiguration, new MailpitConfiguration(resourceConfiguration));
}

/// <inheritdoc />
protected override MailpitBuilder Merge(
MailpitConfiguration oldValue,
MailpitConfiguration newValue
)
{
return new MailpitBuilder(new MailpitConfiguration(oldValue, newValue));
}
}
99 changes: 99 additions & 0 deletions src/Testcontainers.Mailpit/Testcontainers.MailpitConfiguration.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
namespace Testcontainers.Mailpit;

/// <inheritdoc cref="ContainerConfiguration" />
[PublicAPI]
public sealed class MailpitConfiguration : ContainerConfiguration
{
public sealed class AuthCredentials
{
public string Username { get; }
public string Password { get; }

public AuthCredentials(string username, string password)
{
Username = username;
Password = password;
}
}

/// <summary>
/// Initializes a new instance of the <see cref="MailpitConfiguration" /> class.
/// </summary>
/// <param name="config">The Testcontainers.Mailpit config.</param>
public MailpitConfiguration(
List<AuthCredentials> smtpAuthCredentials = null,
bool smtpAuthAllowInsecure = true,
uint maxMessages = 100
)
{
SmtpAuthCredentials = smtpAuthCredentials;
SmtpAuthAllowInsecure = smtpAuthAllowInsecure;
MaxMessages = maxMessages;
}

/// <summary>
/// Initializes a new instance of the <see cref="MailpitConfiguration" /> class.
/// </summary>
/// <param name="resourceConfiguration">The Docker resource configuration.</param>
public MailpitConfiguration(
IResourceConfiguration<CreateContainerParameters> resourceConfiguration
)
: base(resourceConfiguration)
{
// Passes the configuration upwards to the base implementations to create an updated immutable copy.
}

/// <summary>
/// Initializes a new instance of the <see cref="MailpitConfiguration" /> class.
/// </summary>
/// <param name="resourceConfiguration">The Docker resource configuration.</param>
public MailpitConfiguration(IContainerConfiguration resourceConfiguration)
: base(resourceConfiguration)
{
// Passes the configuration upwards to the base implementations to create an updated immutable copy.
}

/// <summary>
/// Initializes a new instance of the <see cref="MailpitConfiguration" /> class.
/// </summary>
/// <param name="resourceConfiguration">The Docker resource configuration.</param>
public MailpitConfiguration(MailpitConfiguration resourceConfiguration)
: this(new MailpitConfiguration(), resourceConfiguration)
{
// Passes the configuration upwards to the base implementations to create an updated immutable copy.
}

/// <summary>
/// Initializes a new instance of the <see cref="MailpitConfiguration" /> class.
/// </summary>
/// <param name="oldValue">The old Docker resource configuration.</param>
/// <param name="newValue">The new Docker resource configuration.</param>
public MailpitConfiguration(MailpitConfiguration oldValue, MailpitConfiguration newValue)
: base(oldValue, newValue)
{
SmtpAuthCredentials = BuildConfiguration.Combine(
oldValue.SmtpAuthCredentials,
newValue.SmtpAuthCredentials
);
SmtpAuthAllowInsecure = BuildConfiguration.Combine(
oldValue.SmtpAuthAllowInsecure,
newValue.SmtpAuthAllowInsecure
);
}

/// <summary>
/// A list of usernames and passwords for SMTP authentication.
/// See <a href="https://mailpit.axllent.org/docs/configuration/smtp-authentication/">Mailpit docs</a> for more information.
/// </summary>
public List<AuthCredentials> SmtpAuthCredentials { get; }

/// <summary>
/// Typically STARTTLS is enforced for all SMTP authentication. This option allows insecure PLAIN & LOGIN SMTP authentication.
/// </summary>
public bool SmtpAuthAllowInsecure { get; }

/// <summary>
/// Maximum number of messages to store. Mailpit will periodically delete the oldest messages if greater than this. Set to 0 to disable auto-deletion.
/// </summary>
public uint MaxMessages { get; }
}
30 changes: 30 additions & 0 deletions src/Testcontainers.Mailpit/Testcontainers.MailpitContainer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
namespace Testcontainers.Mailpit;

/// <inheritdoc cref="DockerContainer" />
[PublicAPI]
public sealed class MailpitContainer : DockerContainer
{
/// <summary>
/// Initializes a new instance of the <see cref="MailpitContainer" /> class.
/// </summary>
/// <param name="configuration">The container configuration.</param>
/// <param name="logger">The logger.</param>
public MailpitContainer(MailpitConfiguration configuration, ILogger logger)
: base(configuration, logger) { }

/// <summary>
/// SMTP server port.
/// </summary>
public ushort SmtpPort
{
get => GetMappedPublicPort(MailpitBuilder.MAILPIT_SMTP_PORT);
}

/// <summary>
/// Web API server port.
/// </summary>
public ushort ApiPort
{
get => GetMappedPublicPort(MailpitBuilder.MAILPIT_API_PORT);
}
}
10 changes: 10 additions & 0 deletions src/Testcontainers.Mailpit/Usings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
global using System;
global using System.Collections.Generic;
global using System.Linq;
global using Docker.DotNet.Models;
global using DotNet.Testcontainers;
global using DotNet.Testcontainers.Builders;
global using DotNet.Testcontainers.Configurations;
global using DotNet.Testcontainers.Containers;
global using JetBrains.Annotations;
global using Microsoft.Extensions.Logging;
1 change: 1 addition & 0 deletions tests/Testcontainers.Mailpit.Tests/.editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
root = true
Loading
Loading