Skip to content

Commit

Permalink
bug(fixing create article mocking tests):Only cloudinary function is …
Browse files Browse the repository at this point in the history
…mocked instead of the whole creating article route [Finishes #168000401]
  • Loading branch information
habinezadalvan committed Aug 20, 2019
1 parent 2be12b4 commit 95900ce
Show file tree
Hide file tree
Showing 12 changed files with 13 additions and 65 deletions.
13 changes: 2 additions & 11 deletions src/controllers/articles.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ import 'dotenv/config';
import slug from 'slug';
import _ from 'lodash';
import uniqid from 'uniqid';
import cloudinary from 'cloudinary';
import models from '../models';
import Userservice from '../services/user.service';
import articleService from '../services/article.service';
import Helper from '../helpers/helper';
import cloudinaryHelper from '../helpers/cloudinaryHelper';


const db = models.Article;
Expand All @@ -28,23 +28,15 @@ class Articles {
static async createArticles(req, res) {
const userId = req.auth.id;
const findUser = await Userservice.getOneUser(userId);
let images = req.files;
images = await Promise.all(
images.map(async (file) => {
const { secure_url } = await cloudinary.v2.uploader.upload(file.path);
return secure_url;
})
);
const images = await cloudinaryHelper(req.files);

if (findUser) {
const { title } = req.body;
const tags = req.body.taglist.split(' ');
const article = {
slug: `${slug(title)}-${uniqid()}`,
title,
description: req.body.description,
body: req.body.body,
taglist: tags,
authorId: req.auth.id,
images
};
Expand All @@ -56,7 +48,6 @@ class Articles {
title: createdArticle.title,
description: createdArticle.description,
body: createdArticle.body,
taglist: createdArticle.taglist,
flagged: createdArticle.flagged,
favorited: createdArticle.favorited,
favoritedcount: createdArticle.favoritedcount,
Expand Down
11 changes: 11 additions & 0 deletions src/helpers/cloudinaryHelper.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import 'dotenv/config';
import cloudinary from 'cloudinary';

export default async (images = []) => Promise.all(
images.map(async (file) => {
const { secure_url } = process.env.NODE_ENV === 'test'
? 'image.jpg'
: await cloudinary.v2.uploader.upload(file.path);
return secure_url;
})
);
9 changes: 0 additions & 9 deletions src/middlewares/fakecloud.js

This file was deleted.

2 changes: 0 additions & 2 deletions src/middlewares/validators/socialLogin-mock.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ dotenv.config();
export const mock = (req, res, next) => {
if (req.url.includes('login/')) {
res.redirect(`/auth/fake?provider=${req.url.split('/')[2]}`);
} else if (req.url.includes('articles') && req.method === 'POST') {
res.redirect(307, '/fake');
} else {
next();
}
Expand Down
3 changes: 0 additions & 3 deletions src/migrations/20190813125249-create-article.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,6 @@ module.exports = {
type: Sequelize.TEXT,
allowNull: false,
},
taglist: {
type: Sequelize.ARRAY(Sequelize.STRING)
},
favorited: {
type: Sequelize.BOOLEAN,
allowNull: true,
Expand Down
1 change: 0 additions & 1 deletion src/models/article.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ module.exports = (sequelize, DataTypes) => {
title: { type: DataTypes.STRING, allowNull: false },
description: { type: DataTypes.TEXT, allowNull: false },
body: { type: DataTypes.TEXT, allowNull: false },
taglist: { type: DataTypes.ARRAY(DataTypes.STRING), allowNull: true },
favorited: { type: DataTypes.BOOLEAN, allowNull: true, defaultValue: false },
favoritedcount: { type: DataTypes.INTEGER, defaultValue: 0 },
flagged: { type: DataTypes.BOOLEAN, defaultValue: false },
Expand Down
3 changes: 0 additions & 3 deletions src/routes/api/article/article.routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,10 @@ import articleController from '../../../controllers/articles.controller';
import imageUpload from '../../../middlewares/multer';
import validate from '../../../middlewares/validators/general.validation';
import { schema } from '../../../middlewares/validators/schemas';
import fakeCloud from '../../../middlewares/fakecloud';
import confirmEmailAuth from '../../../middlewares/emailVarification.middleware';

const router = express.Router();

router.post('/fake', auth, fakeCloud, validate(schema.articleSchema), articleController.createArticles);


router.post('/articles', [auth, confirmEmailAuth], imageUpload.array('images', 10), validate(schema.articleSchema), articleController.createArticles);
router.get('/articles', [auth, confirmEmailAuth], articleController.getAllArticles);
Expand Down
2 changes: 0 additions & 2 deletions src/routes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import oauth from './api/oauth/oauth.routes';
import error from '../middlewares/error.middleware';
import notfound from '../middlewares/404.middleware';
import { mock } from '../middlewares/validators/socialLogin-mock';
import article from './api/article/article.routes';

dotenv.config();

Expand Down Expand Up @@ -38,7 +37,6 @@ const apiVersion = process.env.API_VERSION;
const baseUrl = `/api/${apiVersion}`;

router.get('/', (req, res) => res.status(200).json({ status: 200, data: 'Welcome to Authors Haven.' }));
router.use(article);
router.use(baseUrl, api);
router.use(oauth);

Expand Down
1 change: 0 additions & 1 deletion src/seeders/20190814094408-demo-article.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ export default {
title: 'faketitle',
description: 'fakedescription',
body: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum',
taglist: ['asdf', 'sdfsf'],
favorited: true,
favoritedcount: 12,
flagged: true,
Expand Down
1 change: 0 additions & 1 deletion src/seeders/20190820133339-demo-article-2.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 0 additions & 30 deletions test/mock/mock.cloud.js

This file was deleted.

2 changes: 0 additions & 2 deletions test/test.articles.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ describe('Articles', () => {
.post('/api/v1/articles')
.set('Content-Type', 'application/json')
.set('Authorization', usertoken)
.field('tagList', 'tag1, tag2, tag3')
.attach('images', fs.readFileSync(`${__dirname}/mock/pic.jpeg`), 'pic.jpeg')
.end((err, res) => {
expect(res.status).to.be.deep.equal(400);
Expand All @@ -34,7 +33,6 @@ describe('Articles', () => {
.field('title', 'Title')
.field('body', 'body field')
.field('description', 'description is here')
.field('tagList', 'tag1, tag2, tag3')
.attach('images', fs.readFileSync(`${__dirname}/mock/pic.jpeg`), 'pic.jpeg')
.end((err, res) => {
expect(res.status).to.be.deep.equal(201);
Expand Down

0 comments on commit 95900ce

Please sign in to comment.