aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/middlewares/validators
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2017-12-22 12:10:40 +0100
committerChocobozzz <me@florianbigard.com>2017-12-22 12:12:33 +0100
commitd3ea89759104e6c14b00443526f2c2a0a13aeb97 (patch)
treee30dc7025f284c163a450cb08f7060875e368e6c /server/middlewares/validators
parentbf1f650817dadfd5eeee9e5e0b6b6938c136e25d (diff)
downloadPeerTube-d3ea89759104e6c14b00443526f2c2a0a13aeb97.tar.gz
PeerTube-d3ea89759104e6c14b00443526f2c2a0a13aeb97.tar.zst
PeerTube-d3ea89759104e6c14b00443526f2c2a0a13aeb97.zip
Begin unit tests
Diffstat (limited to 'server/middlewares/validators')
-rw-r--r--server/middlewares/validators/video-comments.ts21
1 files changed, 11 insertions, 10 deletions
diff --git a/server/middlewares/validators/video-comments.ts b/server/middlewares/validators/video-comments.ts
index 5e1be00f2..1d19fac58 100644
--- a/server/middlewares/validators/video-comments.ts
+++ b/server/middlewares/validators/video-comments.ts
@@ -4,6 +4,7 @@ import { logger } from '../../helpers'
4import { isIdOrUUIDValid, isIdValid } from '../../helpers/custom-validators/misc' 4import { isIdOrUUIDValid, isIdValid } from '../../helpers/custom-validators/misc'
5import { isValidVideoCommentText } from '../../helpers/custom-validators/video-comments' 5import { isValidVideoCommentText } from '../../helpers/custom-validators/video-comments'
6import { isVideoExist } from '../../helpers/custom-validators/videos' 6import { isVideoExist } from '../../helpers/custom-validators/videos'
7import { VideoModel } from '../../models/video/video'
7import { VideoCommentModel } from '../../models/video/video-comment' 8import { VideoCommentModel } from '../../models/video/video-comment'
8import { areValidationErrors } from './utils' 9import { areValidationErrors } from './utils'
9 10
@@ -11,7 +12,7 @@ const listVideoCommentThreadsValidator = [
11 param('videoId').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid videoId'), 12 param('videoId').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid videoId'),
12 13
13 async (req: express.Request, res: express.Response, next: express.NextFunction) => { 14 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
14 logger.debug('Checking blacklistRemove parameters.', { parameters: req.params }) 15 logger.debug('Checking listVideoCommentThreads parameters.', { parameters: req.params })
15 16
16 if (areValidationErrors(req, res)) return 17 if (areValidationErrors(req, res)) return
17 if (!await isVideoExist(req.params.videoId, res)) return 18 if (!await isVideoExist(req.params.videoId, res)) return
@@ -25,11 +26,11 @@ const listVideoThreadCommentsValidator = [
25 param('threadId').custom(isIdValid).not().isEmpty().withMessage('Should have a valid threadId'), 26 param('threadId').custom(isIdValid).not().isEmpty().withMessage('Should have a valid threadId'),
26 27
27 async (req: express.Request, res: express.Response, next: express.NextFunction) => { 28 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
28 logger.debug('Checking blacklistRemove parameters.', { parameters: req.params }) 29 logger.debug('Checking listVideoThreadComments parameters.', { parameters: req.params })
29 30
30 if (areValidationErrors(req, res)) return 31 if (areValidationErrors(req, res)) return
31 if (!await isVideoExist(req.params.videoId, res)) return 32 if (!await isVideoExist(req.params.videoId, res)) return
32 if (!await isVideoCommentThreadExist(req.params.threadId, req.params.videoId, res)) return 33 if (!await isVideoCommentThreadExist(req.params.threadId, res.locals.video, res)) return
33 34
34 return next() 35 return next()
35 } 36 }
@@ -40,7 +41,7 @@ const addVideoCommentThreadValidator = [
40 body('text').custom(isValidVideoCommentText).not().isEmpty().withMessage('Should have a valid comment text'), 41 body('text').custom(isValidVideoCommentText).not().isEmpty().withMessage('Should have a valid comment text'),
41 42
42 async (req: express.Request, res: express.Response, next: express.NextFunction) => { 43 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
43 logger.debug('Checking blacklistRemove parameters.', { parameters: req.params }) 44 logger.debug('Checking addVideoCommentThread parameters.', { parameters: req.params })
44 45
45 if (areValidationErrors(req, res)) return 46 if (areValidationErrors(req, res)) return
46 if (!await isVideoExist(req.params.videoId, res)) return 47 if (!await isVideoExist(req.params.videoId, res)) return
@@ -55,11 +56,11 @@ const addVideoCommentReplyValidator = [
55 body('text').custom(isValidVideoCommentText).not().isEmpty().withMessage('Should have a valid comment text'), 56 body('text').custom(isValidVideoCommentText).not().isEmpty().withMessage('Should have a valid comment text'),
56 57
57 async (req: express.Request, res: express.Response, next: express.NextFunction) => { 58 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
58 logger.debug('Checking blacklistRemove parameters.', { parameters: req.params }) 59 logger.debug('Checking addVideoCommentReply parameters.', { parameters: req.params })
59 60
60 if (areValidationErrors(req, res)) return 61 if (areValidationErrors(req, res)) return
61 if (!await isVideoExist(req.params.videoId, res)) return 62 if (!await isVideoExist(req.params.videoId, res)) return
62 if (!await isVideoCommentExist(req.params.commentId, req.params.videoId, res)) return 63 if (!await isVideoCommentExist(req.params.commentId, res.locals.video, res)) return
63 64
64 return next() 65 return next()
65 } 66 }
@@ -76,7 +77,7 @@ export {
76 77
77// --------------------------------------------------------------------------- 78// ---------------------------------------------------------------------------
78 79
79async function isVideoCommentThreadExist (id: number, videoId: number, res: express.Response) { 80async function isVideoCommentThreadExist (id: number, video: VideoModel, res: express.Response) {
80 const videoComment = await VideoCommentModel.loadById(id) 81 const videoComment = await VideoCommentModel.loadById(id)
81 82
82 if (!videoComment) { 83 if (!videoComment) {
@@ -87,7 +88,7 @@ async function isVideoCommentThreadExist (id: number, videoId: number, res: expr
87 return false 88 return false
88 } 89 }
89 90
90 if (videoComment.videoId !== videoId) { 91 if (videoComment.videoId !== video.id) {
91 res.status(400) 92 res.status(400)
92 .json({ error: 'Video comment is associated to this video.' }) 93 .json({ error: 'Video comment is associated to this video.' })
93 .end() 94 .end()
@@ -107,7 +108,7 @@ async function isVideoCommentThreadExist (id: number, videoId: number, res: expr
107 return true 108 return true
108} 109}
109 110
110async function isVideoCommentExist (id: number, videoId: number, res: express.Response) { 111async function isVideoCommentExist (id: number, video: VideoModel, res: express.Response) {
111 const videoComment = await VideoCommentModel.loadById(id) 112 const videoComment = await VideoCommentModel.loadById(id)
112 113
113 if (!videoComment) { 114 if (!videoComment) {
@@ -118,7 +119,7 @@ async function isVideoCommentExist (id: number, videoId: number, res: express.Re
118 return false 119 return false
119 } 120 }
120 121
121 if (videoComment.videoId !== videoId) { 122 if (videoComment.videoId !== video.id) {
122 res.status(400) 123 res.status(400)
123 .json({ error: 'Video comment is associated to this video.' }) 124 .json({ error: 'Video comment is associated to this video.' })
124 .end() 125 .end()