Skip to content

Commit

Permalink
refactored mockdata
Browse files Browse the repository at this point in the history
  • Loading branch information
mezlet committed Mar 14, 2019
1 parent ebafe9f commit b349a8c
Show file tree
Hide file tree
Showing 8 changed files with 9,966 additions and 65 deletions.
9,905 changes: 9,905 additions & 0 deletions package-lock.json

Large diffs are not rendered by default.

18 changes: 10 additions & 8 deletions server/controllers/auth-controller.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import '@babel/polyfill';
import db from '../models';
import { registerSchema } from '../utils/validators';
import env from '../config/env-config';
Expand Down Expand Up @@ -60,7 +59,7 @@ class UsersController {
generateVerificationLink(verificationToken)
)
.then(() => {
successResponse(res, { user, emailSent: true }, 201);
return successResponse(res, { user, emailSent: true }, 201);
})
.catch(() => {
successResponse(res, { user, emailSent: false }, 201);
Expand Down Expand Up @@ -99,7 +98,7 @@ class UsersController {
([rowsAffected]) => {
if (!rowsAffected) errorResponse(res, 'no user found to verify', 400);
else if (rowsAffected === 1) {
successResponse(res, { verified: true }, 200);
return successResponse(res, { verified: true }, 200);
}
}
);
Expand All @@ -114,14 +113,17 @@ class UsersController {
'invalid token'
].includes(e.message)
) {
errorResponse(res, 'Invalid token, verification unsuccessful', 400);
} else {
errorResponse(
return errorResponse(
res,
'Something went wrong, verification unsuccessful',
500
'Invalid token, verification unsuccessful',
400
);
}
errorResponse(
res,
'Something went wrong, verification unsuccessful',
500
);
}
}

Expand Down
11 changes: 5 additions & 6 deletions server/controllers/follow-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export default class Follow {
errorResponse(
res,
{
message: 'you cannot follow yourself'
message: 'you are not allowed to yourself'
},
400
);
Expand Down Expand Up @@ -80,8 +80,7 @@ export default class Follow {
});
if (users.length < 1) {
return successResponse(res, {
message: 'nobody is currently following you',
followers: users
message: 'nobody following you currently'
});
}
const num = users.length;
Expand Down Expand Up @@ -117,7 +116,7 @@ export default class Follow {
});
if (following.length < 1) {
return successResponse(res, {
message: 'you are not following anyone',
message: 'you are currently not following anyone',
following
});
}
Expand All @@ -144,13 +143,13 @@ export default class Follow {
try {
if (username === req.user.username) {
return errorResponse(res, {
message: 'you cannot unfollow yourself'
message: 'you are not allowed to unfollow yourself'
});
}

await Follower.destroy({ where: { userId: id } });
return successResponse(res, {
message: `${username} has been unfollowed`
message: `you just unfollowed ${username}`
});
} catch (err) {
return errorResponse(res, err.message);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { errorResponse, verifyToken } from '../utils/helpers';

const { User } = db;
/**
*Users endpoint middlewares
*Users endpoint middleware
*
* @export
* @class UsersMiddleware
Expand Down
15 changes: 15 additions & 0 deletions server/routes/profile-routes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { Router } from 'express';
import User from '../middlewares/users-middleware';
import profileController from '../controllers/profile-controller';
import followcontroller from '../controllers/follow-controller';

const router = new Router();
router.get('/', User.validUser, profileController.getAll);
router.get('/:username', User.validUser, profileController.getProfile);
router.post('/:username/follow', User.validUser, followcontroller.followUser);
router.delete(
'/:username/follow',
User.validUser,
followcontroller.unfollowUser
);
export default router;
2 changes: 1 addition & 1 deletion server/routes/user-routes.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Router } from 'express';
import User from '../middlewares/index';
import User from '../middlewares/users-middleware';
import userController from '../controllers/user-controller';
import followController from '../controllers/follow-controller';

Expand Down
22 changes: 11 additions & 11 deletions test/mockdata/mock.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import faker from 'faker';

export const user1 = {
username: faker.internet.userName(),
email: faker.internet.email(),
password: 'esiaguleticia1234'

};
export const user2 = {
username: faker.internet.userName(),
email: faker.internet.email(),
bio: 'i am not going',
};
/**
* @function MockData
* @return {object} user
*/
export default function mockData() {
return {
username: faker.random.alphaNumeric(10),
email: faker.internet.email(),
password: faker.random.alphaNumeric(10)
};
}
56 changes: 18 additions & 38 deletions test/users.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,13 @@ import chaiHttp from 'chai-http';
import faker from 'faker';
import server from '../server';
import { generateToken } from '../server/utils/helpers';
import generateRandomUser from './mockdata/mock';

chai.use(chaiHttp);

const user1 = {
username: faker.random.alphaNumeric(10),
email: faker.internet.email(),
password: faker.random.alphaNumeric(10)
};

const user2 = {
username: faker.random.alphaNumeric(10),
email: faker.internet.email(),
password: faker.random.alphaNumeric(10)
};
const user1 = generateRandomUser();
const user2 = generateRandomUser();
const { username, password, email } = generateRandomUser();

describe('Authentication', () => {
const tokenPayload = {};
Expand Down Expand Up @@ -63,7 +56,6 @@ describe('Authentication', () => {
expect(res).to.have.status(409);
expect(res.body.errors).to.be.an('Array');
expect(res.body.errors[0]).to.contain('already exists');
expect(res.body);
done(err);
});
});
Expand All @@ -77,7 +69,6 @@ describe('Authentication', () => {
expect(res).to.have.status(409);
expect(res.body.errors).to.be.an('Array');
expect(res.body.errors[0]).to.contain('already exists');
expect(res.body);
done(err);
});
});
Expand All @@ -87,15 +78,14 @@ describe('Authentication', () => {
.request(server)
.post(baseUrl)
.send({
username: faker.internet.userName(),
username,
email: 'invalid email',
password: 'jakejake'
password
})
.end((err, res) => {
expect(res).to.have.status(400);
expect(res.body.errors).to.be.an('object');
expect(res.body.errors).to.haveOwnProperty('email');
expect(res.body);
done(err);
});
});
Expand All @@ -105,15 +95,14 @@ describe('Authentication', () => {
.request(server)
.post(baseUrl)
.send({
username: faker.internet.userName(),
email: faker.internet.email(),
username,
email,
password: 'short'
})
.end((err, res) => {
expect(res).to.have.status(400);
expect(res.body.errors).to.be.an('object');
expect(res.body.errors).to.haveOwnProperty('password');
expect(res.body);
done(err);
});
});
Expand All @@ -123,15 +112,14 @@ describe('Authentication', () => {
.request(server)
.post(baseUrl)
.send({
username: faker.internet.userName(),
email: faker.internet.email(),
username,
email,
password: '!&@*@(@)!)!'
})
.end((err, res) => {
expect(res).to.have.status(400);
expect(res.body.errors).to.be.an('object');
expect(res.body.errors).to.haveOwnProperty('password');
expect(res.body);
done(err);
});
});
Expand Down Expand Up @@ -270,11 +258,10 @@ describe('User', () => {
let loggedInUser;

before(() => {
const { email, password } = user1;
return chai
.request(server)
.post('/api/users/login')
.send({ email, password })
.send({ email: user1.email, password: user1.password })
.then(res => {
loggedInUser = res.body.user;
});
Expand Down Expand Up @@ -417,10 +404,9 @@ describe('User', () => {
});

it('should get profile of a specific user', done => {
const { username } = user2;
chai
.request(server)
.get(`/api/profiles/${username}`)
.get(`/api/profiles/${user2.username}`)
.set({ authorization: loggedInUser.token })
.end((err, res) => {
const { user } = res.body;
Expand All @@ -435,10 +421,9 @@ describe('User', () => {
});

it('should return an error if profile is not found', done => {
const username = faker.random.alphaNumeric(10);
chai
.request(server)
.get(`/api/profiles/${username}`)
.get(`/api/profiles/andela`)
.set({ authorization: loggedInUser.token })
.end((err, res) => {
expect(res).to.have.status(404);
Expand Down Expand Up @@ -532,10 +517,9 @@ describe('User', () => {
});

it('should follow a user', done => {
const { username } = user2;
chai
.request(server)
.post(`/api/profiles/${username}/follow`)
.post(`/api/profiles/${user2.username}/follow`)
.set({ authorization: loggedInUser.token })
.end((err, res) => {
expect(res).to.have.status(200);
Expand All @@ -544,10 +528,9 @@ describe('User', () => {
});

it('should return an error is user has already followed a particular user', done => {
const { username } = user2;
chai
.request(server)
.post(`/api/profiles/${username}/follow`)
.post(`/api/profiles/${user2.username}/follow`)
.set({ authorization: loggedInUser.token })
.end((err, res) => {
expect(res).to.have.status(400);
Expand All @@ -556,10 +539,9 @@ describe('User', () => {
});

it('should return an error is username is current user', done => {
const { username } = user1;
chai
.request(server)
.post(`/api/profiles/${username}/follow`)
.post(`/api/profiles/${user1.username}/follow`)
.set({ authorization: loggedInUser.token })
.end((err, res) => {
expect(res).to.have.status(400);
Expand All @@ -579,10 +561,9 @@ describe('User', () => {
});

it('should unfollow a user', done => {
const { username } = user2;
chai
.request(server)
.delete(`/api/profiles/${username}/follow`)
.delete(`/api/profiles/${user2.username}/follow`)
.set({ authorization: loggedInUser.token })
.end((err, res) => {
expect(res).to.have.status(200);
Expand All @@ -591,10 +572,9 @@ describe('User', () => {
});

it('should return an error if user try to unfollow themself', done => {
const { username } = user1;
chai
.request(server)
.delete(`/api/profiles/${username}/follow`)
.delete(`/api/profiles/${user1.username}/follow`)
.set({ authorization: loggedInUser.token })
.end((err, res) => {
expect(res).to.have.status(500);
Expand Down

0 comments on commit b349a8c

Please sign in to comment.