Skip to content

Commit 8a1aa9f

Browse files
author
John Campion Jr
committed
chore: update testing credentials
Can now set credentials for testing in a .env file
1 parent 0be0b43 commit 8a1aa9f

12 files changed

Lines changed: 222 additions & 66 deletions

File tree

.github/workflows/dotnet-core.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ name: Build & Test
22

33
on:
44
pull_request:
5-
branches: [master]
5+
branches: [main]
66

77
jobs:
88
build:

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,3 +246,5 @@ ModelManifest.xml
246246

247247
# Verify test files
248248
*.received.txt
249+
250+
.env

src/Senders/FluentEmail.Mailtrap/MailtrapSender.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@ namespace FluentEmail.Mailtrap
1919
/// </summary>
2020
public class MailtrapSender : IMailtrapSender, IDisposable
2121
{
22-
private const string URL = "https://send.api.mailtrap.io/api/send";
2322
private readonly SmtpClient _smtpClient;
2423
private static readonly int[] ValidPorts = {25,587, 2525};
2524
private readonly string _apiKey;
25+
private readonly string _apiHost;
2626

2727
/// <summary>
2828
/// Creates a sender that uses the given Mailtrap credentials, but does not dispose it.
@@ -31,7 +31,7 @@ public class MailtrapSender : IMailtrapSender, IDisposable
3131
/// <param name="password">Password of your mailtrap.io SMTP inbox</param>
3232
/// <param name="host">Host address for the Mailtrap.io SMTP inbox</param>
3333
/// <param name="port">Port for the Mailtrap.io SMTP server. Accepted values are 25, 465 or 2525.</param>
34-
public MailtrapSender(string userName, string password, string host = "smtp.mailtrap.io", int? port = null)
34+
public MailtrapSender(string userName, string password, string host = "smtp.mailtrap.io", int? port = null, string apiHost = "https://send.api.mailtrap.io/api/send")
3535
{
3636
if (string.IsNullOrWhiteSpace(userName))
3737
throw new ArgumentException("Mailtrap UserName needs to be supplied", nameof(userName));
@@ -42,6 +42,7 @@ public MailtrapSender(string userName, string password, string host = "smtp.mail
4242
if (port.HasValue && !ValidPorts.Contains(port.Value))
4343
throw new ArgumentException("Mailtrap Port needs to be either 25, 465 or 2525", nameof(port));
4444
_apiKey = password;
45+
_apiHost = apiHost;
4546
_smtpClient = new SmtpClient(host, port.GetValueOrDefault(587))
4647
{
4748
Credentials = new NetworkCredential(userName, password),
@@ -66,12 +67,12 @@ public Task<SendResponse> SendAsync(IFluentEmail email, CancellationToken? token
6667
public async Task<SendResponse> SendWithTemplateAsync(IFluentEmail email, string templateName, object templateData, CancellationToken? token = null)
6768
{
6869
token?.ThrowIfCancellationRequested();
69-
using (var httpClient = new HttpClient { BaseAddress = new Uri(URL) })
70+
using (var httpClient = new HttpClient { BaseAddress = new Uri(_apiHost) })
7071
{
7172
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", _apiKey);
7273
httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
7374
var jsonContent = HttpClientHelpers.GetJsonBody(BuildMailtrapParameters(email, templateName, templateData));
74-
var response = await httpClient.Post<MailtrapResponse>(URL, jsonContent);
75+
var response = await httpClient.Post<MailtrapResponse>(_apiHost, jsonContent);
7576
var result = new SendResponse { MessageId = response.Data?.Id };
7677
if (!response.Success)
7778
{
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
FE_TEST_TO_EMAIL=
2+
FE_TEST_FROM_EMAIL=
3+
4+
# MailTrap Tests Credentials
5+
FE_TEST_MAILTRAP_TO_EMAIL=
6+
FE_TEST_MAILTRAP_FROM_EMAIL=
7+
FE_TEST_MAILTRAP_HOST=
8+
FE_TEST_MAILTRAP_USER=
9+
FE_TEST_MAILTRAP_PWD=
10+
FE_TEST_MAILTRAP_PORT=587
11+
FE_TEST_MAILTRAP_API_HOST=
12+
FE_TEST_MAILTRAP_API_KEY=
13+
FE_TEST_MAILTRAP_TEMPLATE=
14+
15+
# MailGun Tests Credentials
16+
FE_TEST_MAILGUN_TO_EMAIL=
17+
FE_TEST_MAILGUN_FROM_EMAIL=
18+
FE_TEST_MAILGUN_DOMAIN=
19+
FE_TEST_MAILGUN_API_KEY=
20+
21+
# Azure Email Service Credentials
22+
FE_TEST_AZURE_TO_EMAIL=
23+
FE_TEST_AZURE_FROM_EMAIL=
24+
FE_TEST_AZURE_API_HOST=
25+
26+
# Postmark Credentials
27+
FE_TEST_POSTMARK_API_KEY=
28+
FE_TEST_POSTMARK_FROM_EMAIL=
29+
30+
# SendGrid Credentials
31+
FE_TEST_SENDGRID_API_KEY=
32+
FE_TEST_SENDGRID_TEMPLATE=
33+
34+
# Graph Credentials
35+
FE_TEST_GRAPH_TO_EMAIL=
36+
FE_TEST_GRAPH_FROM_EMAIL=
37+
FE_TEST_GRAPH_TENANT_ID=
38+
FE_TEST_GRAPH_APP_ID=
39+
FE_TEST_GRAPH_CLIENT_SECRET=

test/FluentEmail.Core.Tests/AzureEmailSenderTests.cs

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,20 @@
1+
using FluentEmail.Core.Tests;
2+
using NUnit.Framework;
13
using System;
24
using System.IO;
35
using System.Threading.Tasks;
4-
using NUnit.Framework;
56
using Attachment = FluentEmail.Core.Models.Attachment;
67

78
namespace FluentEmail.Azure.Email.Tests
89
{
910
[NonParallelizable]
1011
public class AzureEmailSenderTests
1112
{
12-
const string connectionString = ""; // TODO: Put your ConnectionString here
13+
private readonly string toEmail = Credentials.Azure.ToEmail ?? Credentials.ToEmail;
14+
private readonly string fromEmail = Credentials.Azure.FromEmail ?? Credentials.FromEmail;
15+
private readonly string connectionString = Credentials.Azure.ApiHost;
1316

14-
const string toEmail = "fluentEmail@mailinator.com";
15-
const string toName = "FluentEmail Mailinator";
16-
const string fromEmail = "test@fluentmail.com"; // TODO: Put a valid/verified sender here
17+
const string toName = "FluentEmail tester";
1718
const string fromName = "AzureEmailSender Test";
1819

1920
[SetUp]
@@ -25,7 +26,7 @@ public void SetUp()
2526
Core.Email.DefaultSender = sender;
2627
}
2728

28-
[Test, Ignore("No azure credentials")]
29+
[Test] //, Ignore("No azure credentials")]
2930
public async Task CanSendEmail()
3031
{
3132
const string subject = "SendMail Test";
@@ -41,8 +42,8 @@ public async Task CanSendEmail()
4142

4243
Assert.IsTrue(response.Successful);
4344
}
44-
45-
[Test, Ignore("No azure credentials")]
45+
46+
[Test] //, Ignore("No azure credentials")]
4647
public async Task CanSendEmailWithReplyTo()
4748
{
4849
const string subject = "SendMail Test";
@@ -60,7 +61,7 @@ public async Task CanSendEmailWithReplyTo()
6061
Assert.IsTrue(response.Successful);
6162
}
6263

63-
[Test, Ignore("No azure credentials")]
64+
[Test] //, Ignore("No azure credentials")]
6465
public async Task CanSendEmailWithAttachments()
6566
{
6667
const string subject = "SendMail With Attachments Test";
@@ -87,7 +88,7 @@ public async Task CanSendEmailWithAttachments()
8788
Assert.IsTrue(response.Successful);
8889
}
8990

90-
[Test, Ignore("No azure credentials")]
91+
[Test] //, Ignore("No azure credentials")]
9192
public async Task CanSendHighPriorityEmail()
9293
{
9394
const string subject = "SendMail Test";
@@ -105,7 +106,7 @@ public async Task CanSendHighPriorityEmail()
105106
Assert.IsTrue(response.Successful);
106107
}
107108

108-
[Test, Ignore("No azure credentials")]
109+
[Test] //, Ignore("No azure credentials")]
109110
public async Task CanSendLowPriorityEmail()
110111
{
111112
const string subject = "SendMail Test";

test/FluentEmail.Core.Tests/FluentEmail.Core.Tests.csproj

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,14 @@
3030
</None>
3131
<EmbeddedResource Include="EmailTemplates\test-embedded.txt" />
3232
<EmbeddedResource Include="test-embedded.txt" />
33+
<PackageReference Include="dotenv.net" Version="4.0.0" />
3334
<PackageReference Include="FluentAssertions" Version="6.12.0" />
35+
<None Update=".env">
36+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
37+
</None>
38+
<None Update=".env.sample">
39+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
40+
</None>
3441
<None Update="logotest.png">
3542
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
3643
</None>

test/FluentEmail.Core.Tests/GraphSenderTests.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,17 @@
44
using System;
55
using System.IO;
66
using System.Threading.Tasks;
7+
using FluentEmail.Core.Tests;
78

89
namespace FluentEmail.Graph.Tests
910
{
1011
public class Tests
1112
{
12-
//TODO: For these tests to pass you will need to supply the following details from an Azure AD / Office 365 Tenant
13-
const string appId = ""; //Add your AAD Graph App ID here
14-
const string tenantId = ""; //Add your AAD Tenant ID here
15-
const string graphSecret = ""; //Add your AAD Graph Client Secret here
16-
const string senderEmail = ""; //Add a sender email address from your Office 365 tenant
17-
const string toEmail = "fluentemail@mailinator.com"; //change this if you like
13+
private readonly string appId = Credentials.Graph.AppId;
14+
private readonly string tenantId = Credentials.Graph.TenantId;
15+
private readonly string graphSecret = Credentials.Graph.ClientSecret;
16+
private readonly string senderEmail = Credentials.Graph.FromEmail ?? Credentials.FromEmail;
17+
private readonly string toEmail = Credentials.Graph.ToEmail ?? Credentials.ToEmail;
1818
private bool saveSent = false;
1919

2020
[SetUp]

test/FluentEmail.Core.Tests/MailgunSenderTests.cs

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,28 @@
1-
using System.IO;
2-
using System.Threading.Tasks;
3-
using FluentEmail.Core;
1+
using FluentEmail.Core;
42
using FluentEmail.Core.Models;
5-
using NUnit.Framework;
3+
using FluentEmail.Core.Tests;
64
using Newtonsoft.Json;
5+
using NUnit.Framework;
6+
using System.IO;
7+
using System.Threading.Tasks;
78

89
namespace FluentEmail.Mailgun.Tests
910
{
1011
public class MailgunSenderTests
1112
{
12-
const string toEmail = "bentest1@mailinator.com";
13-
const string fromEmail = "ben@test.com";
13+
private readonly string toEmail = Credentials.Mailgun.ToEmail ?? Credentials.ToEmail;
14+
private readonly string fromEmail = Credentials.Mailgun.FromEmail ?? Credentials.FromEmail;
1415
const string subject = "Attachment Tests";
1516
const string body = "This email is testing the attachment functionality of MailGun.";
1617

1718
[SetUp]
1819
public void SetUp()
1920
{
20-
var sender = new MailgunSender("<name>", "<key>");
21+
var sender = new MailgunSender(Credentials.Mailgun.Domain, Credentials.Mailgun.ApiKey);
2122
Email.DefaultSender = sender;
2223
}
2324

24-
[Test, Ignore("Missing credentials")]
25+
[Test] //, Ignore("Missing credentials")]
2526
public async Task CanSendEmail()
2627
{
2728
var email = Email
@@ -35,7 +36,7 @@ public async Task CanSendEmail()
3536
Assert.IsTrue(response.Successful);
3637
}
3738

38-
[Test, Ignore("Missing credentials")]
39+
[Test] //, Ignore("Missing credentials")]
3940
public async Task GetMessageIdInResponse()
4041
{
4142
var email = Email
@@ -50,7 +51,7 @@ public async Task GetMessageIdInResponse()
5051
Assert.IsNotEmpty(response.MessageId);
5152
}
5253

53-
[Test, Ignore("Missing credentials")]
54+
[Test] //, Ignore("Missing credentials")]
5455
public async Task CanSendEmailWithTag()
5556
{
5657
var email = Email
@@ -65,7 +66,7 @@ public async Task CanSendEmailWithTag()
6566
Assert.IsTrue(response.Successful);
6667
}
6768

68-
[Test, Ignore("Missing credentials")]
69+
[Test] //, Ignore("Missing credentials")]
6970
public async Task CanSendEmailWithVariables()
7071
{
7172
var email = Email
@@ -80,7 +81,7 @@ public async Task CanSendEmailWithVariables()
8081
Assert.IsTrue(response.Successful);
8182
}
8283

83-
[Test, Ignore("Missing credentials")]
84+
[Test] //, Ignore("Missing credentials")]
8485
public async Task CanSendEmailWithAttachments()
8586
{
8687
var stream = new MemoryStream();
@@ -108,7 +109,7 @@ public async Task CanSendEmailWithAttachments()
108109
Assert.IsTrue(response.Successful);
109110
}
110111

111-
[Test, Ignore("Missing credentials")]
112+
[Test] //, Ignore("Missing credentials")]
112113
public async Task CanSendEmailWithInlineImages()
113114
{
114115
using (var stream = File.OpenRead($"{Path.Combine(Directory.GetCurrentDirectory(), "logotest.png")}"))

test/FluentEmail.Core.Tests/MailtrapSenderTests.cs

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,34 @@
22
using System.Threading.Tasks;
33
using FluentEmail.Core;
44
using FluentEmail.Core.Models;
5+
using FluentEmail.Core.Tests;
56
using NUnit.Framework;
67

78
namespace FluentEmail.Mailtrap.Tests
89
{
910
public class MailtrapSenderTests
1011
{
11-
const string toEmail = "neo.js.cn@gmail.com";
12-
const string fromEmail = "mailtrap@blazorserver.com";
1312
const string subject = "Mailtrap Email Test";
1413
const string body = "This email is testing the functionality of mailtrap.";
15-
const string username = ""; // Mailtrap SMTP inbox username
16-
const string password = ""; // Mailtrap SMTP inbox password
17-
const string templateid = "";
14+
15+
private readonly string toEmail = Credentials.MailTrap.ToEmail ?? Credentials.ToEmail;
16+
private readonly string fromEmail = Credentials.MailTrap.FromEmail ?? Credentials.FromEmail;
17+
private readonly string host = Credentials.MailTrap.Host;
18+
private readonly string username = Credentials.MailTrap.User;
19+
private readonly string password = Credentials.MailTrap.Password;
20+
private readonly int port = Credentials.MailTrap.Port ?? 587;
21+
private readonly string apiKey = Credentials.MailTrap.ApiKey;
22+
private readonly string apiHost = Credentials.MailTrap.ApiHost;
23+
private readonly string templateid = Credentials.MailTrap.Template;
24+
1825
[SetUp]
1926
public void SetUp()
2027
{
21-
var sender = new MailtrapSender(username, password, "send.api.mailtrap.io", 587);
28+
var sender = new MailtrapSender(username, password, host, port);
2229
Email.DefaultSender = sender;
2330
}
2431

25-
[Test, Ignore("Missing credentials")]
32+
[Test] //, Ignore("Missing credentials")]
2633
public void CanSendEmail()
2734
{
2835
var email = Email
@@ -37,7 +44,7 @@ public void CanSendEmail()
3744
}
3845

3946

40-
[Test, Ignore("Missing credentials")]
47+
[Test] //, Ignore("Missing credentials")]
4148
public async Task CanSendEmailAsync()
4249
{
4350
var email = Email
@@ -51,7 +58,7 @@ public async Task CanSendEmailAsync()
5158
Assert.IsTrue(response.Successful);
5259
}
5360

54-
[Test, Ignore("Missing credentials")]
61+
[Test] //, Ignore("Missing credentials")]
5562
public async Task CanSendEmailWithAttachments()
5663
{
5764
var stream = new MemoryStream();
@@ -79,7 +86,7 @@ public async Task CanSendEmailWithAttachments()
7986
Assert.IsTrue(response.Successful);
8087
}
8188

82-
[Test, Ignore("Missing credentials")]
89+
[Test] //, Ignore("Missing credentials")]
8390
public async Task CanSendEmailWithInlineImages()
8491
{
8592
using (var stream = File.OpenRead($"{Path.Combine(Directory.GetCurrentDirectory(), "logotest.png")}"))
@@ -106,9 +113,11 @@ public async Task CanSendEmailWithInlineImages()
106113
}
107114
}
108115

109-
[Test, Ignore("Missing credentials")]
116+
[Test] //, Ignore("Missing credentials")]
110117
public async Task CanSendEmailWithTemplate()
111118
{
119+
var sender = new MailtrapSender(username, apiKey, host, 587, apiHost);
120+
Email.DefaultSender = sender;
112121
var email = Email.From(fromEmail).To(toEmail);
113122
var response = await email.SendWithTemplateAsync(templateid, new { var1 = "Test", var2 = "VVVVVVVVVVVVV" });
114123
Assert.IsTrue(response.Successful);

0 commit comments

Comments
 (0)