-
Notifications
You must be signed in to change notification settings - Fork 4
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
17 changed files
with
657 additions
and
398 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 |
---|---|---|
|
@@ -47,6 +47,7 @@ shrinkwrap.yaml | |
|
||
yarn.lock | ||
|
||
package-lock.json | ||
# Winston logger files | ||
|
||
error.log | ||
|
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,198 @@ | ||
import request from 'supertest'; | ||
import jwt from 'jsonwebtoken'; | ||
import { User } from '../../database/models'; | ||
import { signupUser, profile } from '../mocks/db.json'; | ||
import { urlPrefix } from '../mocks/variables.json'; | ||
import app from '../../app'; | ||
|
||
const { JWT_SECRET } = process.env; | ||
|
||
let testUserToken; | ||
describe('Profile', () => { | ||
beforeAll(async () => { | ||
const { body } = await request(app) | ||
.post(`${urlPrefix}/users`) | ||
.send({ user: { | ||
username: signupUser.username, | ||
email: signupUser.email, | ||
password: signupUser.password | ||
} }); | ||
testUserToken = body.user.token; | ||
}); | ||
|
||
afterAll(async () => { | ||
await User.destroy({ | ||
where: { email: signupUser.email } | ||
}); | ||
await User.destroy({ | ||
where: { email: profile.email } | ||
}) | ||
}); | ||
|
||
test('should create profile and send confirmation when email is provided', async () => { | ||
expect.assertions(11); | ||
const res = await request(app) | ||
.put(`${urlPrefix}/user`) | ||
.set('Authorization', testUserToken) | ||
.send({user: { | ||
...profile | ||
}}) | ||
|
||
expect(res.status).toBe(200); | ||
expect(res.body.message).toBe('Your email has changed. Please check your email for confirmation'); | ||
expect(res.body.user.firstName).toBe(profile.firstName); | ||
expect(res.body.user.lastName).toBe(profile.lastName); | ||
expect(res.body.user.username).toBe(profile.username); | ||
expect(res.body.user.email).toBe(profile.email); | ||
expect(res.body.user.bio).toBe(profile.bio); | ||
expect(res.body.user.gender).toBe(profile.gender); | ||
expect(res.body.user.birthDate).toBeDefined(); | ||
expect(res.body.user.image).toBe(profile.image); | ||
expect(res.body.user.cover).toBe(profile.cover); | ||
}); | ||
|
||
test('should create profile and not send confirmation email when email is not provided', async () => { | ||
expect.assertions(3); | ||
const res = await request(app) | ||
.put(`${urlPrefix}/user`) | ||
.set('Authorization', testUserToken) | ||
.send({user: { | ||
firstName: 'Peter' | ||
}}) | ||
|
||
expect(res.status).toBe(200); | ||
expect(res.body.user.firstName).toBe('Peter'); | ||
expect(res.body.message).toBe('The information was updated successful'); | ||
}); | ||
|
||
test('should not create profile with --taken email and username', async () => { | ||
expect.assertions(2); | ||
const res = await request(app) | ||
.put(`${urlPrefix}/user`) | ||
.set('Authorization', testUserToken) | ||
.send({user: { | ||
...profile | ||
}}) | ||
|
||
expect(res.status).toBe(409); | ||
expect(res.body.errors.body[0]).toBe('email and username are already taken'); | ||
}); | ||
|
||
test('should not create profile with --taken email ', async () => { | ||
expect.assertions(2); | ||
const res = await request(app) | ||
.put(`${urlPrefix}/user`) | ||
.set('Authorization', testUserToken) | ||
.send({user: { | ||
email: profile.email | ||
}}) | ||
|
||
expect(res.status).toBe(409); | ||
expect(res.body.errors.body[0]).toBe('email is already taken'); | ||
}); | ||
|
||
test('should not create profile with --taken username ', async () => { | ||
expect.assertions(2); | ||
const res = await request(app) | ||
.put(`${urlPrefix}/user`) | ||
.set('Authorization', testUserToken) | ||
.send({user: { | ||
username: profile.username | ||
}}) | ||
|
||
expect(res.status).toBe(409); | ||
expect(res.body.errors.body[0]).toBe('username is already taken'); | ||
}); | ||
|
||
test('should not create profile with --taken username and untaken email ', async () => { | ||
expect.assertions(2); | ||
const res = await request(app) | ||
.put(`${urlPrefix}/user`) | ||
.set('Authorization', testUserToken) | ||
.send({user: { | ||
username: profile.username, | ||
email: 'papasava@email.com' | ||
}}) | ||
|
||
expect(res.status).toBe(409); | ||
expect(res.body.errors.body[0]).toBe('username is already taken'); | ||
}); | ||
|
||
test('should not create profile with --taken email and untaken username ', async () => { | ||
expect.assertions(2); | ||
const res = await request(app) | ||
.put(`${urlPrefix}/user`) | ||
.set('Authorization', testUserToken) | ||
.send({user: { | ||
username: 'papasava', | ||
email: profile.email | ||
}}) | ||
|
||
expect(res.status).toBe(409); | ||
expect(res.body.errors.body[0]).toBe('email is already taken'); | ||
}); | ||
|
||
test('should not create profile without --token', async () => { | ||
expect.assertions(2); | ||
await User.destroy({ | ||
where: { email: 'john.doe@andela.com' } | ||
}); | ||
const res = await request(app) | ||
.put(`${urlPrefix}/user`) | ||
.send({user: { | ||
...profile | ||
}}) | ||
|
||
expect(res.status).toBe(401); | ||
expect(res.body.errors.body[0]).toBe('No auth token'); | ||
}); | ||
|
||
test('should not create profile with --unexisting user', async () => { | ||
expect.assertions(2); | ||
await User.destroy({ | ||
where: { email: signupUser.email } | ||
}); | ||
const res = await request(app) | ||
.put(`${urlPrefix}/user`) | ||
.set('Authorization', testUserToken) | ||
.send({user: { | ||
...profile | ||
}}); | ||
|
||
expect(res.status).toBe(404); | ||
expect(res.body.errors.body).toBeDefined(); | ||
}); | ||
|
||
test('should not create profile with --malformed token', async () => { | ||
expect.assertions(2); | ||
await User.destroy({ | ||
where: { email: signupUser.email } | ||
}); | ||
const res = await request(app) | ||
.put(`${urlPrefix}/user`) | ||
.set('Authorization', 'thgdihueh-jz') | ||
.send({user: { | ||
...profile | ||
}}) | ||
|
||
expect(res.status).toBe(401); | ||
expect(res.body.errors.body).toBeDefined(); | ||
}); | ||
|
||
test('should not create profile with --incorrect token', async () => { | ||
expect.assertions(2); | ||
await User.destroy({ | ||
where: { email: signupUser.email } | ||
}); | ||
const token = jwt.sign({ id: 12345, userType: 'user' }, JWT_SECRET); | ||
const res = await request(app) | ||
.put(`${urlPrefix}/user`) | ||
.set('Authorization', token) | ||
.send({user: { | ||
...profile | ||
}}) | ||
|
||
expect(res.status).toBe(520); | ||
expect(res.body.errors.body).toBeDefined(); | ||
}); | ||
}); |
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,109 @@ | ||
import uuid from 'uuid'; | ||
import { User } from '../database/models'; | ||
import { sendEmailConfirmationLink } from './MailController'; | ||
|
||
/** | ||
* @description ProfileContoller class | ||
*/ | ||
class ProfileController { | ||
/** | ||
* @author Daniel | ||
* @param {*} req | ||
* @param {*} res | ||
* @returns {*} user object | ||
*/ | ||
static async createProfile(req, res) { | ||
const { user } = req.body; | ||
const { id } = req.currentUser; | ||
const { email, username } = req.body.user; | ||
if (email || username) { | ||
let message; | ||
let errorMessage; | ||
if (email && username ) { | ||
try { | ||
const emailOwner = await User.findOne({ | ||
where:{email} | ||
}) | ||
const usernameOwner = await User.findOne({ | ||
where:{username} | ||
}) | ||
if (emailOwner && usernameOwner) { | ||
message = 'email and username are'; | ||
} else if (emailOwner) { | ||
message = 'email is'; | ||
} else if (usernameOwner) { | ||
message = 'username is'; | ||
} | ||
} catch(error) { | ||
errorMessage = error.message; | ||
} | ||
} else if (email) { | ||
try { | ||
const emailOwner = await User.findOne({ | ||
where:{email} | ||
}); | ||
if(emailOwner){ | ||
message = 'email is'; | ||
} | ||
} catch(error) { | ||
errorMessage = error.message; | ||
} | ||
}else if (username) { | ||
try { | ||
const usernameOwner = await User.findOne({ | ||
where:{username} | ||
}); | ||
if (usernameOwner) { | ||
message = 'username is'; | ||
} | ||
} catch(error) { | ||
errorMessage = error.message; | ||
} | ||
} | ||
if (message) { | ||
return res.status(409).send({ | ||
errors:{ | ||
body:[`${message} already taken`] | ||
} | ||
}); | ||
} if (errorMessage) { | ||
return res.status(520).send({ | ||
errors:{ | ||
body:[ | ||
errorMessage | ||
] | ||
} | ||
}); | ||
} | ||
} | ||
try { | ||
const profile = await User.findOne({ | ||
attributes:{ exclude: ['password', 'status', 'following','userType','createdAt']}, | ||
where:{id}}); | ||
profile.update({updateAt: new Date(),...user, confirmationCode: uuid.v4(), confirmed: 'pending'}); | ||
let message; | ||
if (user.email) { | ||
await sendEmailConfirmationLink({ ...profile.get() }); | ||
message = 'Your email has changed. Please check your email for confirmation' | ||
} else { | ||
message = 'The information was updated successful'; | ||
} | ||
const{ confirmationCode, ...userData } = profile.get(); | ||
return res.status(200).send({ | ||
statu: 200, | ||
message, | ||
user: userData | ||
}) | ||
} catch(error) { | ||
return res.status(520).send({ | ||
errors:{ | ||
body:[ | ||
error.message | ||
] | ||
} | ||
}) | ||
} | ||
} | ||
} | ||
|
||
export default ProfileController; |
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 |
---|---|---|
@@ -1,5 +1,8 @@ | ||
import AuthController from './AuthController'; | ||
import UserController from './UserController'; | ||
import * as MailController from './MailController'; | ||
import ProfileController from './ProfileController'; | ||
|
||
export { AuthController, UserController, MailController, ProfileController }; | ||
|
||
|
||
export { AuthController, UserController, MailController }; |
Oops, something went wrong.