-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcomment.server.routes.test.js
More file actions
269 lines (223 loc) · 6.55 KB
/
comment.server.routes.test.js
File metadata and controls
269 lines (223 loc) · 6.55 KB
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
'use strict';
var should = require('should'),
request = require('supertest'),
app = require('../../server'),
mongoose = require('mongoose'),
User = mongoose.model('User'),
Comment = mongoose.model('Comment'),
Deal = mongoose.model('Deal'),
agent = request.agent(app);
/**
* Globals
*/
var credentials, user, comment;
/**
* Comment routes tests
*/
describe('Comment CRUD tests', function() {
beforeEach(function(done) {
// Create user credentials
credentials = {
username: 'username',
password: 'password'
};
// Create a new user
user = new User({
firstName: 'Full',
lastName: 'Name',
displayName: 'Full Name',
email: 'test@test.com',
username: credentials.username,
password: credentials.password,
provider: 'local'
});
// Save a user to the test db and create new Comment
user.save(function() {
comment = {
name: 'Comment Name'
};
done();
});
});
it('should be able to save Comment instance if logged in', function(done) {
agent.post('/auth/signin')
.send(credentials)
.expect(200)
.end(function(signinErr, signinRes) {
// Handle signin error
if (signinErr) done(signinErr);
// Get the userId
var userId = user.id;
// Save a new Comment
agent.post('/comments')
.send(comment)
.expect(200)
.end(function(commentSaveErr, commentSaveRes) {
// Handle Comment save error
if (commentSaveErr) done(commentSaveErr);
// Get a list of Comments
agent.get('/comments')
.end(function(commentsGetErr, commentsGetRes) {
// Handle Comment save error
if (commentsGetErr) done(commentsGetErr);
// Get Comments list
var comments = commentsGetRes.body;
// Set assertions
(comments[0].user._id).should.equal(userId);
(comments[0].name).should.match('Comment Name');
// Call the assertion callback
done();
});
});
});
});
it('should not be able to save Comment instance if not logged in', function(done) {
agent.post('/comments')
.send(comment)
.expect(401)
.end(function(commentSaveErr, commentSaveRes) {
// Call the assertion callback
done(commentSaveErr);
});
});
it('should not be able to save Comment instance if no name is provided', function(done) {
// Invalidate name field
comment.name = '';
agent.post('/auth/signin')
.send(credentials)
.expect(200)
.end(function(signinErr, signinRes) {
// Handle signin error
if (signinErr) done(signinErr);
// Get the userId
var userId = user.id;
// Save a new Comment
agent.post('/comments')
.send(comment)
.expect(400)
.end(function(commentSaveErr, commentSaveRes) {
// Set message assertion
(commentSaveRes.body.message).should.match('Please fill Comment name');
// Handle Comment save error
done(commentSaveErr);
});
});
});
it('should be able to update Comment instance if signed in', function(done) {
agent.post('/auth/signin')
.send(credentials)
.expect(200)
.end(function(signinErr, signinRes) {
// Handle signin error
if (signinErr) done(signinErr);
// Get the userId
var userId = user.id;
// Save a new Comment
agent.post('/comments')
.send(comment)
.expect(200)
.end(function(commentSaveErr, commentSaveRes) {
// Handle Comment save error
if (commentSaveErr) done(commentSaveErr);
// Update Comment name
comment.name = 'WHY YOU GOTTA BE SO MEAN?';
// Update existing Comment
agent.put('/comments/' + commentSaveRes.body._id)
.send(comment)
.expect(200)
.end(function(commentUpdateErr, commentUpdateRes) {
// Handle Comment update error
if (commentUpdateErr) done(commentUpdateErr);
// Set assertions
(commentUpdateRes.body._id).should.equal(commentSaveRes.body._id);
(commentUpdateRes.body.name).should.match('WHY YOU GOTTA BE SO MEAN?');
// Call the assertion callback
done();
});
});
});
});
it('should be able to get a list of Comments if not signed in', function(done) {
// Create new Comment model instance
var commentObj = new Comment(comment);
// Save the Comment
commentObj.save(function() {
// Request Comments
request(app).get('/comments')
.end(function(req, res) {
// Set assertion
res.body.should.be.an.Array.with.lengthOf(1);
// Call the assertion callback
done();
});
});
});
it('should be able to get a single Comment if not signed in', function(done) {
// Create new Comment model instance
var commentObj = new Comment(comment);
// Save the Comment
commentObj.save(function() {
request(app).get('/comments/' + commentObj._id)
.end(function(req, res) {
// Set assertion
res.body.should.be.an.Object.with.property('name', comment.name);
// Call the assertion callback
done();
});
});
});
it('should be able to delete Comment instance if signed in', function(done) {
agent.post('/auth/signin')
.send(credentials)
.expect(200)
.end(function(signinErr, signinRes) {
// Handle signin error
if (signinErr) done(signinErr);
// Get the userId
var userId = user.id;
// Save a new Comment
agent.post('/comments')
.send(comment)
.expect(200)
.end(function(commentSaveErr, commentSaveRes) {
// Handle Comment save error
if (commentSaveErr) done(commentSaveErr);
// Delete existing Comment
agent.delete('/comments/' + commentSaveRes.body._id)
.send(comment)
.expect(200)
.end(function(commentDeleteErr, commentDeleteRes) {
// Handle Comment error error
if (commentDeleteErr) done(commentDeleteErr);
// Set assertions
(commentDeleteRes.body._id).should.equal(commentSaveRes.body._id);
// Call the assertion callback
done();
});
});
});
});
it('should not be able to delete Comment instance if not signed in', function(done) {
// Set Comment user
comment.user = user;
// Create new Comment model instance
var commentObj = new Comment(comment);
// Save the Comment
commentObj.save(function() {
// Try deleting Comment
request(app).delete('/comments/' + commentObj._id)
.expect(401)
.end(function(commentDeleteErr, commentDeleteRes) {
// Set message assertion
(commentDeleteRes.body.message).should.match('User is not logged in');
// Handle Comment error error
done(commentDeleteErr);
});
});
});
afterEach(function(done) {
User.remove().exec();
Comment.remove().exec();
done();
});
});