category.js 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. process.env.NODE_ENV = 'test'
  2. let chai = require('chai')
  3. let server = require('../server')
  4. let should = chai.should()
  5. let expect = chai.expect
  6. let { sequelize, Thread } = require('../models')
  7. const Errors = require('../lib/errors.js')
  8. chai.use(require('chai-http'))
  9. chai.use(require('chai-things'))
  10. describe('Category', () => {
  11. //Wait for app to start before commencing
  12. before((done) => {
  13. if(server.locals.appStarted) done()
  14. server.on('appStarted', () => {
  15. done()
  16. })
  17. })
  18. //Delete all rows in table after
  19. //tests completed
  20. after((done) => {
  21. sequelize.sync({ force: true })
  22. .then(() => {
  23. done(null);
  24. })
  25. .catch((err) => {
  26. done(err)
  27. })
  28. })
  29. describe('POST /category', () => {
  30. let agent = chai.request.agent(server)
  31. it('should add a new category if logged in', async () => {
  32. await agent
  33. .post('/api/v1/user')
  34. .set('content-type', 'application/json')
  35. .send({
  36. username: 'adminaccount',
  37. password: 'password',
  38. admin: true
  39. })
  40. let res = await agent
  41. .post('/api/v1/category')
  42. .set('content-type', 'application/json')
  43. .send({ name: 'category' })
  44. res.should.be.json
  45. res.should.have.status(200)
  46. res.body.should.have.property('name', 'category')
  47. res.body.should.have.property('color')
  48. })
  49. it('should have an "underscored" value field', async () => {
  50. let res = await agent
  51. .post('/api/v1/category')
  52. .set('content-type', 'application/json')
  53. .send({ name: ' another category here ' })
  54. res.should.be.json
  55. res.should.have.status(200)
  56. res.body.should.have.property('name', ' another category here ')
  57. res.body.should.have.property('value', 'ANOTHER_CATEGORY_HERE')
  58. })
  59. it('should return an error if category already exists', async () => {
  60. try {
  61. let res = await agent
  62. .post('/api/v1/category')
  63. .set('content-type', 'application/json')
  64. .send({ name: 'category' })
  65. res.should.be.json
  66. res.should.have.status(400)
  67. res.body.errors.should.contain.something.that.deep.equals(Errors.categoryAlreadyExists)
  68. } catch (res) {
  69. res.should.have.status(400)
  70. JSON.parse(res.response.text).errors.should.contain.something.that.deep.equals(Errors.categoryAlreadyExists)
  71. }
  72. })
  73. it('should return an error if not an admin account', async () => {
  74. let agent = chai.request.agent(server)
  75. await agent
  76. .post('/api/v1/user')
  77. .set('content-type', 'application/json')
  78. .send({
  79. username: 'username',
  80. password: 'password',
  81. })
  82. try {
  83. let res = await agent
  84. .post('/api/v1/category')
  85. .set('content-type', 'application/json')
  86. .send({ name: 'category1' })
  87. res.should.be.json
  88. res.should.have.status(401)
  89. res.body.errors.should.contain.something.that.deep.equals(Errors.requestNotAuthorized)
  90. } catch (res) {
  91. res.should.have.status(401)
  92. JSON.parse(res.response.text).errors.should.contain.something.that.deep.equals(Errors.requestNotAuthorized)
  93. }
  94. })
  95. it('should return an error if not logged', async () => {
  96. try {
  97. await chai.request(server)
  98. .post('/api/v1/category')
  99. .set('content-type', 'application/json')
  100. .send({ name: 'category1' })
  101. res.should.be.json
  102. res.should.have.status(401)
  103. res.body.errors.should.contain.something.that.deep.equals(Errors.requestNotAuthorized)
  104. } catch (res) {
  105. res.should.have.status(401)
  106. JSON.parse(res.response.text).errors.should.contain.something.that.deep.equals(Errors.requestNotAuthorized)
  107. }
  108. })
  109. })
  110. describe('GET /category', () => {
  111. before(async () => {
  112. let agent = chai.request.agent(server)
  113. await agent
  114. .post('/api/v1/user/adminaccount/login')
  115. .set('content-type', 'application/json')
  116. .send({ password: 'password' })
  117. await agent
  118. .post('/api/v1/category')
  119. .set('content-type', 'application/json')
  120. .send({ name: 'another_category' })
  121. })
  122. it('should return all categories', async () => {
  123. let res = await chai.request(server)
  124. .get('/api/v1/category')
  125. res.should.be.json
  126. res.should.have.status(200)
  127. res.body.should.contain.an.item.with.property('name', 'category')
  128. res.body.should.contain.an.item.with.property('name', 'another_category')
  129. })
  130. })
  131. describe('GET /category/:category', () => {
  132. it('should return allow pagination for category ALL', async () => {
  133. let agent = chai.request.agent(server)
  134. await agent
  135. .post('/api/v1/user/adminaccount/login')
  136. .set('content-type', 'application/json')
  137. .send({ password: 'password' })
  138. await agent
  139. .post('/api/v1/category')
  140. .set('content-type', 'application/json')
  141. .send({ name: 'pagination1' })
  142. await agent
  143. .post('/api/v1/category')
  144. .set('content-type', 'application/json')
  145. .send({ name: 'pagination2' })
  146. for(var i = 0; i < 30; i++) {
  147. let category = 'pagination1'
  148. if(i % 2) category = 'pagination2'
  149. let thread = await agent
  150. .post('/api/v1/thread')
  151. .set('content-type', 'application/json')
  152. .send({ name: `THREAD ${i}`, category })
  153. await agent
  154. .post('/api/v1/post')
  155. .set('content-type', 'application/json')
  156. .send({ content: `POST ${i}`, threadId: thread.body.id })
  157. }
  158. let pageOne = await agent.get('/api/v1/category/ALL')
  159. let pageTwo = await agent.get(pageOne.body.meta.nextURL)
  160. let pageThree = await agent.get(pageTwo.body.meta.nextURL)
  161. pageOne.body.Threads.should.have.length(10)
  162. pageOne.body.meta.should.have.property('nextThreadsCount', 10)
  163. pageOne.body.Threads[0].Posts[0].should.have.property('content', '<p>POST 29</p>\n')
  164. pageTwo.body.Threads.should.have.length(10)
  165. pageTwo.body.meta.should.have.property('nextThreadsCount', 10)
  166. pageTwo.body.Threads[0].Posts[0].should.have.property('content', '<p>POST 19</p>\n')
  167. pageThree.body.Threads.should.have.length(10)
  168. pageThree.body.meta.should.have.property('nextThreadsCount', 0)
  169. pageThree.body.Threads[0].Posts[0].should.have.property('content', '<p>POST 9</p>\n')
  170. pageThree.body.Threads[9].Posts[0].should.have.property('content', '<p>POST 0</p>\n')
  171. expect(pageThree.body.meta.nextURL).to.be.null
  172. })
  173. it('should return all threads in a category', async () => {
  174. let agent = chai.request.agent(server)
  175. await agent
  176. .post('/api/v1/user/adminaccount/login')
  177. .set('content-type', 'application/json')
  178. .send({ password: 'password' })
  179. for(var i = 0; i < 3; i++) {
  180. let thread = await agent
  181. .post('/api/v1/thread')
  182. .set('content-type', 'application/json')
  183. .send({ name: 'thread ' + i, category: 'category' })
  184. await agent
  185. .post('/api/v1/post')
  186. .set('content-type', 'application/json')
  187. .send({ content: 'content here ' + i, threadId: thread.body.id })
  188. }
  189. let res = await chai.request(server)
  190. .get('/api/v1/category/CATEGORY')
  191. res.should.be.json
  192. res.should.have.status(200)
  193. res.body.should.have.property('name', 'category')
  194. res.body.Threads.should.have.property('length', 3)
  195. res.body.Threads.should.contain.an.item.with.deep.property('User.username', 'adminaccount')
  196. res.body.Threads[0].Posts[0].should.have.property('content', '<p>content here 2</p>\n')
  197. res.body.Threads[1].Posts[0].should.have.property('content', '<p>content here 1</p>\n')
  198. res.body.Threads[2].Posts[0].should.have.property('content', '<p>content here 0</p>\n')
  199. res.body.Threads.should.contain.an.item.with.deep.property('Posts.0.User.username', 'adminaccount')
  200. })
  201. it('should return an error if category does not exist', async () => {
  202. try {
  203. let res = await chai.request(server)
  204. .get('/api/v1/category/not_real')
  205. res.should.be.json
  206. res.should.have.status(400)
  207. res.body.errors.should.contain.something.that.deep.equals(Errors.invalidParameter('id', 'thread does not exist'))
  208. } catch (res) {
  209. let body = JSON.parse(res.response.text)
  210. res.should.have.status(400)
  211. body.errors.should.contain.something.that.deep.equals(Errors.invalidParameter('id', 'thread does not exist'))
  212. }
  213. })
  214. })
  215. })