Skip to content

Commit 0e3d0fb

Browse files
committed
refactoring controllers and routes before testing
1 parent 0af8dd5 commit 0e3d0fb

File tree

6 files changed

+207
-29
lines changed

6 files changed

+207
-29
lines changed

lab-allie/controller/gallery-controller.js

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,15 @@ const Gallery = require('../models/gallery.js');
66

77
module.exports = exports = {};
88

9-
exports.addPicture = function(req, res) {
10-
if(!req) return createError(400, 'Invalid body');
9+
exports.addPicture = function(req) {
10+
console.log('Scott was here', req.body);
11+
if(!req.body.name) return Promise.reject(createError(400, 'Invalid name property'));
12+
if(!req.body.desc) return Promise.reject(createError(400, 'Invalid desc property'));
1113
req.body.userId = req.user._id;
1214

1315
new Gallery(req.body).save()
14-
.then(gallery => res.json(gallery))
15-
.catch(err => {
16-
console.log(err);
17-
res.status(err.status).send(err.message);
18-
});
16+
.then(gallery => gallery)
17+
.catch(err => Promise.reject(createError(400, err.message)));
1918
};
2019

2120
exports.getPicture = function(req, res, id, userId) {
@@ -34,15 +33,11 @@ exports.getPicture = function(req, res, id, userId) {
3433
exports.updatePicture = function(req, res, gallery, id) {
3534
if(!gallery) return Promise.reject(createError(400, 'ID required'));
3635

37-
Gallery.findOneAndUpdate(id, req.body, {new: true})
38-
.then(pic => res.json(pic))
39-
.catch(err => console.error(err));
36+
Gallery.findOneAndUpdate(id, req.body, {new: true});
4037
};
4138

4239
exports.deletePicture = function(req, res, id) {
4340
if(!id) return Promise.reject(createError(400, 'ID required'));
4441

45-
Gallery.findByIdAndRemove(id)
46-
.then(() => res.status(204).send())
47-
.catch(err => res.send(err));
42+
Gallery.findByIdAndRemove(id);
4843
};

lab-allie/controller/pic-controller.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ function s3UploadProm(params) {
3232
exports.uploadPic = function(req) {
3333
debug('#POST /gallery/:id/pic');
3434

35-
if(!req.file) return createError(400, 'Resouce required');
36-
if(!req.file.path) return createError(500, 'File not saved');
35+
if(!req.file) return Promise.reject(createError(400, 'Resouce required'));
36+
if(!req.file.path) return Promise.reject(createError(500, 'File not saved'));
3737

3838
let ext = path.extname(req.file.originalname);
3939

@@ -64,6 +64,8 @@ exports.uploadPic = function(req) {
6464
};
6565

6666
exports.deletePic = function(picid) {
67+
if(!picid) return Promise.reject(createError(400, 'Pic ID required'));
68+
6769
debug('#DELETE /gallery/:id/pic/:id');
6870
console.log('req', picid);
6971
// if(!req.file) return Promise.reject(createError(400, 'Resouce required'));

lab-allie/controller/user-controller.js

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ const User = require('../models/user.js');
66

77
module.exports = exports = {};
88

9-
exports.signUp = function(req, res) {
9+
exports.signUp = function(req) {
10+
if(!req.body.password) return Promise.reject(createError(400, 'Invalid password'));
11+
1012
let tempPassword = null;
1113
tempPassword = req.body.password;
1214
req.body.password = null;
@@ -18,16 +20,16 @@ exports.signUp = function(req, res) {
1820
.then(user => user.save())
1921
.then(user => {
2022
return user.generateToken();
21-
})
22-
.then(token => {
23-
res.json(token);
24-
})
25-
.catch(err => createError(404, err.message));
23+
});
2624
};
2725

28-
exports.signIn = function(reqAuth, res) {
29-
return User.findOne({username: reqAuth.username})
30-
.then(user => user.comparePasswordHash(reqAuth.password))
26+
exports.signIn = function(req) {
27+
if(!req.body.username) return Promise.reject(createError(400, 'Invalid username'));
28+
29+
if(!req.body.password) return Promise.reject(createError(400, 'Invalid password'));
30+
31+
return User.findOne({username: req.body.username})
32+
.then(user => user.comparePasswordHash(req.body.password))
3133
.then(user => user.generateToken())
3234
.then(token => token)
3335
.catch(err => createError(404, err.message));

lab-allie/routes/gallery-routes.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ const debug = require('debug')('cfgram:user-model');
77
module.exports = function(router) {
88
router.post('/gallery', bearerAuth, (req, res) => {
99
debug('#POST /api/gallery');
10-
galleryCtrl.addPicture(req, res);
10+
galleryCtrl.addPicture(req)
11+
.then(gallery => res.json(gallery))
12+
.catch(err => res.status(err.status).send(err.message));
1113
});
1214

1315
router.get('/gallery/:id', bearerAuth, (req, res) => {
@@ -22,12 +24,16 @@ module.exports = function(router) {
2224

2325
router.put('/gallery/:id', bearerAuth, (req, res) => {
2426
debug('#PUT /api/gallery/:id');
25-
galleryCtrl.updatePicture(req);
27+
galleryCtrl.updatePicture(req)
28+
.then(pic => res.json(pic))
29+
.catch(err => console.error(err));
2630
});
2731

2832
router.delete('/gallery/:id', bearerAuth, (req, res) => {
2933
debug('#DELETE /api/gallery/:id');
30-
galleryCtrl.deletePicture(req, res, req.params.id);
34+
galleryCtrl.deletePicture(req, res, req.params.id)
35+
.then(() => res.status(204).send())
36+
.catch(err => res.send(err));
3137
});
3238

3339
return router;

lab-allie/routes/user-routes.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,20 @@
11
'use strict';
22

3+
const createError = require('http-errors');
34
const userCtrl = require('../controller/user-controller.js');
45
const basicAuth = require('../lib/basic-auth-middleware.js');
56

67
module.exports = function(router) {
78
router.post('/signup', (req, res) => {
8-
userCtrl.signUp(req, res);
9+
userCtrl.signUp(req)
10+
.then(token => {
11+
res.json(token);
12+
})
13+
.catch(err => createError(404, err.message));
914
});
1015

1116
router.get('/signin', basicAuth, (req, res) => {
12-
userCtrl.signIn(req.auth)
17+
userCtrl.signIn(req)
1318
.then(token => res.json(token))
1419
.catch(err => res.status(err.status).send(err));
1520
});

lab-allie/test/gallery-test.js

Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
1+
'use strict';
2+
3+
const expect = require('chai').expect;
4+
const request = require('superagent');
5+
const mongoose = require('mongoose');
6+
const Promise = require('bluebird');
7+
8+
const User = require('../models/user.js');
9+
const Gallery = require('../models/gallery.js');
10+
11+
require('../server');
12+
13+
const url = `http://localhost:${process.env.PORT}`;
14+
15+
const testUser = {
16+
username: 'testy',
17+
password: 'abc123',
18+
email: 'fake@fake.com',
19+
};
20+
21+
const testGallery = {
22+
name: 'test name',
23+
desc: 'test description',
24+
};
25+
26+
mongoose.Promise = Promise;
27+
28+
describe('Gallery routes', function() {
29+
afterEach(done => {
30+
Promise.all([
31+
User.remove({}),
32+
Gallery.remove({}),
33+
])
34+
.then(() => done())
35+
.catch(() => done());
36+
});
37+
38+
describe('POST tests', function() {
39+
before(done => {
40+
new User(testUser)
41+
.generatePasswordHash(testUser.password)
42+
.then(user => user.save())
43+
.then(user => {
44+
this.tempUser = user;
45+
console.log('temporary user', this.tempUser);
46+
return user.generateToken();
47+
})
48+
.then(token => {
49+
console.log('test token', token);
50+
this.tempToken = token;
51+
done();
52+
})
53+
.catch(() => done());
54+
});
55+
56+
it('should return a gallery', done => {
57+
request.post(`${url}/api/gallery`)
58+
.send(testGallery)
59+
.set({
60+
Authorization: `Bearer ${this.tempToken}`,
61+
})
62+
.end((err, res) => {
63+
if(err) return done(err);
64+
let date = new Date(res.body.created).toString();
65+
expect(res.body.name).to.equal(testGallery.name);
66+
expect(res.body.desc).to.equal(testGallery.desc);
67+
expect(res.body.userId).to.equal(this.tempUser._id.toString());
68+
expect(date).to.not.equal('Invalid Date');
69+
expect(res.status).to.equal(200);
70+
done();
71+
});
72+
});
73+
74+
it('should thrown an error if not given a token', done => {
75+
request.post(`${url}/api/gallery`)
76+
.send(testGallery)
77+
.set({
78+
Authorization: `Bearer `,
79+
})
80+
.end((err, res) => {
81+
expect(res.status).to.equal(401);
82+
done();
83+
});
84+
});
85+
86+
it.only('should return a "bad request" error if not given a correct body', done => {
87+
request.post(`${url}/api/gallery`)
88+
.send({name:''})
89+
.set({
90+
Authorization: `Bearer ${this.tempToken}`,
91+
})
92+
.end((err, res) => {
93+
expect(res.status).to.equal(400); //put the expect in a catch
94+
done();
95+
});
96+
});
97+
});
98+
99+
describe('GET routes', function() {
100+
before(done => {
101+
new User(testUser)
102+
.generatePasswordHash(testUser.password)
103+
.then(user => user.save())
104+
.then(user => {
105+
this.tempUser = user;
106+
return user.generateToken();
107+
})
108+
.then(token => {
109+
this.tempToken = token;
110+
done();
111+
})
112+
.catch(() => done());
113+
});
114+
115+
before(done => {
116+
testGallery.userId = this.tempUser._id.toString();
117+
new Gallery(testGallery).save()
118+
.then(gallery => {
119+
this.tempGallery = gallery;
120+
done();
121+
})
122+
.catch(() => done());
123+
});
124+
125+
after(() => {
126+
delete testGallery.userId;
127+
});
128+
129+
it('should return a gallery', done => {
130+
request.get(`${url}/api/gallery/${this.tempGallery._id}`)
131+
.set({
132+
Authorization: `Bearer ${this.tempToken}`,
133+
})
134+
.end((err, res) => {
135+
if (err) return done(err);
136+
let date = new Date(res.body.created).toString();
137+
expect(res.body.name).to.equal(testGallery.name);
138+
expect(res.body.desc).to.equal(testGallery.desc);
139+
expect(res.body.userId).to.equal(this.tempUser._id.toString());
140+
expect(date).to.not.equal('Invalid Date');
141+
expect(res.status).to.equal(200);
142+
done();
143+
});
144+
});
145+
146+
it('should throw an error if given the wrong credentials', done => {
147+
request.get(`${url}/api/gallery/${this.tempGallery._id}`)
148+
.set({
149+
Authorization: `Bearer `,
150+
})
151+
.end((err, res) => {
152+
expect(res.status).to.equal(401);
153+
done();
154+
});
155+
});
156+
157+
it('should throw an error if given an invalid id', done => {
158+
request.get(`${url}/api/gallery/`)
159+
.set({
160+
Authorization: `Bearer ${this.tempToken}`,
161+
})
162+
.end((err, res) => {
163+
expect(res.status).to.equal(404);
164+
done();
165+
});
166+
});
167+
});
168+
});

0 commit comments

Comments
 (0)