Skip to content

Commit 03a1d87

Browse files
Andela DeveloperAndela Developer
Andela Developer
authored and
Andela Developer
committed
refactoring code
1 parent dcdb88c commit 03a1d87

File tree

8 files changed

+304
-303
lines changed

8 files changed

+304
-303
lines changed

server/config/config.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,12 @@ module.exports = {
1010
dialect: 'postgres'
1111
},
1212
test: {
13-
use_env_variable: 'TEST_DB'
13+
username: 'andeladeveloper',
14+
password: null,
15+
database: 'docmanager-test',
16+
host: '127.0.0.1',
17+
port: 5432,
18+
dialect: 'postgres'
1419
},
1520
production: {
1621
use_env_variable: 'PRODUCTION_DB',

server/controllers/users.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -140,12 +140,12 @@ function login(req, res) {
140140
function findUser(req, res) {
141141
const userQuery = Number(req.params.id);
142142
if ((req.decoded.id !== userQuery) && (req.decoded.roleId !== 1)) {
143-
return res.status(401).json({
144-
message: 'Unauthorized Access'
143+
return res.status(403).json({
144+
message: 'Please register or login'
145145
});
146146
}
147147
if (isNaN(userQuery)) {
148-
return res.status(401).json({
148+
return res.status(400).json({
149149
message: `invalid input syntax for integer: "${req.params.id}"`
150150
});
151151
}
@@ -176,7 +176,7 @@ function findUser(req, res) {
176176
function updateUser(req, res) {
177177
if (Number(req.decoded.id) !== Number(req.params.id)) {
178178
return res.status(401).json({
179-
message: 'You are not authorized to access this user'
179+
message: 'You are not authorized to update this user'
180180
});
181181
}
182182
const userId = Number(req.params.id);

server/middlewares/auth.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ function verifyToken(req, res, next) {
2222
}
2323
});
2424
} else {
25-
return res.status(403).send({
25+
return res.status(401).send({
2626
success: false,
27-
message: 'No token provided.'
27+
message: 'Please register or login.'
2828
});
2929
}
3030
}
@@ -41,7 +41,7 @@ function adminAccess(req, res, next) {
4141
if (req.decoded.roleId === 1) {
4242
next();
4343
} else {
44-
return res.status(401).json({
44+
return res.status(403).json({
4545
message: 'You are not authorized',
4646
});
4747
}

server/test/controllers/document.spec.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,9 @@ describe('Documents', () => {
8080
.post('/api/v1/documents')
8181
.send(document)
8282
.end((err, res) => {
83-
expect(res.status).to.equal(403);
83+
expect(res.status).to.equal(401);
8484
expect(res.body).to.be.a('object');
85-
expect(res.body.message).to.eql('No token provided.');
85+
expect(res.body.message).to.eql('Please register or login.');
8686
done();
8787
});
8888
});
@@ -163,9 +163,9 @@ describe('Documents', () => {
163163
superRequest
164164
.get('/api/v1/documents/')
165165
.end((err, res) => {
166-
expect(res.status).to.equal(403);
166+
expect(res.status).to.equal(401);
167167
expect(res.body).to.be.a('object');
168-
expect(res.body.message).be.eql('No token provided.');
168+
expect(res.body.message).be.eql('Please register or login.');
169169
expect(res.body.success).to.eql(false);
170170
done();
171171
});

server/test/controllers/mockData.js

+16-3
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ export default {
1919
2020
password: 'test'
2121
},
22+
blessing: {
23+
24+
password: process.env.PASSWORD
25+
},
2226
user1: {
2327
2428
password: 'test'
@@ -27,7 +31,7 @@ export default {
2731
fullName: 'Baasbank Adams',
2832
userName: 'tiaandela',
2933
30-
password: 'pass123',
34+
password: process.env.PASSWORD,
3135
roleId: 1,
3236
createdAt: new Date(),
3337
updatedAt: new Date()
@@ -36,7 +40,16 @@ export default {
3640
fullName: 'Baas Bank',
3741
userName: 'bank',
3842
39-
password: 'pass123',
43+
password: process.env.PASSWORD,
44+
roleId: 1,
45+
createdAt: new Date(),
46+
updatedAt: new Date()
47+
},
48+
JohnB: {
49+
fullName: 'John Bosco',
50+
userName: 'john',
51+
52+
password: process.env.PASSWORD,
4053
roleId: 1,
4154
createdAt: new Date(),
4255
updatedAt: new Date()
@@ -45,7 +58,7 @@ export default {
4558
fullName: 'Daniel Cfh',
4659
userName: 'cfh',
4760
48-
password: 'pass123',
61+
password: process.env.PASSWORD,
4962
roleId: 2,
5063
createdAt: 'date',
5164
updatedAt: new Date()

server/test/controllers/role.spec.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ describe('Roles', () => {
6262
.set({ authorization: userToken })
6363
.send(role)
6464
.end((err, res) => {
65-
expect(res.status).to.equal(401);
65+
expect(res.status).to.equal(403);
6666
expect(res.body).to.be.a('object');
6767
expect(res.body.message).to.eql('You are not authorized');
6868
done();
@@ -91,7 +91,7 @@ describe('Roles', () => {
9191
.get('/api/v1/roles')
9292
.set({ authorization: userToken })
9393
.end((err, res) => {
94-
expect(res.status).to.equal(401);
94+
expect(res.status).to.equal(403);
9595
expect(res.body.message)
9696
.to.eql('You are not authorized');
9797
done();
@@ -120,7 +120,7 @@ describe('Roles', () => {
120120
.get(`/api/v1/roles/${id}`)
121121
.set({ authorization: userToken })
122122
.end((err, res) => {
123-
expect(res.status).to.equal(401);
123+
expect(res.status).to.equal(403);
124124
expect(res.body.message).to.eql('You are not authorized');
125125
done();
126126
});
@@ -184,7 +184,7 @@ describe('Roles', () => {
184184
.set({ authorization: userToken })
185185
.send({ title: 'boromir' })
186186
.end((err, res) => {
187-
expect(res.status).to.equal(401);
187+
expect(res.status).to.equal(403);
188188
expect(res.body).to.be.a('object');
189189
expect(res.body.message).to.eql('You are not authorized');
190190
done();
@@ -228,7 +228,7 @@ describe('Roles', () => {
228228
.delete(`/api/v1/roles/${id}`)
229229
.set({ authorization: userToken })
230230
.end((err, res) => {
231-
expect(res.status).to.equal(401);
231+
expect(res.status).to.equal(403);
232232
expect(res.body).to.be.a('object');
233233
expect(res.body.message).to.eql('You are not authorized');
234234
done();

0 commit comments

Comments
 (0)