Skip to content

Commit

Permalink
feature-[167783727]:view user profile
Browse files Browse the repository at this point in the history
- write controller method to view user profile
- write integration test
[Delivers #167783727]
  • Loading branch information
JuwonAbiola committed Aug 14, 2019
1 parent 2c7f554 commit 83a3e05
Show file tree
Hide file tree
Showing 9 changed files with 129 additions and 16 deletions.
35 changes: 30 additions & 5 deletions server/controllers/Profiles.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import models from '../database/models';
import { serverResponse, serverError, imageUpload } from '../helpers';
import {
serverResponse,
serverError,
imageUpload,
userResponse
} from '../helpers';

const { User } = models;

Expand Down Expand Up @@ -60,10 +65,30 @@ class Profiles {
}
);
const currentUser = await User.findById(user.id);
delete currentUser.dataValues.password;
return serverResponse(res, 200, {
user: currentUser
});
return userResponse(res, 200, currentUser);
} catch (error) {
return serverError(res);
}
}

/**
* @name view
* @description allows a user to view other users profile
* @param {object} req request object
* @param {object} res response object
* @returns {json} the json response been return by the server
* @memberof ProfilesController
*/
static async view(req, res) {
try {
const {
params: { username }
} = req;
const userProfile = await User.findByUsername(username);
if (!userProfile) {
return serverResponse(res, 404, { error: 'user does not exist' });
}
return userResponse(res, 200, userProfile);
} catch (error) {
return serverError(res);
}
Expand Down
8 changes: 4 additions & 4 deletions server/controllers/Sessions.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import {
serverResponse,
serverError,
expiryDate,
getUserAgent
getUserAgent,
userResponse
} from '../helpers';
import models from '../database/models';

Expand Down Expand Up @@ -43,7 +44,7 @@ class Sessions {
});
}
const { devicePlatform, userAgent } = getUserAgent(req);
const { id, dataValues } = user;
const { id } = user;
const expiresAt = expiryDate(devicePlatform);
const token = generateToken({ id });
await Session.create({
Expand All @@ -55,8 +56,7 @@ class Sessions {
devicePlatform
});
res.set('Authorization', token);
delete dataValues.password;
return serverResponse(res, 200, { user: { ...dataValues }, token });
return userResponse(res, 200, user, token);
} catch (error) {
serverError(res);
}
Expand Down
6 changes: 3 additions & 3 deletions server/controllers/Users.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ import {
getUserAgent,
sendVerificationEmail,
sendResetPasswordEmail,
verifyResetPasswordToken
verifyResetPasswordToken,
userResponse
} from '../helpers';

const { User, Session, ResetPassword } = models;
Expand Down Expand Up @@ -62,9 +63,8 @@ class Users {
});

res.set('Authorization', token);
delete user.dataValues.password;
sendVerificationEmail({ ...user.dataValues, token });
return serverResponse(res, 201, { user, token });
return userResponse(res, 201, user, token);
} catch (error) {
return serverError(res);
}
Expand Down
21 changes: 19 additions & 2 deletions server/docs/authors-haven-api.yml
Original file line number Diff line number Diff line change
Expand Up @@ -425,7 +425,6 @@ paths:
application/json:
schema:
"$ref": "#/components/schemas/serverResponse"

/api/v1/users/resetpassword/{token}:
patch:
summary: Reset password route
Expand Down Expand Up @@ -500,7 +499,25 @@ paths:
409:
description: Conflict. Email or username exists
500:
description: Internal server error
description: Internal server error
/api/v1/profiles/{id}:
get:
parameters:
- name: id
in: path
required: true
description: user id
schema:
type : string
summary: Route for getting other users profile
description: Allow user to view other user details
responses:
200:
description: user fetched successfully
404:
description: user does not exist
500:
description: Internal server error
components:
securitySchemes:
BearerAuth:
Expand Down
4 changes: 3 additions & 1 deletion server/helpers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import getSocialUserData from './getSocialUserData';
import imageUpload from './imageUpload';
import emailTemplates from './emailTemplates';
import verifyResetPasswordToken from './verifyResetPasswordToken';
import userResponse from './userResponse';

const { expiryDate } = dateHelper;
const { sendResetPasswordEmail, sendVerificationEmail } = emailTemplates;
Expand All @@ -29,5 +30,6 @@ export {
sendVerificationEmail,
imageUpload,
sendResetPasswordEmail,
verifyResetPasswordToken
verifyResetPasswordToken,
userResponse
};
15 changes: 15 additions & 0 deletions server/helpers/userResponse.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/**
* @name userResponse
* @param {Object} res express response object
* @param {Number} code status code to return
* @param {Object} user object with response details
* @param {Object} token strings
* @returns {JSON} JSON response with status and response information
*/
const userResponse = (res, code, user, token) => {
delete user.dataValues.password;
return token
? res.status(code).json({ user, token })
: res.status(code).json({ user });
};
export default userResponse;
2 changes: 2 additions & 0 deletions server/routes/profile.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,6 @@ route.patch(
Profiles.update
);

route.get('/:username', Profiles.view);

export default route;
3 changes: 2 additions & 1 deletion test/profiles/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import * as update from './update.test';
import * as view from './view.test';

export default update;
export { update, view };
51 changes: 51 additions & 0 deletions test/profiles/view.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import chai, { expect } from 'chai';
import chaiHttp from 'chai-http';
import { config } from 'dotenv';
import sinon from 'sinon';
import app from '../../server';
import Profiles from '../../server/controllers/Profiles';

const { view } = Profiles;

config();

chai.use(chaiHttp);

const baseUrl = process.env.BASE_URL;

describe('GET Profile', () => {
context('when a user checks their profile', () => {
it(' returns the user profile', async () => {
const response = await chai
.request(app)
.get(`${baseUrl}/profiles/JhayXXX`);
expect(response).to.have.status(200);
});
});

context('when a user checks another profile that does not exist', () => {
it('returns a not found error', async () => {
const response = await chai
.request(app)
.get(`${baseUrl}/profiles/user90000`);
expect(response).to.have.status(404);
});
});

context('when the database is unable to query the user table ', () => {
it('will throw a server error', async () => {
const stubfunc = { view };
const sandbox = sinon.createSandbox();
sandbox.stub(stubfunc, 'view').rejects(new Error('Server Error'));

const next = sinon.spy();
const res = {
status: () => ({
json: next
})
};
await view({}, res);
sinon.assert.calledOnce(next);
});
});
});

0 comments on commit 83a3e05

Please sign in to comment.