sbkwgh преди 8 години
родител
ревизия
c2e272f3b1
променени са 2 файла, в които са добавени 28 реда и са изтрити 27 реда
  1. 12 11
      routes/post.js
  2. 16 16
      test/thread_post.js

+ 12 - 11
routes/post.js

@@ -23,16 +23,16 @@ router.post('/', async (req, res) => {
 		if(req.body.content === undefined) {
 			validationErrors.push(Errors.missingParameter('content'))
 		} else if(typeof req.body.content !== 'string') {
-			validationErrors.push(Errors.invalidParamterType('content', 'string'))
+			validationErrors.push(Errors.invalidParameterType('content', 'string'))
 		} if (req.body.threadId === undefined) {
 			validationErrors.push(Errors.missingParameter('threadId'))
-		} else if(!Object.isInteger(req.body.threadId)) {
-			validationErrors.push(Errors.invalidParamterType('threadId', 'integer'))
-		} if(req.body.replyingToId !== undefined && !Object.isInteger(req.body.replyingToId)) {
-			validationErrors.push(Errors.invalidParamterType('replyingToId', 'integer'))
+		} else if(!Number.isInteger(req.body.threadId)) {
+			validationErrors.push(Errors.invalidParameterType('threadId', 'integer'))
+		} if(req.body.replyingToId !== undefined && !Number.isInteger(req.body.replyingToId)) {
+			validationErrors.push(Errors.invalidParameterType('replyingToId', 'integer'))
 		}
 
-		if(validationErrors) throw Errors.VALIDATION_ERROR
+		if(validationErrors.length) throw Errors.VALIDATION_ERROR
 
 		post = await Post.create({
 			content: req.body.content
@@ -45,7 +45,7 @@ router.post('/', async (req, res) => {
 			username: req.session.username
 		}})
 
-		if(!thread) throw Errors.invalidParamter('threadId', 'thread does not exist')
+		if(!thread) throw Errors.invalidParameter('threadId', 'thread does not exist')
 
 		await post.setUser(user)
 		await post.setThread(thread)
@@ -57,9 +57,9 @@ router.post('/', async (req, res) => {
 			}, include: [Thread] })
 
 			if(!replyingToPost) {
-				throw Errors.invalidParamter('replyingToId', 'post does not exist')
+				throw Errors.invalidParameter('replyingToId', 'post does not exist')
 			} else if(replyingToPost.Thread.id !== thread.id) {
-				throw Errors.invalidParamter('replyingToId', 'replies must be in same thread')
+				throw Errors.invalidParameter('replyingToId', 'replies must be in same thread')
 			} else {
 				await post.setReplyingTo(replyingToPost)
 				await replyingToPost.addReplies(post)
@@ -81,12 +81,13 @@ router.post('/', async (req, res) => {
 			res.json({
 				errors: validationErrors
 			})
-		} else if(e.name === 'invalidParamter') {
+		} else if(e.name === 'invalidParameter') {
 			res.status(400)
 			res.json({
 				errors: [e]
 			})
 		} else {
+			console.log(e)
 			res.status(500)
 			res.json({
 				errors: [Errors.unknown]
@@ -95,4 +96,4 @@ router.post('/', async (req, res) => {
 	}
 })
 
-module.exports  router
+module.exports = router

+ 16 - 16
test/thread_post.js

@@ -187,18 +187,18 @@ describe('Thread and post', () => {
 				res.should.be.json
 				res.should.have.status(400)
 				res.body.errors.should.contain.something.that.deep.equals(Errors.missingParameter('content'))
-				res.body.errors.should.contain.something.that.deep.equals(Errors.missingParameter('thread'))
+				res.body.errors.should.contain.something.that.deep.equals(Errors.missingParameter('threadId'))
 			} catch (res) {
 				let body = JSON.parse(res.response.text)
 				res.should.have.status(400)
 				body.errors.should.contain.something.that.deep.equals(Errors.missingParameter('content'))
-				body.errors.should.contain.something.that.deep.equals(Errors.missingParameter('thread'))
+				body.errors.should.contain.something.that.deep.equals(Errors.missingParameter('threadId'))
 			}
 		})
 		it('should return an error if invalid types', async () => {
 			try {
 				let res = await userAgent
-					.post('/api/v1/thread')
+					.post('/api/v1/post')
 					.set('content-type', 'application/json')
 					.send({
 						content: 123,
@@ -209,13 +209,13 @@ describe('Thread and post', () => {
 				res.should.be.json
 				res.should.have.status(400)
 				res.body.errors.should.contain.something.that.deep.equals(Errors.invalidParameterType('content', 'string'))
-				res.body.errors.should.contain.something.that.deep.equals(Errors.invalidParameterType('thread', 'integer'))
+				res.body.errors.should.contain.something.that.deep.equals(Errors.invalidParameterType('threadId', 'integer'))
 				res.body.errors.should.contain.something.that.deep.equals(Errors.invalidParameterType('replyingToId', 'integer'))
 			} catch (res) {
 				let body = JSON.parse(res.response.text)
 				res.should.have.status(400)
 				body.errors.should.contain.something.that.deep.equals(Errors.invalidParameterType('content', 'string'))
-				body.errors.should.contain.something.that.deep.equals(Errors.invalidParameterType('thread', 'integer'))
+				body.errors.should.contain.something.that.deep.equals(Errors.invalidParameterType('threadId', 'integer'))
 				body.errors.should.contain.something.that.deep.equals(Errors.invalidParameterType('replyingToId', 'integer'))
 			}
 		})
@@ -231,11 +231,11 @@ describe('Thread and post', () => {
 
 				res.should.be.json
 				res.should.have.status(400)
-				res.body.should.include.something.that.deep.equals(Errors.invalidParameter('thread', 'thread does not exist'))
-			} catch (e) {
+				res.body.errors.should.include.something.that.deep.equals(Errors.invalidParameter('threadId', 'thread does not exist'))
+			} catch (res) {
 				let body = JSON.parse(res.response.text)
 				res.should.have.status(400)
-				body.should.include.something.that.deep.equals(Errors.invalidParameter('thread', 'thread does not exist'))
+				body.errors.should.include.something.that.deep.equals(Errors.invalidParameter('threadId', 'thread does not exist'))
 			}
 		})
 		it('should be able to reply to a post', async () => {
@@ -247,7 +247,7 @@ describe('Thread and post', () => {
 					password: 'password'
 				})
 
-			let res = replyAgent
+			let res = await replyAgent
 				.post('/api/v1/post')
 				.set('content-type', 'application/json')
 				.send({
@@ -261,11 +261,11 @@ describe('Thread and post', () => {
 			res.body.should.have.property('content', '<p>another post</p>\n')
 			res.body.should.have.deep.property('User.username', 'username1')
 			res.body.should.have.deep.property('Thread.name', 'thread')
-			res.body.should.have.deep.property('replyingToId.username', 'username')
+			res.body.should.have.deep.property('ReplyingTo.User.username', 'username')
 		})
 		it('should return an error if reply id does not exist', async () => {
 			try {
-				let res = replyAgent
+				let res = await replyAgent
 					.post('/api/v1/post')
 					.set('content-type', 'application/json')
 					.send({
@@ -275,12 +275,12 @@ describe('Thread and post', () => {
 					})
 
 				res.should.have.status(400)
-				res.body.should.contain.something.that.deep.equals(Errors.invalidParameter('replyingToId', 'post does not exist'))
-			} catch (e) {
+				res.body.errors.should.contain.something.that.deep.equals(Errors.invalidParameter('replyingToId', 'post does not exist'))
+			} catch (res) {
 				let body = JSON.parse(res.response.text)
 
 				res.should.have.status(400)
-				body.should.contain.something.that.deep.equals(Errors.invalidParameter('replyingToId', 'post does not exist'))
+				body.errors.should.contain.something.that.deep.equals(Errors.invalidParameter('replyingToId', 'post does not exist'))
 			}
 		})
 		it('should return an error if post reply not in same thread', async () => {
@@ -303,12 +303,12 @@ describe('Thread and post', () => {
 					})
 
 				res.should.have.status(400)
-				res.body.should.contain.something.that.deep.equals(Errors.invalidParameter('replyingToId', 'replies must be in same thread'))
+				res.body.errors.should.contain.something.that.deep.equals(Errors.invalidParameter('replyingToId', 'replies must be in same thread'))
 			} catch (res) {
 				let body = JSON.parse(res.response.text)
 
 				res.should.have.status(400)
-				body.should.contain.something.that.deep.equals(Errors.invalidParameter('replyingToId', 'replies must be in same thread'))
+				body.errors.should.contain.something.that.deep.equals(Errors.invalidParameter('replyingToId', 'replies must be in same thread'))
 			}
 		})
 	})