-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
297 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import responseUtil from '../utils/responseUtil'; | ||
import responseError from '../utils/responseError'; | ||
import string from '../utils/stringsUtil'; | ||
import models from '../database/models'; | ||
|
||
const { users } = models; | ||
|
||
export default class profile { | ||
static async updateProfile(req, res) { | ||
if (req.user.payload.email !== req.params.email) { | ||
return responseError(res, 400, string.users.error.USER_SAME_EMAIL); | ||
} | ||
try { | ||
const newUser = await users.update( | ||
req.body, | ||
{ | ||
where: { email: req.params.email }, | ||
returning: true, | ||
plain: true | ||
} | ||
); | ||
const { | ||
password, | ||
createdAt, | ||
updatedAt, | ||
isVerified, | ||
...data | ||
} = newUser[1].dataValues; | ||
return responseUtil(res, 200, string.users.success.SUCCESS_UPDATE, data); | ||
} catch (error) { | ||
return responseError(res, 500, string.users.error.SOMETHING_WRONG); | ||
} | ||
} | ||
|
||
static async getProfile(req, res) { | ||
if (req.user.payload.email !== req.params.email) { | ||
return responseError(res, 400, string.users.error.USER_SAME_EMAIL); | ||
} | ||
try { | ||
const { email } = req.params; | ||
const user = await users.findOne({ | ||
where: { email }, | ||
raw: true | ||
}); | ||
if (!user) { | ||
return res.status(404).json({ | ||
error: 'user does not exist' | ||
}); | ||
} | ||
const { | ||
password, | ||
isVerified, | ||
createdAt, | ||
updatedAt, | ||
...data | ||
} = user; | ||
return res.status(200).json({ | ||
message: 'User Profile', | ||
profile: data, | ||
}); | ||
} catch (error) { | ||
return res.status(500).json({ | ||
error: 'Something wrong', | ||
}); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
import chai from 'chai'; | ||
import chaiHttp from 'chai-http'; | ||
import { describe, it } from 'mocha'; | ||
import app from '../index'; | ||
import mockdata from './mockData/mockData'; | ||
import generateToken from '../utils/generateToken'; | ||
import string from '../utils/stringsUtil'; | ||
|
||
const token = generateToken(mockdata.verifiedUser); | ||
|
||
chai.should(); | ||
chai.use(chaiHttp); | ||
|
||
const email = mockdata.verifiedUser.email; | ||
let newEmail; | ||
let malformedToken; | ||
|
||
const newProfile = { | ||
language: 'Spanish', | ||
}; | ||
|
||
describe('Profile Test Suite', () => { | ||
it('Should edit Profile Page Settings', (done) => { | ||
chai.request(app) | ||
.patch(`/api/v1/users/profile/${email}`) | ||
.set('Authorization', `Bearer ${token}`) | ||
.send(newProfile) | ||
.end((err, res) => { | ||
res.should.have.status(200); | ||
res.body.should.be.a('object'); | ||
res.body.should.have.property('message').eql(`${string.users.success.SUCCESS_UPDATE}`); | ||
done(); | ||
}); | ||
}); | ||
it('Should return 400 when gender is not female or male', (done) => { | ||
chai.request(app) | ||
.patch(`/api/v1/users/profile/${email}`) | ||
.set('Authorization', `Bearer ${token}`) | ||
.send(mockdata.fakeGender) | ||
.end((err, res) => { | ||
res.should.have.status(400); | ||
res.body.should.be.a('object'); | ||
done(); | ||
}); | ||
}); | ||
it('should return 400 if email is not the some of token', (done) => { | ||
chai.request(app) | ||
.patch(`/api/v1/users/profile/${newEmail}`) | ||
.set('Authorization', `Bearer ${token}`) | ||
.send(newProfile) | ||
.end((err, res) => { | ||
res.should.have.status(400); | ||
done(); | ||
}); | ||
}); | ||
it('should not accept malformed', (done) => { | ||
chai.request(app) | ||
.patch(`/api/v1/users/profile/${email}`) | ||
.set('Authorization', `Bearer ${malformedToken}`) | ||
.send(newProfile) | ||
.end((err, res) => { | ||
res.should.have.status(400); | ||
done(); | ||
}); | ||
}); | ||
it('should get a specific user', (done) => { | ||
chai.request(app) | ||
.get(`/api/v1/users/profile/${email}`) | ||
.set('Authorization', `Bearer ${token}`) | ||
.end((err, res) => { | ||
res.should.have.status(200); | ||
done(); | ||
}); | ||
}); | ||
it('should return 400 if email is not the some of token', (done) => { | ||
chai.request(app) | ||
.get(`/api/v1/users/profile/${newEmail}`) | ||
.set('Authorization', `Bearer ${token}`) | ||
.end((err, res) => { | ||
console.log(res.body) | ||
res.should.have.status(400); | ||
done(); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.