1 | var mocha = require('mocha');
|
2 | var assert = require('assert');
|
3 | var nconf = require('nconf');
|
4 | var testingKeys = nconf.env().file({
|
5 | file: __dirname + '/testing_keys.json'
|
6 | });
|
7 | var util = require('util');
|
8 | var merge = require('merge');
|
9 |
|
10 | var postmark = require('../lib/postmark/index.js');
|
11 |
|
12 | describe('client template handling', function() {
|
13 | this.timeout(10000);
|
14 | var _client = null;
|
15 |
|
16 | beforeEach(function() {
|
17 | _client = new postmark.Client(testingKeys.get('WRITE_TEST_SERVER_TOKEN'));
|
18 | });
|
19 |
|
20 | after(function() {
|
21 | _client.getTemplates(function(err, results) {
|
22 |
|
23 | while (results.Templates.length > 0) {
|
24 | var t = results.Templates.pop();
|
25 | if (/testing-template-node-js/.test(t.Name)) {
|
26 | _client.deleteTemplate(t.TemplateId);
|
27 | }
|
28 | }
|
29 | });
|
30 | });
|
31 |
|
32 | it('should retrieve a list of templates.', function(done) {
|
33 | _client.getTemplates(done);
|
34 | });
|
35 |
|
36 | it('should get a single template.', function(done) {
|
37 | _client.createTemplate({
|
38 | name: "testing-template-node-js" + Date(),
|
39 | textBody: "text body for template {{id}}!",
|
40 | htmlBody: "{{content}}",
|
41 | subject: "{{subject}}"
|
42 | }, function(err, template) {
|
43 | _client.getTemplate(template.TemplateId, done);
|
44 | });
|
45 | });
|
46 |
|
47 | it('should a update a template.', function(done) {
|
48 | _client.createTemplate({
|
49 | name: "testing-template-node-js" + Date(),
|
50 | textBody: "text body for template {{id}}!",
|
51 | htmlBody: "{{content}}",
|
52 | subject: "{{subject}}"
|
53 | }, function(error, newTemplate) {
|
54 | _client.editTemplate(newTemplate.TemplateId, {
|
55 | name: "testing-template-node-js" + Date(),
|
56 | textBody: "text body for template {{id}}!",
|
57 | htmlBody: "{{content}}",
|
58 | subject: "{{subject}}"
|
59 | }, done);
|
60 | });
|
61 | });
|
62 |
|
63 | it('should a create a template.', function(done) {
|
64 | _client.createTemplate({
|
65 | name: "testing-template-node-js" + Date(),
|
66 | textBody: "text body for template {{id}}!",
|
67 | htmlBody: "{{content}}",
|
68 | subject: "{{subject}}"
|
69 | }, done);
|
70 | });
|
71 |
|
72 | it('should a delete a template.', function(done) {
|
73 | _client.createTemplate({
|
74 | name: "testing-template-node-js" + Date(),
|
75 | textBody: "text body for template {{id}}!",
|
76 | htmlBody: "{{content}}",
|
77 | subject: "{{subject}}"
|
78 | }, function(err, template) {
|
79 | _client.deleteTemplate(template.TemplateId, done);
|
80 | });
|
81 | });
|
82 |
|
83 | it('send an email using a template.', function(done) {
|
84 | _client.createTemplate({
|
85 | name: "testing-template-node-js" + Date(),
|
86 | textBody: "text body for template {{id}}!",
|
87 | htmlBody: "{{content}}",
|
88 | subject: "{{subject}}"
|
89 | }, function(error, t) {
|
90 | _client.sendEmailWithTemplate({
|
91 | To: testingKeys.get('WRITE_TEST_EMAIL_RECIPIENT_ADDRESS'),
|
92 | From: testingKeys.get('WRITE_TEST_SENDER_EMAIL_ADDRESS'),
|
93 | TemplateId: t.TemplateId,
|
94 | TemplateModel: {
|
95 | subject: "Hello from the node.js client! " + new Date(),
|
96 | content: "Testing templated email"
|
97 | }
|
98 | }, done);
|
99 | });
|
100 | });
|
101 |
|
102 | it('should validate template', function(done) {
|
103 | _client.validateTemplate({
|
104 | testRenderModel: {
|
105 | Name: "joe!"
|
106 | },
|
107 | textBody: "text body for template {{id}}!",
|
108 | htmlBody: "{{content}}",
|
109 | subject: "{{subject}}"
|
110 | }, done);
|
111 | })
|
112 |
|
113 | });
|