Skip to content

Commit

Permalink
Feedback (#32)
Browse files Browse the repository at this point in the history
* linter(line spaces) :

Lines spacing should be at a maximum of 80 chars

* Unused packages should be removed

* Documentation(slate): The Api documentation should be in a folder called `documentation`

* Delete unused files

* Refactor(helpers) : Helper functions should be exported connsistently

* refactor(tests): Tests were refactored to adequately test new changes to the codebase

* Refactor(linter):

Code was refactored to maintain a proper max length of less than 80 characters

* Refactor(dates):

Dates should be saved in an easily readable format

* Refactor(validation)

Certain edge cases should be handled and return an appropriate response

* Refactor:

Users should be able to get a list of all publicly available documents

* Refactor helpers

* Refactor:

Repipe test coverage

* Bug(hound)

Hound should be ablt to work with eslint

* helpers (validation)

Abstracted the id validation into a helper

* Chore(tests)

Expected tests to return the appropriate response message

* Models

Added Validation to models and migrations

* Bugs(create)

Returns a readable mesage when a user tries to create a document with the wrong access level

Users cannot create multiple documents with same title

When users search for documents the appropriate response is returned

* Bug(roles)

When an admin tries to change the role of a user that doesnt exist an error message is returned

* Documentation

Refactored the documentation to reflect the new responses and added the status codes used in the api

* chore(hound)

Disabled hound

* documentation

Added The title to the documentation

* Bug(documents)

Returns an error message when a user tries to update a document to one that already exists

* chore(tests)

Added tests for new functionality

* Bug(users)

Returns an error message when a user tries to update their email to an email that already exists
  • Loading branch information
seyi-adeleke committed Aug 9, 2017
1 parent 8fd82b5 commit a546710
Show file tree
Hide file tree
Showing 5 changed files with 116 additions and 13 deletions.
1 change: 1 addition & 0 deletions documentation/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,7 @@
<div class="page-wrapper">
<div class="dark-box"></div>
<div class="content">
<h1 id='introduction'>Docman</h1>
<h1 id='introduction'>Introduction</h1>
<p>Welcome to the Docman Api. Docman is an Api for managing documents. Implemented with an Admin, Editors and users. It provides all that is essential for a management system, users can create documents complete with roles and privileges. Each document defines its access right and the date which it was created.</p>
<h1 id='authentication'>Authentication</h1>
Expand Down
19 changes: 13 additions & 6 deletions server/controllers/documentControllers.js
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,11 @@ export default {
Document
.findById(req.params.id)
.then((document) => {
if (document === null) {
return res.status(404).json({
message: 'This document doesn\'t exist'
});
}
const updatedDocument = {
title: req.body.title,
content: req.body.content,
Expand All @@ -216,11 +221,7 @@ export default {
message: 'You cannot create role based documents'
});
}
if (!document) {
return res.status(404).json({
message: 'this document doesnt exist'
});
} else if (document.userId !== req.decoded.id) {
if (document.userId !== req.decoded.id) {
return res.status(404).json({
message: 'you cannot edit this document'
});
Expand All @@ -235,7 +236,13 @@ export default {
message: 'Document updated', updatedDocument
}));
})
.catch(error => res.status(400).send(error));
.catch((error) => {
if (error.parent.code === '23505') {
return res.status(400)
.send({ message: 'This document exists already' });
}
return error;
});
},

/**
Expand Down
8 changes: 7 additions & 1 deletion server/controllers/userControllers.js
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,13 @@ export default {
res.status(200)
.send({ message: 'User updated succesfully', updatedUser });
})
.catch(error => res.status(400).send(error));
.catch((error) => {
if (error.parent.code === '23505') {
return res.status(400)
.send({ message: 'This email already exists' });
}
return error;
});
});
},

Expand Down
56 changes: 54 additions & 2 deletions server/tests/documentController.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -483,7 +483,7 @@ describe('Document Controller ', () => {
});
});

it('returns a 400 if a user tries to update a document that doesnt exist', (done) => {
it('returns a 404 if a user tries to update a document that doesnt exist', (done) => {
request(app)
.post('/api/v1/users')
.send({
Expand All @@ -505,7 +505,8 @@ describe('Document Controller ', () => {
.expect('Content-Type', /json/)
.expect(404)
.end((err, res) => {
expect(res.status).to.equal(400);
expect(res.body.message).to.equal('This document doesn\'t exist');
expect(res.status).to.equal(404);
done();
});
});
Expand Down Expand Up @@ -644,6 +645,57 @@ describe('Document Controller ', () => {
});
});

it('returns a 400 if a user updates a document to a document that already exists', (done) => {
request(app)
.post('/api/v1/users')
.send({
name: 'seyi',
password: 'seyi',
email: 'seyi@seyi.com',
roleId: 2
})
.expect(200)
.end((err, res) => {
token = res.body.token;
request(app)
.post('/api/v1/documents')
.send({
title: 'title',
content: 'content'
})
.set('Authorization', `${token}`)
.set('Accept', 'application/json')
.expect('Content-Type', /json/)
.expect(200)
.end((err, res) => {
request(app)
.post('/api/v1/documents')
.send({
title: 'document',
content: 'content'
})
.set('Authorization', `${token}`)
.set('Accept', 'application/json')
.expect('Content-Type', /json/)
.expect(200)
.end((err, res) => {
request(app)
.put('/api/v1/documents/1')
.send({
title: 'document'
})
.set('Authorization', `${token}`)
.set('Accept', 'application/json')
.expect('Content-Type', /json/)
.end((err, res) => {
expect((res.body.message)).to.equals('This document exists already');
done();
});
});
});
});
});

it('return a 400 if a normal user tries to change the access of a document to `role` ', (done) => {
request(app)
.post('/api/v1/users')
Expand Down
45 changes: 41 additions & 4 deletions server/tests/usercontroller.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -493,7 +493,45 @@ describe('User Controller ', () => {
});
});
});
it('returns an error message if the user tries to update their email with an invalid value', (done) => {
it('returns an error message if the user tries to update their email to an email that already exists', (done) => {
request(app)
.post('/api/v1/users')
.send({
name: 'test',
password: 'test',
email: 'test@test.com',
roleId: 2
})
.expect(200)
.end((err, res) => {
request(app)
.post('/api/v1/users')
.send({
name: 'seyi',
password: 'seyi',
email: 'seyi@seyi.com',
roleId: 2
})
.expect(200)
.end((err, res) => {
token = res.body.token;
request(app)
.put('/api/v1/users/2')
.send({
email: 'test@test.com',
})
.set('Authorization', `${token}`)
.set('Accept', 'application/json')
.expect(400)
.end((err, res) => {
expect((res.body.message)).to.equals('This email already exists');
done();
});
});
});
});

it('encrypts a password when a user tries to update it', (done) => {
request(app)
.post('/api/v1/users')
.send({
Expand All @@ -508,16 +546,15 @@ describe('User Controller ', () => {
request(app)
.put('/api/v1/users/1')
.send({
email: 'adeleke.com',
password: 'new password',
})
.set('Authorization', `${token}`)
.set('Accept', 'application/json')
.expect(400)
.end((err, res) => {
expect((res.body.message)).to.equals('Please use a valid email');
expect((res.body.message)).to.equals('User updated succesfully');
done();
});
done();
});
});
});
Expand Down

0 comments on commit a546710

Please sign in to comment.