summaryrefslogtreecommitdiffstats
path: root/test/lib/smtpapi_header.test.js
blob: 699ad4d69d2cce7334e133d4d226cb5f37281b64 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
var SmtpapiHeaders = require('../../lib/smtpapi_headers');

describe('SmtpapiHeader', function() {
  var header;
  beforeEach(function() {
    header = new SmtpapiHeaders();
  });

  it('should allow multiple to addresses', function() {
    header.addTo('kyle.partridge@sendgrid.com');
    header.to.should.eql(['kyle.partridge@sendgrid.com']);
    header.addTo('david.tomberlin@sendgrid.com');
    header.to.should.eql(['kyle.partridge@sendgrid.com', 'david.tomberlin@sendgrid.com']);
  });

  describe('unique_args', function() {
    var unique_args = { foo: 'bar', apple: 'sauce' };
    it('should allow setting unique args', function() {
      header.setUniqueArgs(unique_args);
      header.unique_args.should.eql(unique_args);
    });

    it('should allow adding unique_args one at a time', function() {
      for (var key in unique_args) {
        var args = {};
        args[key] = unique_args[key];
        header.addUniqueArgs(args);
      }
      header.unique_args.should.eql(unique_args);
      header.addUniqueArgs({secret_test: 'rawr'});
      header.unique_args.secret_test.should.eql('rawr');
    });

    it('should overwrite when calling the with a value that already exists', function() {
      header.addUniqueArgs(unique_args);
      header.unique_args.should.eql(unique_args);
      header.addUniqueArgs({apple: 'pie'});
      header.unique_args.should.not.eql(unique_args);
      header.unique_args.apple.should.eql('pie');
    });
  });

  describe('categories', function() {
    var categories = ['azure', 'dreams'];

    it('should allow setting a single category', function() {
      header.setCategory(categories[0]);
      header.category.should.equal(categories[0]);
    });

    it('should allow adding categories one at a time', function() {
      header.addCategory(categories[0]);
      header.addCategory(categories[1]);
      header.category.should.eql(categories);
    });

    it('should allow setting categories', function() {
      var categories = ['azure', 'dreams'];
      header.setCategory(categories);
      header.category.should.eql(categories);
    });
  });

  describe('filters', function() {
    it('should be able to set basic filters', function() {
      header.addFilterSetting('footer', 'enable', 1);
      header.addFilterSetting('footer', 'text/html', '<b>boo</b>');
      header.filters.should.eql({
        'footer': {
          'settings': {
            'enable': 1,
            'text/html': '<b>boo</b>'
          }
        }
      });
    });

    it('should accept filter settings as a straight up object literal', function() {
      var filters = {
        'footer': {
          'setting': {
            'enable': 1,
            'text/plain': 'You can haz footers!'
          }
        }
      }
      header.setFilterSetting(filters);
      header.filters.should.eql(filters);
    });
  });

  describe('json', function() {
    it('should produce valid json', function() {
      header.addTo('kyle.partridge@sendgrid.com');
      header.addTo(['david.tomberlin@sendgrid.com']);
      header.addUniqueArgs({foo: 'bar'});
      header.addFilterSetting('footer', 'enable', 1);
      header.addFilterSetting('footer', 'text/html', '<b>boo</b>');
      JSON.parse(header.toJson()).should.eql(header);
    });

    it('should not include the "to" parameter when there are none', function() {
      header.setCategory('nothing');
      var json = header.toJson();

      assert(!_.isEmpty(JSON.parse(json).to), 'should be empty');
    });

    it('should not include the "sub" parameter when there are none', function() {
      header.setCategory('nothing');
      var json = header.toJson();

      assert(!_.isEmpty(JSON.parse(json).sub), 'should be empty');
    });

    it('should not include the "unique_args" parameter when there are none', function() {
      header.setCategory('nothing');
      var json = header.toJson();

      assert(!_.isEmpty(JSON.parse(json).unique_args), 'should be empty');
    });

    it('should not include the "category" parameter when there are none', function() {
      header.addUniqueArgs({food: 'bar'});
      var json = header.toJson();

      assert(!_.isEmpty(JSON.parse(json).category), 'should be empty');
    });

    it('should not include the "filters" parameter when there are none', function() {
      header.addUniqueArgs({food: 'bar'});
      var json = header.toJson();

      assert(!_.isEmpty(JSON.parse(json).filters), 'should be empty');
    });
  });
});