瀏覽代碼

Add poll POST route and tests

sbkwgh 8 年之前
父節點
當前提交
62ad392607
共有 5 個文件被更改,包括 320 次插入0 次删除
  1. 23 0
      models/poll_answer.js
  2. 32 0
      models/poll_question.js
  3. 67 0
      routes/poll.js
  4. 1 0
      server.js
  5. 197 0
      test/poll.js

+ 23 - 0
models/poll_answer.js

@@ -0,0 +1,23 @@
+module.exports = (sequelize, DataTypes) => {
+	let Sequelize = sequelize.Sequelize
+
+	let PollAnswer = sequelize.define('PollAnswer', {
+		answer: {
+			type: DataTypes.STRING,
+			allowNull: false,
+			validate: {
+				len: {
+					args: [1, 256],
+					msg: 'The answer must be between 1 and 256 characters'
+				},
+				isString (val) {
+					if(typeof val !== 'string') {
+						throw new Sequelize.ValidationError('The answer must be a string')
+					}
+				}
+			}
+		}
+	})
+
+	return PollAnswer
+}

+ 32 - 0
models/poll_question.js

@@ -0,0 +1,32 @@
+module.exports = (sequelize, DataTypes) => {
+	let Sequelize = sequelize.Sequelize
+
+	let PollQuestion = sequelize.define('PollQuestion', {
+		question: {
+			type: DataTypes.STRING,
+			allowNull: false,
+			validate: {
+				len: {
+					args: [1, 256],
+					msg: 'The question must be between 1 and 256 characters'
+				},
+				isString (val) {
+					if(typeof val !== 'string') {
+						throw new Sequelize.ValidationError('The question must be a string')
+					}
+				}
+			}
+		}
+	}, {
+		classMethods: {
+			associate (models) {
+				PollQuestion.belongsTo(models.User)
+				PollQuestion.hasMany(models.PollAnswer, {
+					foreignKey: { name: 'question_id' }
+				})
+			}
+		}
+	})
+
+	return PollQuestion
+}

+ 67 - 0
routes/poll.js

@@ -0,0 +1,67 @@
+let express = require('express')
+let router = express.Router()
+
+let { PollAnswer, PollQuestion, User, Sequelize } = require('../models')
+const Errors = require('../lib/errors')
+
+router.all('*', (req, res, next) => {
+	if(req.session.loggedIn) {
+		next()
+	} else {
+		res.status(401)
+		res.json({
+			errors: [Errors.requestNotAuthorized]
+		})
+	}
+})
+
+router.post('/', async (req, res) => {
+	try {
+		let answers = req.body.answers
+
+		if(!answers || answers.length < 2) {
+			throw Errors.sequelizeValidation(Sequelize, {
+				error: 'You must provide at least 2 answers',
+				value: answers
+			})
+		} else if(answers.length !== new Set(answers).size) {
+			throw Errors.sequelizeValidation(Sequelize, {
+				error: 'Answers cannot contain any duplicates',
+				value: answers
+			})
+		}
+		
+		let user = await User.findById(req.session.UserId)
+		let pollQuestion = await PollQuestion.create({ question: req.body.question })
+		let pollAnswers = await Promise.all(
+			answers.map(answer => {
+				return PollAnswer.create({ answer })
+			})
+		)
+
+		//Set associations
+		await pollQuestion.setUser(user)
+		await Promise.all(
+			pollAnswers.map(pollAnswer => {
+				return pollQuestion.addPollAnswer(pollAnswer)
+			})
+		)
+
+		res.json(pollQuestion.toJSON())
+
+	} catch (e) {
+		if(e instanceof Sequelize.ValidationError) {
+			res.status(400)
+			res.json(e)
+		} else {
+			console.log(e)
+
+			res.status(500)
+			res.json({
+				errors: [Errors.unknown]
+			})
+		}
+	}
+})
+
+module.exports = router

+ 1 - 0
server.js

@@ -41,6 +41,7 @@ app.use('/api/v1/report', require('./routes/report'))
 app.use('/api/v1/ban', require('./routes/ban'))
 app.use('/api/v1/search', require('./routes/search'))
 app.use('/api/v1/log', require('./routes/log'))
+app.use('/api/v1/poll', require('./routes/poll'))
 
 sequelize
 	.sync({ force: true })

+ 197 - 0
test/poll.js

@@ -0,0 +1,197 @@
+process.env.NODE_ENV = 'test'
+
+let chai = require('chai')
+let server = require('../server')
+let should = chai.should()
+
+let { sequelize, PollQuestion, PollAnswer, User } = require('../models')
+
+const Errors = require('../lib/errors.js')
+
+chai.use(require('chai-http'))
+chai.use(require('chai-things'))
+
+describe('Poll', () => {
+	let admin = chai.request.agent(server)
+	let user1 = chai.request.agent(server)
+	let user2 = chai.request.agent(server)
+	let user3 = chai.request.agent(server)
+
+	//Wait for app to start before commencing
+	before((done) => {
+		if(server.locals.appStarted) done()
+			
+		server.on('appStarted', () => {
+			done()
+		})
+	})
+
+	describe('POST /', () => {
+		before(async () => {
+			try {
+				let accounts = []
+
+				accounts.push(
+					admin
+					.post('/api/v1/user')
+					.set('content-type', 'application/json')
+					.send({
+						username: 'adminaccount',
+						password: 'password',
+						admin: true
+					})
+				)
+				accounts.push(
+					user1
+					.post('/api/v1/user')
+					.set('content-type', 'application/json')
+					.send({
+						username: 'useraccount1',
+						password: 'password'
+					})
+				)
+				accounts.push(
+					user2
+					.post('/api/v1/user')
+					.set('content-type', 'application/json')
+					.send({
+						username: 'useraccount2',
+						password: 'password'
+					})
+				)
+				accounts.push(
+					user3
+					.post('/api/v1/user')
+					.set('content-type', 'application/json')
+					.send({
+						username: 'useraccount3',
+						password: 'password'
+					})
+				)
+
+				await Promise.all(accounts)
+
+				return true
+			} catch (e) {
+				return e
+			}
+		})
+
+		describe('POST /poll', () => {
+			it('should create a PollQuestion and PollAnswers', async () => {
+				let res = await user1
+					.post('/api/v1/poll')
+					.set('content-type', 'application/json')
+					.send({
+						question: 'Question here',
+						answers: ['answer 1', 'answer 2', 'answer 3']
+					})
+
+				res.should.be.json
+				res.should.have.status(200)
+
+				res.body.should.have.property('question', 'Question here')
+				res.body.should.have.property('id', 1)
+
+				let poll = await PollQuestion.findById(1, {
+					include: [User, PollAnswer]
+				})
+
+				poll.should.have.property('question', 'Question here')
+
+				poll.PollAnswers.should.have.property('length', 3)
+				poll.PollAnswers.should.contain.something.with.property('answer', 'answer 1')
+				poll.PollAnswers.should.contain.something.with.property('answer', 'answer 2')
+				poll.PollAnswers.should.contain.something.with.property('answer', 'answer 3')
+			})
+			
+			it('should return an error if answers is missing', done => {
+				user1
+					.post('/api/v1/poll')
+					.set('content-type', 'application/json')
+					.send({
+						question: 'Question here'
+					})
+					.end((err, res) => {
+						res.should.have.status(400)
+						res.body.errors.should.contain.something.with.property(
+							'message',
+							'You must provide at least 2 answers'
+						)
+
+						done()
+					})
+			})
+			it('should return an error if answers is less than two', done => {
+				user1
+					.post('/api/v1/poll')
+					.set('content-type', 'application/json')
+					.send({
+						question: 'Question here',
+						answers: []
+					})
+					.end((err, res) => {
+						res.should.have.status(400)
+						res.body.errors.should.contain.something.with.property(
+							'message',
+							'You must provide at least 2 answers'
+						)
+
+						done()
+					})
+			})
+			it('should return an error if answers contains duplicates', done => {
+				user1
+					.post('/api/v1/poll')
+					.set('content-type', 'application/json')
+					.send({
+						question: 'Question here',
+						answers: ['answer', 'answer 1', 'answer']
+					})
+					.end((err, res) => {
+						res.should.have.status(400)
+						res.body.errors.should.contain.something.with.property(
+							'message',
+							'Answers cannot contain any duplicates'
+						)
+
+						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']
+					})
+					.end((err, res) => {
+						res.should.have.status(400)
+						res.body.errors.should.contain.something.with.property(
+							'message',
+							'question cannot be null'
+						)
+
+						done()
+					})
+			})
+			it('should return an error if not logged in', done => {
+				chai.request(server)
+					.post('/api/v1/poll')
+					.set('content-type', 'application/json')
+					.send({
+						question: 'Question here',
+						answers: ['answer', 'answer 2']
+					})
+					.end((err, res) => {
+						res.should.have.status(401)
+						res.body.errors.should.contain.something.which.deep.equals(Errors.requestNotAuthorized)
+
+						done()
+					})
+			})
+		})
+	})
+
+	after(() => sequelize.sync({ force: true }))
+})