thread_post.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. process.env.NODE_ENV = 'test'
  2. let chai = require('chai')
  3. let server = require('../server')
  4. let should = chai.should()
  5. let { sequelize } = require('../models')
  6. const Errors = require('../lib/errors.js')
  7. chai.use(require('chai-http'))
  8. chai.use(require('chai-things'))
  9. describe('Thread and post', () => {
  10. let userAgent
  11. //Wait for app to start before commencing
  12. before((done) => {
  13. if(server.locals.appStarted) mockData()
  14. server.on('appStarted', () => {
  15. mockData()
  16. })
  17. function mockData() {
  18. userAgent = chai.request.agent(server)
  19. userAgent
  20. .post('/api/v1/user')
  21. .set('content-type', 'application/json')
  22. .send({
  23. username: 'username',
  24. password: 'password',
  25. admin: true
  26. })
  27. .then(() => {
  28. userAgent
  29. .post('/api/v1/category')
  30. .set('content-type', 'application/json')
  31. .send({ name: 'category_name' })
  32. .then(() => { done() })
  33. .catch(done)
  34. })
  35. .catch(done)
  36. }
  37. })
  38. //Delete all rows in table after
  39. //tests completed
  40. after(() => {
  41. sequelize.sync({ force: true })
  42. })
  43. describe('POST /thread', () => {
  44. it('should create a thread if logged in', async () => {
  45. let res = await userAgent
  46. .post('/api/v1/thread')
  47. .set('content-type', 'application/json')
  48. .send({
  49. name: 'thread'
  50. })
  51. res.should.be.status(200)
  52. res.body.should.be.json
  53. res.body.should.have.property('name', 'thread')
  54. })
  55. it('should return an error if not logged in', async () => {
  56. try {
  57. let res = await chai.request(server)
  58. .post('/api/v1/thread')
  59. .set('content-type', 'application/json')
  60. .send({
  61. name: 'thread',
  62. category: 'category_name'
  63. })
  64. res.should.be.json
  65. res.should.have.status(401)
  66. res.body.errors.should.contain.something.that.deep.equals(Errors.requestNotAuthorized)
  67. } catch (res) {
  68. res.should.have.status(401)
  69. JSON.parse(res.response.text).errors.should.contain.something.that.deep.equals(Errors.requestNotAuthorized)
  70. }
  71. })
  72. it('should return an error if missing parameters', async () => {
  73. try {
  74. let res = await agent
  75. .post('/api/v1/thread')
  76. res.should.be.json
  77. res.should.have.status(400)
  78. res.body.errors.should.contain.something.that.deep.equals(Errors.missingParameter('name'))
  79. res.body.errors.should.contain.something.that.deep.equals(Errors.missingParameter('category'))
  80. } catch (res) {
  81. let body = JSON.parse(res.response.text)
  82. res.should.have.status(400)
  83. body.errors.should.contain.something.that.deep.equals(Errors.missingParameter('name'))
  84. body.errors.should.contain.something.that.deep.equals(Errors.missingParameter('category'))
  85. }
  86. })
  87. it('should return an error if invalid types', async () => {
  88. try {
  89. let res = await agent
  90. .post('/api/v1/thread')
  91. .set('content-type', 'application/json')
  92. .send({
  93. name: 123,
  94. category: 123
  95. })
  96. res.should.be.json
  97. res.should.have.status(400)
  98. res.body.errors.should.contain.something.that.deep.equals(Errors.invalidParameterType('name', 'string'))
  99. res.body.errors.should.contain.something.that.deep.equals(Errors.invalidParameterType('category', 'string'))
  100. } catch (res) {
  101. let body = JSON.parse(res.response.text)
  102. res.should.have.status(400)
  103. body.errors.should.contain.something.that.deep.equals(Errors.invalidParameterType('name', 'string'))
  104. body.errors.should.contain.something.that.deep.equals(Errors.invalidParameterType('category', 'string'))
  105. }
  106. })
  107. it('should return an error if category does not exist', async () => {
  108. try {
  109. let res = await agent
  110. .post('/api/v1/thread')
  111. .set('content-type', 'application/json')
  112. .send({
  113. name: 'thread1',
  114. category: 'non-existent'
  115. })
  116. res.should.be.json
  117. res.should.have.status(400)
  118. res.body.errors.should.contain.something.that.deep.equals(Errors.invalidCategory)
  119. } catch (res) {
  120. res.should.have.status(400)
  121. JSON.parse(res.response.text).errors.should.contain.something.that.deep.equals(Errors.invalidCategory)
  122. }
  123. })
  124. })
  125. describe('GET /thread/:id', () => {
  126. it('should return the thread and corresponding posts', async () => {})
  127. it('should return an error if :id is invalid', async () => {})
  128. })
  129. describe('POST /post', () => {
  130. it('should create a post if logged in', async () => {})
  131. it('should return an error if not logged in', async () => {})
  132. it('should return an error if missing parameters', async () => {})
  133. it('should return an error if invalid types', async () => {})
  134. it('should return an error if thread id does not exist', async () => {})
  135. it('should be able to reply to a post', async () => {})
  136. it('should return an error if reply id does not exist', async () => {})
  137. it('should return an error if post reply not in same thread', async () => {})
  138. })
  139. describe('GET /post/:id', () => {
  140. it('should return the post', async () => {})
  141. it('should return an error if invalid post id', async () => {})
  142. })
  143. })