public void Renders_all() { // ARRANGE var expected = "<strong>bob</strong>"; var t = new Template(); t.Content.Subject = "<strong>{{name}}</strong>"; t.Content.Body = "<strong>{{name}}</strong>"; t.Content.TextBody = "<strong>{{name}}</strong>"; t.Content.SummaryBody = "<strong>{{name}}</strong>"; t.Content.SummaryHeader = "<strong>{{name}}</strong>"; t.Content.SummaryFooter = "<strong>{{name}}</strong>"; t.Content.SummarySubject = "<strong>{{name}}</strong>"; t.Content.SummaryTextBody = "<strong>{{name}}</strong>"; t.Content.SummaryTextHeader = "<strong>{{name}}</strong>"; t.Content.SummaryTextFooter = "<strong>{{name}}</strong>"; var r = new HandlebarsRenderer(t); // ACT var mail = new Models.Mail(); r.Render(mail, new { name = "bob" }); // ASSERT Assert.AreEqual(expected, mail.Body); Assert.AreEqual(expected, mail.Subject); Assert.AreEqual(expected, mail.SummaryBody); Assert.AreEqual(expected, mail.SummaryFooter); Assert.AreEqual(expected, mail.SummaryHeader); Assert.AreEqual(expected, mail.SummarySubject); Assert.AreEqual(expected, mail.TextBody); Assert.AreEqual(expected, mail.SummaryTextBody); Assert.AreEqual(expected, mail.SummaryTextFooter); Assert.AreEqual(expected, mail.SummaryTextHeader); }
public void Register(Template template) { m_templates = m_templates ?? new Dictionary<string, Template>(); m_renderer = m_renderer ?? new Dictionary<string, IRenderer>(); var localizedKey = template.LocalizedKey.ToLower(); if (m_templates.ContainsKey(localizedKey)) { throw new Exception("Template with identical key already added."); } else { RegisterWithKey(localizedKey, template); } var shorterLocalizedKey = localizedKey.Substring(0, localizedKey.LastIndexOf('-')).ToLower(); if (!m_templates.ContainsKey(shorterLocalizedKey)) { RegisterWithKey(shorterLocalizedKey, template); } var templateKey = template.Key.ToLower(); if (!m_templates.ContainsKey(templateKey)) { RegisterWithKey(templateKey, template); } }
public HandlebarsRenderer(Template t) { m_compiledTemplateBody = Handlebars.Compile(t.Content.Body ?? String.Empty); m_compiledTemplateTextBody = Handlebars.Compile(t.Content.TextBody ?? String.Empty); m_compiledTemplateSubject = Handlebars.Compile(t.Content.Subject ?? String.Empty); m_compiledTemplateSummaryBody = Handlebars.Compile(t.Content.SummaryBody ?? String.Empty); m_compiledTemplateSummaryHeader = Handlebars.Compile(t.Content.SummaryHeader ?? String.Empty); m_compiledTemplateSummaryFooter = Handlebars.Compile(t.Content.SummaryFooter ?? String.Empty); m_compiledTemplateSummaryTextBody = Handlebars.Compile(t.Content.SummaryTextBody ?? String.Empty); m_compiledTemplateSummaryTextHeader = Handlebars.Compile(t.Content.SummaryTextHeader ?? String.Empty); m_compiledTemplateSummaryTextFooter = Handlebars.Compile(t.Content.SummaryTextFooter ?? String.Empty); m_compiledTemplateSummarySubject = Handlebars.Compile(t.Content.SummarySubject ?? String.Empty); }
public void Sanitized_correctly() { // ARRANGE var expectedBody = "<strong><b>o</b></strong>"; var expectedText = "<b>o</b>"; var t = new Template(); t.Content.Body = "<strong>{{name}}</strong>"; t.Content.TextBody = "{{{name}}}"; var r = new HandlebarsRenderer(t); // ACT var mail = new Models.Mail(); r.Render(mail, new { name = "<b>o</b>" }); // ASSERT Assert.AreEqual(expectedBody, mail.Body); Assert.AreEqual(expectedText, mail.TextBody); }
public void Renders_Complex() { // ARRANGE var expected = "<strong>alice</strong>"; var t = new Template(); t.Content.Body = "<strong>{{name.sub}}</strong>"; var r = new HandlebarsRenderer(t); // ACT var mail = new Models.Mail(); r.Render(mail, new { name = new { sub = "alice" } }); // ASSERT Assert.AreEqual(expected, mail.Body); }
public void EnumerateTemplates(Action<Template> act) { m_fileSystem.EnumerateDirectory(m_baseFolder, sub => { if (sub.StartsWith(m_ignorePrefix)) { return; } var t = new Template(); t.Key = sub.ToLower(); var dir = Path.Combine(m_baseFolder, sub); FillTemplateContent(t.Content, dir); t.Language = m_cultureInfo; act(t); // languages m_fileSystem.EnumerateDirectory(dir, lang => { var langt = new Template(); langt.Key = sub.ToLower(); // use key from top folder var langdir = Path.Combine(dir, lang); FillTemplateContent(langt.Content, langdir); try { var ci = CultureInfo.CreateSpecificCulture(lang.ToLower()); langt.Language = ci; } catch (CultureNotFoundException ex) { throw new Exception(string.Format("Path '{0}' ends in an invalid " + "language specifier. It should " + "be something like 'en' or 'en-us'.", langdir)); } act(langt); }); }); }
private void RegisterWithKey(string key, Template template) { m_templates.Add(key, template); m_renderer.Add(key, m_rendererFactory(template)); }
public void Basic_AsyncOn() { // ARRANGE var senderMock = new Mock<IMailSenderService>(); var configMock = new Mock<IConfiguration>(); configMock.Setup(m => m.GetValue(DefaultSenderConfiguration.APP_KEY_FROM_ADDRESS)) .Returns("*****@*****.**"); configMock.Setup(m => m.GetValue(DefaultSenderConfiguration.APP_KEY_FROM_NAME)) .Returns("John Doe"); configMock.Setup(m => m.GetValue(MailGeneratorService.APP_KEY_ASYNCACTIVE)) .Returns("1"); dynamic url1 = new ExpandoObject(); url1.Var1 = "Foo1"; url1.Var2 = "Bar2"; dynamic url2 = new ExpandoObject(); dynamic url2sub = new ExpandoObject(); url2.Sub1 = url2sub; url2sub.Var3 = "Foo3"; url2sub.Var4 = "Bar4"; dynamic user = new ExpandoObject(); user.email = "*****@*****.**"; user.fullname = "Tina Test"; user.fname = "Tina"; var datastoreMock = new Mock<IDataStore>(); datastoreMock.Setup(m => m.Get(new Uri("http://example.org/url1"))).Returns((object)url1); datastoreMock.Setup(m => m.Get(new Uri("http://example.org/url2"))).Returns((object)url2); datastoreMock.Setup(m => m.Get(new Uri("http://example.org/user"))).Returns((object)user); var queueMock = new Mock<IMailQueue>(); var template = new Template(); template.Key = "key1"; template.Content.Body = "Hello {{user.fullname}} <br/> {{two.Sub1.Var3}} {{one.Var2}}"; template.Content.SummaryBody = "H {{user.fname}}"; template.Content.SummaryFooter = "B {{user.fname}}"; template.Content.SummaryHeader = "C {{user.fname}}"; template.Content.SummarySubject = "D {{user.fname}}"; dynamic receiver = new Uri("http://example.org/user"); dynamic data = new ExpandoObject(); data.one = new Uri("http://example.org/url1"); data.two = new Uri("http://example.org/url2"); data.user = new Uri("http://example.org/user"); var mailService = new MailGeneratorService( queueMock.Object, datastoreMock.Object, configuration: configMock.Object, mailSenderService: senderMock.Object); mailService.Register(template); // ACT mailService.Send(receiver, "key1", data); // ASSERT Expression<Func<Models.Mail, bool>> mailCheck = mail => mail.Sender.Address == "*****@*****.**" && mail.Sender.DisplayName == "John Doe" && mail.Receiver.Address == "*****@*****.**" && mail.Receiver.DisplayName == "Tina Test" && mail.Body == "Hello Tina Test <br/> Foo3 Bar2" && mail.SummaryBody == "H Tina" && mail.SummaryFooter == "B Tina" && mail.SummaryHeader == "C Tina" && mail.SummarySubject == "D Tina"; queueMock.Verify(m => m.Enqueue(It.Is<Models.Mail>(mailCheck), It.IsAny<object>())); senderMock.Verify(m => m.ProcessNext(), Times.Never); }