Skip to content

Commit

Permalink
Merge fff9cf3 into 94ebd3b
Browse files Browse the repository at this point in the history
  • Loading branch information
jabichris committed May 17, 2019
2 parents 94ebd3b + fff9cf3 commit 0316136
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 18 deletions.
10 changes: 4 additions & 6 deletions controllers/profile.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ class Users {
* @returns {object} return object containg user info
*/
static async user(req, res) {
const { id } = req.params;
const { username } = req.params;
try {
const foundUser = await User.findOne({ where: { id } });
const foundUser = await User.findOne({ where: { username } });
if (!foundUser) {
return res.status(404).json({
status: 404,
Expand Down 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/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ router.use((err, req, res, next) => {
}
return next(err);
});
router.use('/users', users);
router.use('/profiles', users);
router.use('/users', resetPassword);

export default router;
4 changes: 2 additions & 2 deletions routes/api/profile/profile.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import canEditProfile from '../../../middlewares/editProfile';

const router = express.Router();

router.get('/:id', users.user);
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 0316136

Please sign in to comment.