Skip to content

Commit

Permalink
Merge c5eba1f into 7322627
Browse files Browse the repository at this point in the history
  • Loading branch information
Nziranziza committed Feb 18, 2019
2 parents 7322627 + c5eba1f commit f49022b
Show file tree
Hide file tree
Showing 17 changed files with 657 additions and 398 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ shrinkwrap.yaml

yarn.lock

package-lock.json
# Winston logger files

error.log
Expand Down
12 changes: 12 additions & 0 deletions __tests__/mocks/db.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,17 @@
"id": "578c8188-cba6-45f6-842f-8d80c247b818",
"username": "oemapp",
"email": "oemapp.dev@gmail.com"
},
"profile":{
"firstName": "John",
"lastName": "Doe",
"username": "doe201",
"email": "john.doe@andela.com",
"bio": "I am software at Andela",
"gender": "Male",
"birthDate": "12 June 1999",
"image": "https://planetbotanix.com/wp-content/uploads/2017/08/Female-Avatar-1-300x300.jpg",
"cover": "https://www.eta.co.uk/wp-content/uploads/2012/09/Cycling-by-water-resized-min.jpg"
}

}
198 changes: 198 additions & 0 deletions __tests__/routes/profile.test.js
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();
});
});
3 changes: 3 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import cors from 'cors';
import morgan from 'morgan';
import swaggerUI from 'swagger-ui-express';
import YAML from 'yamljs';
import cookie from 'cookie-parser';
import methodOverride from 'method-override';
import { joiErrors } from './middlewares';
import routes from './routes';
Expand All @@ -14,6 +15,8 @@ const app = express();
const swaggerYAMLDocs = YAML.load('./docs/swagger.yml');

app.use(cors());
app.use(cookie());

app.use(morgan('dev'));
app.use(express.urlencoded({ extended: false }));
app.use(express.json());
Expand Down
109 changes: 109 additions & 0 deletions controllers/ProfileController.js
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;
5 changes: 4 additions & 1 deletion controllers/index.js
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 };
Loading

0 comments on commit f49022b

Please sign in to comment.