1 | // During the rest the en variable is set to test |
||
2 | /* global describe it beforeEach */ |
||
3 | process.env.NODE_ENV = 'test'; |
||
4 | |||
5 | const Author = require('../app/models/author'); |
||
6 | |||
7 | // Require the dev-dependencies |
||
8 | const chai = require('chai'); |
||
9 | const chaiHttp = require('chai-http'); |
||
10 | const server = require('../server'); |
||
11 | |||
12 | const should = chai.should(); |
||
13 | |||
14 | chai.use(chaiHttp); |
||
15 | |||
16 | // Out parent block |
||
17 | |||
18 | describe('Authors', () => { |
||
19 | beforeEach((done) => { |
||
20 | Author.remove({}, (err) => { |
||
0 ignored issues
–
show
|
|||
21 | done(); |
||
22 | }); |
||
23 | }); |
||
24 | |||
25 | /** |
||
26 | * Test the GET /api/authors |
||
27 | */ |
||
28 | describe('GET /api/authors', () => { |
||
29 | it('it should GET all the authors', (done) => { |
||
30 | chai.request(server) |
||
31 | .get('/api/authors') |
||
32 | .end((err, res) => { |
||
33 | res.should.have.status(200); |
||
34 | res.body.should.be.a('array'); |
||
35 | res.body.length.should.be.eql(0); |
||
36 | done(); |
||
37 | }); |
||
38 | }); |
||
39 | }); |
||
40 | |||
41 | /** |
||
42 | * Test the POST /api/authors |
||
43 | */ |
||
44 | describe('POST /api/authors', () => { |
||
45 | it('it should not POST an author without name field', (done) => { |
||
46 | const author = { |
||
47 | birth_year: 1978, |
||
48 | bio: 'JavaScript Lover', |
||
49 | country: 'US' |
||
50 | }; |
||
51 | chai.request(server) |
||
52 | .post('/api/authors') |
||
53 | .send(author) |
||
54 | .end((err, res) => { |
||
55 | res.should.have.status(400); |
||
56 | res.body.should.be.a('object'); |
||
57 | res.body.should.have.property('errors'); |
||
58 | res.body.errors.should.have.property('name'); |
||
59 | res.body.errors.name.should.have.property('kind').eql('required'); |
||
60 | done(); |
||
61 | }); |
||
62 | }); |
||
63 | |||
64 | it('it should POST author', (done) => { |
||
65 | const author = { |
||
66 | name: 'Author name', |
||
67 | birth_year: 1978, |
||
68 | bio: 'JavaScript Lover', |
||
69 | country: 'US' |
||
70 | }; |
||
71 | chai.request(server) |
||
72 | .post('/api/authors') |
||
73 | .send(author) |
||
74 | .end((err, res) => { |
||
75 | res.should.have.status(201); |
||
76 | res.body.should.be.a('object'); |
||
77 | res.body.should.have.property('message', 'Author successfully added!'); |
||
78 | res.body.should.have.property('author'); |
||
79 | res.body.author.should.be.a('object'); |
||
80 | res.body.author.should.have.property('name'); |
||
81 | res.body.author.should.have.property('birth_year'); |
||
82 | res.body.author.should.have.property('bio'); |
||
83 | res.body.author.should.have.property('country'); |
||
84 | done(); |
||
85 | }); |
||
86 | }); |
||
87 | }); |
||
88 | |||
89 | /** |
||
90 | * Test The GET /api/authors/:id route |
||
91 | */ |
||
92 | describe('GET /api/authors/:id', () => { |
||
93 | it('It Should GET a author by given its id', (done) => { |
||
94 | const author = new Author({ |
||
95 | name: 'Author name', |
||
96 | birth_year: 1978, |
||
97 | bio: 'JavaScript Lover', |
||
98 | country: 'US' |
||
99 | }); |
||
100 | author.save((err, author) => { |
||
101 | chai.request(server) |
||
102 | .get(`/api/authors/${author.id}`) |
||
103 | .end((err, res) => { |
||
104 | res.should.have.status(200); |
||
105 | res.body.should.be.a('object'); |
||
106 | res.body.should.have.property('name'); |
||
107 | res.body.should.have.property('birth_year'); |
||
108 | res.body.should.have.property('bio'); |
||
109 | res.body.should.have.property('country'); |
||
110 | res.body.should.have.property('_id', author.id); |
||
111 | done(); |
||
112 | }); |
||
113 | }); |
||
114 | }); |
||
115 | |||
116 | it('It Should GET an error if the author ID is invalid', (done) => { |
||
117 | chai.request(server) |
||
118 | .get('/api/authors/90') |
||
119 | .end((err, res) => { |
||
120 | res.should.have.status(400); |
||
121 | res.body.should.be.a('object'); |
||
122 | res.body.should.have.property('message'); |
||
123 | res.body.should.have.property('name'); |
||
124 | res.body.should.have.property('kind', 'ObjectId'); |
||
125 | res.body.should.have.property('value', '90'); |
||
126 | res.body.should.have.property('path', '_id'); |
||
127 | done(); |
||
128 | }); |
||
129 | }); |
||
130 | |||
131 | it('it should GET not found if the author ID don\'t exist', (done) => { |
||
132 | chai.request(server) |
||
133 | .get('/api/authors/589e02e559f531603fe40322') |
||
134 | .end((err, res) => { |
||
135 | res.should.have.status(404); |
||
136 | res.body.should.be.a('object'); |
||
137 | res.body.should.have.property('message').eql('Author not found!'); |
||
138 | res.body.should.have.property('id').eql('589e02e559f531603fe40322'); |
||
139 | done(); |
||
140 | }); |
||
141 | }); |
||
142 | |||
143 | }); |
||
144 | |||
145 | /** |
||
146 | * Test the PUT /api/authors/:id Route |
||
147 | */ |
||
148 | describe('PUT /api/authors/:id', () => { |
||
149 | it('it should UPDATE an author given its id', (done) => { |
||
150 | const author = new Author({ |
||
151 | name: 'Author name', |
||
152 | birth_year: 1988, |
||
153 | bio: 'JavaScript Lover', |
||
154 | country: 'US' |
||
155 | }); |
||
156 | author.save((err, author) => { |
||
157 | chai.request(server) |
||
158 | .put(`/api/authors/${author.id}`) |
||
159 | .send({ |
||
160 | birth_year: 1972 |
||
161 | }) |
||
162 | .end((err, res) => { |
||
163 | res.should.have.status(200); |
||
164 | res.body.should.be.a('object'); |
||
165 | res.body.should.have.property('message').eql('Author updated!'); |
||
166 | res.body.author.should.have.property('birth_year').eql(1972); |
||
167 | done(); |
||
168 | }); |
||
169 | }); |
||
170 | }); |
||
171 | |||
172 | it('it should return an error if the author birth_year is invalid', (done) => { |
||
173 | const author = new Author({ |
||
174 | name: 'Author name', |
||
175 | birth_year: 1988, |
||
176 | bio: 'JavaScript Lover', |
||
177 | country: 'US' |
||
178 | }); |
||
179 | author.save((err, author) => { |
||
180 | chai.request(server) |
||
181 | .put(`/api/authors/${author.id}`) |
||
182 | .send({ |
||
183 | birth_year: '1952a' |
||
184 | }) |
||
185 | .end((err, res) => { |
||
186 | res.should.have.status(500); |
||
187 | res.body.should.be.a('object'); |
||
188 | res.body.should.have.property('errors'); |
||
189 | res.body.errors.should.have.property('birth_year'); |
||
190 | res.body.errors.birth_year.should.have.property('kind').eql('Number'); |
||
191 | done(); |
||
192 | }); |
||
193 | }); |
||
194 | }); |
||
195 | |||
196 | it('it should return an error if the author ID is not valid', (done) => { |
||
197 | chai.request(server) |
||
198 | .put('/api/authors/90') |
||
199 | .send({ |
||
200 | birth_year: 2009 |
||
201 | }) |
||
202 | .end((err, res) => { |
||
203 | res.should.have.status(400); |
||
204 | res.body.should.be.a('object'); |
||
205 | res.body.should.have.property('message'); |
||
206 | res.body.should.have.property('name'); |
||
207 | res.body.should.have.property('kind').eql('ObjectId'); |
||
208 | res.body.should.have.property('value').eql('90'); |
||
209 | res.body.should.have.property('path').eql('_id'); |
||
210 | done(); |
||
211 | }); |
||
212 | }); |
||
213 | |||
214 | it('it should return not found if the author ID don\'t exist', (done) => { |
||
215 | chai.request(server) |
||
216 | .put('/api/authors/589e02e559f531603fe40322') |
||
217 | .send({ |
||
218 | year: 2010 |
||
219 | }) |
||
220 | .end((err, res) => { |
||
221 | res.should.have.status(404); |
||
222 | res.body.should.be.a('object'); |
||
223 | res.body.should.have.property('message').eql('Author not found!'); |
||
224 | res.body.should.have.property('id').eql('589e02e559f531603fe40322'); |
||
225 | done(); |
||
226 | }); |
||
227 | }); |
||
228 | }); |
||
229 | |||
230 | /** |
||
231 | * Test the DELETE /api/authors/:id |
||
232 | */ |
||
233 | describe('DELETE /api/authors/:id', () => { |
||
234 | it('it should DELETE the author given its id', (done) => { |
||
235 | const author = new Author({ |
||
236 | name: 'Author name', |
||
237 | birth_year: 1988, |
||
238 | bio: 'JavaScript Lover', |
||
239 | country: 'US' |
||
240 | }); |
||
241 | author.save((err, author) => { |
||
242 | chai.request(server) |
||
243 | .delete(`/api/authors/${author.id}`) |
||
244 | .end((err, res) => { |
||
245 | res.should.have.status(200); |
||
246 | res.body.should.be.a('object'); |
||
247 | res.body.should.have.property('message').eql('Author successfully deleted!'); |
||
248 | res.body.result.should.have.property('ok').eql(1); |
||
249 | res.body.result.should.have.property('n').eql(1); |
||
250 | done(); |
||
251 | }); |
||
252 | }); |
||
253 | }); |
||
254 | it('it should return an error if the author ID is not valid', (done) => { |
||
255 | chai.request(server) |
||
256 | .delete('/api/authors/90') |
||
257 | .end((err, res) => { |
||
258 | res.should.have.status(400); |
||
259 | res.body.should.be.a('object'); |
||
260 | res.body.should.have.property('message'); |
||
261 | res.body.should.have.property('name'); |
||
262 | res.body.should.have.property('kind').eql('ObjectId'); |
||
263 | res.body.should.have.property('value').eql('90'); |
||
264 | res.body.should.have.property('path').eql('_id'); |
||
265 | done(); |
||
266 | }); |
||
267 | }); |
||
268 | |||
269 | it('it should return not found if the author ID don\'t exist', (done) => { |
||
270 | chai.request(server) |
||
271 | .delete('/api/authors/589e02e559f531603fe40322') |
||
272 | .end((err, res) => { |
||
273 | res.should.have.status(404); |
||
274 | res.body.should.be.a('object'); |
||
275 | res.body.should.have.property('message').eql('Author not found!'); |
||
276 | res.body.should.have.property('id').eql('589e02e559f531603fe40322'); |
||
277 | done(); |
||
278 | }); |
||
279 | }); |
||
280 | }); |
||
281 | |||
282 | }); |
This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.