Skip to content

Commit db56f1d

Browse files
Andela DeveloperAndela Developer
Andela Developer
authored and
Andela Developer
committed
remove hard coded data in spec
1 parent 4b51cde commit db56f1d

File tree

9 files changed

+118
-85
lines changed

9 files changed

+118
-85
lines changed

codeclimate.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,4 @@ ratings:
1717
- "**.jsx"
1818
exclude_paths:
1919
- node_modules/**/*
20-
- server/test/**
20+
- test/

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/documents.js

+19-5
Original file line numberDiff line numberDiff line change
@@ -93,9 +93,23 @@ function getAllDocument(req, res) {
9393
limit,
9494
offset,
9595
where: {
96-
access: {
97-
$ne: 'private'
98-
}
96+
$or: [
97+
{
98+
access: {
99+
$eq: 'private'
100+
}
101+
},
102+
{
103+
access: {
104+
$eq: 'public'
105+
}
106+
},
107+
{
108+
access: {
109+
$eq: 'role'
110+
}
111+
}
112+
]
99113
},
100114
include: [
101115
{
@@ -161,7 +175,7 @@ function findDocument(req, res) {
161175
}
162176
if (document.access === 'private') {
163177
if (document.userId !== req.decoded.id) {
164-
return res.status(401).json({
178+
return res.status(403).json({
165179
message: 'You are not authorized to view this document'
166180
});
167181
}
@@ -174,7 +188,7 @@ function findDocument(req, res) {
174188
if (
175189
Number(documentOwner.roleId) !== Number(req.decoded.roleId)
176190
) {
177-
return res.status(401).json({
191+
return res.status(403).json({
178192
message: 'You are not authorized to view this document'
179193
});
180194
}

server/controllers/search.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import models from '../models';
33

44
const Document = models.Document;
55
const User = models.User;
6-
const metaData = helper.paginationMetaData;
6+
const pagination = helper.paginationMetaData;
77

88
/**
99
* Search for user using a query string
@@ -36,7 +36,7 @@ function searchUser(req, res) {
3636
}
3737
res.status(200).send({
3838
user,
39-
pagination: metaData(count, limit, offset)
39+
pagination: pagination(count, limit, offset)
4040
});
4141
}).catch(error => res.status(400).send(error));
4242
}
@@ -84,7 +84,7 @@ function searchDocuments(req, res) {
8484
}
8585
res.status(200).send({
8686
document,
87-
pagination: metaData(count, limit, offset),
87+
pagination: pagination(count, limit, offset),
8888
});
8989
})
9090
.catch(error => res.status(400).send(error));
@@ -119,7 +119,7 @@ function searchDocuments(req, res) {
119119
}
120120
res.status(200).send({
121121
document,
122-
pagination: metaData(count, limit, offset),
122+
pagination: pagination(count, limit, offset),
123123
});
124124
})
125125
.catch(error => res.status(400).send(error));

server/controllers/users.js

+8-6
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ require('dotenv').config();
99
const jwtSecret = process.env.JWT_SECRET;
1010
const Document = models.Document;
1111
const User = models.User;
12-
const metaData = helper.paginationMetaData;
12+
const pagination = helper.paginationMetaData;
1313

1414
/**
1515
* Get all users
@@ -29,7 +29,7 @@ function getUsers(req, res) {
2929
.then(({ rows: user, count }) => {
3030
res.status(200).send({
3131
user,
32-
pagination: metaData(count, limit, offset)
32+
pagination: pagination(count, limit, offset)
3333
});
3434
})
3535
.catch(error => res.status(400).send(error));
@@ -66,8 +66,10 @@ function createUser(req, res) {
6666
roleId: req.body.roleId || 2
6767
}).then((userDetails) => {
6868
res.status(200).json({
69-
userDetails,
70-
success: true,
69+
email: userDetails.email,
70+
fullName: userDetails.fullName,
71+
id: userDetails.id,
72+
roleId: userDetails.roleId,
7173
message: 'You have successfully registered.'
7274
});
7375
}).catch((error) => {
@@ -141,7 +143,7 @@ function findUser(req, res) {
141143
const userQuery = Number(req.params.id);
142144
if ((req.decoded.id !== userQuery) && (req.decoded.roleId !== 1)) {
143145
return res.status(403).json({
144-
message: 'Please register or login'
146+
message: 'Unauthorized Access'
145147
});
146148
}
147149
if (isNaN(userQuery)) {
@@ -266,7 +268,7 @@ function getUserDocuments(req, res) {
266268
.then(({ rows: document, count }) => {
267269
res.status(200).send({
268270
document,
269-
pagination: metaData(count, limit, offset),
271+
pagination: pagination(count, limit, offset),
270272
});
271273
})
272274
.catch(error => res.status(400).send(error));

server/routes/documents.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -176,8 +176,8 @@ router.route('/:id')
176176
* "$ref": "#/definitions/Document"
177177
* 400:
178178
* description: Error.
179-
* 401:
180-
* description: You do not have access to this document.
179+
* 403:
180+
* description: You do not have permission to access this document.
181181
* 404:
182182
* description: Document not found.
183183
* security:

server/routes/users.js

+11-13
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,11 @@ const router = express.Router();
3636
* createdAt:
3737
* type: string
3838
* format: int32
39-
* example: 2016-08-29T09:12:33.001Z
39+
* example: 2016-08-29
4040
* updatedAt:
4141
* type: string
4242
* format: int32
43-
* example: 2016-08-29T09:12:33.001Z
43+
* example: 2016-08-29
4444
* Document:
4545
* type: object
4646
* required:
@@ -67,11 +67,11 @@ const router = express.Router();
6767
* createdAt:
6868
* type: string
6969
* format: int32
70-
* example: 2016-08-29T09:12:33.001Z
70+
* example: 2016-08-29
7171
* updatedAt:
7272
* type: string
7373
* format: int32
74-
* example: 2016-08-29T09:12:33.001Z
74+
* example: 2016-08-29
7575
*/
7676
// Security schema definition
7777
/**
@@ -95,7 +95,7 @@ router.route('/login')
9595
* - User
9696
* summary: Login a user
9797
* operationId: login
98-
* description: Logs in a user and provides them with a jwt token to access other routes
98+
* description: Logs in a user and provides them with a jwt token
9999
* consumes:
100100
* - application/x-www-form-urlencoded
101101
* produces:
@@ -134,7 +134,7 @@ router.route('/')
134134
/**
135135
* @swagger
136136
* paths:
137-
* /api/v1/users/:
137+
* /api/v1/users:
138138
* post:
139139
* tags:
140140
* - User
@@ -162,13 +162,11 @@ router.route('/')
162162
* name: password
163163
* description: User's password
164164
* required: true
165-
* schema:
166-
* type: array
167-
* items:
168-
* $ref: '#/definitions/User'
169165
* responses:
170166
* 200:
171-
* description: You have successfully registered.
167+
* description: Succesfull Operation.
168+
* schema:
169+
* $ref: "#/definitions/User"
172170
* 400:
173171
* description: Error.
174172
* get:
@@ -177,7 +175,7 @@ router.route('/')
177175
* summary: Get all users
178176
* operationId: getUsers
179177
* description:
180-
* This route is only accessible to an admin to enable her get all users.
178+
* This route is only accessible to an admin.
181179
* produces:
182180
* - application/json
183181
* parameters:
@@ -346,7 +344,7 @@ router.route('/:id/documents')
346344
/**
347345
* @swagger
348346
* paths:
349-
* /api/v1/users/{id}/documents/ :
347+
* /api/v1/users/{id}/documents :
350348
* get:
351349
* tags:
352350
* - User

0 commit comments

Comments
 (0)