123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499 |
- process.env.NODE_ENV = 'test'
- let chai = require('chai')
- let server = require('../server')
- let should = chai.should()
- let { sequelize, PollQuestion, PollAnswer, PollVote, 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('Poll', () => {
- 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)
- 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
- }
- })
- 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'],
- threadId: 1
- })
- 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',
- threadId: 2
- })
- .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: [],
- threadId: 2
- })
- .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'],
- threadId: 2
- })
- .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 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'],
- threadId: 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'],
- threadId: 2
- })
- .end((err, res) => {
- res.should.have.status(401)
- res.body.errors.should.contain.something.which.deep.equals(Errors.requestNotAuthorized)
- done()
- })
- })
- })
- 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'],
- threadId: 2
- })
- 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'],
- threadId: 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)
- res.body.should.have.property('question', 'Poll question')
- res.body.should.have.deep.property('PollAnswers')
- let answer = await PollAnswer.findOne({
- where: {
- answer: 'poll answer 1'
- }
- })
- let vote = await PollVote.findById(
- res.body.PollAnswers[0].PollVotes[0].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()
- })
- })
- describe('GET /poll/:id', () => {
- let pollId
- before(async () => {
- try {
- let pollRes = await user1
- .post('/api/v1/poll')
- .set('content-type', 'application/json')
- .send({
- question: 'Do you like polls?',
- answers: ['yes', 'no', 'meh'],
- threadId: 4
- })
- pollId = pollRes.body.id
- await user1
- .post('/api/v1/poll/' + pollId)
- .set('content-type', 'application/json')
- .send({ answer: 'yes' })
- await user2
- .post('/api/v1/poll/' + pollId)
- .set('content-type', 'application/json')
- .send({ answer: 'yes' })
- await user3
- .post('/api/v1/poll/' + pollId)
- .set('content-type', 'application/json')
- .send({ answer: 'no' })
- return true
- } catch (e) {
- console.log(e)
- return e
- }
- })
- it('should get the poll question and accompanying answers and votes', async () => {
- let res = await user1.get('/api/v1/poll/' + pollId)
-
- res.should.be.json
- res.should.have.status(200)
- res.body.should.have.property('question', 'Do you like polls?')
- res.body.PollAnswers.should.have.property('length', 3)
- res.body.should.have.property('totalVotes', 3)
- res.body.should.have.property('hasVoted', true)
- res.body.should.have.deep.property('PollAnswers.0.answer', 'yes')
- res.body.should.have.deep.property('PollAnswers.0.PollVotes.length', 2)
- res.body.should.have.deep.property('PollAnswers.0.percent', 66.7)
- res.body.should.have.deep.property('PollAnswers.1.answer', 'no')
- res.body.should.have.deep.property('PollAnswers.1.PollVotes.length', 1)
- res.body.should.have.deep.property('PollAnswers.1.percent', 33.3)
- res.body.should.have.deep.property('PollAnswers.2.answer', 'meh')
- res.body.should.have.deep.property('PollAnswers.2.PollVotes.length', 0)
- res.body.should.have.deep.property('PollAnswers.2.percent', 0)
- })
- it('should set hasVoted to false if user not voted', async () => {
- let res = await admin.get('/api/v1/poll/' + pollId)
-
- res.should.be.json
- res.should.have.status(200)
- res.body.should.have.property('hasVoted', false)
- })
- it('should set hasVoted to false if user not logged in', async () => {
- let res = await chai.request(server).get('/api/v1/poll/' + pollId)
-
- res.should.be.json
- res.should.have.status(200)
- res.body.should.have.property('hasVoted', false)
- })
- it('should return an error if invalid id', done => {
- chai.request(server)
- .get('/api/v1/poll/' + pollId)
- .end((err, res) => {
- res.should.have.status(400)
- res.body.errors.should.contain.something.with.property(
- 'message',
- 'invalid poll id'
- )
- })
- done()
- })
- })
- })
- after(() => {
- sequelize.sync({ force: true })
- })
- })
|