Skip to content
This repository was archived by the owner on Aug 30, 2021. It is now read-only.

Commit a799a23

Browse files
committed
Merge pull request #189 from w-green/adding_server_route_tests
Feature: Article CRUD ops. Added supertests. closes #92
2 parents cc1c124 + 2ec6fa0 commit a799a23

File tree

1 file changed

+219
-0
lines changed

1 file changed

+219
-0
lines changed
Lines changed: 219 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,219 @@
1+
'use strict';
2+
3+
var should = require('should'),
4+
request = require('supertest'),
5+
app = require('../../server'),
6+
mongoose = require('mongoose'),
7+
User = mongoose.model('User'),
8+
Article = mongoose.model('Article'),
9+
// note: the agent allows us to preserve a session and cookies for the logged in user
10+
agent = request.agent(app);
11+
12+
13+
/**
14+
* Globals
15+
*/
16+
var user, article;
17+
18+
describe('Article CRUD tests', function () {
19+
20+
beforeEach(function(done) {
21+
// saves a user to the test db.
22+
user = new User({
23+
firstName: 'Full',
24+
lastName: 'Name',
25+
displayName: 'Full Name',
26+
27+
username: 'username',
28+
password: 'password',
29+
provider: 'local'
30+
});
31+
user.save();
32+
33+
article = new Article({
34+
title: 'Article Title',
35+
content: 'Article Content',
36+
user: user
37+
});
38+
39+
done();
40+
});
41+
42+
// ------------ CREATE (SAVE) operations ------------
43+
it('should be able to save an article if logged in', function (done) {
44+
agent
45+
.post('/auth/signin')
46+
.send(user)
47+
.end(function (err, res){
48+
var userId = res.body._id;
49+
agent
50+
.post('/articles')
51+
.send(article)
52+
.expect(200)
53+
.end(function (err, res) {
54+
agent
55+
.get('/articles')
56+
.end(function(error, res) {
57+
(res.body[0].user._id).should.equal(userId);
58+
(res.body[0].title).should.match('Article Title');
59+
done();
60+
});
61+
});
62+
});
63+
});
64+
65+
66+
it('should not be able to save an article if not logged in', function (done) {
67+
agent
68+
.post('/articles')
69+
.send(article)
70+
.expect(200)
71+
.end(function (err, res) {
72+
should.exist(err);
73+
(res.status).should.equal(401);
74+
(res.unauthorized).should.equal(true);
75+
done();
76+
});
77+
});
78+
79+
it('should not be able to save an article if no title is provided', function (done) {
80+
article.title = '';
81+
agent
82+
.post('/auth/signin')
83+
.send(user)
84+
.end(function (err, res){
85+
agent
86+
.post('/articles')
87+
.send(article)
88+
.expect(200)
89+
.end(function (err, res) {
90+
should.exist(err);
91+
(res.body.message).should.match('Title cannot be blank');
92+
done();
93+
});
94+
});
95+
});
96+
97+
98+
// ------------ PUT (UPDATE) operations ------------
99+
it('should be able to update an article if signed in', function(done){
100+
agent
101+
.post('/auth/signin')
102+
.send(user)
103+
.end(function (err, res){
104+
var userId = res.body._id;
105+
agent
106+
.post('/articles')
107+
.send(article)
108+
.expect(200)
109+
.end(function (err, res) {
110+
agent
111+
.get('/articles')
112+
.end(function(error, res) {
113+
(res.body[0].user._id).should.equal(userId);
114+
(res.body[0].title).should.match('Article Title');
115+
var article_Id = res.body[0]._id;
116+
article.title = 'WHY YOU GOTTA BE SO MEAN?';
117+
agent
118+
.put('/articles/' + article_Id)
119+
.send(article)
120+
.end(function (err, res) {
121+
(res.body._id).should.equal(article_Id);
122+
(res.body.title).should.match('WHY YOU GOTTA BE SO MEAN?');
123+
done();
124+
});
125+
});
126+
});
127+
});
128+
});
129+
130+
// ------------ READ operations ------------
131+
it('should be able to get a list of articles if not signed in', function (done) {
132+
// Adding 10 articles to the db
133+
var numArticles = 10;
134+
while (numArticles--) {
135+
var article = new Article({
136+
title: numArticles + ' Another Article Title',
137+
content: numArticles + ' Another Article Content',
138+
user: user
139+
});
140+
article.save();
141+
}
142+
143+
// note there is no need to use the supertest agent here as we do not need to be signed in
144+
request(app)
145+
.get('/articles')
146+
.end(function (req, res) {
147+
(res.body[0].title).should.match('0 Another Article Title');
148+
done();
149+
});
150+
151+
});
152+
153+
154+
it('should be able to get a single article if not signed in', function (done) {
155+
var article = new Article({
156+
title: 'Another Article Title',
157+
content: 'Another Article Content',
158+
user: user
159+
});
160+
article.save();
161+
162+
request(app)
163+
.get('/articles/' + article._id)
164+
.end(function (req, res) {
165+
(res.body.title).should.match('Another Article Title');
166+
done();
167+
});
168+
169+
});
170+
171+
// ------------ DELETE operations ------------
172+
it('should be able to delete an article if signed in', function (done) {
173+
var article = new Article({
174+
title: 'Another Article Title',
175+
content: 'Another Article Content',
176+
user: user
177+
});
178+
article.save();
179+
180+
agent
181+
.post('/auth/signin')
182+
.send(user)
183+
.end(function (err, res) {
184+
(err === null).should.equal(true);
185+
agent
186+
.delete('/articles/' + article._id)
187+
.end(function (err, res) {
188+
(res.req.method).should.match('DELETE');
189+
(res.body.title).should.match('Another Article Title');
190+
done();
191+
});
192+
});
193+
});
194+
195+
it('should not be able to delete an article if not signed in', function (done) {
196+
var article = new Article({
197+
title: 'Another Article Title',
198+
content: 'Another Article Content',
199+
user: user
200+
});
201+
article.save();
202+
203+
request(app)
204+
.delete('/articles/' + article._id)
205+
.end(function (err, res) {
206+
(res.req.method).should.match('DELETE');
207+
(res.status).should.equal(401);
208+
(res.unauthorized).should.equal(true);
209+
done();
210+
});
211+
});
212+
213+
afterEach(function(done) {
214+
User.remove().exec();
215+
Article.remove().exec();
216+
done();
217+
});
218+
219+
});

0 commit comments

Comments
 (0)