Skip to content

Commit

Permalink
Merge 61af403 into 27b4778
Browse files Browse the repository at this point in the history
  • Loading branch information
oesukam committed Apr 11, 2019
2 parents 27b4778 + 61af403 commit d60f0ce
Show file tree
Hide file tree
Showing 33 changed files with 553 additions and 279 deletions.
6 changes: 6 additions & 0 deletions __mocks__/mockRequest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
const mockRequest = ({ user, body }) => ({
user,
body
});

export default mockRequest;
8 changes: 8 additions & 0 deletions __mocks__/mockResponse.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const mockResponse = () => {
const res = {};
res.status = jest.fn().mockReturnValue(res);
res.json = jest.fn().mockReturnValue(res);
return res;
};

export default mockResponse;
7 changes: 7 additions & 0 deletions __mocks__/socialLogin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const socialLogin = async (req, res) =>
res.status(200).json({
status: 200,
user: req.user
});

export default socialLogin;
13 changes: 7 additions & 6 deletions __tests__/middlewares/verifyJWT.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import express from 'express';
import request from 'supertest';
import bcrypt from 'bcrypt';
import jwt from 'jsonwebtoken';
import { Op } from 'sequelize';
import { verifyJwt } from '../../middlewares';
import app from '../../app';
import { User, Token } from '../../database/models';
Expand All @@ -12,7 +13,6 @@ const { JWT_SECRET } = process.env;
const appTest = express();
const router = express.Router();


router.get('/testJwtWithUser', verifyJwt({ access: ['user'] }));
router.get('/testJwtwithSuperAdmin', verifyJwt({ access: ['super-admin'] }));
router.get('/testJwtwithAdmin', verifyJwt({ access: ['admin'] }));
Expand All @@ -21,8 +21,12 @@ router.get('/testWithConfirmEmail', verifyJwt({ confirmEmail: true }));
appTest.use(router);
let admin;
let user2;
jest.setTimeout(30000);
describe('verifyJWT', () => {
beforeAll(async () => {
await User.destroy({
where: { [Op.or]: [{ username: signupUser.username }, { username: signupUser2.username }] }
}).then(() => true);
const encryptedPassword = bcrypt.hashSync(signupUser.password, 10);
const encryptedPassword2 = bcrypt.hashSync(signupUser2.password, 10);
await User.create({
Expand Down Expand Up @@ -57,11 +61,8 @@ describe('verifyJWT', () => {
});
afterAll(async () => {
await User.destroy({
where: { id: admin.id }
});
await User.destroy({
where: { id: user2.id }
});
where: { [Op.or]: [{ username: signupUser.username }, { username: signupUser2.username }] }
}).then(() => true);
});

test('it should fail without user token for user only router', async () => {
Expand Down
34 changes: 28 additions & 6 deletions __tests__/routes/accessControl.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import request from 'supertest';
import bcrypt from 'bcrypt';
import { Op } from 'sequelize';
import app from '../../app';
import { User } from '../../database/models';
import { urlPrefix } from '../mocks/variables.json';
Expand All @@ -13,8 +14,15 @@ let admin;
describe('RBAC', () => {
beforeAll(async () => {
await User.destroy({
where: {}
});
where: {
[Op.or]: [
{ email: signupUser.email },
{ email: signupUser2.email },
{ username: 'superAdmin' },
{ username: 'admin' }
]
}
}).then(() => true);
const encryptedPassword = bcrypt.hashSync('123456', 10);
await User.create({
username: 'superAdmin',
Expand Down Expand Up @@ -79,13 +87,20 @@ describe('RBAC', () => {
});

afterAll(async () => {
await User.destroy({ where: { id: superAdmin.id } });
await User.destroy({ where: { id: user1.id } });
await User.destroy({ where: { id: user2.id } });
await User.destroy({ where: { id: admin.id } });
await User.destroy({
where: {
[Op.or]: [
{ email: signupUser.email },
{ email: signupUser2.email },
{ username: 'superAdmin' },
{ username: 'admin' }
]
}
}).then(() => true);
});

test('should not grant access if user not admin', async () => {
expect.assertions(3);
const res = await request(app)
.put(`${urlPrefix}/users/${user1.username}/grant`)
.set('authorization', user1.token)
Expand All @@ -97,6 +112,7 @@ describe('RBAC', () => {
});

test('should not grant access with invalid input', async () => {
expect.assertions(2);
const res = await request(app)
.put(`${urlPrefix}/users/${user1.username}/grant`)
.set('authorization', superAdmin.token)
Expand All @@ -107,6 +123,7 @@ describe('RBAC', () => {
});

test('should grant access', async () => {
expect.assertions(4);
const res = await request(app)
.put(`${urlPrefix}/users/${user1.username}/grant`)
.set('authorization', superAdmin.token)
Expand All @@ -119,6 +136,8 @@ describe('RBAC', () => {
});

test('should inform a user in case role is already granted', async () => {
expect.assertions(3);
await User.update({ userType: 'admin' }, { where: { username: user1.username } });
const res = await request(app)
.put(`${urlPrefix}/users/${user1.username}/grant`)
.set('authorization', superAdmin.token)
Expand All @@ -130,6 +149,7 @@ describe('RBAC', () => {
});

test('should inform a user in case role is already granted', async () => {
expect.assertions(3);
const res = await request(app)
.put(`${urlPrefix}/users/${user2.username}/grant`)
.set('authorization', superAdmin.token)
Expand All @@ -141,6 +161,7 @@ describe('RBAC', () => {
});

test('should not grant access if user does not exist', async () => {
expect.assertions(3);
const fakeName = 'rtvdr';
const res = await request(app)
.put(`${urlPrefix}/users/${fakeName}/grant`)
Expand All @@ -153,6 +174,7 @@ describe('RBAC', () => {
});

test('should not grant super-admin when you are an admin', async () => {
expect.assertions(3);
const res = await request(app)
.put(`${urlPrefix}/users/${user2.username}/grant`)
.set('authorization', admin.token)
Expand Down
20 changes: 15 additions & 5 deletions __tests__/routes/article.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ const email = 'test_login@gmail.com';
const username = 'test_login';
const password = '123456';
const fakeSlug = 'fake-slug';
jest.setTimeout(30000);

describe('articles', () => {
beforeAll(async done => {
Expand Down Expand Up @@ -79,6 +80,17 @@ describe('articles', () => {
});

afterAll(async () => {
await User.destroy({
where: {
[Op.or]: [
{ email: signupUser.email },
{ email },
{ username: 'test_login' },
{ username: 'test_login1' },
{ username: 'admin_test' }
]
}
}).then(() => true);
await User.destroy({
where: {
[Op.or]: [
Expand Down Expand Up @@ -165,12 +177,11 @@ describe('articles', () => {
expect(res.body.articlesCount).toBeDefined();
});

test('Fetch Articles - should return articles by favorited tag test', async () => {
expect.assertions(4);
test('Fetch Articles - should return articles by favorited', async () => {
expect.assertions(3);
const res = await request(app).get(`${urlPrefix}/articles?favorited=${loginUser1.username}`);
expect(res.status).toBe(200);
expect(res.body.articles).toBeDefined();
expect(res.body.articles[0].tagList).toContain('test');
expect(res.body.articlesCount).toBeDefined();
});

Expand Down Expand Up @@ -269,7 +280,7 @@ describe('articles', () => {
test('like an article', async () => {
expect.assertions(3);
const article = await Article.findOne({ where: { slug: newArticle.slug } });
article.update({ status: 'published' });
await article.update({ status: 'published' });
const res = await request(app)
.post(`${urlPrefix}/articles/${newArticle.slug}/like`)
.set('Authorization', loginUser2.token);
Expand Down Expand Up @@ -523,5 +534,4 @@ describe('articles', () => {
expect(res.status).toBe(200);
expect(res.body.articles).toBeDefined();
});

});
18 changes: 15 additions & 3 deletions __tests__/routes/comment.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,12 @@ let newArticle;
let newComment;
jest.setTimeout(50000);
describe('comments', () => {
beforeAll(async () => {
beforeAll(async done => {
await User.destroy({
where: {
[Op.or]: [{ email: signupUser.email }, { email: signupUser2.email }]
}
}).then(() => true);
const encryptedPassword = bcrypt.hashSync(signupUser.password, 10);
const encryptedPassword2 = bcrypt.hashSync(signupUser2.password, 10);
await User.create({
Expand All @@ -40,6 +45,7 @@ describe('comments', () => {
userId: loginUser1.id
});
newArticle = res3.get();
done();
});

afterAll(async () => {
Expand All @@ -48,7 +54,9 @@ describe('comments', () => {
[Op.or]: [{ email: signupUser.email }, { email: signupUser2.email }]
}
}).then(() => true);
await Article.destroy({ where: { id: newArticle.id } });
await Article.destroy({
where: [{ id: newArticle.id }, { tagList: { [Op.contains]: ['test'] } }]
});
await Comment.destroy({
where: {
[Op.or]: [{ userId: loginUser1.id }, { userId: loginUser2.id }]
Expand Down Expand Up @@ -286,7 +294,11 @@ describe('comments', () => {

test('Comment history - should fail to return old version', async done => {
const res = await request(app)
.get(`${urlPrefix}/articles/${newArticle.slug}/comments/0ded7537-c7c2-4d4c-84d8-e941c84e965f/edited`)
.get(
`${urlPrefix}/articles/${
newArticle.slug
}/comments/0ded7537-c7c2-4d4c-84d8-e941c84e965f/edited`
)
.set('Authorization', loginUser1.token);
expect(res.status).toBe(404);
expect(res.body.status).toBe(404);
Expand Down
1 change: 1 addition & 0 deletions __tests__/routes/likeComment.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { createArticle, signupUser, createComment } from '../mocks/db.json';
let testToken, testComment, testArticle;
describe('likeComment', () => {
beforeAll(async () => {
await User.destroy({ where: { email: signupUser.email } });
const encryptedPassword = bcrypt.hashSync(signupUser.password, 10);
await User.create({
...signupUser,
Expand Down
7 changes: 0 additions & 7 deletions __tests__/routes/profile.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -284,13 +284,6 @@ describe('Profile', () => {
done();
});

test('should return page is required', async done => {
expect.assertions(2);
const res = await request(app).get(`${urlPrefix}/profiles`);
expect(res.body.message).toBe('Bad Request');
expect(res.body.errors[0].message).toBe('"page" is required');
done();
});
test('Should return page does not exist', async done => {
expect.assertions(3);
const res = await request(app).get(`${urlPrefix}/profiles?page=1000`);
Expand Down
17 changes: 8 additions & 9 deletions __tests__/routes/rating.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ describe('5 star Rating', () => {
}
});
const encryptedPassword = bcrypt.hashSync(signupUser.password, 10);
const signUpUser = {...signupUser, email: 'newTest@email.com'};
const signUpUser = { ...signupUser, email: 'newTest@email.com' };
await User.create({
...signUpUser,
confirmed: 'confirmed',
Expand Down Expand Up @@ -56,12 +56,11 @@ describe('5 star Rating', () => {
test('should rate an article', async () => {
expect.assertions(4);
const article = await Article.findOne({ where: { slug: articleSlug } });
article.update({ status: 'published' });
await article.update({ status: 'published' });
const res = await request(app)
.post(`${urlPrefix}/articles/${articleSlug}/rating`)
.set('Authorization', testUser.token)
.send({ rate: 3 });

expect(res.status).toBe(201);
expect(res.body.message).toBe('article has been rated successfully');
expect(res.body.rate.rating).toBe(3);
Expand All @@ -70,7 +69,7 @@ describe('5 star Rating', () => {
test('should overide an existing article rating', async () => {
expect.assertions(4);
const article = await Article.findOne({ where: { slug: articleSlug } });
article.update({ status: 'published' });
await article.update({ status: 'published' });
const res = await request(app)
.post(`${urlPrefix}/articles/${articleSlug}/rating`)
.set('Authorization', testUser.token)
Expand Down Expand Up @@ -114,7 +113,7 @@ describe('5 star Rating', () => {
test('should not delete rate form unexisting article', async () => {
expect.assertions(2);
const article = await Article.findOne({ where: { slug: articleSlug } });
article.update({ status: 'unpublished' });
await article.update({ status: 'unpublished' });
const res = await request(app)
.delete(`${urlPrefix}/articles/${articleSlug}/rating`)
.set('authorization', testUser.token);
Expand All @@ -125,7 +124,7 @@ describe('5 star Rating', () => {
test('should delete rate from article', async () => {
expect.assertions(2);
const article = await Article.findOne({ where: { slug: articleSlug } });
article.update({ status: 'published' });
await article.update({ status: 'published' });
const res = await request(app)
.delete(`${urlPrefix}/articles/${articleSlug}/rating`)
.set('authorization', testUser.token);
Expand Down Expand Up @@ -153,7 +152,7 @@ describe('5 star Rating', () => {
test('should not get rating for unpublished article', async () => {
expect.assertions(3);
const article = await Article.findOne({ where: { slug: articleSlug } });
article.update({ status: 'unpublished' });
await article.update({ status: 'unpublished' });
const res = await request(app).get(`${urlPrefix}/articles/${articleSlug}/rating`);

expect(res.status).toBe(404);
Expand All @@ -163,9 +162,9 @@ describe('5 star Rating', () => {
test('should get rating for a given article', async () => {
expect.assertions(6);
const article = await Article.findOne({ where: { slug: articleSlug } });
article.update({ status: 'published' });
await article.update({ status: 'published' });
const rate = await Favorite.findOne({ where: { articleId: article.get().id } });
rate.update({ rating: 4 });
await rate.update({ rating: 4 });
const res = await request(app).get(`${urlPrefix}/articles/${articleSlug}/rating`);

expect(res.status).toBe(200);
Expand Down
Loading

0 comments on commit d60f0ce

Please sign in to comment.