Skip to content

Commit

Permalink
chore(profile): Edit Id params to username
Browse files Browse the repository at this point in the history
-Feedback from the PO to edit the ID that was being requested to username.

[Delivers #165967230]
  • Loading branch information
Christian JABIRO authored and Christian JABIRO committed May 17, 2019
1 parent 4003f7f commit fff9cf3
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 14 deletions.
6 changes: 2 additions & 4 deletions controllers/profile.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,17 +44,15 @@ class Users {
*/
static async editProfile(req, res) {
try {
const { id } = req.params;

const { username } = req.params;
const updateProfile = await User.update(
{
username: req.body.username,
bio: req.body.bio,
image: req.body.image
},
{ where: { id }, returning: true, plain: true }
{ where: { username }, returning: true, plain: true }
);

const newProfile = {
username: updateProfile[1].username,
email: updateProfile[1].email,
Expand Down
10 changes: 7 additions & 3 deletions middlewares/editProfile.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
const canEditProfile = (req, res, next) => {
const { id } = req.params;
import model from '../models';

return req.user.id !== Number(id)
const { User } = model;

const canEditProfile = async (req, res, next) => {
const { username } = req.params;
const userModel = await User.findOne({ where: { id: req.user.id } });
return userModel.username !== username
? res.status(401).send({
status: 401,
message: 'You are not allowed to edit this profile'
Expand Down
2 changes: 1 addition & 1 deletion routes/api/profile/profile.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ const router = express.Router();

router.get('/:username', users.user);

router.put('/:id', auth, canEditProfile, users.editProfile);
router.put('/:username', auth, canEditProfile, users.editProfile);

export default router;
12 changes: 6 additions & 6 deletions tests/profile.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import helper from '../helpers';
const { expect } = chai;

chai.use(chaiHttp);
let userId;
let userName;
describe('GET /api/v1/users/:id', () => {
let Token;
before(async () => {
Expand All @@ -25,7 +25,7 @@ describe('GET /api/v1/users/:id', () => {
},
{ logging: false }
);
userId = user.dataValues.id;
userName = user.username;
});

it('the user should be logged in', (done) => {
Expand All @@ -45,7 +45,7 @@ describe('GET /api/v1/users/:id', () => {
it('should get a specific profile', (done) => {
chai
.request(app)
.get(`/api/v1/users/${userId}`)
.get(`/api/v1/profiles/${userName}`)
.end((err, res) => {
expect(res.status).to.equal(200);
expect(res.body.data)
Expand All @@ -64,7 +64,7 @@ describe('GET /api/v1/users/:id', () => {
it('should get an error message', (done) => {
chai
.request(app)
.get('/api/v1/users/1000000')
.get('/api/v1/profiles/1000000')
.end((err, res) => {
expect(res.status).to.equal(404);
expect(res.body)
Expand All @@ -77,7 +77,7 @@ describe('GET /api/v1/users/:id', () => {
it('should update bio of the profile', (done) => {
chai
.request(app)
.put(`/api/v1/users/${userId}`)
.put(`/api/v1/profiles/${userName}`)
.set('Authorization', Token)
.send({ bio: 'alhamdulillah' })
.end((err, res) => {
Expand All @@ -95,7 +95,7 @@ describe('GET /api/v1/users/:id', () => {
it('should update image url ', (done) => {
chai
.request(app)
.put(`/api/v1/users/${userId}`)
.put(`/api/v1/profiles/${userName}`)
.set('Authorization', Token)
.send({ image: 'www.jgdh.com' })
.end((err, res) => {
Expand Down

0 comments on commit fff9cf3

Please sign in to comment.