diff --git a/src/SparkPost.Tests/DataMapperTests.cs b/src/SparkPost.Tests/DataMapperTests.cs index 3a6e54e3..9faa1f18 100644 --- a/src/SparkPost.Tests/DataMapperTests.cs +++ b/src/SparkPost.Tests/DataMapperTests.cs @@ -1307,5 +1307,56 @@ public void limit() Assert.That(dict["limit"].CastTo(), Is.EqualTo(r)); } } + + [TestFixture] + public class TemplateMappingTests + { + private DataMapper _mapper; + private Template _template; + + [SetUp] + public void Setup() + { + _template = new Template(); + _mapper = new DataMapper("v1"); + } + + [Test] + public void has_draft() + { + bool hasDraftValue = false; + _template.HasDraft = hasDraftValue; + var dict = _mapper.ToDictionary(_template); + Assert.AreEqual(hasDraftValue, dict["has_draft"]); + } + + [Test] + public void has_published() + { + bool value = true; + _template.HasPublished = value; + var dict = _mapper.ToDictionary(_template); + Assert.AreEqual(value, dict["has_published"]); + } + + [Test] + public void has_option_click_tracking() + { + bool value = true; + _template.Options.ClickTracking = value; + var dict = _mapper.ToDictionary(_template); + var optionsDict = (Dictionary)dict["options"]; + Assert.AreEqual(value, optionsDict["click_tracking"]); + } + + [Test] + [ExpectedException(typeof(KeyNotFoundException))] + public void has_option_click_tracking_null() + { + var dict = _mapper.ToDictionary(_template); + var optionsDict = (Dictionary)dict["options"]; + Assert.AreEqual(true, optionsDict["click_tracking"]); + } + } } } \ No newline at end of file diff --git a/src/SparkPost.Tests/SparkPost.Tests.csproj b/src/SparkPost.Tests/SparkPost.Tests.csproj index 5a2d99f7..d002a6e0 100644 --- a/src/SparkPost.Tests/SparkPost.Tests.csproj +++ b/src/SparkPost.Tests/SparkPost.Tests.csproj @@ -117,6 +117,7 @@ + diff --git a/src/SparkPost.Tests/TemplateTests.cs b/src/SparkPost.Tests/TemplateTests.cs new file mode 100644 index 00000000..4c95f424 --- /dev/null +++ b/src/SparkPost.Tests/TemplateTests.cs @@ -0,0 +1,207 @@ +using AutoMoq.Helpers; +using Moq; +using NUnit.Framework; +using Should; +using SparkPost.RequestSenders; +using System; +using System.Collections.Generic; +using System.Net; +using System.Threading.Tasks; + +namespace SparkPost.Tests +{ + public class TemplateTests + { + [TestFixture] + public class DeleteTests : AutoMoqTestFixture + { + private Response _response; + private Template _template; + + [SetUp] + public void Setup() + { + ResetSubject(); + + _response = new Response { StatusCode = HttpStatusCode.NoContent }; + + Mocked() + .Setup(x => x.Send(It.IsAny())) + .Returns(Task.FromResult(_response)); + + _template = new Template() + { + Id = Guid.NewGuid().ToString() + }; + } + + [Test] + public async void It_should_return_false_if_the_status_is_not_ok() + { + var result = await Subject.Delete(_template.Id); + result.ShouldBeFalse(); + } + + [Test] + public async void It_should_return_false_if_the_web_request_returns_anything_but_no_content() + { + _response.StatusCode = HttpStatusCode.Accepted; + (await Subject.Delete(_template.Id)).ShouldBeFalse(); + + _response.StatusCode = HttpStatusCode.Ambiguous; + (await Subject.Delete(_template.Id)).ShouldBeFalse(); + + _response.StatusCode = HttpStatusCode.UpgradeRequired; + (await Subject.Delete(_template.Id)).ShouldBeFalse(); + } + + [Test] + public async void It_should_build_the_web_request_parameters_correctly() + { + var version = Guid.NewGuid().ToString(); + + Mocked() + .Setup(x => x.Version) + .Returns(version); + + Mocked() + .Setup(x => x.Send(It.IsAny())) + .Callback((Request r) => + { + r.Url.ShouldEqual($"api/{version}/templates/{_template.Id}"); + r.Method.ShouldEqual("DELETE"); + }) + .Returns(Task.FromResult(_response)); + + await Subject.Delete(_template.Id); + } + } + + [TestFixture] + public class CreateOrUpdateTests : AutoMoqTestFixture + { + private Response _response; + private List