Skip to content

Commit

Permalink
chore: Tests refactored and updated to recognise new routes
Browse files Browse the repository at this point in the history
  • Loading branch information
Benny Ogidan authored and Benny Ogidan committed Nov 13, 2017
1 parent e055f16 commit f559f3f
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 21 deletions.
6 changes: 3 additions & 3 deletions server/src/controllers/userbooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export default {
* @memmberOf UserBooks Controller
*/
loanbook(req, res) {
req.params.userId = req.user.id.id;
req.params.userId = req.user.id.id || req.user.id;
if (!req.body.returndate) {
return res.status(404).send({ message: 'Please specify a valid return date' });
}
Expand Down Expand Up @@ -95,7 +95,7 @@ export default {
getborrowedBooklist(req, res) {
const offset = req.query.offset || 0;
const limit = req.query.limit || 3;
req.params.userId = req.user.id.id;
req.params.userId = req.user.id.id || req.user.id;
if (!req.query.returned) {
return res
.status(404).send({ message: 'Please specify a value for returned books' });
Expand Down Expand Up @@ -137,7 +137,7 @@ export default {
* @memmberOf UserBooks Controller
*/
returnbook(req, res) {
req.params.userId = req.user.id.id;
req.params.userId = req.user.id.id || req.user.id;
UserBooks.findOne({
where: {
bookid: req.body.bookId,
Expand Down
36 changes: 18 additions & 18 deletions server/src/test/userbooks.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,14 +99,14 @@ describe('HelloBooks', () => {
});

describe('/POST loan a book', () => {
it('should allow a logged in user to loan a book', (done) => {
it('should allow an authenticated user to loan a book', (done) => {
const userbook = {
bookId: bookId.toString(),
returndate: testdate
};
chai
.request(app)
.post(`/api/v1/users/${userId}/books`)
.post(`/api/v1/users/loanbook`)
.set('x-access-token', token)
.send(userbook)
.end((err, res) => {
Expand All @@ -122,7 +122,7 @@ describe('HelloBooks', () => {
};
chai
.request(app)
.post(`/api/v1/users/${userId}/books`)
.post(`/api/v1/users/loanbook`)
.set('x-access-token', token)
.send(userbook)
.end((err, res) => {
Expand All @@ -143,7 +143,7 @@ describe('HelloBooks', () => {
};
chai
.request(app)
.post(`/api/v1/users/${userId}/books`)
.post(`/api/v1/users/loanbook`)
.set('x-access-token', token)
.send(userbook)
.end((err, res) => {
Expand All @@ -165,7 +165,7 @@ describe('HelloBooks', () => {
};
chai
.request(app)
.post(`/api/v1/users/${userId}/books`)
.post(`/api/v1/users/loanbook`)
.set('x-access-token', token)
.send(userbook)
.end((err, res) => {
Expand Down Expand Up @@ -196,20 +196,20 @@ describe('HelloBooks', () => {
done();
});
});
it('should not be able to borrow book if the user id is not authenticated', (done) => {
it('should not be able to borrow book with previous route', (done) => {
const userbook = {
bookId: bookId.toString(),
returndate: testdate
};
chai
.request(app)
.post(`/api/v1/users/${200}/books`)
.post(`/api/v1/users/2/books`)
.set('x-access-token', token)
.send(userbook)
.end((err, res) => {
const response = res.body;
expect(response.message).to
.equal('User does not exist');
.equal(undefined);
expect(res.status)
.to
.equal(404);
Expand All @@ -224,7 +224,7 @@ describe('HelloBooks', () => {
};
chai
.request(app)
.post(`/api/v1/users/${userId}/books`)
.post(`/api/v1/users/loanbook`)
.set('x-access-token', token)
.send(userbook)
.end((err, res) => {
Expand All @@ -245,7 +245,7 @@ describe('HelloBooks', () => {
};
chai
.request(app)
.post(`/api/v1/users/${userId}/books`)
.post(`/api/v1/users/loanbook`)
.set('x-access-token', token)
.send(userbook)
.end((err, res) => {
Expand All @@ -267,7 +267,7 @@ describe('HelloBooks', () => {
};
chai
.request(app)
.post(`/api/v1/users/${userId}/books`)
.post(`/api/v1/users/loanbook`)
.set('x-access-token', token)
.send(userbook)
.end((err, res) => {
Expand All @@ -285,7 +285,7 @@ describe('HelloBooks', () => {
};
chai
.request(app)
.post(`/api/v1/users/${userId}/books`)
.post(`/api/v1/users/loanbook`)
.set('x-access-token', token)
.send(userbook)
.end((err, res) => {
Expand All @@ -305,7 +305,7 @@ describe('HelloBooks', () => {
it('should return a list of books loaned by the user', (done) => {
chai
.request(app)
.get(`/api/v1/users/${userId}/books`)
.get(`/api/v1/users/borrowedbooks`)
.set('Accept', 'application/x-www-form-urlencoded')
.query({ returned: false })
.set('x-access-token', token)
Expand All @@ -319,7 +319,7 @@ describe('HelloBooks', () => {
it('should not return a borrow list if return query is not set', (done) => {
chai
.request(app)
.get(`/api/v1/users/${userId}/books`)
.get(`/api/v1/users/borrowedbooks`)
.set('Accept', 'application/x-www-form-urlencoded')
.set('x-access-token', token)
.end((err, res) => {
Expand All @@ -337,7 +337,7 @@ describe('HelloBooks', () => {
it('should be able to return a book with a book id', (done) => {
chai
.request(app)
.put(`/api/v1/users/${userId}/books`)
.put(`/api/v1/users/returnbook`)
.set('x-access-token', token)
.send({ bookId })
.end((err, res) => {
Expand All @@ -350,7 +350,7 @@ describe('HelloBooks', () => {
it('should not be able to return a book they have not borrowed', (done) => {
chai
.request(app)
.put(`/api/v1/users/${userId}/books`)
.put(`/api/v1/users/returnbook`)
.set('x-access-token', token)
.send({ bookId: 7 })
.end((err, res) => {
Expand All @@ -365,7 +365,7 @@ describe('HelloBooks', () => {
it('should not be able to return a book with an invalid id they have not borrowed', (done) => {
chai
.request(app)
.put(`/api/v1/users/${userId}/books`)
.put(`/api/v1/users/returnbook`)
.set('x-access-token', token)
.send({ bookId: 'one' })
.end((err, res) => {
Expand All @@ -379,7 +379,7 @@ describe('HelloBooks', () => {
it('should not return a book more than once', (done) => {
chai
.request(app)
.put(`/api/v1/users/${userId}/books`)
.put(`/api/v1/users/returnbook`)
.set('x-access-token', token)
.send({ bookId })
.end((err, res) => {
Expand Down

0 comments on commit f559f3f

Please sign in to comment.