category.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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('Category', () => {
  10. //Wait for app to start before commencing
  11. before((done) => {
  12. if(server.locals.appStarted) done()
  13. server.on('appStarted', () => {
  14. done()
  15. })
  16. })
  17. //Delete all rows in table after
  18. //tests completed
  19. after((done) => {
  20. sequelize.sync({ force: true })
  21. .then(() => {
  22. done(null);
  23. })
  24. .catch((err) => {
  25. done(err)
  26. })
  27. })
  28. describe('POST /category', () => {
  29. let agent = chai.request.agent(server)
  30. it('should add a new category if logged in', async () => {
  31. await agent
  32. .post('/api/v1/user')
  33. .set('content-type', 'application/json')
  34. .send({
  35. username: 'adminaccount',
  36. password: 'password',
  37. admin: true
  38. })
  39. let res = await agent
  40. .post('/api/v1/category')
  41. .set('content-type', 'application/json')
  42. .send({ name: 'category' })
  43. res.should.be.json
  44. res.should.have.status(200)
  45. res.body.should.have.property('name', 'category')
  46. })
  47. it('should have an "underscored" value field', async () => {
  48. let res = await agent
  49. .post('/api/v1/category')
  50. .set('content-type', 'application/json')
  51. .send({ name: ' another category here ' })
  52. res.should.be.json
  53. res.should.have.status(200)
  54. res.body.should.have.property('name', ' another category here ')
  55. res.body.should.have.property('value', 'ANOTHER_CATEGORY_HERE')
  56. })
  57. it('should return an error if category already exists', async () => {
  58. try {
  59. let res = await agent
  60. .post('/api/v1/category')
  61. .set('content-type', 'application/json')
  62. .send({ name: 'category' })
  63. res.should.be.json
  64. res.should.have.status(400)
  65. res.body.errors.should.contain.something.that.deep.equals(Errors.categoryAlreadyExists)
  66. } catch (res) {
  67. res.should.have.status(400)
  68. JSON.parse(res.response.text).errors.should.contain.something.that.deep.equals(Errors.categoryAlreadyExists)
  69. }
  70. })
  71. it('should return an error if not an admin account', async () => {
  72. let agent = chai.request.agent(server)
  73. await agent
  74. .post('/api/v1/user')
  75. .set('content-type', 'application/json')
  76. .send({
  77. username: 'username',
  78. password: 'password',
  79. })
  80. try {
  81. let res = await agent
  82. .post('/api/v1/category')
  83. .set('content-type', 'application/json')
  84. .send({ name: 'category1' })
  85. res.should.be.json
  86. res.should.have.status(401)
  87. res.body.errors.should.contain.something.that.deep.equals(Errors.requestNotAuthorized)
  88. } catch (res) {
  89. res.should.have.status(401)
  90. JSON.parse(res.response.text).errors.should.contain.something.that.deep.equals(Errors.requestNotAuthorized)
  91. }
  92. })
  93. it('should return an error if not logged', async () => {
  94. try {
  95. await chai.request(server)
  96. .post('/api/v1/category')
  97. .set('content-type', 'application/json')
  98. .send({ name: 'category1' })
  99. res.should.be.json
  100. res.should.have.status(401)
  101. res.body.errors.should.contain.something.that.deep.equals(Errors.requestNotAuthorized)
  102. } catch (res) {
  103. res.should.have.status(401)
  104. JSON.parse(res.response.text).errors.should.contain.something.that.deep.equals(Errors.requestNotAuthorized)
  105. }
  106. })
  107. })
  108. describe('GET /category', () => {
  109. before(async () => {
  110. let agent = chai.request.agent(server)
  111. await agent
  112. .post('/api/v1/user/adminaccount/login')
  113. .set('content-type', 'application/json')
  114. .send({ password: 'password' })
  115. await agent
  116. .post('/api/v1/category')
  117. .set('content-type', 'application/json')
  118. .send({ name: 'another_category' })
  119. })
  120. it('should return all categories', async () => {
  121. let res = await chai.request(server)
  122. .get('/api/v1/category')
  123. res.should.be.json
  124. res.should.have.status(200)
  125. res.body.should.contain.an.item.with.property('name', 'category')
  126. res.body.should.contain.an.item.with.property('name', 'another_category')
  127. })
  128. })
  129. describe('GET /category/:category', () => {
  130. before(async () => {
  131. let agent = chai.request.agent(server)
  132. await agent
  133. .post('/api/v1/user/adminaccount/login')
  134. .set('content-type', 'application/json')
  135. .send({ password: 'password' })
  136. await agent
  137. .post('/api/v1/thread')
  138. .set('content-type', 'application/json')
  139. .send({ name: 'thread', category: 'category' })
  140. await agent
  141. .post('/api/v1/thread')
  142. .set('content-type', 'application/json')
  143. .send({ name: 'another_thread', category: 'category' })
  144. await agent
  145. .post('/api/v1/post')
  146. .set('content-type', 'application/json')
  147. .send({ content: 'content here', threadId: 1 })
  148. await agent
  149. .post('/api/v1/post')
  150. .set('content-type', 'application/json')
  151. .send({ content: 'content here', threadId: 2 })
  152. })
  153. it('should return all threads in a category', async () => {
  154. let res = await chai.request(server)
  155. .get('/api/v1/category/category')
  156. res.should.be.json
  157. res.should.have.status(200)
  158. res.body.should.have.property('name', 'category')
  159. res.body.Threads.should.have.property('length', 2)
  160. res.body.Threads.should.contain.an.item.with.deep.property('User.username', 'adminaccount')
  161. res.body.Threads.should.contain.an.item.with.deep.property('Posts.0.content', '<p>content here</p>\n')
  162. res.body.Threads.should.contain.an.item.with.deep.property('Posts.0.User.username', 'adminaccount')
  163. })
  164. it('should return an error if category does not exist', async () => {
  165. try {
  166. let res = await chai.request(server)
  167. .get('/api/v1/category/not_real')
  168. res.should.be.json
  169. res.should.have.status(400)
  170. res.body.errors.should.contain.something.that.deep.equals(Errors.invalidParameter('id', 'thread does not exist'))
  171. } catch (res) {
  172. let body = JSON.parse(res.response.text)
  173. res.should.have.status(400)
  174. body.errors.should.contain.something.that.deep.equals(Errors.invalidParameter('id', 'thread does not exist'))
  175. }
  176. })
  177. })
  178. })