poll.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. process.env.NODE_ENV = 'test'
  2. let chai = require('chai')
  3. let server = require('../server')
  4. let should = chai.should()
  5. let { sequelize, PollQuestion, PollAnswer, User } = require('../models')
  6. const Errors = require('../lib/errors.js')
  7. chai.use(require('chai-http'))
  8. chai.use(require('chai-things'))
  9. describe('Poll', () => {
  10. let admin = chai.request.agent(server)
  11. let user1 = chai.request.agent(server)
  12. let user2 = chai.request.agent(server)
  13. let user3 = chai.request.agent(server)
  14. //Wait for app to start before commencing
  15. before((done) => {
  16. if(server.locals.appStarted) done()
  17. server.on('appStarted', () => {
  18. done()
  19. })
  20. })
  21. describe('POST /', () => {
  22. before(async () => {
  23. try {
  24. let accounts = []
  25. accounts.push(
  26. admin
  27. .post('/api/v1/user')
  28. .set('content-type', 'application/json')
  29. .send({
  30. username: 'adminaccount',
  31. password: 'password',
  32. admin: true
  33. })
  34. )
  35. accounts.push(
  36. user1
  37. .post('/api/v1/user')
  38. .set('content-type', 'application/json')
  39. .send({
  40. username: 'useraccount1',
  41. password: 'password'
  42. })
  43. )
  44. accounts.push(
  45. user2
  46. .post('/api/v1/user')
  47. .set('content-type', 'application/json')
  48. .send({
  49. username: 'useraccount2',
  50. password: 'password'
  51. })
  52. )
  53. accounts.push(
  54. user3
  55. .post('/api/v1/user')
  56. .set('content-type', 'application/json')
  57. .send({
  58. username: 'useraccount3',
  59. password: 'password'
  60. })
  61. )
  62. await Promise.all(accounts)
  63. return true
  64. } catch (e) {
  65. return e
  66. }
  67. })
  68. describe('POST /poll', () => {
  69. it('should create a PollQuestion and PollAnswers', async () => {
  70. let res = await user1
  71. .post('/api/v1/poll')
  72. .set('content-type', 'application/json')
  73. .send({
  74. question: 'Question here',
  75. answers: ['answer 1', 'answer 2', 'answer 3']
  76. })
  77. res.should.be.json
  78. res.should.have.status(200)
  79. res.body.should.have.property('question', 'Question here')
  80. res.body.should.have.property('id', 1)
  81. let poll = await PollQuestion.findById(1, {
  82. include: [User, PollAnswer]
  83. })
  84. poll.should.have.property('question', 'Question here')
  85. poll.PollAnswers.should.have.property('length', 3)
  86. poll.PollAnswers.should.contain.something.with.property('answer', 'answer 1')
  87. poll.PollAnswers.should.contain.something.with.property('answer', 'answer 2')
  88. poll.PollAnswers.should.contain.something.with.property('answer', 'answer 3')
  89. })
  90. it('should return an error if answers is missing', done => {
  91. user1
  92. .post('/api/v1/poll')
  93. .set('content-type', 'application/json')
  94. .send({
  95. question: 'Question here'
  96. })
  97. .end((err, res) => {
  98. res.should.have.status(400)
  99. res.body.errors.should.contain.something.with.property(
  100. 'message',
  101. 'You must provide at least 2 answers'
  102. )
  103. done()
  104. })
  105. })
  106. it('should return an error if answers is less than two', done => {
  107. user1
  108. .post('/api/v1/poll')
  109. .set('content-type', 'application/json')
  110. .send({
  111. question: 'Question here',
  112. answers: []
  113. })
  114. .end((err, res) => {
  115. res.should.have.status(400)
  116. res.body.errors.should.contain.something.with.property(
  117. 'message',
  118. 'You must provide at least 2 answers'
  119. )
  120. done()
  121. })
  122. })
  123. it('should return an error if answers contains duplicates', done => {
  124. user1
  125. .post('/api/v1/poll')
  126. .set('content-type', 'application/json')
  127. .send({
  128. question: 'Question here',
  129. answers: ['answer', 'answer 1', 'answer']
  130. })
  131. .end((err, res) => {
  132. res.should.have.status(400)
  133. res.body.errors.should.contain.something.with.property(
  134. 'message',
  135. 'Answers cannot contain any duplicates'
  136. )
  137. done()
  138. })
  139. })
  140. it('should return an error if question not provided', done => {
  141. user1
  142. .post('/api/v1/poll')
  143. .set('content-type', 'application/json')
  144. .send({
  145. answers: ['answer 1', 'answer 2']
  146. })
  147. .end((err, res) => {
  148. res.should.have.status(400)
  149. res.body.errors.should.contain.something.with.property(
  150. 'message',
  151. 'question cannot be null'
  152. )
  153. done()
  154. })
  155. })
  156. it('should return an error if not logged in', done => {
  157. chai.request(server)
  158. .post('/api/v1/poll')
  159. .set('content-type', 'application/json')
  160. .send({
  161. question: 'Question here',
  162. answers: ['answer', 'answer 2']
  163. })
  164. .end((err, res) => {
  165. res.should.have.status(401)
  166. res.body.errors.should.contain.something.which.deep.equals(Errors.requestNotAuthorized)
  167. done()
  168. })
  169. })
  170. })
  171. })
  172. after(() => sequelize.sync({ force: true }))
  173. })