Skip to content

Commit

Permalink
165933917-chore: improve get user profile route (#90)
Browse files Browse the repository at this point in the history
- write tests
- modify routes and controllers

[Finishes #165933917]
  • Loading branch information
chikeozulumba authored and Temmyogunbo committed May 10, 2019
1 parent 3e557e9 commit a3608b9
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 20 deletions.
1 change: 0 additions & 1 deletion src/controllers/article.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ export const addArticle = async (req, res) => {
delete body.isDeletedByAuthor;
let article = await Article.create({ userId, slug: slug(body.title), ...body });
const { dataValues } = article;
console.log(dataValues);
article = { authorName: fullName, ...dataValues };
responseHandler(res, 201, {
status: 'success',
Expand Down
16 changes: 11 additions & 5 deletions src/controllers/authentication/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,14 @@ export const verifyUser = async (req, res) => {
};

export const viewUser = async (req, res) => {
let { id } = req.params;
if (!id) {
const { id: userId } = req.user;
id = userId;
}

try {
const user = await User.findByPk(req.params.id, {
const user = await User.findByPk(id, {
attributes: {
exclude: [
'password',
Expand Down Expand Up @@ -94,11 +100,11 @@ export const viewUser = async (req, res) => {
message: 'Invalid userId supplied',
}));
}
return res.status(500).json(errorResponseFormat({
status: 'error',
message: 'Something Went Wrong',
}));
}
return res.status(500).json(errorResponseFormat({
status: 'error',
message: 'Something Went Wrong',
}));
}
};

Expand Down
2 changes: 1 addition & 1 deletion src/routers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ router.get('/followers', Auth.authenticateUser, getFollowers);
router.put('/profile/edit', Auth.authenticateUser, checkEditBody, editUser);

// Route for viewing a profile
router.get('/profile/view/:id', Auth.authenticateUser, viewUser);
router.get('/profile/:id?', Auth.authenticateUser, viewUser);

// route for twitter authentication
router.get('/auth/twitter', passport.authenticate('twitter'));
Expand Down
33 changes: 21 additions & 12 deletions tests/integration/viewProfile.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,28 +10,37 @@ chai.use(chaiHttp);
let app = null;
let agent = null;

before(async () => {
app = await startServer(9000);
agent = chai.request(app);
});

describe('User View Profile Test', () => {
it('Should return success for fetching user profile', (done) => {
beforeEach(async () => {
app = await startServer(5000);
agent = chai.request(app);
});
it('Should return success for fetching user profile when an id is not passed', (done) => {
agent
.get('/api/v1/profile')
.set({ Authorization: Auth.generateToken(payload) })
.end((err, res) => {
const { body } = res;
expect(res).to.have.status(200);
expect(body)
.to.have.property('status')
.eql('success');
expect(body).to.have.property('data');
done();
});
});

it('Should return success for fetching user profile when an id is passed', (done) => {
agent
.get('/api/v1/profile/view/979eaa2e-5b8f-4103-8192-4639afae2ba8')
.get('/api/v1/profile/0560a5cc-99d2-4bed-84cd-f1c7e8d98d47')
.set({ Authorization: Auth.generateToken(payload) })
.end((err, res) => {
const { body } = res;
const { user } = body.data;
expect(res).to.have.status(200);
expect(body)
.to.have.property('status')
.eql('success');
expect(body).to.have.property('data');
expect(user.fullName).to.have.eql('Martins Aloba');
expect(user.email).to.have.eql('martins@gmail.com');
expect(user.username).to.have.eql('martinsaloba');
expect(user.imageUrl).to.have.eql('http://waterease.herokuapp.com/images/board/comfort.jpg');
done();
});
});
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/profileTest.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ before(async () => {
describe('User View Error Profile Test that does not exist', () => {
it('Should return error for fetching user profile that does not exist', () => {
agent
.get('/api/v1/profile/view/979eaa2e-5b8f-4103-8192-4639afae2ba')
.get('/api/v1/profile/979eaa2e-5b8f-4103-8192-4639afae2ba')
.set({ Authorization: Auth.generateToken(payload) })
.end((err, res) => {
const { body } = res;
Expand Down

0 comments on commit a3608b9

Please sign in to comment.