summaryrefslogtreecommitdiffstats
path: root/common/src/main/webapp/usageguide/appserver/node_modules/node-restful/test/model.js
blob: a51d6cb2698f3c138a1f7c9945e190473eb24eb3 (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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
var should = require('should'),
    request = require('supertest'),
    config = require('./fixtures/config'),
    sinon = require('sinon'),
    checkForHexRegExp = new RegExp("^[0-9a-fA-F]{24}$");

var oldA = should.Assertion.prototype.a;
should.Assertion.prototype.a = function(type, desc) {
  if (type === '_id') {
    this.assert(checkForHexRegExp.test(this.obj),
        function() { return 'expected ' + this.inspect + ' to be a ' + type + (desc ? " | " + desc : ""); },
        function(){ return 'expected ' + this.inspect + ' not to be a ' + type  + (desc ? " | " + desc : ""); });
    return this;
  }
  return oldA.call(this, type, desc);
};

describe('Model', function() {
  var movies, 
      users,
      app, 
      movie1, 
      movie2, 
      movie3, 
      user1, 
      user2,
      review;
  before(function(done) {
    config.ready(function() {
      app = config.app;
      movies = config.movie;
      users = config.user;
      movie1 = config.movies[0];
      movie2 = config.movies[1];
      movie3 = config.movies[2];
      user1 = config.users[0];
      user2 = config.users[1];
      review = config.reviews[0];
      done();
    });
  });
  describe('handlers', function() {
    it('should handle schema request', function(done) {
      request(app)
        .get('/api/movies/schema')
        .expect('Content-Type', /json/)
        .expect(200)
        .end(function(err, res) {
          res.body.resource.should.equal('movies');
          res.body.allowed_methods.should.eql(Object.keys(movies.allowed_methods));
          res.body.fields.should.be.an.instanceOf(Object);
          Object.keys(movies.schema.paths).forEach(function(path) {
            res.body.fields.should.have.property(path);
          });
          res.body.list_uri.should.equal('/api/movies');
          res.body.detail_uri.should.equal('/api/movies/:id');
          done();
        });
    });
    
    it('should dispatch to GET', function(done) {
      request(app)
        .get('/api/movies')
        .expect('Content-Type', /json/)
        .expect(200, done);
    }); 
    it('should fail POST with no data', function(done) {
      request(app)
        .post('/api/movies')
        .expect('Content-Type', /json/)
        .expect(400, done);
    });
    it('should POST with data', function(done) {
      request(app)
        .post('/api/movies')
        .send({
          title: "A very stupid movie",
          year: "214243"
        })
        .expect('Content-Type', /json/)
        .expect(201 )
        .end(function(err, res) {
          res.body.title.should.equal('A very stupid movie');
          res.body._id.should.type('string');
          done(err);
        });
    });
    it('should PUT data', function(done) {
      request(app)
        .put('/api/movies/' + movie2._id)
        .send({
          title: 'I changed the movie title'
        })
        .expect('Content-Type', /json/)
        .expect(200)
        .end(function(err, res) {
          res.body.title.should.equal('I changed the movie title');
          movies.findById(movie2._id, function(err, movie) {
            movie.title.should.equal('I changed the movie title');
            done();
          });
        });
    });
    it('should fail on GET missing resource', function(done) {
      request(app)
        .get('/api/movies/55e8169191ad293c221a6c9d')
        .expect(404, done);
    });
    it('should fail on PUT missing resource (shouldUseAtomicUpdate=false)', function(done) {
      request(app)
        .put('/api/genres/55e8169191ad293c221a6c9d')
        .send({
          name: "Mysterious genre"
        })
        .expect(404, done);
    });
    it('should fail on PUT missing resource (shouldUseAtomicUpdate=true)', function(done) {
      request(app)
        .put('/api/movies/55e8169191ad293c221a6c9d')
        .send({
          title: "Mysterious genre"
        })
        .expect(404, done);
    });
    it('should fail on DELETE missing resource (shouldUseAtomicUpdate=false)', function(done) {
      request(app)
        .del('/api/genres/55e8169191ad293c221a6c9d')
        .expect(404, done);
    });
    it('should fail on PUT without filter on unsortable model', function(done) {
      request(app)
        .put('/api/movies')
        .send({
          title: "A very stupid movie"
        })
        .expect(404, done);
    });
    it('should fail on DELETE without a filter', function(done) {
      request(app)
        .del('/users')
        .expect(404, done);
    });
    it('should DELETE a movie', function(done) {
      request(app)
        .del('/api/movies/' + movie3._id)
        .expect(200)
        .expect('Content-Type', /json/)
        .end(function(err, res) {
          movies.findById(movie3._id, function(err, movie) {
            should.not.exist(movie);
            done();
          });
        });
    });
    it("shouldn't put data on deleted resource", function(done) {
      request(app)
        .del('/api/movies/' + config.movies[5]._id)
        .end(function(err, res) {
          request(app)
            .put('/api/movies/' + config.movies[5]._id)
            .send({
              title: 'But I already deleted you'
            })
          .expect(404, done);
        });
    });
    it('should 400 deleting a resource twice', function(done) {
      request(app)
        .del('/api/movies/' + config.movies[6]._id)
        .end(function() {
          request(app)
            .del('/api/movies/' + config.movies[6]._id)
            .expect(404, done);
        });
    });
    it('should 404 on undefined route', function(done) {
      request(app)
        .del('/api/movies')
        .expect(404, done);
    });
    it('should return a nested model at the generated endpoint creator', function(done) {
      request(app)
        .get('/api/movies/' + movie1._id + '/creator')
        .expect('Content-Type', /json/)
        .expect(200)
        .end(function(err, res) {
          if (err) return done(err);
          res.should.be.json;
          res.body.username.should.equal('test');
          res.body.pass_hash.should.equal(12374238719845134515);
          done();
        }); 
    });
    it('should 404 if we request an object endpoint without a filter', function(done) {
      request(app)
        .get('/api/movies/creator')
        .expect(404, done);
    });
    it('should retrieve a deeply nested endpoint', function(done) {
      request(app)
        .get('/api/movies/' + movie1._id + '/meta/director')
        .expect('Content-Type', /json/)
        .expect(200)
        .end(function(err, res) {
          res.should.be.json;
          res.body.username.should.equal('test2');
          res.body.pass_hash.should.equal(1237987381263189273123);
          done();
        });

    });
    it('should 404 on a nested object', function(done) {
      request(app)
        .get('/api/movies/' + movie1._id + '/meta')
        .expect(404, done)
    });
    it('should get a user defined route', function(done) {
      request(app)
        .get('/api/movies/recommend')
        .expect('Content-Type', /json/)
        .expect(200)
        .end(function(err, res) {
          res.should.be.json;
          res.body.recommend.should.equal("called");
          done();
        });
    });
    it('should get anotheroute (user defined route)', function(done) {
      request(app)
        .get('/api/movies/anotherroute')
        .expect('Content-Type', /json/)
        .expect(200)
        .end(function(err, res) {
          res.should.be.json;
          res.body.anotherroute.should.equal("called");
          done();
        });
    });
    it('should get athirdroute (user defined route)', function(done) {
      request(app)
        .get('/api/movies/' + movie1._id + '/athirdroute')
        .expect('Content-Type', /json/)
        .expect(200)
        .end(function(err, res) {
          res.should.be.json;
          res.body.athirdroute.should.equal("called");
          done();
        });
    });
    it('should fail athirdroute (user defined route)', function(done) {
      request(app)
        .get('/api/movies/athirdroute')
        .expect(404, done);
    });
    it('should fail athirdroute (user defined route)', function(done) {
      request(app)
        .put('/api/movies/' + movie1._id + '/athirdroute')
        .expect(404, done);
    });
    it('should allow put of entire object', function(done) {
      request(app)
        .get('/api/movies/' + config.movies[7]._id)
        .end(function(err, res) {
          var movie = res.body;
          movie.title = 'A different title';
          request(app)
            .put('/api/movies/' + movie._id)
            .send(movie)
            .expect('Content-Type', /json/)
            .expect(201)
            .end(function(err, res) {
              res.body.title.should.equal('A different title');
              done();
            });
        });
    });
    it('should allow overriding of schema route', function(done) {
      request(app)
        .get('/users/schema')
        .expect(404, done);
    });
    it('should allow multiple handlers to be called on a single route', function(done) {
      request(app)
        .get('/api/movies/pshh')
        .expect(200)
        .end(function(err, res) {
          res.body.pshh.should.equal('called');
          done(err);
        });
    });
    it('should allow get request for model with field named `length`', function(done) {
      request(app)
        .get('/api/reviews/' + review._id)
        .expect(200)
        .end(function(err, res) {
          res.body.body.should.equal('This is a movie review!');
          done(err);
        });
    });
  });
});