Skip to content

Instantly share code, notes, and snippets.

@anderssonjohan
Created March 23, 2013 23:18
Show Gist options
  • Save anderssonjohan/5229712 to your computer and use it in GitHub Desktop.
Save anderssonjohan/5229712 to your computer and use it in GitHub Desktop.
Saturday night coding... faking HTTP and SMTP all night long.
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Mail;
using NUnit.Framework;
using RemoteX.Libraries.ClientDataAccess.Entities.DTO;
using RemoteX.Libraries.Integration;
using RemoteX.Libraries.Integration.Testing;
using Should;
namespace CommandFileImporter.Test
{
[TestFixture]
public class ErrorReporterTests
{
const string MailTo = "[email protected]";
ErrorReporter _reporter;
FileInfo _file;
DateTime _importDate;
MailMessage _message;
string _body;
IEnumerable<CommandResponse> _failedCommands;
[SetUp]
public void Setup()
{
TestClient.Enable();
TestEmailSender.Enable();
ApplicationConfiguration.ApiSettings["MailTo"] = MailTo;
ApplicationConfiguration.ApiSettings["MailTemplate"] = "File: ${file}\nDate: ${date}\nCommands:\n${commands}";
ApplicationConfiguration.ApiSettings["SubjectTemplate"] = "File ${file} Date ${date}";
_reporter = new ErrorReporter();
_file = TestUtils.NewCsvFile();
_importDate = DateTime.Now;
_failedCommands = new List<CommandResponse>
{
new CommandResponse { ErrorMessage = "Error1", Command = new Command { Name = "UpsertUnit", Target = "123" }.Parameter("Param1", "value1").Parameter("Param2", "value2") },
new CommandResponse { ErrorMessage = "Error2", Command = new Command { Name = "UpsertContract", Target = "987" }.Parameter( "ParamA", "valueA1", "valueA2" ).Parameter( "ParamB", "valueB1", "valueB2" ) }
};
_reporter.SendReport( _file, _importDate, _failedCommands );
_message = TestEmailSender.Log.Last();
_body = _message.Body;
}
[Test]
public void ReportsToLog()
{
var logEntry = TestClient.Posts<LogEntry>().LastOrDefault();
logEntry.ShouldNotBeNull();
logEntry.Category.ShouldEqual("Error");
logEntry.Message.ShouldContain(_file.Name);
}
[Test]
public void CanHaveFileAndDateInSubject()
{
_message.Subject.ShouldContain( "File " + _file.Name );
_message.Subject.ShouldContain( "Date " + _importDate.ToShortDateString() );
}
[Test]
public void CanHaveFileNameInEmail()
{
_body.ShouldContain( "File: " + _file.Name );
}
[Test]
public void CanHaveImportDateInEmail()
{
_body.ShouldContain( "Date: " + _importDate );
}
[Test]
public void CanHaveCommandInfoInEmail()
{
_body.ShouldContain( "Commands:" );
_body.ShouldContain( "Command: UpsertUnit" );
_body.ShouldContain( "Error: Error1" );
_body.ShouldContain( "Target: 123" );
_body.ShouldContain( "Param1: value1" );
_body.ShouldContain( "Param2: value2" );
_body.ShouldContain( "Command: UpsertContract" );
_body.ShouldContain( "Error: Error2" );
_body.ShouldContain( "Target: 987" );
_body.ShouldContain( "ParamA: valueA1, valueA2" );
_body.ShouldContain( "ParamB: valueB1, valueB2" );
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment