thread_post.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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) createUser()
  14. server.on('appStarted', () => {
  15. createUser()
  16. })
  17. function createUser() {
  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. })
  26. .then(() => {
  27. done()
  28. })
  29. .catch(done)
  30. }
  31. })
  32. //Delete all rows in table after
  33. //tests completed
  34. after(() => {
  35. sequelize.sync({ force: true })
  36. })
  37. describe('POST /thread', () => {
  38. it('should create a thread if logged in', async () => {
  39. let res = await userAgent
  40. .post('/api/v1/thread')
  41. .set('content-type', 'application/json')
  42. .send({
  43. name: 'thread'
  44. })
  45. res.should.be.status(200)
  46. res.body.should.be.json
  47. res.body.should.have.property('name', 'thread')
  48. })
  49. it('should return an error if not logged in', async () => {
  50. try {
  51. let res = await chai.request(server)
  52. .post('/api/v1/thread')
  53. .set('content-type', 'application/json')
  54. .send({
  55. name: 'thread',
  56. category: 'category_name'
  57. })
  58. res.should.be.json
  59. res.should.have.status(401)
  60. res.body.errors.should.contain.something.that.deep.equals(Errors.requestNotAuthorized)
  61. } catch (res) {
  62. res.should.have.status(401)
  63. JSON.parse(res.response.text).errors.should.contain.something.that.deep.equals(Errors.requestNotAuthorized)
  64. }
  65. })
  66. it('should return an error if missing parameters', async () => {
  67. try {
  68. let res = await agent
  69. .post('/api/v1/thread')
  70. res.should.be.json
  71. res.should.have.status(400)
  72. res.body.errors.should.contain.something.that.deep.equals(Errors.missingParameter('name'))
  73. res.body.errors.should.contain.something.that.deep.equals(Errors.missingParameter('category'))
  74. } catch (res) {
  75. let body = JSON.parse(res.response.text)
  76. res.should.have.status(400)
  77. body.errors.should.contain.something.that.deep.equals(Errors.missingParameter('name'))
  78. body.errors.should.contain.something.that.deep.equals(Errors.missingParameter('category'))
  79. }
  80. })
  81. it('should return an error if invalid types', async () => {
  82. try {
  83. let res = await agent
  84. .post('/api/v1/thread')
  85. .set('content-type', 'application/json')
  86. .send({
  87. name: 123
  88. })
  89. res.should.be.json
  90. res.should.have.status(400)
  91. res.body.errors.should.contain.something.that.deep.equals(Errors.invalidParameterType('name', 'string'))
  92. } catch (res) {
  93. res.should.have.status(400)
  94. JSON.parse(res.response.text).errors.should.contain.something.that.deep.equals(Errors.invalidParameterType('name', 'string'))
  95. }
  96. })
  97. it('should return an error if category does not exist', async () => {
  98. try {
  99. let res = await agent
  100. .post('/api/v1/thread')
  101. .set('content-type', 'application/json')
  102. .send({
  103. name: 'thread1',
  104. category: 'non-existent'
  105. })
  106. res.should.be.json
  107. res.should.have.status(400)
  108. res.body.errors.should.contain.something.that.deep.equals(Errors.invalidCategory)
  109. } catch (res) {
  110. res.should.have.status(400)
  111. JSON.parse(res.response.text).errors.should.contain.something.that.deep.equals(Errors.invalidCategory)
  112. }
  113. })
  114. })
  115. describe('GET /thread/:id', () => {
  116. it('should return the thread and corresponding posts', async () => {})
  117. it('should return an error if :id is invalid', async () => {})
  118. })
  119. describe('POST /post', () => {
  120. it('should create a post if logged in', async () => {})
  121. it('should return an error if not logged in', async () => {})
  122. it('should return an error if missing parameters', async () => {})
  123. it('should return an error if invalid types', async () => {})
  124. it('should return an error if thread id does not exist', async () => {})
  125. it('should be able to reply to a post', async () => {})
  126. it('should return an error if reply id does not exist', async () => {})
  127. it('should return an error if post reply not in same thread', async () => {})
  128. })
  129. describe('GET /post/:id', () => {
  130. it('should return the post', async () => {})
  131. it('should return an error if invalid post id', async () => {})
  132. })
  133. })