Skip to content

Commit

Permalink
reporting(testing) add testing for reporting article endpoint
Browse files Browse the repository at this point in the history
[Contimue #167313418]
  • Loading branch information
salviosage committed Aug 27, 2019
1 parent 9e84226 commit c99ccf0
Show file tree
Hide file tree
Showing 5 changed files with 350 additions and 32 deletions.
64 changes: 43 additions & 21 deletions src/controllers/report.comtroller.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,17 @@ class Report {
*/
static async getAllReport(req, res) {
const counter = await db.count();
const { offset, limit } = paginate(req.param.page, req.param.limit, counter);

const reports = await reportService.getAllReport(offset, limit);
if (!reports) {
if (counter === 0) {
util.setError(200, 'No reports found');
return util.send(res);
}
util.setSuccess(200, 'Reports retrieved successfully', reports);
const { offset, limit } = paginate(req.param.page, req.param.limit, counter);
const reports = await reportService.getAllReport(offset, limit);
const response = {
count: counter,
reports,
};
util.setSuccess(200, 'Reports retrieved successfully', response);
return util.send(res);
}

Expand All @@ -45,16 +48,25 @@ class Report {
* @memberof Report
*/
static async getMyReport(req, res) {
const counter = await db.count({ where: { userId: req.auth.id } });
const { offset, limit } = paginate(req.param.page, req.param.limit, counter);
try {
const counter = await db.count({ where: { userId: req.auth.id } });
if (counter === 0) {
util.setError(200, 'No reports found');
return util.send(res);
}
const { offset, limit } = paginate(req.param.page, req.param.limit, counter);

const reports = await reportService.getMyReport(offset, limit, req.auth.id);
if (!reports) {
util.setError(200, 'No reports found');
const reports = await reportService.getMyReport(offset, limit, req.auth.id);
const response = {
count: counter,
yourReport: reports,
};
util.setSuccess(200, 'Your reports retrieved successfully', response);
return util.send(res);
} catch (error) {
util.setError(500, 'server error contact admin');
return util.send(res);
}
util.setSuccess(200, 'Your reports retrieved successfully', reports);
return util.send(res);
}

/**
Expand All @@ -67,16 +79,25 @@ class Report {
* @memberof Report
*/
static async getReportsForArticle(req, res) {
const counter = await db.count({ where: { articleSlug: req.params.Article } });
const { offset, limit } = paginate(req.param.page, req.param.limit, counter);
const articleSlug = req.params.Article;
const reports = await reportService.getAllReportForOneArticle(offset, limit, articleSlug);
if (!reports) {
util.setError(200, 'This article is not yet Reported');
try {
const counter = await db.count({ where: { articleSlug: req.params.Article } });
if (counter === 0) {
util.setError(200, 'This article is not yet Reported');
return util.send(res);
}
const { offset, limit } = paginate(req.param.page, req.param.limit, counter);
const articleSlug = req.params.Article;
const reports = await reportService.getAllReportForOneArticle(offset, limit, articleSlug);
const response = {
count: counter,
reports,
};
util.setSuccess(200, 'Reports retrieved successfully', response);
return util.send(res);
} catch (error) {
util.setError(500, 'server error contact admin');
return util.send(res);
}
util.setSuccess(200, 'Reports retrieved successfully', reports);
return util.send(res);
}

/**
Expand Down Expand Up @@ -135,7 +156,8 @@ class Report {
const reportArticle = await reportService.report(report);
const newReport = {
reason: reportArticle.reason,
ArticleSlug: reportArticle.articleSlug,
articleSlug: reportArticle.articleSlug,
id: reportArticle.id
};
util.setSuccess(200, 'Article Successfully reported', newReport);
return util.send(res);
Expand Down
4 changes: 0 additions & 4 deletions src/middlewares/likes.middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,6 @@ const util = new Util();
export default async (req, res, next) => {
try {
const user = await models.user.findOne({ where: { email: req.auth.email } });
if (!user) {
util.setError(404, 'you are anonimous');
return util.send(res);
}
const userId = user.id;
const ArticleSlug = req.params.Article;
const post = await models.Article.findOne({ where: { slug: ArticleSlug } });
Expand Down
4 changes: 0 additions & 4 deletions src/middlewares/report.middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,6 @@ export default async (req, res, next) => {
return util.send(res);
}
const user = await models.user.findOne({ where: { email: req.auth.email } });
if (!user) {
util.setError(404, 'you are anonimous');
return util.send(res);
}
const userId = user.id;
if (userId === post.authorId) {
util.setError(403, 'Sorry you can not report your article.');
Expand Down
6 changes: 3 additions & 3 deletions src/services/report.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class ReportService {
try {
return await db.findAll({
attributes: {
exclude: ['id', 'updatedAt']
exclude: ['updatedAt']
},
// include: [{ model: users, username }],
order: [
Expand Down Expand Up @@ -66,7 +66,7 @@ class ReportService {
return await db.findAll({
where: { articleSlug },
attributes: {
exclude: ['id', 'updatedAt']
exclude: ['updatedAt']
},
// include: [{ model: users, username }],
order: [
Expand Down Expand Up @@ -95,7 +95,7 @@ class ReportService {
return await db.findAll({
where: { userId },
attributes: {
exclude: ['id', 'updatedAt', 'userId']
exclude: ['updatedAt', 'userId']
},
order: [
['createdAt', 'DESC']
Expand Down
Loading

0 comments on commit c99ccf0

Please sign in to comment.