Skip to content

Commit

Permalink
PR review changed
Browse files Browse the repository at this point in the history
  • Loading branch information
RikuVirtanen committed Nov 29, 2023
1 parent cdd89e2 commit c66db81
Show file tree
Hide file tree
Showing 11 changed files with 467 additions and 472 deletions.
316 changes: 157 additions & 159 deletions Frends.SMTP.SendEmail/Frends.Smtp.SendEmail.Tests/SendEmailTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,197 +3,195 @@
using System.IO;
using Frends.SMTP.SendEmail.Definitions;

namespace Frends.SMTP.SendEmail.Tests
namespace Frends.SMTP.SendEmail.Tests;

[TestFixture]
public class SendEmailTests
{
[TestFixture]
public class SendEmailTests
// ****************************************** FILL THESE ******************************************************
private static readonly string USERNAME = Environment.GetEnvironmentVariable("Frends_SMTP_Username");
private static readonly string PASSWORD = Environment.GetEnvironmentVariable("Frends_SMTP_Password");
private static readonly string SMTPADDRESS = Environment.GetEnvironmentVariable("Frends_SMTP_Address");
private static readonly string TOEMAILADDRESS = Environment.GetEnvironmentVariable("Frends_SMTP_Email");
private static readonly string FROMEMAILADDRESS = Environment.GetEnvironmentVariable("Frends_SMTP_Email");
private const int PORT = 587;
private const bool USESSL = true;
private const bool USEWINDOWSAUTHENTICATION = false;
// ************************************************************************************************************


private const string TEMP_ATTACHMENT_SOURCE = "emailtestattachments";
private const string TEST_FILE_NAME = "testattachment.txt";
private const string TEST_FILE_NOT_EXISTING = "doesntexist.txt";

private string _localAttachmentFolder;
private string _filepath;
private Input _input;
private Input _input2;
private Options _options;

[SetUp]
public void EmailTestSetup()
{
// ****************************************** FILL THESE ******************************************************
private static readonly string USERNAME = Environment.GetEnvironmentVariable("Frends_SMTP_Username");
private static readonly string PASSWORD = Environment.GetEnvironmentVariable("Frends_SMTP_Password");
private static readonly string SMTPADDRESS = Environment.GetEnvironmentVariable("Frends_SMTP_Address");
private static readonly string TOEMAILADDRESS = Environment.GetEnvironmentVariable("Frends_SMTP_Email");
private static readonly string FROMEMAILADDRESS = Environment.GetEnvironmentVariable("Frends_SMTP_Email");
private const int PORT = 587;
private const bool USESSL = true;
private const bool USEWINDOWSAUTHENTICATION = false;
// ************************************************************************************************************


private const string TEMP_ATTACHMENT_SOURCE = "emailtestattachments";
private const string TEST_FILE_NAME = "testattachment.txt";
private const string TEST_FILE_NOT_EXISTING = "doesntexist.txt";

private string _localAttachmentFolder;
private string _filepath;
private Input _input;
private Input _input2;
private Options _options;

[SetUp]
public void EmailTestSetup()
{
_localAttachmentFolder = Path.Combine(Path.GetTempPath(), TEMP_ATTACHMENT_SOURCE);

if (!Directory.Exists(_localAttachmentFolder))
Directory.CreateDirectory(_localAttachmentFolder);

_filepath = Path.Combine(_localAttachmentFolder, TEST_FILE_NAME);

if (!File.Exists(_filepath))
{
File.Create(_filepath).Dispose();
}

_input = new Input()
{
From = FROMEMAILADDRESS,
To = TOEMAILADDRESS,
Cc = "",
Bcc = "",
Message = "testmsg",
IsMessageHtml = false,
SenderName = "EmailTestSender",
MessageEncoding = "utf-8"
};

_input2 = new Input()
{
From = FROMEMAILADDRESS,
To = TOEMAILADDRESS,
Cc = null,
Bcc = null,
Message = "testmsg",
IsMessageHtml = false,
SenderName = "EmailTestSender",
MessageEncoding = "utf-8"
};

_options = new Options()
{
UserName = USERNAME,
Password = PASSWORD,
SMTPServer = SMTPADDRESS,
Port = PORT,
UseSsl = USESSL,
UseWindowsAuthentication = USEWINDOWSAUTHENTICATION,
};
_localAttachmentFolder = Path.Combine(Path.GetTempPath(), TEMP_ATTACHMENT_SOURCE);

}
[TearDown]
public void EmailTestTearDown()
if (!Directory.Exists(_localAttachmentFolder))
Directory.CreateDirectory(_localAttachmentFolder);

_filepath = Path.Combine(_localAttachmentFolder, TEST_FILE_NAME);

if (!File.Exists(_filepath))
{
if (Directory.Exists(_localAttachmentFolder))
Directory.Delete(_localAttachmentFolder, true);
File.Create(_filepath).Dispose();
}

[Test]
public void SendEmailWithPlainText()
_input = new Input()
{
var input = _input;
input.Subject = "Email test - PlainText";
From = FROMEMAILADDRESS,
To = TOEMAILADDRESS,
Cc = "",
Bcc = "",
Message = "testmsg",
IsMessageHtml = false,
SenderName = "EmailTestSender",
MessageEncoding = "utf-8"
};

_input2 = new Input()
{
From = FROMEMAILADDRESS,
To = TOEMAILADDRESS,
Cc = null,
Bcc = null,
Message = "testmsg",
IsMessageHtml = false,
SenderName = "EmailTestSender",
MessageEncoding = "utf-8"
};

_options = new Options()
{
UserName = USERNAME,
Password = PASSWORD,
SMTPServer = SMTPADDRESS,
Port = PORT,
UseSsl = USESSL,
UseWindowsAuthentication = USEWINDOWSAUTHENTICATION,
};

var result = SMTP.SendEmail(input, null, _options, default);
Assert.IsTrue(result.EmailSent);
}
}
[TearDown]
public void EmailTestTearDown()
{
if (Directory.Exists(_localAttachmentFolder))
Directory.Delete(_localAttachmentFolder, true);
}

[Test]
public void SendEmailWithFileAttachment()
{
var input = _input;
input.Subject = "Email test - FileAttachment";
[Test]
public void SendEmailWithPlainText()
{
var input = _input;
input.Subject = "Email test - PlainText";

var attachment = new Attachment
{
AttachmentType = AttachmentType.FileAttachment,
FilePath = _filepath,
SendIfNoAttachmentsFound = false,
ThrowExceptionIfAttachmentNotFound = true
};
var result = SMTP.SendEmail(input, null, _options, default);
Assert.IsTrue(result.EmailSent);
}

[Test]
public void SendEmailWithFileAttachment()
{
var input = _input;
input.Subject = "Email test - FileAttachment";

var Attachments = new AttachmentOptions { Attachments = new Attachment[] { attachment } };
var attachment = new Attachment
{
AttachmentType = AttachmentType.FileAttachment,
FilePath = _filepath,
SendIfNoAttachmentsFound = false,
ThrowExceptionIfAttachmentNotFound = true
};

var result = SMTP.SendEmail(input, Attachments, _options, default);
Assert.IsTrue(result.EmailSent);
}

[Test]
public void SendEmailWithStringAttachment()
var Attachments = new AttachmentOptions { Attachments = new Attachment[] { attachment } };

var result = SMTP.SendEmail(input, Attachments, _options, default);
Assert.IsTrue(result.EmailSent);
}

[Test]
public void SendEmailWithStringAttachment()
{
var input = _input;
input.Subject = "Email test - AttachmentFromString";
var fileAttachment = new AttachmentFromString() { FileContent = "teststring � �", FileName = "testfilefromstring.txt" };
var attachment = new Attachment()
{
var input = _input;
input.Subject = "Email test - AttachmentFromString";
var fileAttachment = new AttachmentFromString() { FileContent = "teststring � �", FileName = "testfilefromstring.txt" };
var attachment = new Attachment()
{
AttachmentType = AttachmentType.AttachmentFromString,
StringAttachment = fileAttachment
};
var Attachments = new AttachmentOptions { Attachments = new Attachment[] { attachment } };

var result = SMTP.SendEmail(input, Attachments, _options, default);
Console.WriteLine(result.StatusString);
Assert.IsTrue(result.EmailSent);
}
AttachmentType = AttachmentType.AttachmentFromString,
StringAttachment = fileAttachment
};
var Attachments = new AttachmentOptions { Attachments = new Attachment[] { attachment } };

var result = SMTP.SendEmail(input, Attachments, _options, default);
Assert.IsTrue(result.EmailSent);
}

[Test]
public void TrySendingEmailWithNoFileAttachmentFound()
{
var input = _input;
input.Subject = "Email test";

[Test]
public void TrySendingEmailWithNoFileAttachmentFound()
var attachment = new Attachment
{
var input = _input;
input.Subject = "Email test";
FilePath = Path.Combine(_localAttachmentFolder, TEST_FILE_NOT_EXISTING),
SendIfNoAttachmentsFound = false,
ThrowExceptionIfAttachmentNotFound = false
};

var attachment = new Attachment
{
FilePath = Path.Combine(_localAttachmentFolder, TEST_FILE_NOT_EXISTING),
SendIfNoAttachmentsFound = false,
ThrowExceptionIfAttachmentNotFound = false
};

var Attachments = new AttachmentOptions { Attachments = new Attachment[] { attachment } };

var Attachments = new AttachmentOptions { Attachments = new Attachment[] { attachment } };
var result = SMTP.SendEmail(input, Attachments, _options, default);
Assert.IsFalse(result.EmailSent);
}

var result = SMTP.SendEmail(input, Attachments, _options, default);
Assert.IsFalse(result.EmailSent);
}
[Test]
public void TrySendingEmailWithNoCcAndBcc()
{
var input = _input2;
input.Subject = "Email test";

[Test]
public void TrySendingEmailWithNoCcAndBcc()
var attachment = new Attachment
{
var input = _input2;
input.Subject = "Email test";
FilePath = Path.Combine(_localAttachmentFolder, TEST_FILE_NOT_EXISTING),
SendIfNoAttachmentsFound = false,
ThrowExceptionIfAttachmentNotFound = false
};

var attachment = new Attachment
{
FilePath = Path.Combine(_localAttachmentFolder, TEST_FILE_NOT_EXISTING),
SendIfNoAttachmentsFound = false,
ThrowExceptionIfAttachmentNotFound = false
};

var Attachments = new AttachmentOptions { Attachments = new Attachment[] { attachment } };

var Attachments = new AttachmentOptions { Attachments = new Attachment[] { attachment } };
var result = SMTP.SendEmail(input, Attachments, _options, default);
Assert.IsFalse(result.EmailSent);
}

var result = SMTP.SendEmail(input, Attachments, _options, default);
Assert.IsFalse(result.EmailSent);
}
[Test]
public void TrySendingEmailWithNoFileAttachmentFoundException()
{
var input = _input;
input.Subject = "Email test";

[Test]
public void TrySendingEmailWithNoFileAttachmentFoundException()
var attachment = new Attachment
{
var input = _input;
input.Subject = "Email test";
FilePath = Path.Combine(_localAttachmentFolder, TEST_FILE_NOT_EXISTING),
SendIfNoAttachmentsFound = false,
ThrowExceptionIfAttachmentNotFound = true
};

var attachment = new Attachment
{
FilePath = Path.Combine(_localAttachmentFolder, TEST_FILE_NOT_EXISTING),
SendIfNoAttachmentsFound = false,
ThrowExceptionIfAttachmentNotFound = true
};

var Attachments = new AttachmentOptions { Attachments = new Attachment[] { attachment } };

var Attachments = new AttachmentOptions { Attachments = new Attachment[] { attachment } };
Assert.Throws<FileNotFoundException>(() => SMTP.SendEmail(input, Attachments, _options, default));

Assert.Throws<FileNotFoundException>(() => SMTP.SendEmail(input, Attachments, _options, default));

}
}
}
Loading

0 comments on commit c66db81

Please sign in to comment.