thread_post.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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. category: 'category_name'
  51. })
  52. res.should.have.status(200)
  53. res.should.be.json
  54. res.body.should.have.property('name', 'thread')
  55. res.body.should.have.deep.property('User.username', 'username')
  56. res.body.should.have.deep.property('Category.name', 'category_name')
  57. })
  58. it('should return an error if not logged in', async () => {
  59. try {
  60. let res = await chai.request(server)
  61. .post('/api/v1/thread')
  62. .set('content-type', 'application/json')
  63. .send({
  64. name: 'thread',
  65. category: 'category_name'
  66. })
  67. res.should.be.json
  68. res.should.have.status(401)
  69. res.body.errors.should.contain.something.that.deep.equals(Errors.requestNotAuthorized)
  70. } catch (res) {
  71. res.should.have.status(401)
  72. JSON.parse(res.response.text).errors.should.contain.something.that.deep.equals(Errors.requestNotAuthorized)
  73. }
  74. })
  75. it('should return an error if missing parameters', async () => {
  76. try {
  77. let res = await userAgent
  78. .post('/api/v1/thread')
  79. res.should.be.json
  80. res.should.have.status(400)
  81. res.body.errors.should.contain.something.that.deep.equals(Errors.missingParameter('name'))
  82. res.body.errors.should.contain.something.that.deep.equals(Errors.missingParameter('category'))
  83. } catch (res) {
  84. let body = JSON.parse(res.response.text)
  85. res.should.have.status(400)
  86. body.errors.should.contain.something.that.deep.equals(Errors.missingParameter('name'))
  87. body.errors.should.contain.something.that.deep.equals(Errors.missingParameter('category'))
  88. }
  89. })
  90. it('should return an error if invalid types', async () => {
  91. try {
  92. let res = await userAgent
  93. .post('/api/v1/thread')
  94. .set('content-type', 'application/json')
  95. .send({
  96. name: 123,
  97. category: 123
  98. })
  99. res.should.be.json
  100. res.should.have.status(400)
  101. res.body.errors.should.contain.something.that.deep.equals(Errors.invalidParameterType('name', 'string'))
  102. res.body.errors.should.contain.something.that.deep.equals(Errors.invalidParameterType('category', 'string'))
  103. } catch (res) {
  104. let body = JSON.parse(res.response.text)
  105. res.should.have.status(400)
  106. body.errors.should.contain.something.that.deep.equals(Errors.invalidParameterType('name', 'string'))
  107. body.errors.should.contain.something.that.deep.equals(Errors.invalidParameterType('category', 'string'))
  108. }
  109. })
  110. it('should return an error if category does not exist', async () => {
  111. try {
  112. let res = await userAgent
  113. .post('/api/v1/thread')
  114. .set('content-type', 'application/json')
  115. .send({
  116. name: 'thread1',
  117. category: 'non-existent'
  118. })
  119. res.should.be.json
  120. res.should.have.status(400)
  121. res.body.errors.should.contain.something.that.deep.equals(Errors.invalidCategory)
  122. } catch (res) {
  123. res.should.have.status(400)
  124. JSON.parse(res.response.text).errors.should.contain.something.that.deep.equals(Errors.invalidCategory)
  125. }
  126. })
  127. })
  128. describe('GET /thread/:id', () => {
  129. it('should return the thread and corresponding posts', async () => {})
  130. it('should return an error if :id is invalid', async () => {})
  131. })
  132. describe('POST /post', () => {
  133. it('should create a post if logged in', async () => {})
  134. it('should return an error if not logged in', async () => {})
  135. it('should return an error if missing parameters', async () => {})
  136. it('should return an error if invalid types', async () => {})
  137. it('should return an error if thread id does not exist', async () => {})
  138. it('should be able to reply to a post', async () => {})
  139. it('should return an error if reply id does not exist', async () => {})
  140. it('should return an error if post reply not in same thread', async () => {})
  141. })
  142. describe('GET /post/:id', () => {
  143. it('should return the post', async () => {})
  144. it('should return an error if invalid post id', async () => {})
  145. })
  146. })