|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.IO; |
| 4 | +using System.Linq; |
| 5 | +using System.Threading.Tasks; |
| 6 | +using Mailjet.Client; |
| 7 | +using Mailjet.Client.Resources; |
| 8 | +using Mailjet.Client.TransactionalEmails; |
| 9 | +using Microsoft.VisualStudio.TestTools.UnitTesting; |
| 10 | +using Newtonsoft.Json.Linq; |
| 11 | + |
| 12 | +namespace Mailjet.Tests.Integration |
| 13 | +{ |
| 14 | + [TestClass] |
| 15 | + public class TemplateIntegrationTests |
| 16 | + { |
| 17 | + private MailjetClient _client; |
| 18 | + private string _senderEmail; |
| 19 | + |
| 20 | + [TestInitialize] |
| 21 | + public async Task TestInitialize() |
| 22 | + { |
| 23 | + _client = new MailjetClient(Environment.GetEnvironmentVariable("MJ_APIKEY_PUBLIC"), |
| 24 | + Environment.GetEnvironmentVariable("MJ_APIKEY_PRIVATE")); |
| 25 | + |
| 26 | + _senderEmail = await GetValidSenderEmail(_client); |
| 27 | + } |
| 28 | + |
| 29 | + [TestMethod] |
| 30 | + public async Task SendTransactionalEmailAsync_SendsEmail() |
| 31 | + { |
| 32 | + long templateId = await CreateTemplate(); |
| 33 | + |
| 34 | + Assert.IsTrue(templateId > 0); |
| 35 | + |
| 36 | + await FillTemplateContent(templateId); |
| 37 | + |
| 38 | + await SendEmailWithTemplate(templateId); |
| 39 | + |
| 40 | + await DeleteTemplate(templateId); |
| 41 | + } |
| 42 | + |
| 43 | + private async Task DeleteTemplate(long templateId) |
| 44 | + { |
| 45 | + // arrange |
| 46 | + MailjetRequest request = new MailjetRequest |
| 47 | + { |
| 48 | + Resource = Template.Resource, |
| 49 | + ResourceId = ResourceId.Numeric(templateId) |
| 50 | + }; |
| 51 | + |
| 52 | + // act |
| 53 | + MailjetResponse response = await _client.DeleteAsync(request); |
| 54 | + |
| 55 | + // assert |
| 56 | + Assert.AreEqual(204, response.StatusCode); |
| 57 | + } |
| 58 | + |
| 59 | + private async Task FillTemplateContent(long templateId) |
| 60 | + { |
| 61 | + // arrange |
| 62 | + var content = File.ReadAllText(Path.Combine("Resources", "MJMLTemplate.mjml")); |
| 63 | + |
| 64 | + MailjetRequest request = new MailjetRequest |
| 65 | + { |
| 66 | + Resource = TemplateDetailcontent.Resource, |
| 67 | + ResourceId = ResourceId.Numeric(templateId) |
| 68 | + } |
| 69 | + .Property(TemplateDetailcontent.MJMLContent, content) |
| 70 | + .Property(TemplateDetailcontent.Headers, JObject.FromObject(new Dictionary<string, string>() |
| 71 | + { |
| 72 | + {"Subject", "Test transactional template subject " + DateTime.UtcNow}, |
| 73 | + {"SenderName", "Test transactional template"}, |
| 74 | + {"SenderEmail", _senderEmail}, |
| 75 | + {"From", _senderEmail}, |
| 76 | + })); |
| 77 | + |
| 78 | + // act |
| 79 | + MailjetResponse response = await _client.PostAsync(request); |
| 80 | + |
| 81 | + // assert |
| 82 | + Assert.IsTrue(response.IsSuccessStatusCode); |
| 83 | + Assert.AreEqual(1, response.GetTotal()); |
| 84 | + Assert.AreEqual(content, response.GetData().Single().Value<string>("MJMLContent")); |
| 85 | + } |
| 86 | + |
| 87 | + private async Task<long> CreateTemplate() |
| 88 | + { |
| 89 | + // arrange |
| 90 | + var templateName = "C# integration test template " + DateTime.UtcNow; |
| 91 | + |
| 92 | + MailjetRequest request = new MailjetRequest |
| 93 | + { |
| 94 | + Resource = Template.Resource, |
| 95 | + } |
| 96 | + .Property(Template.Author, "Mailjet team") |
| 97 | + .Property(Template.Copyright, "Mailjet") |
| 98 | + .Property(Template.Description, "Used to send templated emails in C# SDK integration test") |
| 99 | + .Property(Template.EditMode, Template.EditModeValue_MJMLBuilder) |
| 100 | + .Property(Template.IsTextPartGenerationEnabled, true) |
| 101 | + .Property(Template.Locale, "en_US") |
| 102 | + .Property(Template.Name, templateName) |
| 103 | + .Property(Template.OwnerType, Template.OwnerTypeValue_Apikey) |
| 104 | + .Property(Template.Purposes, JArray.FromObject(new[]{ "transactional" })); |
| 105 | + |
| 106 | + // act |
| 107 | + MailjetResponse response = await _client.PostAsync(request); |
| 108 | + |
| 109 | + // assert |
| 110 | + Assert.IsTrue(response.IsSuccessStatusCode); |
| 111 | + |
| 112 | + Assert.AreEqual(1, response.GetTotal()); |
| 113 | + Assert.AreEqual(templateName, response.GetData().Single().Value<string>("Name")); |
| 114 | + |
| 115 | + long templateId = response.GetData().Single().Value<long>("ID"); |
| 116 | + return templateId; |
| 117 | + } |
| 118 | + |
| 119 | + public async Task SendEmailWithTemplate(long templateId) |
| 120 | + { |
| 121 | + // arrange |
| 122 | + var testArrayWithValues = new [] |
| 123 | + { |
| 124 | + new {title = "testTitle1"}, |
| 125 | + new {title = "testTitle2"}, |
| 126 | + }; |
| 127 | + |
| 128 | + var variables = new Dictionary<string, object> |
| 129 | + { |
| 130 | + {"testVariableName", "testVariableValue"}, |
| 131 | + {"items", testArrayWithValues} |
| 132 | + }; |
| 133 | + |
| 134 | + var email = new TransactionalEmailBuilder() |
| 135 | + .WithTo(new SendContact(_senderEmail)) |
| 136 | + .WithFrom(new SendContact(_senderEmail)) |
| 137 | + .WithSubject("Test subject " + DateTime.UtcNow) |
| 138 | + .WithTemplateId(templateId) |
| 139 | + .WithVariables(variables) |
| 140 | + .WithTemplateLanguage(true) |
| 141 | + .WithTemplateErrorDeliver(true) |
| 142 | + .WithTemplateErrorReporting(new SendContact(_senderEmail)) |
| 143 | + .Build(); |
| 144 | + |
| 145 | + // act |
| 146 | + var response = await _client.SendTransactionalEmailAsync(email); |
| 147 | + |
| 148 | + // assert |
| 149 | + Assert.AreEqual(1, response.Messages.Length); |
| 150 | + var message = response.Messages[0]; |
| 151 | + |
| 152 | + Assert.AreEqual("success", message.Status); |
| 153 | + Assert.AreEqual(_senderEmail, message.To.Single().Email); |
| 154 | + } |
| 155 | + |
| 156 | + public static async Task<string> GetValidSenderEmail(MailjetClient client) |
| 157 | + { |
| 158 | + MailjetRequest request = new MailjetRequest |
| 159 | + { |
| 160 | + Resource = Sender.Resource |
| 161 | + }; |
| 162 | + |
| 163 | + MailjetResponse response = await client.GetAsync(request); |
| 164 | + |
| 165 | + Assert.AreEqual(200, response.StatusCode); |
| 166 | + |
| 167 | + foreach (var emailObject in response.GetData()) |
| 168 | + { |
| 169 | + if (emailObject.Type != JTokenType.Object) |
| 170 | + continue; |
| 171 | + |
| 172 | + if (emailObject.Value<string>("Status") == "Active") |
| 173 | + return emailObject.Value<string>("Email"); |
| 174 | + } |
| 175 | + |
| 176 | + Assert.Fail("Cannot find Active sender address under given account"); |
| 177 | + throw new AssertFailedException(); |
| 178 | + } |
| 179 | + } |
| 180 | +} |
0 commit comments