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

Issue branch 1 #6

Open
wants to merge 8 commits into
base: master
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net461</TargetFramework>

<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Azure.WebJobs.Extensions.SendGrid" Version="2.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.5.0" />
<PackageReference Include="Moq" Version="4.8.2" />
<PackageReference Include="Sendgrid" Version="8.0.5" />
<PackageReference Include="Shouldly" Version="3.0.0" />
<PackageReference Include="xunit" Version="2.3.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.3.1" />
<DotNetCliToolReference Include="dotnet-xunit" Version="2.3.1" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\AllReady.Processing\AllReady.Processing.csproj" />
</ItemGroup>

</Project>


<!--<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net461</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Azure.WebJobs.Extensions.SendGrid" Version="2.0.0" />
<PackageReference Include="Microsoft.NET.Sdk.Functions" Version="1.0.6" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.0.0" />
<PackageReference Include="Shouldly" Version="2.8.3" />
<PackageReference Include="xunit" Version="2.3.1" />
<PackageReference Include="Moq" Version="4.7.99" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.3.1" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\AllReady.Processing\AllReady.Processing.csproj" />
</ItemGroup>
</Project>-->
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
using Moq;
using Xunit;
using Shouldly;
using Microsoft.Azure.WebJobs.Host;
using SendGrid.Helpers.Mail;
using System.Collections.Generic;
using System.Diagnostics;
using System;

namespace AllReady.Processing.Tests
{
public class ProcessEmailQueueMessageTests
{
// comes from Environment Variable
const string EmailSentFrom = "[email protected]";
const string FromEnvironmentVariable = "Authentication:SendGrid:FromEmail";

[Fact]
public void ShouldOutputMailWithPlainTextOnly()
{
// Arrange
var loggerMock = new MockTrace();
var messageInQueue = MessageInQueue("Subject test", "[email protected]", "Message text plain");
Environment.SetEnvironmentVariable(FromEnvironmentVariable, EmailSentFrom);
// Act
ProcessEmailQueueMessage.Run(messageInQueue, out Mail message, loggerMock);
// Assert
message.From.Name.ShouldBe("AllReady");
message.From.Address.ShouldBe(EmailSentFrom);
message.Subject.ShouldBe("Subject test");
message.Contents[0].Type.ShouldBe("text/plain");
message.Contents[0].Value.ShouldBe("Message text plain");
message.Personalization[0].Tos[0].Address.ShouldBe("[email protected]");
}

[Fact]
public void ShouldOutputMailWithHtmlTextOnly()
{
// Arrange
var loggerMock = new MockTrace();
var messageInQueue = MessageInQueue("Subject test", "[email protected]", null, "<span>text</span>");
//Environment.SetEnvironmentVariable(FromEnvironmentVariable, EmailSentFrom);
// Act
ProcessEmailQueueMessage.Run(messageInQueue, out Mail message, loggerMock);
// Assert
message.From.Name.ShouldBe("AllReady");
message.From.Address.ShouldBe(EmailSentFrom);
message.Subject.ShouldBe("Subject test");
message.Contents[0].Type.ShouldBe("text/html");
message.Contents[0].Value.ShouldBe("<span>text</span>");
message.Personalization[0].Tos[0].Address.ShouldBe("[email protected]");
}

[Fact]
public void ShouldTraceTheSubjectAndRecepient()
{
// Arrange
var loggerMock = new MockTrace();
var messageInQueue = MessageInQueue("Subject test", "[email protected]", null, "<span>text</span>");
Environment.SetEnvironmentVariable(FromEnvironmentVariable, EmailSentFrom);
// Act
ProcessEmailQueueMessage.Run(messageInQueue, out Mail message, loggerMock);
// Assert
loggerMock.Events.Count.ShouldBe(1);
loggerMock.Events[0].Level.ShouldBe(TraceLevel.Info);
loggerMock.Events[0].Message.ShouldBe("Sending email with subject `Subject test` to `[email protected]`");
}

[Fact]
public void ShouldThrowOnDeserialization()
{
// Arrange
var loggerMock = new MockTrace();
Environment.SetEnvironmentVariable(FromEnvironmentVariable, EmailSentFrom);
// Act
// Assert
Should.Throw<Exception>(() => ProcessEmailQueueMessage.Run("invalid message", out Mail message, loggerMock));
}

[Fact]
public void ShouldThrowForMissing_From()
{
// Arrange
var loggerMock = new MockTrace();
Environment.SetEnvironmentVariable(FromEnvironmentVariable, null);
var messageInQueue = MessageInQueue("Subject test", "[email protected]", null, "<span>text</span>");

// Act
// Assert
Should.Throw<InvalidOperationException>(() => ProcessEmailQueueMessage.Run(messageInQueue, out Mail message, loggerMock));
}

public class MockTrace : TraceWriter
{
public List<TraceEvent> Events = new List<TraceEvent>();

public MockTrace() : base(TraceLevel.Verbose)
{
}

public override void Trace(TraceEvent traceEvent)
{
this.Events.Add(traceEvent);
}
}

private string MessageInQueue(string subject, string recipient, string message = null, string htmlMessage = null)
{
return $"{{" +
$"\"Recipient\":\"{recipient}\"," +
$"\"Subject\":\"{subject}\"," +
(message != null ? "\"Message\":\"" + message + "\"" : "") +
(htmlMessage!= null ? "\"HtmlMessage\":\"" + htmlMessage + "\"" : "") +
$"}}";
}

}
}


8 changes: 7 additions & 1 deletion AllReady.Processing/AllReady.Processing.sln
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.27130.2027
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AllReady.Processing", "AllReady.Processing\AllReady.Processing.csproj", "{C619B518-08A3-4CFC-BC22-CB0B4A63CB31}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AllReady.Processing", "AllReady.Processing\AllReady.Processing.csproj", "{C619B518-08A3-4CFC-BC22-CB0B4A63CB31}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AllReady.Processing.Tests", "AllReady.Processing.Tests\AllReady.Processing.Tests.csproj", "{1FDEA4EB-E405-44B3-81B1-05F2B5AEB034}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand All @@ -15,6 +17,10 @@ Global
{C619B518-08A3-4CFC-BC22-CB0B4A63CB31}.Debug|Any CPU.Build.0 = Debug|Any CPU
{C619B518-08A3-4CFC-BC22-CB0B4A63CB31}.Release|Any CPU.ActiveCfg = Release|Any CPU
{C619B518-08A3-4CFC-BC22-CB0B4A63CB31}.Release|Any CPU.Build.0 = Release|Any CPU
{1FDEA4EB-E405-44B3-81B1-05F2B5AEB034}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{1FDEA4EB-E405-44B3-81B1-05F2B5AEB034}.Debug|Any CPU.Build.0 = Debug|Any CPU
{1FDEA4EB-E405-44B3-81B1-05F2B5AEB034}.Release|Any CPU.ActiveCfg = Release|Any CPU
{1FDEA4EB-E405-44B3-81B1-05F2B5AEB034}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
<TargetFramework>net461</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Azure.WebJobs.Extensions.SendGrid" Version="2.0.0" />
<PackageReference Include="Microsoft.NET.Sdk.Functions" Version="1.0.6" />
<PackageReference Include="Sendgrid" Version="8.0.5" />
</ItemGroup>
<ItemGroup>
<Reference Include="Microsoft.CSharp" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
using System;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Host;
using Newtonsoft.Json;
using SendGrid.Helpers.Mail;

namespace AllReady.Processing
{
public static class ProcessEmailQueueMessage
{
[FunctionName("ProcessEmailQueueMessage")]
[StorageAccount("AzureWebJobsStorage")]
public static void Run([QueueTrigger("email-pending-deliveries")] string queueItem,
[SendGrid(ApiKey = "AzureWebJobsSendGridApiKey")] out Mail message, TraceWriter log)
{
var queuedEmailMessage = JsonConvert.DeserializeObject<QueuedEmailMessage>(queueItem);

var from = GuardAgainstInvalidEmailAddress(
Environment.GetEnvironmentVariable("Authentication:SendGrid:FromEmail"));

log.Info($"Sending email with subject `{queuedEmailMessage.Subject}` to `{queuedEmailMessage.Recipient}`");

message = new Mail
{
From = new Email(from, "AllReady"),
Subject = queuedEmailMessage.Subject
};

if (queuedEmailMessage.Message != null)
{
message.AddContent(new Content
{
Type = "text/plain",
Value = queuedEmailMessage.Message
});
}

if (queuedEmailMessage.HtmlMessage != null)
{
message.AddContent(new Content
{
Type = "text/html",
Value = queuedEmailMessage.HtmlMessage
});
}

var personalization = new Personalization();
personalization.AddTo(new Email(queuedEmailMessage.Recipient));
message.AddPersonalization(personalization);
}

private static string GuardAgainstInvalidEmailAddress(string @from)
{
if (string.IsNullOrWhiteSpace(@from))
{
throw new InvalidOperationException(
"Environment variable `Authentication:SendGrid:FromEmail` is missing or contains invalid entry.");
}

return @from;
}
}
}

10 changes: 5 additions & 5 deletions AllReady.Processing/AllReady.Processing/QueueTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ public static class QueueTest
// This function can be used locally to test interaction with your
// local storage emulator.

[FunctionName("QueueTest")]
public static void Run([QueueTrigger("queue-test", Connection = "")]string item, TraceWriter log)
{
log.Info($"A message was dequeued from the queue-test queue: {item}");
}
//[FunctionName("QueueTest")]
//public static void Run([QueueTrigger("queue-test")]string item, TraceWriter log)
//{
// log.Info($"A message was dequeued from the queue-test queue: {item}");
//}
}
}
15 changes: 15 additions & 0 deletions AllReady.Processing/AllReady.Processing/QueuedEmailMessage.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
namespace AllReady.Processing
{

public class QueuedEmailMessage
{

public string Recipient { get; set; }

public string Message { get; set; }

public string HtmlMessage { get; set; }

public string Subject { get; set; }
}
}