Skip to content

Commit 53fa350

Browse files
MOre User Tests
1 parent 897818f commit 53fa350

6 files changed

Lines changed: 140 additions & 43 deletions

File tree

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"babel-node": "babel-node --presets=es2015",
88
"nodemon": "nodemon -e pug,js,css --exec npm run babel-node -- src/bin/www",
99
"start": "SET NODE_ENV=dev && npm run nodemon",
10-
"test": "mocha --timeout 20000 --compilers js:babel-register --recursive ./test/"
10+
"test": "mocha --timeout 5000 --compilers js:babel-register --recursive ./test/"
1111
},
1212
"author": "",
1313
"license": "ISC",

src/app.js

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,6 @@ const app = express();
2525
//mongoDb Connection
2626
mongoose.connect(dbConfig);
2727

28-
29-
30-
3128
const db = mongoose.connection;
3229
db.on('error', console.error.bind(console, 'connection error:'));
3330

@@ -50,7 +47,6 @@ db.on('connected', function() {
5047
});
5148
});
5249

53-
5450
// view engine setup
5551
app.set('views', path.join(__dirname, 'views'));
5652
app.set('view engine', 'pug');
@@ -82,9 +78,9 @@ app.use(function(err, req, res, next) {
8278
res.locals.message = err.message;
8379
res.locals.error = req.app.get('env') === 'development' ? err : {};
8480

81+
let status = err.status || 500;
8582
// render the error page
86-
res.status(err.status || 500);
87-
res.render('error');
83+
res.status(status).json({message: err.message});
8884
});
8985

9086
module.exports = app; //This for testing...

src/models/user.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ UserSchema.statics.authenticate = function(email, password, callback) {
4848
/*
4949
May need some logic here later to check if email in correct format.
5050
*/
51-
UserSchema.statics.validEmail = function(email, callback) {
51+
UserSchema.statics.validEmail = function(email) {
5252
email = email + '';
5353
return isEmail(email);
5454
};

src/routes/index.js

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -18,39 +18,34 @@ router.post('/course', (req, res, next) => {
1818
const err = new Error("Bad Request");
1919
err.status = 400;
2020
return next(err);
21-
//return res.json({body: req.body});
2221
}
23-
24-
const courseData = {
25-
user: req.body.user._id,
26-
title: req.body.title,
27-
description: req.body.description,
28-
estimatedTime: req.body.estimatedTime,
29-
materialsNeeded: req.body.materialsNeeded,
30-
steps: req.body.steps,
31-
reviews: req.body.reviews
32-
};
33-
console.log(courseData);
22+
const courseData = req.body;
3423
Course.create(courseData, (err, course) => {
35-
if(err){
36-
console.log("hitting error?");
37-
return next(err);
24+
if(err){
25+
return next(err);
3826
}
39-
console.log("This shit ever get hit?");
40-
return res.json({message: "Course Successfully added!", course});
27+
return res.status(201).json({message: "User Successfully added!", status: 201, course});
4128
});
4229
});
4330

44-
4531
router.post('/user', (req, res, next) => {
46-
console.log("hit the request...");
32+
if(!req.body.emailAddress || !req.body.fullName || !req.body.password){
33+
const err = new Error("Missing Parameters");
34+
err.status = 422;
35+
return next(err);
36+
}
37+
if(!User.validEmail(req.body.emailAddress)){
38+
const err = new Error("Malformed Email Supplied");
39+
err.status = 400;
40+
return next(err);
41+
}
42+
4743
const userData = req.body;
4844
User.create(userData, (err, user) => {
49-
console.log("got into the create method?!?!");
5045
if (err) {
5146
return next(err);
5247
}
53-
return res.json({message: "User Successfully added!", user});
48+
return res.status(201).json({message: "User Successfully added!", status: 201, user});
5449
});
5550

5651
});

test/course_test.js

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,11 @@ const should = chai.should();
1111
chai.use(chaiHttp);
1212

1313
describe('Courses', () => {
14-
//This was causing hanging errors....
15-
// beforeEach((done) => {
16-
// Course.remove({}, (err) => {
17-
// done();
18-
// });
19-
// });
14+
beforeEach((done) => {
15+
Course.remove({}, (err) => {
16+
done();
17+
});
18+
});
2019

2120
/*
2221
Our GET Tests
@@ -53,23 +52,20 @@ describe('Courses', () => {
5352
.post('/course')
5453
.send(course)
5554
.end((err, res) => {
56-
res.should.have.status(200);
55+
res.should.have.status(201);
5756
res.body.should.be.a('object');
5857
res.body.course.should.have.property('title');
59-
6058
done();
6159
});
62-
63-
6460
});
6561

6662

67-
it('should not POST a course without no fields', (done) => {
63+
it('should not POST a course with no fields', (done) => {
6864
let course = {};
6965
postCourse(course, done);
7066
});
7167

72-
it('should not POST a course without title field', (done) => {
68+
it('should not POST a course missing title field', (done) => {
7369
let course = {
7470
description: "My course description",
7571
user: {
@@ -85,7 +81,7 @@ describe('Courses', () => {
8581
postCourse(course, done);
8682
});
8783

88-
it('should not POST a course without description field', (done) => {
84+
it('should not POST a course missing description field', (done) => {
8985
let course = {
9086
title: "my title",
9187
user: {
@@ -106,7 +102,6 @@ describe('Courses', () => {
106102
.send(course)
107103
.end((err, res) => {
108104
res.should.have.status(400);
109-
110105
done();
111106
});
112107
}

test/user_test.js

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
//During the test the env variable is set to test
2+
process.env.NODE_ENV = 'test';
3+
4+
const User = require('../src/models/user');
5+
6+
const chai = require('chai');
7+
const chaiHttp = require('chai-http');
8+
const server = require('../src/app');
9+
const should = chai.should();
10+
11+
const postAPI = '/user';
12+
13+
14+
describe('Users', () => {
15+
beforeEach((done) => {
16+
User.remove({}, (err) => {
17+
done();
18+
});
19+
});
20+
21+
22+
describe('/POST user', () => {
23+
let user400 = {
24+
emailAddress: 'wre23sd',
25+
password: 'password',
26+
fullName: 'jerry springer'
27+
};
28+
29+
it('should not POST if email address supplied is invalid email address "wre23sd"', (done) => {
30+
user400.email = "wre23sd";
31+
postUser400(user400, done);
32+
});
33+
it('should not POST if email address supplied is invalid email address "garbage@moreGarbage."', (done) => {
34+
user400.email = "garbage@moreGarbage.";
35+
postUser400(user400, done);
36+
});
37+
it('should not POST if email address supplied is invalid email address ""test@owiejadskla""', (done) => {
38+
user400.email = "test@owiejadskla";
39+
postUser400(user400, done);
40+
});
41+
42+
43+
44+
it('should not POST if email Address already exists in DB', (done) => {
45+
let user = {
46+
emailAddress: 'test@gmail.com',
47+
password: 'password',
48+
fullName: 'jerry springer'
49+
};
50+
chai.request(server)
51+
.post(postAPI)
52+
.send(user)
53+
.end((err, res) => {
54+
res.should.have.status(201);
55+
chai.request(server)
56+
.post(postAPI)
57+
.send(user)
58+
.end((err, res) => {
59+
err.message.should.be.equal('test');
60+
res.should.have.status(500);
61+
done();
62+
});
63+
});
64+
});
65+
66+
it('should not POST a user supplied with no data', (done) => {
67+
let user = {};
68+
postUser(user, done);
69+
});
70+
it('should not POST a user missing email address', (done) => {
71+
let user = {
72+
password: 'password',
73+
fullName: 'jerry springer'
74+
};
75+
postUser(user, done);
76+
});
77+
it('should not POST a user missing Full Name', (done) => {
78+
let user = {
79+
emailAddress: 'test@gmail.com',
80+
password: 'password'
81+
};
82+
postUser(user, done);
83+
});
84+
it('should not POST a user missing Password', (done) => {
85+
let user = {
86+
emailAddress: 'test@gmail.com',
87+
fullName: 'jerry springer'
88+
};
89+
postUser(user, done);
90+
});
91+
function postUser(user, done){
92+
chai.request(server)
93+
.post(postAPI)
94+
.send(user)
95+
.end((err, res) => {
96+
res.should.have.status(422);
97+
done();
98+
});
99+
}
100+
function postUser400(user, done ) {
101+
chai.request(server)
102+
.post(postAPI)
103+
.send(user)
104+
.end((err, res) => {
105+
res.should.have.status(400);
106+
res.body.should.have.property('message').equal("Malformed Email Supplied");
107+
done();
108+
});
109+
}
110+
});
111+
});

0 commit comments

Comments
 (0)