summaryrefslogtreecommitdiff
path: root/test/PodcastView.js
blob: 24acef2963ea206050d79ac658aa42f5dea2c18e (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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
var assert = require('assert');
var mockery = require('mockery');
var sinon = require('sinon');

var PodcastView; // Subject of the test

var dynamoStub;
var s3Stub;

var view456 = {
  feedId: '123',
  viewId: '456',
  name: 'Test View',
  template: 'Hello World'
};

var view789 = {
  feedId: '123',
  viewId: '789',
  name: 'Test View 2',
  template: 'Goodbye World'
};

var episodes = [
  { title: 'Episode 1' },
  { title: 'Episode 2' },
];

describe('PodcastView', function() {
  before(function() {
    mockery.enable({warnOnUnregistered: false});
  });

  beforeEach(function() {
    mockery.registerAllowable('underscore');

    mockery.registerMock('dynamodb-doc', { DynamoDB: function() { return dynamoStub } });
    dynamoStub = {
      getItem: sinon.stub(),
      query: sinon.stub(),
    };

    mockery.registerMock('aws-sdk', { S3: function() { return s3Stub } });
    s3Stub = {
      putObject: sinon.stub(),
    };

    mockery.registerAllowable('../lib/PodcastView', true);
    PodcastView = require('../lib/PodcastView');
  });

  it('calls the callback with a view when findById is called with a valid viewId', function() {
    // Setup stubs
    dynamoStub
      .getItem
      .withArgs({
        TableName: 'podcast-views',
        Key: { feedId: '123', viewId: '456' }
      })
      .callsArgWith(1, null, {
        Item: view456
      });

    var callback = sinon.spy();

    // Call findById
    PodcastView.findById({ feedId: '123', viewId: '456' }, callback);

    // Assertions
    assert(callback.calledOnce);

    var error = callback.getCall(0).args[0];
    var view = callback.getCall(0).args[1];
    assert.equal(error, null);
    assert.equal(view.feedId, '123');
    assert.equal(view.viewId, '456');
    assert.equal(view.name, 'Test View');
    assert.equal(view.template, 'Hello World');
  });

  it('calls the callback with each view when forEachView is called', function() {
    // Setup stubs
    dynamoStub
      .query 
      .withArgs(
      {
        TableName: 'podcast-views',
        KeyConditionExpression: "feedId = :feedId",
        ExpressionAttributeValues: {
          ':feedId': '123'
        }
      })
      .callsArgWith(1, null, {
        Items: [view456, view789]
      });

    var callback = sinon.spy();

    // Call findById
    PodcastView.forEachView('123', callback);

    // Assertions
    assert(callback.calledTwice);

    var error = callback.getCall(0).args[0];
    var view = callback.getCall(0).args[1];
    var last = callback.getCall(0).args[2];
    assert.equal(error, null);
    assert.equal(view.feedId, '123');
    assert.equal(view.viewId, '456');
    assert.equal(view.name, 'Test View');
    assert.equal(view.template, 'Hello World');
    assert(!last);

    var error = callback.getCall(1).args[0];
    var view = callback.getCall(1).args[1];
    var last = callback.getCall(1).args[2];
    assert.equal(error, null);
    assert.equal(view.feedId, '123');
    assert.equal(view.viewId, '789');
    assert.equal(view.name, 'Test View 2');
    assert.equal(view.template, 'Goodbye World');
    assert(last);
  });

  it("calls the callback with each view when forEachView's query is truncated", function() {
    // Setup stubs
    dynamoStub
      .query 
      .withArgs(
      {
        TableName: 'podcast-views',
        KeyConditionExpression: "feedId = :feedId",
        ExpressionAttributeValues: {
          ':feedId': '123'
        }
      })
      .callsArgWith(1, null, {
        Items: [view456],
        LastEvaluatedKey: { feedId: '123', viewId: '456' }
      });
    dynamoStub
      .query 
      .withArgs(
      {
        TableName: 'podcast-views',
        KeyConditionExpression: "feedId = :feedId",
        ExpressionAttributeValues: {
          ':feedId': '123'
        },
        ExclusiveStartKey: { feedId: '123', viewId: '456' },
      })
      .callsArgWith(1, null, {
        Items: [view789],
      });

    var callback = sinon.spy();

    // Call findById
    PodcastView.forEachView('123', callback);

    // Assertions
    assert(callback.calledTwice);

    var error = callback.getCall(0).args[0];
    var view = callback.getCall(0).args[1];
    var last = callback.getCall(0).args[2];
    assert.equal(error, null);
    assert.equal(view.feedId, '123');
    assert.equal(view.viewId, '456');
    assert.equal(view.name, 'Test View');
    assert.equal(view.template, 'Hello World');
    assert(!last);

    var error = callback.getCall(1).args[0];
    var view = callback.getCall(1).args[1];
    var last = callback.getCall(1).args[2];
    assert.equal(error, null);
    assert.equal(view.feedId, '123');
    assert.equal(view.viewId, '789');
    assert.equal(view.name, 'Test View 2');
    assert.equal(view.template, 'Goodbye World');
    assert(last);
  });

  it('uploads an artifact when render is called', function() {
    // Setup the test.
    s3Stub.putObject.onFirstCall().callsArgWith(1, null);

    var view = new PodcastView({
      bucket: 'test-bucket',
      filenameTemplate: 'episodes.html',
      template: '<% _.each(episodes, function(episode) { %><h1><%= episode.title %></h1><% }); %>',
    });

    var callback = sinon.stub();

    // Render
    view.render(episodes, callback);

    // Verify the correct artifact was rendered.
    assert(s3Stub.putObject.calledOnce);
    var params = s3Stub.putObject.getCall(0).args[0];
    assert.equal(params.Bucket, 'test-bucket');
    assert.equal(params.Key, 'episodes.html');
    assert.equal(params.Body, '<h1>Episode 1</h1><h1>Episode 2</h1>');

    // Verify the callback was called.
    assert(callback.calledOnce);
  });

  it('uploads multiple artifacts when render is called and renderEach is true', function() {
    // Setup the test.
    s3Stub.putObject.onFirstCall().callsArgWith(1, null);
    s3Stub.putObject.onSecondCall().callsArgWith(1, null);

    var view = new PodcastView({
      bucket: 'test-bucket',
      filenameTemplate: '<%= episode.title.toLowerCase().replace(/[^\w ]+/g,'').replace(/ +/g,'-') %>.html',
      template: '<h1><%= episode.title %></h1>',
      renderEach: true,
    });

    var callback = sinon.stub();

    // Render
    view.render(episodes, callback);

    // Verify the correct artifact was rendered.
    assert.equal(s3Stub.putObject.callCount, 2);

    var params = s3Stub.putObject.getCall(0).args[0];
    assert.equal(params.Bucket, 'test-bucket');
    assert.equal(params.Key, 'episode-1.html');
    assert.equal(params.Body, '<h1>Episode 1</h1>');

    var params = s3Stub.putObject.getCall(1).args[0];
    assert.equal(params.Bucket, 'test-bucket');
    assert.equal(params.Key, 'episode-2.html');
    assert.equal(params.Body, '<h1>Episode 2</h1>');

    // Verify the callback was called.
    assert(callback.calledOnce);
  });

  afterEach(function() {
    mockery.deregisterAll();
  }),

  after(function() {
    mockery.disable();
  });
});