Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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 @@ -117,6 +117,7 @@

<ItemGroup>
<PackageVersion Include="FluentAssertions" Version="7.0.0" />
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.13.0" />
<PackageVersion Include="Microsoft.Testing.Platform" Version="1.5.0" />
<PackageVersion Include="Microsoft.Testing.Extensions.CodeCoverage" Version="17.13.1" />
<PackageVersion Include="NSubstitute" Version="5.3.0" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ public class SendGridLocalizedRazorMessage : SendGridRazorMessage
[JsonIgnore]
internal CultureInfo Culture { get; private set; }

[JsonIgnore]
public string? LocalizedSubjectKey { get; set; }

[JsonIgnore]
[System.Diagnostics.CodeAnalysis.SuppressMessage(
"?",
Expand All @@ -26,10 +29,9 @@ public SendGridLocalizedRazorMessage(CultureInfo cultureInfo)
Culture = cultureInfo.IsReadOnly ? cultureInfo : CultureInfo.ReadOnly(cultureInfo);
}

public void SetGlobalSubject(string subjectKey, object[]? subjectFormatArgs)
public void SetLocalizedSubject(string subjectKey, object[]? subjectFormatArgs)
{
SetGlobalSubject(subjectKey);

LocalizedSubjectKey = subjectKey;
SubjectFormatArgs = subjectFormatArgs;
}

Expand Down
7 changes: 6 additions & 1 deletion src/Infrastructure/LeanCode.SendGrid/SendGridRazorClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,12 @@ protected virtual async Task RenderMessageAsync(SendGridRazorMessage msg, Cancel
{
if (msg is SendGridLocalizedRazorMessage lrmsg)
{
lrmsg.Subject = LocalizeSubject(lrmsg.Culture, lrmsg.Subject, lrmsg.SubjectFormatArgs);
lrmsg.Subject ??= LocalizeSubject(
lrmsg.Culture,
lrmsg.LocalizedSubjectKey
?? throw new InvalidOperationException("Subject and LocalizedSubjectKey are null."),
lrmsg.SubjectFormatArgs
);
}

if (msg.PlainTextContentModel is object plainTextModel)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ public static SendGridRazorMessage WithSubject(this SendGridRazorMessage message
{
if (message is SendGridLocalizedRazorMessage localized)
{
localized.SetGlobalSubject(subject, null);
localized.SetLocalizedSubject(subject, null);
}
else
{
Expand All @@ -154,7 +154,7 @@ params object[] formatArgs
{
if (message is SendGridLocalizedRazorMessage localized)
{
localized.SetGlobalSubject(subject, formatArgs);
localized.SetLocalizedSubject(subject, formatArgs);
}
else
{
Expand All @@ -165,6 +165,13 @@ params object[] formatArgs
return message;
}

public static SendGridRazorMessage WithLiteralSubject(this SendGridRazorMessage message, string subject)
{
message.SetGlobalSubject(subject);

return message;
}

public static SendGridRazorMessage WithPlainTextContent(this SendGridRazorMessage message, object model)
{
message.PlainTextContentModel = model;
Expand Down
1 change: 1 addition & 0 deletions test/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
<PackageReference Include="NSubstitute" />
<PackageReference Include="Microsoft.Testing.Platform" />
<PackageReference Include="Microsoft.Testing.Extensions.CodeCoverage" />
<PackageReference Include="Microsoft.NET.Test.Sdk" />

<PackageReference Include="xunit.v3" />
<PackageReference Include="xunit.analyzers" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,19 +59,28 @@ public SendGridRazorClientTests()
}

[SendGridFact]
public async Task Sends_email_correctly()
public async Task Sends_localized_email_correctly()
{
var msg = new SendGridLocalizedRazorMessage("pl")
.WithSender(EmailFrom, "LeanCode Tester")
.WithRecipient(EmailTo)
.WithSubject("email.subject.test")
.WithPlainTextContent(new EmailTextVM())
.WithHtmlContent(new EmailHtmlVM())
.WithAttachment(
Convert.ToBase64String(Encoding.UTF8.GetBytes("Attachment content.")),
"Attachment.txt",
"text/plain"
)
.WithAttachment(Convert.ToBase64String("Attachment content."u8.ToArray()), "Attachment.txt", "text/plain")
Comment thread
Dragemil marked this conversation as resolved.
Outdated
.WithNoTracking();

await client.SendEmailAsync(msg);
}

[SendGridFact]
public async Task Sends_email_with_literal_subject_correctly()
{
var msg = new SendGridLocalizedRazorMessage("pl")
.WithSender(EmailFrom, "LeanCode Tester")
.WithRecipient(EmailTo)
.WithLiteralSubject("Test email with literal subject")
.WithPlainTextContent(new EmailTextVM())
.WithNoTracking();

await client.SendEmailAsync(msg);
Expand All @@ -86,11 +95,7 @@ public async Task Throws_when_sending_failed()
.WithSubject("email.subject.test")
.WithPlainTextContent(new EmailTextVM())
.WithHtmlContent(new EmailHtmlVM())
.WithAttachment(
Convert.ToBase64String(Encoding.UTF8.GetBytes("Attachment content.")),
"Attachment.txt",
"text/plain"
)
.WithAttachment(Convert.ToBase64String("Attachment content."u8.ToArray()), "Attachment.txt", "text/plain")
.WithNoTracking();

var exception = await Assert.ThrowsAsync<SendGridException>(() => client.SendEmailAsync(msg));
Expand Down