Skip to content

Commit

Permalink
chore(Update application): Update application endpoints
Browse files Browse the repository at this point in the history
- update edit user profile route
- update change user role route url
- update reset password validation schema
- modify updateUserDetails controller
- modify user unit test
- modify request test mock data

[Maintains #168405073](https://www.pivotaltracker.com/story/show/168405073)
  • Loading branch information
tobslob committed Sep 11, 2019
1 parent 8432c04 commit 27e8726
Show file tree
Hide file tree
Showing 7 changed files with 86 additions and 24 deletions.
3 changes: 1 addition & 2 deletions src/controllers/userController.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ const updateUserDetails = async (req, res) => {
}
const {
firstName, lastName, phoneNo, birthDate, preferredLanguage,
preferredCurrency, gender, lineManager, currentLocation
preferredCurrency, gender, currentLocation
} = req.body;
const phoneNoExists = await getByOptions(User, {
where: {
Expand All @@ -157,7 +157,6 @@ const updateUserDetails = async (req, res) => {
preferredLanguage,
preferredCurrency,
gender,
lineManager,
currentLocation
};
const options = {
Expand Down
4 changes: 2 additions & 2 deletions src/routes/api/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ const searchRequestRoute = (router) => {
/**
* @swagger
* /api/v1/search/requests:
* post:
* get:
* tags:
* - Requests
* description: search request
Expand Down Expand Up @@ -367,7 +367,7 @@ const searchRequestRoute = (router) => {
* security:
* - bearerAuth: []
*/
.post(checkToken, validate(searchRequestTripSchema), searchRequest);
.get(checkToken, validate(searchRequestTripSchema), searchRequest);

router.route('/requests/reject/:requestId')
/**
Expand Down
76 changes: 70 additions & 6 deletions src/routes/api/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -187,14 +187,57 @@ const userRoute = (router) => {
*/
.get(checkToken, validate(getUserSchema), checkUserId, getUserDetailsById);
router.route('/users/:userId')
/**
/**
* @swagger
* components:
* schemas:
* editUserProfile:
* properties:
* userId:
* type: string
* readOnly: true
* firstName:
* type: string
* lastName:
* type: string
* phoneNo:
* type: string
* birthDate:
* type: string
* format: date
* preferredLanguage:
* type: string
* preferredCurrency:
* type: string
* gender:
* type: string
* currentLocation:
* type: string
* createdAt:
* type: string
* format: date-time
* readOnly: true
* updateAt:
* type: string
* format: date-time
* readOnly: true
* ErrorResponse:
* properties:
* status:
* type: string
* example: error
* data:
* type: string
*/

/**
* @swagger
* paths:
* /api/v1/users/{userId}:
* put:
* tags:
* - Users
* summary: Get one user's details
* summary: update user's profile
* parameters:
* - in: path
* name: userId
Expand All @@ -207,16 +250,37 @@ const userRoute = (router) => {
* content:
* application/json:
* schema:
* $ref: '#/components/schemas/User'
* $ref: '#/components/schemas/editUserProfile'
* responses:
* 200:
* description: User deatails fetched successfully
* description: User profile updated successfully
* content:
* application/json:
* schema:
* type: object
* properties:
* status:
* type: object
* data:
* $ref: '#/components/schemas/editUserProfile'
* 403:
* description: Unauthorized
* content:
* application/json:
* schema:
* $ref: '#/components/schemas/ErrorResponse'
* 404:
* description: User not found
* content:
* application/json:
* schema:
* $ref: '#/components/schemas/ErrorResponse'
* 500:
* description: Internal Server error
* content:
* application/json:
* schema:
* $ref: '#/components/schemas/ErrorResponse'
* security:
* - bearerAuth: [ ]
*/
Expand Down Expand Up @@ -301,10 +365,10 @@ const userRoute = (router) => {
*/
.patch(validate(passwordSchema), resetPassword);

router.route('/users/:userId')
router.route('/role/users/:userId')
/**
* @swagger
* /api/v1/users/{userId}:
* /api/v1/role/users/{userId}:
* patch:
* tags:
* - Users
Expand Down
10 changes: 5 additions & 5 deletions src/test/routes/request.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ describe('REQUESTS', () => {
describe('GET /requests', () => {
it('should able to search for request successfully', (done) => {
chai.request(app)
.post(searchRequestTripEndpoint)
.get(searchRequestTripEndpoint)
.set('authorization', token)
.send({ type: 'return' })
.end((err, res) => {
Expand All @@ -227,7 +227,7 @@ describe('REQUESTS', () => {

it('should able to search for request successfully using origin and destination', (done) => {
chai.request(app)
.post(searchRequestTripEndpoint)
.get(searchRequestTripEndpoint)
.set('authorization', token)
.send({ originCity: 'lagos', destinationCity: 'Istanbul' })
.end((err, res) => {
Expand All @@ -238,7 +238,7 @@ describe('REQUESTS', () => {

it('should able to search for request successfully using approvalstatus', (done) => {
chai.request(app)
.post(searchRequestTripEndpoint)
.get(searchRequestTripEndpoint)
.set('authorization', token)
.send({ originCity: 'Lagos', approvalStatus: 'accepted' })
.end((err, res) => {
Expand All @@ -249,7 +249,7 @@ describe('REQUESTS', () => {

it('should able to search for request successfully using multiCity', (done) => {
chai.request(app)
.post(searchRequestTripEndpoint)
.get(searchRequestTripEndpoint)
.set('authorization', token)
.send({ originCity: 'Lagos', multiCity: true })
.end((err, res) => {
Expand All @@ -260,7 +260,7 @@ describe('REQUESTS', () => {

it('should return 200 for ilike search', (done) => {
chai.request(app)
.post('/api/v1/search/requests/?originCity=LAGOS')
.get('/api/v1/search/requests/?originCity=LAGOS')
.set('authorization', token)
.end((err, res) => {
expect(res.status).to.equal(200);
Expand Down
10 changes: 5 additions & 5 deletions src/test/routes/user.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ describe('User route', () => {

describe('PATCH /users/:userId', () => {
it('should return unauthorize error', async () => {
const response = await chai.request(app).patch(`${BACKEND_BASE_URL}/users/${userMock.anotherUserId}`)
const response = await chai.request(app).patch(`${BACKEND_BASE_URL}/role/users/${userMock.anotherUserId}`)
.type('form')
.set('Content-Type', 'application/json')
.set('authorization', managerToken)
Expand All @@ -134,7 +134,7 @@ describe('User route', () => {
});

it('should return validation error', async () => {
const response = await chai.request(app).patch(`${BACKEND_BASE_URL}/users/${userMock.userId}`)
const response = await chai.request(app).patch(`${BACKEND_BASE_URL}/role/users/${userMock.userId}`)
.type('form')
.set('Content-Type', 'application/json')
.set('authorization', token)
Expand All @@ -147,7 +147,7 @@ describe('User route', () => {
});

it('should succesfully set user role', async () => {
const response = await chai.request(app).patch(`${BACKEND_BASE_URL}/users/${userMock.anotherUserId}`)
const response = await chai.request(app).patch(`${BACKEND_BASE_URL}/role/users/${userMock.anotherUserId}`)
.type('form')
.set('Content-Type', 'application/json')
.set('authorization', token)
Expand All @@ -161,7 +161,7 @@ describe('User route', () => {
});

it('should return incorrect staff id error', async () => {
const response = await chai.request(app).patch(`${BACKEND_BASE_URL}/users/${userMock.wrongId}`)
const response = await chai.request(app).patch(`${BACKEND_BASE_URL}/role/users/${userMock.wrongId}`)
.type('form')
.set('Content-Type', 'application/json')
.set('authorization', token)
Expand All @@ -176,7 +176,7 @@ describe('User route', () => {

it('should return an internal server error', async () => {
const stub = sinon.stub(User, 'findOne').callsFake(() => Promise.reject(new Error('Internal server error')));
const response = await chai.request(app).patch(`${BACKEND_BASE_URL}/users/${userMock.userId}`)
const response = await chai.request(app).patch(`${BACKEND_BASE_URL}/role/users/${userMock.userId}`)
.type('form')
.set('Content-Type', 'application/json')
.set('authorization', token)
Expand Down
2 changes: 1 addition & 1 deletion src/utils/messages.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const messages = {
userNotFoundId: 'User not found',
serverError: 'Internal server error',
unauthorizedUserProfile: 'You are not authorized to edit this profile',
unauthorizedUserRequest: 'You are not authorized to view this users request',
unauthorizedUserRequest: 'You are not authorized to view this users profile',
invalidToken: 'Token you provided is invalid',
invalidUserId: 'userId provided is not a valid uuid string',
noRequests: 'No requests to display',
Expand Down
5 changes: 2 additions & 3 deletions src/validation/userSchema.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ const updateUserSchema = Joi.object({
lastName: JoiValidator.validateString().required(),
phoneNo: JoiValidator.validateNumber().required(),
gender: JoiValidator.validateString().required(),
lineManager: JoiValidator.validateString().required(),
birthDate: JoiValidator.validateDate().required(),
preferredCurrency: JoiValidator.validateString().required(),
preferredLanguage: JoiValidator.validateString().required(),
Expand All @@ -34,7 +33,7 @@ const emailSchema = Joi.object({
});

const passwordSchema = Joi.object({
password: JoiValidator.validatePassword().min(8).alphanum().required(),
password: JoiValidator.validatePassword().min(8).required(),
});


Expand All @@ -45,7 +44,7 @@ const getUserSchema = Joi.object({
const setUserRoleSchema = Joi.object({
userId: JoiValidator.validateUuidV4().required(),
role: JoiValidator.validateString()
.valid('requester', 'travel-admin', 'travel-team-member', 'manager', 'super-admin').required(),
.valid('requester', 'travel-admin', 'travel-team-member', 'manager', 'super-admin', 'accommodation-supplier').required(),
});

export {
Expand Down

0 comments on commit 27e8726

Please sign in to comment.