1 | var should = require('should')
|
2 | , request = require('supertest')
|
3 | , mongoose = require('mongoose')
|
4 | , app = require('./support/')
|
5 | , db = require('./support/db')
|
6 | , models = require('./support/models')
|
7 | , User = models.User
|
8 | , Tag = models.Tag
|
9 | , Post = models.Post
|
10 | , Comment = models.Comment;
|
11 |
|
12 | describe('posts', function() {
|
13 | beforeEach(function(done) {
|
14 | db.setUp(done);
|
15 | });
|
16 | afterEach(function(done) {
|
17 | db.tearDown(done);
|
18 | });
|
19 | describe('GET /', function() {
|
20 | it('should return a list of posts', function(done) {
|
21 | request(app)
|
22 | .get('/posts')
|
23 | .expect(200)
|
24 | .end(function(err, res) {
|
25 | if (err) throw err
|
26 | res.body.posts.length.should.equal(1);
|
27 | done();
|
28 | });
|
29 | });
|
30 | });
|
31 | describe('POST /', function() {
|
32 | it('should create and return a post', function(done) {
|
33 | request(app)
|
34 | .post('/posts').send({ post: {
|
35 | title: 'b',
|
36 | content: 'b'
|
37 | }})
|
38 | .expect(200)
|
39 | .end(function(err, res) {
|
40 | if (err) throw err
|
41 | res.body.post.title.should.equal('b');
|
42 | res.body.post.content.should.equal('b');
|
43 | done();
|
44 | });
|
45 | });
|
46 | });
|
47 | describe('QUERY /', function() {
|
48 | it('should return matched posts', function(done) {
|
49 | request(app)
|
50 | .post('/posts').send(
|
51 | {
|
52 | query: {
|
53 | q:{
|
54 | title: 'a'
|
55 | }
|
56 | }
|
57 | }
|
58 | )
|
59 | .expect(200)
|
60 | .end(function(err, res) {
|
61 | if (err) throw err
|
62 | res.body.posts.length.should.equal(1);
|
63 | done();
|
64 | });
|
65 | });
|
66 | });
|
67 | describe('GET /:id', function() {
|
68 | it('should return a post', function(done) {
|
69 | request(app)
|
70 | .get('/posts/' + post.id)
|
71 | .expect(200)
|
72 | .end(function(err, res) {
|
73 | if (err) throw err
|
74 | res.body.post._id.should.equal(post.id);
|
75 | res.body.post.title.should.equal('a');
|
76 | res.body.post.content.should.equal('a');
|
77 | done();
|
78 | });
|
79 | });
|
80 | });
|
81 | describe('PUT /:id', function() {
|
82 | it('should update and return a post', function(done) {
|
83 | request(app)
|
84 | .put('/posts/' + post.id).send({ post: {
|
85 | title: 'b',
|
86 | content: 'b'
|
87 | }})
|
88 | .expect(200)
|
89 | .end(function(err, res) {
|
90 | res.body.post.title.should.equal('b');
|
91 | res.body.post.content.should.equal('b');
|
92 | if (err) throw err
|
93 | Post.findById(post.id, function(err, post) {
|
94 | if (err) throw err
|
95 | post.title.should.equal('b');
|
96 | post.content.should.equal('b');
|
97 | done();
|
98 | });
|
99 | });
|
100 | });
|
101 | });
|
102 | describe('DELETE /:id', function() {
|
103 | it('should remove a post', function(done) {
|
104 | request(app)
|
105 | .del('/posts/' + post.id)
|
106 | .expect(200)
|
107 | .expect({})
|
108 | .end(function(err, res) {
|
109 | if (err) throw err
|
110 | Post.findById(post.id, function(err, post) {
|
111 | if (err) throw err
|
112 | should.not.exist(post);
|
113 | Tag.findById(tag.id, function(err, tag) {
|
114 | if (err) throw err
|
115 | should.not.exist(tag);
|
116 | Comment.findById(comment.id, function(err, comment) {
|
117 | if (err) throw err;
|
118 | should.not.exist(comment);
|
119 | done();
|
120 | });
|
121 | });
|
122 | });
|
123 | });
|
124 | });
|
125 | });
|
126 | });
|