Skip to content

Commit fa7c3b0

Browse files
author
Christopher Barnes
committed
adjust tests
1 parent 5b9dba9 commit fa7c3b0

File tree

5 files changed

+132
-130
lines changed

5 files changed

+132
-130
lines changed

controllers/topics.js

Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,7 @@ router.post("/new", (req, res) => {
2121
if (req.user) {
2222
const topic = new Topic(req.body);
2323
topic.author = req.user._id;
24-
topic.save()
25-
.then(topic => {
24+
topic.save().then(() => {
2625
return User.findById(req.user._id);
2726
})
2827
.then(user => {
@@ -31,8 +30,7 @@ router.post("/new", (req, res) => {
3130
// REDIRECT TO THE NEW POST
3231
res.json(topic)
3332
// res.redirect(`/topics/${topic.title}`);
34-
})
35-
.catch(err => {
33+
}).catch(err => {
3634
console.log(err.message);
3735
});
3836
} else {
@@ -54,6 +52,7 @@ router.put("/:title", (req, res) => {
5452
if (req.user) {
5553
Topic.findOneAndUpdate({ title: req.params.title }).then(topic => {
5654
topic.title = req.body.title;
55+
// topic.summary = req.body.summary;
5756
topic.save();
5857
res.json(topic);
5958
// res.status(200);
@@ -104,28 +103,21 @@ router.use('/:title/quizzes', quizRoutes);
104103

105104
// CREATE A QUIZ
106105
router.post("/:title/quizzes/new", (req, res) => {
107-
if (req.user) {
106+
if (!req.user) {
107+
return res.status(401); // UNAUTHORIZED
108+
} else {
108109
// INSTANTIATE INSTANCE OF MODEL
109110
const quiz = new Quiz(req.body);
111+
const topic = Topic.findOne({ title: req.params.title });
110112

111113
// SAVE INSTANCE OF Quiz MODEL TO DB
112-
quiz.save()
113-
.then(topic => {
114-
return Topic.findOne({ title: req.params.title });
115-
})
116-
.then(topic => {
117-
topic.quizzes.unshift(quiz);
118-
return topic.save();
119-
})
120-
.then(topic => {
121-
res.json(quiz);
114+
quiz.save().then(topic => {
115+
topic.quizzes.unshift(quiz)
116+
topic.save()
117+
res.json(quiz)
122118
// res.redirect(`/`);
123-
})
124-
.catch(err => {
125-
console.log(err);
126-
});
127-
} else {
128-
return res.status(401); // UNAUTHORIZED
119+
return document()
120+
}).catch(err => { console.log(err) });
129121
}
130122
});
131123

data/quiz-api-db.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ const url = process.env.MONGODB_URI;
66
mongoose.Promise = global.Promise;
77
mongoose.connect(
88
url,
9-
{ useUnifiedTopology: true, useNewUrlParser: true, useCreateIndex: true },
9+
{ useNewUrlParser: true, useFindAndModify: false, useCreateIndex: true, useUnifiedTopology: true},
1010
(err, db) => {
1111
assert.equal(null, err);
1212
console.log("Connected successfully to database");

test/auth.js

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,7 @@ describe('Auth', () => {
3535
});
3636

3737
// it("Should have base auth page", (done) => {
38-
// chai
39-
// .request(server)
38+
// chai.request(server)
4039
// .get("/auth")
4140
// .end((err, res) => {
4241
// if (err) {
@@ -48,8 +47,7 @@ describe('Auth', () => {
4847
// });
4948

5049
// it("Should have login page", (done) => {
51-
// chai
52-
// .request(server)
50+
// chai.request(server)
5351
// .get("/auth/login")
5452
// .end((err, res) => {
5553
// if (err) {
@@ -60,14 +58,14 @@ describe('Auth', () => {
6058
// });
6159
// });
6260

63-
it("should not be able to login if they have not registered", (done) => {
61+
it("Should not be able to login if they have not registered", (done) => {
6462
agent.post("/auth/login", { email: "[email protected]", password: "nope" }).end( (err, res) => {
6563
res.status.should.be.equal(401);
6664
done();
6765
});
6866
});
6967

70-
it('should be able to sign up', (done) => {
68+
it('Should be able to sign up for Auth', (done) => {
7169
chai.request(server)
7270
.post('/auth/sign-up')
7371
.send(sampleUser)
@@ -84,7 +82,7 @@ describe('Auth', () => {
8482
})
8583
})
8684

87-
it('should be able to log in', (done) => {
85+
it('Should be able to log in for Auth', (done) => {
8886
let user = new User(sampleUser)
8987
user.save().then(savedUser => {
9088
chai.request(server)
@@ -103,7 +101,7 @@ describe('Auth', () => {
103101
})
104102

105103
// logout
106-
it("should be able to logout", (done) => {
104+
it("Should be able to logout for Auth", (done) => {
107105
agent.get("/auth/logout").end( (err, res) => {
108106
// res.should.have.status(200);
109107
agent.should.not.have.cookie("jwtToken");

test/quizzes.js

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,9 @@ describe('Quizzes', () => {
6262
.then(res => {
6363
assert.equal(res.status, 200)
6464
assert.exists(res.body.jwtToken)
65-
// User.find({ username: 'quizzestest' }).then(result => {
66-
// assert.equal(result.length, 1)
67-
// })
65+
User.find({ username: 'quizzestest' }).then(result => {
66+
assert.equal(result.length, 1)
67+
})
6868
return done()
6969
}).catch(err => {return done(err)});
7070
})
@@ -102,7 +102,7 @@ describe('Quizzes', () => {
102102
// since we're not actually filling out a form
103103
.set("content-type", "application/x-www-form-urlencoded")
104104
// Make a request to create another
105-
.send(initTopic)
105+
.send(newTopic)
106106
.then( (res) => {
107107
Topic.estimatedDocumentCount()
108108
.then( (newCount) => {
@@ -127,26 +127,26 @@ describe('Quizzes', () => {
127127
});
128128

129129

130-
// it("Should be able to Update a Topic", (done) => {
131-
// agent.put("/topics/Test-Topic-Init")
132-
// .send(newTopic)
133-
// .end((err, res) => {
134-
// if (err) {return done(err)};
135-
// res.body.title.should.be.equal("Test-Topic-New");
136-
// res.status.should.be.equal(200);
137-
// return done();
138-
// });
139-
// });
140-
141-
142-
// it("Should be able to Delete a Topic", (done) => {
143-
// agent.delete("/topics/Test-Topic-New")
144-
// .end((err, res) => {
145-
// if (err) {return done(err)};
146-
// res.status.should.be.equal(200);
147-
// return done();
148-
// });
149-
// });
130+
it("Should be able to Update a Topic", (done) => {
131+
agent.put("/topics/Test-Topic-Init")
132+
.send(newTopic)
133+
.end((err, res) => {
134+
if (err) {return done(err)};
135+
res.body.title.should.be.equal("Test-Topic");
136+
res.status.should.be.equal(200);
137+
return done();
138+
});
139+
});
140+
141+
142+
it("Should be able to Delete a Topic", (done) => {
143+
agent.delete("/topics/Test-Topic-New")
144+
.end((err, res) => {
145+
if (err) {return done(err)};
146+
res.status.should.be.equal(200);
147+
return done();
148+
});
149+
});
150150

151151

152152
after( (done) => {

0 commit comments

Comments
 (0)