Przeglądaj źródła

Associate poll quesiton with thread and add test case constraints

sbkwgh 8 lat temu
rodzic
commit
c7e94d59ca
3 zmienionych plików z 120 dodań i 15 usunięć
  1. 1 0
      models/thread.js
  2. 28 5
      routes/poll.js
  3. 91 10
      test/poll.js

+ 1 - 0
models/thread.js

@@ -94,6 +94,7 @@ module.exports = (sequelize, DataTypes) => {
 			associate (models) {
 				Thread.belongsTo(models.User)
 				Thread.belongsTo(models.Category)
+				Thread.belongsTo(models.PollQuestion)
 				Thread.hasMany(models.Post)
 			},
 			includeOptions (from, limit) {

+ 28 - 5
routes/poll.js

@@ -1,7 +1,7 @@
 let express = require('express')
 let router = express.Router()
 
-let { PollAnswer, PollQuestion, PollVote, User, Sequelize } = require('../models')
+let { PollAnswer, PollQuestion, PollVote, User, Sequelize, Thread } = require('../models')
 const Errors = require('../lib/errors')
 
 router.get('/:id', async (req, res) => {
@@ -63,6 +63,22 @@ router.all('*', (req, res, next) => {
 
 router.post('/', async (req, res) => {
 	try {
+		let threadId = req.body.threadId
+		let thread = await Thread.findById(req.body.threadId)
+		if(!thread) {
+			throw Errors.sequelizeValidation(Sequelize, {
+				error: 'invalid thread id',
+				value: threadId
+			})
+		} else if(thread.UserId !== req.session.UserId) {
+			throw Errors.requestNotAuthorized
+		} else if(thread.PollQuestionId) {
+			throw Errors.sequelizeValidation(Sequelize, {
+				error: 'invalid thread id',
+				value: threadId
+			})
+		}
+
 		let answers = req.body.answers
 
 		if(!answers || answers.length < 2) {
@@ -76,9 +92,11 @@ router.post('/', async (req, res) => {
 				value: answers
 			})
 		}
-		
-		let user = await User.findById(req.session.UserId)
-		let pollQuestion = await PollQuestion.create({ question: req.body.question })
+	
+		let pollQuestion = await PollQuestion.create({
+			UserId: req.session.UserId,
+			question: req.body.question
+		})
 		let pollAnswers = await Promise.all(
 			answers.map(answer => {
 				return PollAnswer.create({ answer })
@@ -86,7 +104,7 @@ router.post('/', async (req, res) => {
 		)
 
 		//Set associations
-		await pollQuestion.setUser(user)
+		await thread.setPollQuestion(pollQuestion)
 		await Promise.all(
 			pollAnswers.map(pollAnswer => {
 				return pollQuestion.addPollAnswer(pollAnswer)
@@ -99,6 +117,11 @@ router.post('/', async (req, res) => {
 		if(e instanceof Sequelize.ValidationError) {
 			res.status(400)
 			res.json(e)
+		} else if(e === Errors.requestNotAuthorized) {
+			res.status(401)
+			res.json({
+				errors: [e]
+			})
 		} else {
 			console.log(e)
 

+ 91 - 10
test/poll.js

@@ -71,6 +71,22 @@ describe('Poll', () => {
 
 				await Promise.all(accounts)
 
+				await admin
+					.post('/api/v1/category')
+					.set('content-type', 'application/json')
+					.send({ name: 'category' })
+
+				let threads = []
+				for(let i = 0; i < 4; i++) {
+					threads.push(
+						user1
+							.post('/api/v1/thread')
+							.set('content-type', 'application/json')
+							.send({ category: 'CATEGORY', name: 'thread' + i })
+					)
+				}
+				await Promise.all(threads)
+
 				return true
 			} catch (e) {
 				return e
@@ -84,7 +100,8 @@ describe('Poll', () => {
 					.set('content-type', 'application/json')
 					.send({
 						question: 'Question here',
-						answers: ['answer 1', 'answer 2', 'answer 3']
+						answers: ['answer 1', 'answer 2', 'answer 3'],
+						threadId: 1
 					})
 
 				res.should.be.json
@@ -110,7 +127,8 @@ describe('Poll', () => {
 					.post('/api/v1/poll')
 					.set('content-type', 'application/json')
 					.send({
-						question: 'Question here'
+						question: 'Question here',
+						threadId: 2
 					})
 					.end((err, res) => {
 						res.should.have.status(400)
@@ -128,7 +146,8 @@ describe('Poll', () => {
 					.set('content-type', 'application/json')
 					.send({
 						question: 'Question here',
-						answers: []
+						answers: [],
+						threadId: 2
 					})
 					.end((err, res) => {
 						res.should.have.status(400)
@@ -146,7 +165,8 @@ describe('Poll', () => {
 					.set('content-type', 'application/json')
 					.send({
 						question: 'Question here',
-						answers: ['answer', 'answer 1', 'answer']
+						answers: ['answer', 'answer 1', 'answer'],
+						threadId: 2
 					})
 					.end((err, res) => {
 						res.should.have.status(400)
@@ -158,12 +178,69 @@ describe('Poll', () => {
 						done()
 					})
 			})
+			it('should return an error if thread invalid', done => {
+				user1
+					.post('/api/v1/poll')
+					.set('content-type', 'application/json')
+					.send({
+						question: 'Question here',
+						answers: ['answer', 'answer 1'],
+						threadId: 404
+					})
+					.end((err, res) => {
+						res.should.have.status(400)
+						res.body.errors.should.contain.something.with.property(
+							'message',
+							'invalid thread id'
+						)
+
+						done()
+					})
+			})
+			it('should return an error if thread already has poll', done => {
+				user1
+					.post('/api/v1/poll')
+					.set('content-type', 'application/json')
+					.send({
+						question: 'Question here',
+						answers: ['answer', 'answer 1'],
+						threadId: 1
+					})
+					.end((err, res) => {
+						res.should.have.status(400)
+						res.body.errors.should.contain.something.with.property(
+							'message',
+							'invalid thread id'
+						)
+
+						done()
+					})
+			})
+			it('should return an error if thread user not same as poll user', done => {
+				admin
+					.post('/api/v1/poll')
+					.set('content-type', 'application/json')
+					.send({
+						question: 'Question here',
+						answers: ['answer', 'answer 1'],
+						threadId: 2
+					})
+					.end((err, res) => {
+						res.should.have.status(401)
+						res.body.errors.should.contain.something.that.deep.equals(
+							Errors.requestNotAuthorized
+						)
+
+						done()
+					})
+			})
 			it('should return an error if question not provided', done => {
 				user1
 					.post('/api/v1/poll')
 					.set('content-type', 'application/json')
 					.send({
-						answers: ['answer 1', 'answer 2']
+						answers: ['answer 1', 'answer 2'],
+						threadId: 2
 					})
 					.end((err, res) => {
 						res.should.have.status(400)
@@ -181,7 +258,8 @@ describe('Poll', () => {
 					.set('content-type', 'application/json')
 					.send({
 						question: 'Question here',
-						answers: ['answer', 'answer 2']
+						answers: ['answer', 'answer 2'],
+						threadId: 2
 					})
 					.end((err, res) => {
 						res.should.have.status(401)
@@ -202,7 +280,8 @@ describe('Poll', () => {
 						.set('content-type', 'application/json')
 						.send({
 							question: 'Poll question',
-							answers: ['poll answer 1', 'poll answer 2', 'poll answer 3']
+							answers: ['poll answer 1', 'poll answer 2', 'poll answer 3'],
+							threadId: 2
 						})
 
 					id = res.body.id
@@ -212,7 +291,8 @@ describe('Poll', () => {
 						.set('content-type', 'application/json')
 						.send({
 							question: 'Poll question',
-							answers: ['poll answer 1', 'poll answer 2', 'poll answer 3']
+							answers: ['poll answer 1', 'poll answer 2', 'poll answer 3'],
+							threadId: 3
 						})
 
 					id2 = res2.body.id
@@ -322,12 +402,13 @@ describe('Poll', () => {
 
 			before(async () => {
 				try {
-					let pollRes = await admin
+					let pollRes = await user1
 						.post('/api/v1/poll')
 						.set('content-type', 'application/json')
 						.send({
 							question: 'Do you like polls?',
-							answers: ['yes', 'no', 'meh']
+							answers: ['yes', 'no', 'meh'],
+							threadId: 4
 						})
 					pollId = pollRes.body.id