|
@@ -4,7 +4,7 @@ let chai = require('chai')
|
|
|
let server = require('../server')
|
|
|
let should = chai.should()
|
|
|
|
|
|
-let { sequelize, PollQuestion, PollAnswer, User } = require('../models')
|
|
|
+let { sequelize, PollQuestion, PollAnswer, PollVote, User } = require('../models')
|
|
|
|
|
|
const Errors = require('../lib/errors.js')
|
|
|
|
|
@@ -191,6 +191,131 @@ describe('Poll', () => {
|
|
|
})
|
|
|
})
|
|
|
})
|
|
|
+
|
|
|
+ describe('POST /poll/:id', () => {
|
|
|
+ let id, id2
|
|
|
+
|
|
|
+ before(async () => {
|
|
|
+ try {
|
|
|
+ let res = await user1
|
|
|
+ .post('/api/v1/poll')
|
|
|
+ .set('content-type', 'application/json')
|
|
|
+ .send({
|
|
|
+ question: 'Poll question',
|
|
|
+ answers: ['poll answer 1', 'poll answer 2', 'poll answer 3']
|
|
|
+ })
|
|
|
+
|
|
|
+ id = res.body.id
|
|
|
+
|
|
|
+ let res2 = await user1
|
|
|
+ .post('/api/v1/poll')
|
|
|
+ .set('content-type', 'application/json')
|
|
|
+ .send({
|
|
|
+ question: 'Poll question',
|
|
|
+ answers: ['poll answer 1', 'poll answer 2', 'poll answer 3']
|
|
|
+ })
|
|
|
+
|
|
|
+ id2 = res2.body.id
|
|
|
+
|
|
|
+ return true
|
|
|
+ } catch (e) {
|
|
|
+ return e
|
|
|
+ }
|
|
|
+ })
|
|
|
+
|
|
|
+ it('should add a vote to the poll', async () => {
|
|
|
+ let res = await user1
|
|
|
+ .post('/api/v1/poll/' + id)
|
|
|
+ .set('content-type', 'application/json')
|
|
|
+ .send({ answer: 'poll answer 1' })
|
|
|
+
|
|
|
+ res.should.be.json
|
|
|
+ res.should.have.status(200)
|
|
|
+
|
|
|
+ let answer = await PollAnswer.findOne({
|
|
|
+ where: {
|
|
|
+ answer: 'poll answer 1'
|
|
|
+ }
|
|
|
+ })
|
|
|
+
|
|
|
+ let vote = await PollVote.findById(res.body.id)
|
|
|
+ vote.should.not.be.null
|
|
|
+ vote.should.have.property('PollQuestionId', id)
|
|
|
+ vote.should.have.property('PollAnswerId', answer.id)
|
|
|
+ })
|
|
|
+ it('should return an error if voting twice', done => {
|
|
|
+ user1
|
|
|
+ .post('/api/v1/poll/' + id)
|
|
|
+ .set('content-type', 'application/json')
|
|
|
+ .send({ answer: 'poll answer 2' })
|
|
|
+ .end((err, res) => {
|
|
|
+ res.should.have.status(400)
|
|
|
+ res.body.errors.should.contain.something.that.has.property(
|
|
|
+ 'message',
|
|
|
+ 'you cannot vote twice'
|
|
|
+ )
|
|
|
+
|
|
|
+ done()
|
|
|
+ })
|
|
|
+ })
|
|
|
+ it('should return an error if invalid id', done => {
|
|
|
+ user1
|
|
|
+ .post('/api/v1/poll/404')
|
|
|
+ .set('content-type', 'application/json')
|
|
|
+ .send({ answer: 'poll answer 1' })
|
|
|
+ .end((err, res) => {
|
|
|
+ res.should.have.status(400)
|
|
|
+ res.body.errors.should.contain.something.that.has.property(
|
|
|
+ 'message',
|
|
|
+ 'invalid poll id'
|
|
|
+ )
|
|
|
+
|
|
|
+ done()
|
|
|
+ })
|
|
|
+ })
|
|
|
+ it('should return an error if missing answer', done => {
|
|
|
+ user1
|
|
|
+ .post('/api/v1/poll/' + id2)
|
|
|
+ .set('content-type', 'application/json')
|
|
|
+ .send()
|
|
|
+ .end((err, res) => {
|
|
|
+ res.should.have.status(400)
|
|
|
+ res.body.errors.should.contain.something.that.has.property(
|
|
|
+ 'message',
|
|
|
+ 'invalid answer'
|
|
|
+ )
|
|
|
+
|
|
|
+ done()
|
|
|
+ })
|
|
|
+ })
|
|
|
+ it('should return an error if answer is invalid', done => {
|
|
|
+ user1
|
|
|
+ .post('/api/v1/poll/' + id2)
|
|
|
+ .set('content-type', 'application/json')
|
|
|
+ .send({ answer: 'not an option' })
|
|
|
+ .end((err, res) => {
|
|
|
+ res.should.have.status(400)
|
|
|
+ res.body.errors.should.contain.something.that.has.property(
|
|
|
+ 'message',
|
|
|
+ 'invalid answer'
|
|
|
+ )
|
|
|
+
|
|
|
+ done()
|
|
|
+ })
|
|
|
+ })
|
|
|
+ it('should return an error if not logged in', done => {
|
|
|
+ chai.request(server)
|
|
|
+ .post('/api/v1/poll/' + id)
|
|
|
+ .set('content-type', 'application/json')
|
|
|
+ .send({ answer: 'poll answer 1' })
|
|
|
+ .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 }))
|