From 90d96b651ef61e7073831d5345363b3fca8efc36 Mon Sep 17 00:00:00 2001 From: Georgi Parlakov Date: Sat, 7 Apr 2018 13:51:55 +0300 Subject: [PATCH 1/8] Copy over the code from original repo(allready) Add the necessary packages Todo add sedn grid auth --- .../AllReady.Processing.csproj | 6 ++ .../ProcessEmailQueueMessage.cs | 64 +++++++++++++++++++ .../AllReady.Processing/QueueTest.cs | 10 +-- .../AllReady.Processing/QueuedEmailMessage.cs | 15 +++++ 4 files changed, 90 insertions(+), 5 deletions(-) create mode 100644 AllReady.Processing/AllReady.Processing/ProcessEmailQueueMessage.cs create mode 100644 AllReady.Processing/AllReady.Processing/QueuedEmailMessage.cs diff --git a/AllReady.Processing/AllReady.Processing/AllReady.Processing.csproj b/AllReady.Processing/AllReady.Processing/AllReady.Processing.csproj index 0715787..1eff0af 100644 --- a/AllReady.Processing/AllReady.Processing/AllReady.Processing.csproj +++ b/AllReady.Processing/AllReady.Processing/AllReady.Processing.csproj @@ -3,7 +3,9 @@ net461 + + @@ -16,5 +18,9 @@ PreserveNewest Never + + PreserveNewest + Never + diff --git a/AllReady.Processing/AllReady.Processing/ProcessEmailQueueMessage.cs b/AllReady.Processing/AllReady.Processing/ProcessEmailQueueMessage.cs new file mode 100644 index 0000000..0bd3496 --- /dev/null +++ b/AllReady.Processing/AllReady.Processing/ProcessEmailQueueMessage.cs @@ -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 = "Authentication:SendGrid:ApiKey")] out Mail message, TraceWriter log) + { + var queuedEmailMessage = JsonConvert.DeserializeObject(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; + } + } +} + diff --git a/AllReady.Processing/AllReady.Processing/QueueTest.cs b/AllReady.Processing/AllReady.Processing/QueueTest.cs index dac8cde..f47a142 100644 --- a/AllReady.Processing/AllReady.Processing/QueueTest.cs +++ b/AllReady.Processing/AllReady.Processing/QueueTest.cs @@ -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}"); + //} } } diff --git a/AllReady.Processing/AllReady.Processing/QueuedEmailMessage.cs b/AllReady.Processing/AllReady.Processing/QueuedEmailMessage.cs new file mode 100644 index 0000000..6ff09f7 --- /dev/null +++ b/AllReady.Processing/AllReady.Processing/QueuedEmailMessage.cs @@ -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; } + } +} From a8b072f6f25418a2e496a327e4c4b1cdd463821f Mon Sep 17 00:00:00 2001 From: Georgi Parlakov Date: Sat, 7 Apr 2018 17:54:03 +0300 Subject: [PATCH 2/8] 1. Make the send grid use the AzureWebJobs ... config value 2. Add a unit test project 3. Comment code until it works ... 4. Simple test works --- .../AllReady.Processing.Tests.csproj | 35 ++++++++++++++++ .../AllReady.Processing.Tests/UnitTest1.cs | 42 +++++++++++++++++++ AllReady.Processing/AllReady.Processing.sln | 8 +++- .../AllReady.Processing.csproj | 4 -- .../ProcessEmailQueueMessage.cs | 2 +- 5 files changed, 85 insertions(+), 6 deletions(-) create mode 100644 AllReady.Processing/AllReady.Processing.Tests/AllReady.Processing.Tests.csproj create mode 100644 AllReady.Processing/AllReady.Processing.Tests/UnitTest1.cs diff --git a/AllReady.Processing/AllReady.Processing.Tests/AllReady.Processing.Tests.csproj b/AllReady.Processing/AllReady.Processing.Tests/AllReady.Processing.Tests.csproj new file mode 100644 index 0000000..2f691ee --- /dev/null +++ b/AllReady.Processing/AllReady.Processing.Tests/AllReady.Processing.Tests.csproj @@ -0,0 +1,35 @@ + + + + net461 + + false + + + + + + + + + + + + + \ No newline at end of file diff --git a/AllReady.Processing/AllReady.Processing.Tests/UnitTest1.cs b/AllReady.Processing/AllReady.Processing.Tests/UnitTest1.cs new file mode 100644 index 0000000..eed7d7a --- /dev/null +++ b/AllReady.Processing/AllReady.Processing.Tests/UnitTest1.cs @@ -0,0 +1,42 @@ +//using Moq; +using Xunit; +//using Shouldly; +//using Microsoft.Azure.WebJobs.Host; +//using SendGrid.Helpers.Mail; + +namespace AllReady.Processing.Tests +{ + public class UnitTest1 + { + // comes from Environment Variable + const string EmailSentFrom = "from@tests.com"; + + //[Fact] + //public void TestMethod1() + //{ + // // Arrange + // var loggerMock = new Mock(); + // loggerMock.Setup(l => l.Info(It.IsAny(), It.IsAny())); + // var expected = new Mail { From = new Email(EmailSentFrom, "AllReady"), Subject = "test" }; + // var personalization = new Personalization(); + // personalization.AddTo(new Email("test@gmail.com")); + // expected.AddPersonalization(personalization); + + // var incomingMessage = new QueuedEmailMessage { Subject = "test", Recipient = "test@gmail.com", Message = "simple text message" }; + // // Act + // ProcessEmailQueueMessage.Run("testItem", out Mail message, loggerMock.Object); + // // Assert + // message.ShouldBe(expected); + //} + + [Theory] + [InlineData("test")] + public void TestMethod2(string input) + { + //input.ShouldBe("test"); + //input.ShouldNotBe("not test"); + + Assert.Equal("test", input); + } + } +} diff --git a/AllReady.Processing/AllReady.Processing.sln b/AllReady.Processing/AllReady.Processing.sln index b664a56..63b94f6 100644 --- a/AllReady.Processing/AllReady.Processing.sln +++ b/AllReady.Processing/AllReady.Processing.sln @@ -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 @@ -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 diff --git a/AllReady.Processing/AllReady.Processing/AllReady.Processing.csproj b/AllReady.Processing/AllReady.Processing/AllReady.Processing.csproj index 1eff0af..0cc3591 100644 --- a/AllReady.Processing/AllReady.Processing/AllReady.Processing.csproj +++ b/AllReady.Processing/AllReady.Processing/AllReady.Processing.csproj @@ -18,9 +18,5 @@ PreserveNewest Never - - PreserveNewest - Never - diff --git a/AllReady.Processing/AllReady.Processing/ProcessEmailQueueMessage.cs b/AllReady.Processing/AllReady.Processing/ProcessEmailQueueMessage.cs index 0bd3496..7748dc4 100644 --- a/AllReady.Processing/AllReady.Processing/ProcessEmailQueueMessage.cs +++ b/AllReady.Processing/AllReady.Processing/ProcessEmailQueueMessage.cs @@ -11,7 +11,7 @@ public static class ProcessEmailQueueMessage [FunctionName("ProcessEmailQueueMessage")] [StorageAccount("AzureWebJobsStorage")] public static void Run([QueueTrigger("email-pending-deliveries")] string queueItem, - [SendGrid(ApiKey = "Authentication:SendGrid:ApiKey")] out Mail message, TraceWriter log) + [SendGrid(ApiKey = "AzureWebJobsSendGridApiKey")] out Mail message, TraceWriter log) { var queuedEmailMessage = JsonConvert.DeserializeObject(queueItem); From a4acf58f732cf1e25c64edcca1402e069e6d7872 Mon Sep 17 00:00:00 2001 From: Georgi Parlakov Date: Sat, 7 Apr 2018 17:58:01 +0300 Subject: [PATCH 3/8] Add shouldly - works --- .../AllReady.Processing.Tests.csproj | 1 + AllReady.Processing/AllReady.Processing.Tests/UnitTest1.cs | 6 +++--- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/AllReady.Processing/AllReady.Processing.Tests/AllReady.Processing.Tests.csproj b/AllReady.Processing/AllReady.Processing.Tests/AllReady.Processing.Tests.csproj index 2f691ee..b04edd2 100644 --- a/AllReady.Processing/AllReady.Processing.Tests/AllReady.Processing.Tests.csproj +++ b/AllReady.Processing/AllReady.Processing.Tests/AllReady.Processing.Tests.csproj @@ -8,6 +8,7 @@ + diff --git a/AllReady.Processing/AllReady.Processing.Tests/UnitTest1.cs b/AllReady.Processing/AllReady.Processing.Tests/UnitTest1.cs index eed7d7a..8637c5d 100644 --- a/AllReady.Processing/AllReady.Processing.Tests/UnitTest1.cs +++ b/AllReady.Processing/AllReady.Processing.Tests/UnitTest1.cs @@ -1,6 +1,6 @@ //using Moq; using Xunit; -//using Shouldly; +using Shouldly; //using Microsoft.Azure.WebJobs.Host; //using SendGrid.Helpers.Mail; @@ -33,8 +33,8 @@ public class UnitTest1 [InlineData("test")] public void TestMethod2(string input) { - //input.ShouldBe("test"); - //input.ShouldNotBe("not test"); + input.ShouldBe("test"); + input.ShouldNotBe("not test"); Assert.Equal("test", input); } From d6c90b6696601889e0732cc289f4adf5cba57bb3 Mon Sep 17 00:00:00 2001 From: Georgi Parlakov Date: Sat, 7 Apr 2018 18:04:04 +0300 Subject: [PATCH 4/8] add Moq - works --- .../AllReady.Processing.Tests.csproj | 1 + .../AllReady.Processing.Tests/UnitTest1.cs | 46 ++++++++++++------- 2 files changed, 30 insertions(+), 17 deletions(-) diff --git a/AllReady.Processing/AllReady.Processing.Tests/AllReady.Processing.Tests.csproj b/AllReady.Processing/AllReady.Processing.Tests/AllReady.Processing.Tests.csproj index b04edd2..4c4c6fb 100644 --- a/AllReady.Processing/AllReady.Processing.Tests/AllReady.Processing.Tests.csproj +++ b/AllReady.Processing/AllReady.Processing.Tests/AllReady.Processing.Tests.csproj @@ -8,6 +8,7 @@ + diff --git a/AllReady.Processing/AllReady.Processing.Tests/UnitTest1.cs b/AllReady.Processing/AllReady.Processing.Tests/UnitTest1.cs index 8637c5d..06b9200 100644 --- a/AllReady.Processing/AllReady.Processing.Tests/UnitTest1.cs +++ b/AllReady.Processing/AllReady.Processing.Tests/UnitTest1.cs @@ -1,4 +1,4 @@ -//using Moq; +using Moq; using Xunit; using Shouldly; //using Microsoft.Azure.WebJobs.Host; @@ -11,32 +11,44 @@ public class UnitTest1 // comes from Environment Variable const string EmailSentFrom = "from@tests.com"; - //[Fact] - //public void TestMethod1() - //{ - // // Arrange - // var loggerMock = new Mock(); - // loggerMock.Setup(l => l.Info(It.IsAny(), It.IsAny())); - // var expected = new Mail { From = new Email(EmailSentFrom, "AllReady"), Subject = "test" }; - // var personalization = new Personalization(); - // personalization.AddTo(new Email("test@gmail.com")); - // expected.AddPersonalization(personalization); + [Fact] + public void TestMethod1() + { + // Arrange + //var loggerMock = new Mock(); + //loggerMock.Setup(l => l.Info(It.IsAny(), It.IsAny())); + //// var expected = new Mail { From = new Email(EmailSentFrom, "AllReady"), Subject = "test" }; + // var personalization = new Personalization(); + // personalization.AddTo(new Email("test@gmail.com")); + // expected.AddPersonalization(personalization); - // var incomingMessage = new QueuedEmailMessage { Subject = "test", Recipient = "test@gmail.com", Message = "simple text message" }; - // // Act - // ProcessEmailQueueMessage.Run("testItem", out Mail message, loggerMock.Object); - // // Assert - // message.ShouldBe(expected); - //} + // var incomingMessage = new QueuedEmailMessage { Subject = "test", Recipient = "test@gmail.com", Message = "simple text message" }; + // // Act + // ProcessEmailQueueMessage.Run("testItem", out Mail message, loggerMock.Object); + // // Assert + // message.ShouldBe(expected); + } [Theory] [InlineData("test")] public void TestMethod2(string input) { + // Arrange + var mock = new Mock(); + mock.Setup(i => i.Foo()).Returns("fooReturn"); + + // Assert input.ShouldBe("test"); input.ShouldNotBe("not test"); Assert.Equal("test", input); + mock.Object.Foo().ShouldBe("fooReturn"); + + } + + public interface IMockable + { + string Foo(); } } } From 285fe25dd0087553167f02003da0bc0e8ae55d0c Mon Sep 17 00:00:00 2001 From: Georgi Parlakov Date: Sat, 7 Apr 2018 18:07:38 +0300 Subject: [PATCH 5/8] Add the cibling project - works --- .../AllReady.Processing.Tests.csproj | 4 ++++ .../AllReady.Processing.Tests/UnitTest1.cs | 20 ++++++++++++++----- 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/AllReady.Processing/AllReady.Processing.Tests/AllReady.Processing.Tests.csproj b/AllReady.Processing/AllReady.Processing.Tests/AllReady.Processing.Tests.csproj index 4c4c6fb..e51f61f 100644 --- a/AllReady.Processing/AllReady.Processing.Tests/AllReady.Processing.Tests.csproj +++ b/AllReady.Processing/AllReady.Processing.Tests/AllReady.Processing.Tests.csproj @@ -15,6 +15,10 @@ + + + + diff --git a/AllReady.Processing/AllReady.Processing.Tests/UnitTest1.cs b/AllReady.Processing/AllReady.Processing.Tests/UnitTest1.cs index 06b9200..5bb274b 100644 --- a/AllReady.Processing/AllReady.Processing.Tests/UnitTest1.cs +++ b/AllReady.Processing/AllReady.Processing.Tests/UnitTest1.cs @@ -17,10 +17,10 @@ public void TestMethod1() // Arrange //var loggerMock = new Mock(); //loggerMock.Setup(l => l.Info(It.IsAny(), It.IsAny())); - //// var expected = new Mail { From = new Email(EmailSentFrom, "AllReady"), Subject = "test" }; - // var personalization = new Personalization(); - // personalization.AddTo(new Email("test@gmail.com")); - // expected.AddPersonalization(personalization); + //var expected = new Mail { From = new Email(EmailSentFrom, "AllReady"), Subject = "test" }; + //var personalization = new Personalization(); + //personalization.AddTo(new Email("test@gmail.com")); + //expected.AddPersonalization(personalization); // var incomingMessage = new QueuedEmailMessage { Subject = "test", Recipient = "test@gmail.com", Message = "simple text message" }; // // Act @@ -43,12 +43,22 @@ public void TestMethod2(string input) Assert.Equal("test", input); mock.Object.Foo().ShouldBe("fooReturn"); - + } + [Fact] + public void TestCiblingProject() + { + // Arrange + var t = new QueuedEmailMessage { Subject = "Test" }; + + // Assert + t.Subject.ShouldBe("Test"); + } public interface IMockable { string Foo(); } + } } From 3cd0cef31074e323c8be339f1fa1e360dbc4a91a Mon Sep 17 00:00:00 2001 From: Georgi Parlakov Date: Sat, 7 Apr 2018 18:21:49 +0300 Subject: [PATCH 6/8] Added all references. Tests start - but we can not mock TraceWriter.... --- .../AllReady.Processing.Tests.csproj | 2 + .../AllReady.Processing.Tests/UnitTest1.cs | 40 +++++++++++++------ 2 files changed, 29 insertions(+), 13 deletions(-) diff --git a/AllReady.Processing/AllReady.Processing.Tests/AllReady.Processing.Tests.csproj b/AllReady.Processing/AllReady.Processing.Tests/AllReady.Processing.Tests.csproj index e51f61f..b9525ef 100644 --- a/AllReady.Processing/AllReady.Processing.Tests/AllReady.Processing.Tests.csproj +++ b/AllReady.Processing/AllReady.Processing.Tests/AllReady.Processing.Tests.csproj @@ -7,8 +7,10 @@ + + diff --git a/AllReady.Processing/AllReady.Processing.Tests/UnitTest1.cs b/AllReady.Processing/AllReady.Processing.Tests/UnitTest1.cs index 5bb274b..78cea1e 100644 --- a/AllReady.Processing/AllReady.Processing.Tests/UnitTest1.cs +++ b/AllReady.Processing/AllReady.Processing.Tests/UnitTest1.cs @@ -1,8 +1,9 @@ using Moq; using Xunit; using Shouldly; -//using Microsoft.Azure.WebJobs.Host; -//using SendGrid.Helpers.Mail; +using Microsoft.Azure.WebJobs.Host; +using SendGrid.Helpers.Mail; +using System.Collections.Generic; namespace AllReady.Processing.Tests { @@ -15,18 +16,18 @@ public class UnitTest1 public void TestMethod1() { // Arrange - //var loggerMock = new Mock(); - //loggerMock.Setup(l => l.Info(It.IsAny(), It.IsAny())); - //var expected = new Mail { From = new Email(EmailSentFrom, "AllReady"), Subject = "test" }; - //var personalization = new Personalization(); - //personalization.AddTo(new Email("test@gmail.com")); - //expected.AddPersonalization(personalization); + var loggerMock = new Mock(new TraceWriter()); + loggerMock.Setup(l => l.Info(It.IsAny(), It.IsAny())); + var expected = new Mail { From = new Email(EmailSentFrom, "AllReady"), Subject = "test" }; + var personalization = new Personalization(); + personalization.AddTo(new Email("test@gmail.com")); + expected.AddPersonalization(personalization); - // var incomingMessage = new QueuedEmailMessage { Subject = "test", Recipient = "test@gmail.com", Message = "simple text message" }; - // // Act - // ProcessEmailQueueMessage.Run("testItem", out Mail message, loggerMock.Object); - // // Assert - // message.ShouldBe(expected); + var incomingMessage = new QueuedEmailMessage { Subject = "test", Recipient = "test@gmail.com", Message = "simple text message" }; + // Act + ProcessEmailQueueMessage.Run("testItem", out Mail message, loggerMock.Object); + // Assert + message.ShouldBe(expected); } [Theory] @@ -60,5 +61,18 @@ public interface IMockable string Foo(); } + public class MockTrace : TraceWriter + { + public List Infos = new List(); + + public override void Trace(TraceEvent traceEvent) + { + throw new System.NotImplementedException(); + } + + public override void Info() + { } + } + } } From 4d707b4b4a903c5f21fe5fc0a659ee03d2f63281 Mon Sep 17 00:00:00 2001 From: Georgi Parlakov Date: Sat, 7 Apr 2018 22:38:21 +0300 Subject: [PATCH 7/8] Use a mocked instance of the trace writer. Test starts... Maybe it even works --- .../AllReady.Processing.Tests/UnitTest1.cs | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/AllReady.Processing/AllReady.Processing.Tests/UnitTest1.cs b/AllReady.Processing/AllReady.Processing.Tests/UnitTest1.cs index 78cea1e..0e9a544 100644 --- a/AllReady.Processing/AllReady.Processing.Tests/UnitTest1.cs +++ b/AllReady.Processing/AllReady.Processing.Tests/UnitTest1.cs @@ -4,6 +4,7 @@ using Microsoft.Azure.WebJobs.Host; using SendGrid.Helpers.Mail; using System.Collections.Generic; +using System.Diagnostics; namespace AllReady.Processing.Tests { @@ -16,8 +17,7 @@ public class UnitTest1 public void TestMethod1() { // Arrange - var loggerMock = new Mock(new TraceWriter()); - loggerMock.Setup(l => l.Info(It.IsAny(), It.IsAny())); + var loggerMock = new MockTrace(); var expected = new Mail { From = new Email(EmailSentFrom, "AllReady"), Subject = "test" }; var personalization = new Personalization(); personalization.AddTo(new Email("test@gmail.com")); @@ -25,11 +25,15 @@ public void TestMethod1() var incomingMessage = new QueuedEmailMessage { Subject = "test", Recipient = "test@gmail.com", Message = "simple text message" }; // Act - ProcessEmailQueueMessage.Run("testItem", out Mail message, loggerMock.Object); + ProcessEmailQueueMessage.Run(@"{""Message"":""test"", ""Recepient"":""test@gmail.com"", ""Subject"":""test"" }", out Mail message, loggerMock); // Assert message.ShouldBe(expected); } + // TODO tests + // send a message that will not deserialize correctly + // have a missing from + [Theory] [InlineData("test")] public void TestMethod2(string input) @@ -65,13 +69,14 @@ public class MockTrace : TraceWriter { public List Infos = new List(); + public MockTrace(): base(TraceLevel.Verbose) + { + } + public override void Trace(TraceEvent traceEvent) { throw new System.NotImplementedException(); } - - public override void Info() - { } } } From 5b7fc17a762447cd785772086326ed091f889e07 Mon Sep 17 00:00:00 2001 From: Georgi Parlakov Date: Sat, 7 Apr 2018 23:49:30 +0300 Subject: [PATCH 8/8] 1.Rename unit test class 2. Add unit tests for plain text, html, invalid message, missing environment variable --- .../ProcessEmailQueueMessageTests.cs | 120 ++++++++++++++++++ .../AllReady.Processing.Tests/UnitTest1.cs | 83 ------------ 2 files changed, 120 insertions(+), 83 deletions(-) create mode 100644 AllReady.Processing/AllReady.Processing.Tests/ProcessEmailQueueMessageTests.cs delete mode 100644 AllReady.Processing/AllReady.Processing.Tests/UnitTest1.cs diff --git a/AllReady.Processing/AllReady.Processing.Tests/ProcessEmailQueueMessageTests.cs b/AllReady.Processing/AllReady.Processing.Tests/ProcessEmailQueueMessageTests.cs new file mode 100644 index 0000000..8b17f24 --- /dev/null +++ b/AllReady.Processing/AllReady.Processing.Tests/ProcessEmailQueueMessageTests.cs @@ -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 = "from@tests.com"; + const string FromEnvironmentVariable = "Authentication:SendGrid:FromEmail"; + + [Fact] + public void ShouldOutputMailWithPlainTextOnly() + { + // Arrange + var loggerMock = new MockTrace(); + var messageInQueue = MessageInQueue("Subject test", "test@testing.com", "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("test@testing.com"); + } + + [Fact] + public void ShouldOutputMailWithHtmlTextOnly() + { + // Arrange + var loggerMock = new MockTrace(); + var messageInQueue = MessageInQueue("Subject test", "test@testing.com", null, "text"); + //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("text"); + message.Personalization[0].Tos[0].Address.ShouldBe("test@testing.com"); + } + + [Fact] + public void ShouldTraceTheSubjectAndRecepient() + { + // Arrange + var loggerMock = new MockTrace(); + var messageInQueue = MessageInQueue("Subject test", "test@testing.com", null, "text"); + 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 `test@testing.com`"); + } + + [Fact] + public void ShouldThrowOnDeserialization() + { + // Arrange + var loggerMock = new MockTrace(); + Environment.SetEnvironmentVariable(FromEnvironmentVariable, EmailSentFrom); + // Act + // Assert + Should.Throw(() => 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", "test@testing.com", null, "text"); + + // Act + // Assert + Should.Throw(() => ProcessEmailQueueMessage.Run(messageInQueue, out Mail message, loggerMock)); + } + + public class MockTrace : TraceWriter + { + public List Events = new List(); + + 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 + "\"" : "") + + $"}}"; + } + + } +} + + diff --git a/AllReady.Processing/AllReady.Processing.Tests/UnitTest1.cs b/AllReady.Processing/AllReady.Processing.Tests/UnitTest1.cs deleted file mode 100644 index 0e9a544..0000000 --- a/AllReady.Processing/AllReady.Processing.Tests/UnitTest1.cs +++ /dev/null @@ -1,83 +0,0 @@ -using Moq; -using Xunit; -using Shouldly; -using Microsoft.Azure.WebJobs.Host; -using SendGrid.Helpers.Mail; -using System.Collections.Generic; -using System.Diagnostics; - -namespace AllReady.Processing.Tests -{ - public class UnitTest1 - { - // comes from Environment Variable - const string EmailSentFrom = "from@tests.com"; - - [Fact] - public void TestMethod1() - { - // Arrange - var loggerMock = new MockTrace(); - var expected = new Mail { From = new Email(EmailSentFrom, "AllReady"), Subject = "test" }; - var personalization = new Personalization(); - personalization.AddTo(new Email("test@gmail.com")); - expected.AddPersonalization(personalization); - - var incomingMessage = new QueuedEmailMessage { Subject = "test", Recipient = "test@gmail.com", Message = "simple text message" }; - // Act - ProcessEmailQueueMessage.Run(@"{""Message"":""test"", ""Recepient"":""test@gmail.com"", ""Subject"":""test"" }", out Mail message, loggerMock); - // Assert - message.ShouldBe(expected); - } - - // TODO tests - // send a message that will not deserialize correctly - // have a missing from - - [Theory] - [InlineData("test")] - public void TestMethod2(string input) - { - // Arrange - var mock = new Mock(); - mock.Setup(i => i.Foo()).Returns("fooReturn"); - - // Assert - input.ShouldBe("test"); - input.ShouldNotBe("not test"); - - Assert.Equal("test", input); - mock.Object.Foo().ShouldBe("fooReturn"); - - } - - [Fact] - public void TestCiblingProject() - { - // Arrange - var t = new QueuedEmailMessage { Subject = "Test" }; - - // Assert - t.Subject.ShouldBe("Test"); - } - public interface IMockable - { - string Foo(); - } - - public class MockTrace : TraceWriter - { - public List Infos = new List(); - - public MockTrace(): base(TraceLevel.Verbose) - { - } - - public override void Trace(TraceEvent traceEvent) - { - throw new System.NotImplementedException(); - } - } - - } -}