Skip to content

Commit

Permalink
feature(users): added list users endpoint
Browse files Browse the repository at this point in the history
[Delivers #167164996]
  • Loading branch information
Nerocodes committed Aug 19, 2019
1 parent 5474b7c commit e62c9c3
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 8 deletions.
20 changes: 17 additions & 3 deletions docs/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -71,14 +71,25 @@ paths:
409:
description: resource already exists
500:
description: internal server error
description: internal server error
get:
tags:
- Users
summary: Gets all users
responses:
200:
description: request successful
401:
description: Authentication failed
security:
- token: []
/novels:
post:
tags:
- Novels
summary: Creates a new novel
security:
- ApiKeyAuth: []
- token: []
requestBody:
description: fields containing novel data
content:
Expand Down Expand Up @@ -146,6 +157,9 @@ paths:
schema:
$ref: '#/components/schemas/StandardServerResponse'

description: internal server error
security:
- token: []
/users/login:
post:
tags:
Expand Down Expand Up @@ -717,7 +731,7 @@ components:
type: boolean
example: true
securitySchemes:
bearerToken:
token:
type: apiKey
name: Authorization
in: header
19 changes: 17 additions & 2 deletions src/controllers/userController.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import services from '../services';
const {
authHelper, successResponse, errorResponse, responseMessage, verifyUser
} = helpers;
const { userServices: { findUser, findFollower } } = services;
const { userServices: { findUser, findFollower, getAllUsers } } = services;
const { User } = models;

/**
Expand Down Expand Up @@ -139,6 +139,21 @@ const editProfile = async (request, response) => {
return responseMessage(response, 400, { error: `the ${errorField} field(s) entered are invalid` });
};

/**
*
* @name listUsers
* @param {Object} request
* @param {Object} response
* @return {Object} json response
*/
const listUsers = async (request, response) => {
const users = await getAllUsers();
return successResponse(response, 200, {
message: 'Request successful',
users
});
};

export default {
getProfile, editProfile, signUp, login
getProfile, editProfile, signUp, login, listUsers
};
5 changes: 4 additions & 1 deletion src/routes/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const {
authorizeUser
} = middlewares;
const {
signUp, login, getProfile, editProfile
signUp, login, getProfile, editProfile, listUsers
} = userController;
const user = express.Router();

Expand All @@ -23,6 +23,9 @@ user.post(`${USER_URL}`, signUpValidator, signUp);
// Route to login a user
user.post(`${USER_URL}/login`, loginValidator, login);

// Route to get all users
user.get(`${USER_URL}`, verifyToken, listUsers);

// Route to get user profile by userId
user.get(`${PROFILE_URL}/:userId`, verifyToken, authorizeUser(['reader', 'author', 'admin', 'superadmin']), profileValidator, getProfile);

Expand Down
15 changes: 14 additions & 1 deletion src/services/userService.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,19 @@ const findFollower = async (followeeId, followerId) => {
return follower;
};

/**
* @name getAllUsers
* @returns {Array} array of users
*/
const getAllUsers = async () => {
const allUsers = await User.findAll({
attributes: ['id', 'firstName', 'lastName', 'bio', 'avatarUrl']
});
return allUsers;
};

export default {
findUser, findFollower
findUser,
findFollower,
getAllUsers
};
7 changes: 7 additions & 0 deletions tests/mockData/userMock.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,13 @@ export default {
password: 'password',
email: 'novel@gmail.com'
},
getUser: {
firstName: 'john',
lastName: 'snow',
username: 'knows',
password: 'password',
email: 'snow@gmail.com'
},
invalidFirstName: {
firstName: 'john77***99he**J',
lastName: 'joe',
Expand Down
39 changes: 38 additions & 1 deletion tests/user.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@ import app from '../src';
const API_VERSION = '/api/v1';
const LOGIN_URL = `${API_VERSION}/users/login`;
const PROFILE_URL = `${API_VERSION}/profiles`;
const endpointUser = '/api/v1/users';
const validId = '122a0d86-8b78-4bb8-b28f-8e5f7811c456';
const invalidId = '9a5f3850-c53b-4450-8ce4-d560aa2ca736';
const {
userMock: {
validProfileLogin, validProfile, invalidProfile1, invalidProfile2,
invalidProfile3, invalidProfileToken: { expiredToken }
invalidProfile3, getUser, invalidProfileToken: { expiredToken }
}
} = mockData;
let authToken;
Expand Down Expand Up @@ -189,3 +190,39 @@ describe('USER ROUTES', () => {
});
});
});

describe('Test for getting users', () => {
before((done) => {
const user = getUser;
chai.request(app)
.post(endpointUser)
.send(user)
.end((err, res) => {
authToken = res.body.user.token;
done();
});
});
describe('GET /api/v1/users', () => {
it('should get all users if user is authenticated', (done) => {
chai.request(app)
.get(endpointUser)
.set('authorization', authToken)
.end((err, res) => {
expect(res).to.have.status(200);
expect(res.body).to.have.property('message');
expect(res.body).to.have.property('users');
done();
});
});

it('should not get all users if user is not authenticated', (done) => {
chai.request(app)
.get(endpointUser)
.end((err, res) => {
expect(res).to.have.status(401);
expect(res.body.error).to.equal('no token provided');
done();
});
});
});
});

0 comments on commit e62c9c3

Please sign in to comment.