-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #66 from mailjet/complex-variables
Complex variables - fixes the case with
- Loading branch information
Showing
10 changed files
with
267 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,180 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using Mailjet.Client; | ||
using Mailjet.Client.Resources; | ||
using Mailjet.Client.TransactionalEmails; | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
using Newtonsoft.Json.Linq; | ||
|
||
namespace Mailjet.Tests.Integration | ||
{ | ||
[TestClass] | ||
public class TemplateIntegrationTests | ||
{ | ||
private MailjetClient _client; | ||
private string _senderEmail; | ||
|
||
[TestInitialize] | ||
public async Task TestInitialize() | ||
{ | ||
_client = new MailjetClient(Environment.GetEnvironmentVariable("MJ_APIKEY_PUBLIC"), | ||
Environment.GetEnvironmentVariable("MJ_APIKEY_PRIVATE")); | ||
|
||
_senderEmail = await GetValidSenderEmail(_client); | ||
} | ||
|
||
[TestMethod] | ||
public async Task SendTransactionalEmailAsync_SendsEmail() | ||
{ | ||
long templateId = await CreateTemplate(); | ||
|
||
Assert.IsTrue(templateId > 0); | ||
|
||
await FillTemplateContent(templateId); | ||
|
||
await SendEmailWithTemplate(templateId); | ||
|
||
await DeleteTemplate(templateId); | ||
} | ||
|
||
private async Task DeleteTemplate(long templateId) | ||
{ | ||
// arrange | ||
MailjetRequest request = new MailjetRequest | ||
{ | ||
Resource = Template.Resource, | ||
ResourceId = ResourceId.Numeric(templateId) | ||
}; | ||
|
||
// act | ||
MailjetResponse response = await _client.DeleteAsync(request); | ||
|
||
// assert | ||
Assert.AreEqual(204, response.StatusCode); | ||
} | ||
|
||
private async Task FillTemplateContent(long templateId) | ||
{ | ||
// arrange | ||
var content = File.ReadAllText(Path.Combine("Resources", "MJMLTemplate.mjml")); | ||
|
||
MailjetRequest request = new MailjetRequest | ||
{ | ||
Resource = TemplateDetailcontent.Resource, | ||
ResourceId = ResourceId.Numeric(templateId) | ||
} | ||
.Property(TemplateDetailcontent.MJMLContent, content) | ||
.Property(TemplateDetailcontent.Headers, JObject.FromObject(new Dictionary<string, string>() | ||
{ | ||
{"Subject", "Test transactional template subject " + DateTime.UtcNow}, | ||
{"SenderName", "Test transactional template"}, | ||
{"SenderEmail", _senderEmail}, | ||
{"From", _senderEmail}, | ||
})); | ||
|
||
// act | ||
MailjetResponse response = await _client.PostAsync(request); | ||
|
||
// assert | ||
Assert.IsTrue(response.IsSuccessStatusCode); | ||
Assert.AreEqual(1, response.GetTotal()); | ||
Assert.AreEqual(content, response.GetData().Single().Value<string>("MJMLContent")); | ||
} | ||
|
||
private async Task<long> CreateTemplate() | ||
{ | ||
// arrange | ||
var templateName = "C# integration test template " + DateTime.UtcNow; | ||
|
||
MailjetRequest request = new MailjetRequest | ||
{ | ||
Resource = Template.Resource, | ||
} | ||
.Property(Template.Author, "Mailjet team") | ||
.Property(Template.Copyright, "Mailjet") | ||
.Property(Template.Description, "Used to send templated emails in C# SDK integration test") | ||
.Property(Template.EditMode, Template.EditModeValue_MJMLBuilder) | ||
.Property(Template.IsTextPartGenerationEnabled, true) | ||
.Property(Template.Locale, "en_US") | ||
.Property(Template.Name, templateName) | ||
.Property(Template.OwnerType, Template.OwnerTypeValue_Apikey) | ||
.Property(Template.Purposes, JArray.FromObject(new[]{ "transactional" })); | ||
|
||
// act | ||
MailjetResponse response = await _client.PostAsync(request); | ||
|
||
// assert | ||
Assert.IsTrue(response.IsSuccessStatusCode); | ||
|
||
Assert.AreEqual(1, response.GetTotal()); | ||
Assert.AreEqual(templateName, response.GetData().Single().Value<string>("Name")); | ||
|
||
long templateId = response.GetData().Single().Value<long>("ID"); | ||
return templateId; | ||
} | ||
|
||
public async Task SendEmailWithTemplate(long templateId) | ||
{ | ||
// arrange | ||
var testArrayWithValues = new [] | ||
{ | ||
new {title = "testTitle1"}, | ||
new {title = "testTitle2"}, | ||
}; | ||
|
||
var variables = new Dictionary<string, object> | ||
{ | ||
{"testVariableName", "testVariableValue"}, | ||
{"items", testArrayWithValues} | ||
}; | ||
|
||
var email = new TransactionalEmailBuilder() | ||
.WithTo(new SendContact(_senderEmail)) | ||
.WithFrom(new SendContact(_senderEmail)) | ||
.WithSubject("Test subject " + DateTime.UtcNow) | ||
.WithTemplateId(templateId) | ||
.WithVariables(variables) | ||
.WithTemplateLanguage(true) | ||
.WithTemplateErrorDeliver(true) | ||
.WithTemplateErrorReporting(new SendContact(_senderEmail)) | ||
.Build(); | ||
|
||
// act | ||
var response = await _client.SendTransactionalEmailAsync(email); | ||
|
||
// assert | ||
Assert.AreEqual(1, response.Messages.Length); | ||
var message = response.Messages[0]; | ||
|
||
Assert.AreEqual("success", message.Status); | ||
Assert.AreEqual(_senderEmail, message.To.Single().Email); | ||
} | ||
|
||
public static async Task<string> GetValidSenderEmail(MailjetClient client) | ||
{ | ||
MailjetRequest request = new MailjetRequest | ||
{ | ||
Resource = Sender.Resource | ||
}; | ||
|
||
MailjetResponse response = await client.GetAsync(request); | ||
|
||
Assert.AreEqual(200, response.StatusCode); | ||
|
||
foreach (var emailObject in response.GetData()) | ||
{ | ||
if (emailObject.Type != JTokenType.Object) | ||
continue; | ||
|
||
if (emailObject.Value<string>("Status") == "Active") | ||
return emailObject.Value<string>("Email"); | ||
} | ||
|
||
Assert.Fail("Cannot find Active sender address under given account"); | ||
throw new AssertFailedException(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<mjml owa="desktop" version="4.3.0"> | ||
<mj-head> | ||
<mj-font href="https://fonts.googleapis.com/css?family=Montserrat" name="Montserrat"></mj-font> | ||
<mj-font href="https://fonts.googleapis.com/css?family=Raleway" name="Raleway"></mj-font> | ||
<mj-font href="https://fonts.googleapis.com/css?family=Open Sans" name="Open Sans"></mj-font> | ||
<mj-preview></mj-preview> | ||
</mj-head> | ||
<mj-body background-color="#f8f8f8" color="#797e82" font-family="Open Sans, Helvetica, Arial, sans-serif"> | ||
<mj-section background-repeat="repeat" background-size="auto" padding-bottom="0px" padding-top="0px" padding="20px 0px 20px 0px" text-align="center" > | ||
<mj-column> | ||
<mj-text align="left" color="#797e82" font-family="Open Sans, Helvetica, Arial, sans-serif" font-size="11px" line-height="22px" padding-bottom="0px" padding-top="0px" padding="0px 0px 0px 25px"> | ||
<p style="text-align: center; margin: 10px 0;"> | ||
{{var:testVariableName:""}} | ||
</p> | ||
</mj-text> | ||
</mj-column> | ||
</mj-section> | ||
<mj-section background-color="#ffffff" background-repeat="repeat" padding-bottom="0px" padding-left="0px" padding-right="0px" padding-top="0px" padding="20px 0" text-align="center" > | ||
<mj-column> | ||
<mj-divider border-color="#fcac24" border-style="solid" border-width="7px" padding-bottom="40px" padding-left="0px" padding-right="0px" padding-top="0px" padding="10px 25px" width="100%"></mj-divider> | ||
</mj-column> | ||
</mj-section> | ||
|
||
<mj-raw> | ||
{% for item in var:items %} | ||
</mj-raw> | ||
|
||
<mj-section background-color="#ffffff" background-repeat="repeat" background-size="auto" padding-bottom="70px" padding-top="30px" padding="20px 0px 20px 0px" text-align="center" > | ||
<mj-column> | ||
<mj-text align="left" color="#797e82" font-family="Open Sans, Helvetica, Arial, sans-serif" font-size="13px" line-height="22px" padding-bottom="0px" padding-left="50px" padding-right="50px" padding-top="0px" padding="0px 25px 0px 25px"> | ||
<h1 style="text-align:center; color: #000000; line-height:32px">Hey {{ item.title }}</h1> | ||
</mj-text> | ||
</mj-column> | ||
</mj-section> | ||
|
||
<mj-raw> | ||
{% endfor %} | ||
</mj-raw> | ||
|
||
</mj-body> | ||
</mjml> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters