category.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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. res.body.should.have.property('color')
  47. })
  48. it('should have an "underscored" value field', async () => {
  49. let res = await agent
  50. .post('/api/v1/category')
  51. .set('content-type', 'application/json')
  52. .send({ name: ' another category here ' })
  53. res.should.be.json
  54. res.should.have.status(200)
  55. res.body.should.have.property('name', ' another category here ')
  56. res.body.should.have.property('value', 'ANOTHER_CATEGORY_HERE')
  57. })
  58. it('should return an error if category already exists', async () => {
  59. try {
  60. let res = await agent
  61. .post('/api/v1/category')
  62. .set('content-type', 'application/json')
  63. .send({ name: 'category' })
  64. res.should.be.json
  65. res.should.have.status(400)
  66. res.body.errors.should.contain.something.that.deep.equals(Errors.categoryAlreadyExists)
  67. } catch (res) {
  68. res.should.have.status(400)
  69. JSON.parse(res.response.text).errors.should.contain.something.that.deep.equals(Errors.categoryAlreadyExists)
  70. }
  71. })
  72. it('should return an error if not an admin account', async () => {
  73. let agent = chai.request.agent(server)
  74. await agent
  75. .post('/api/v1/user')
  76. .set('content-type', 'application/json')
  77. .send({
  78. username: 'username',
  79. password: 'password',
  80. })
  81. try {
  82. let res = await agent
  83. .post('/api/v1/category')
  84. .set('content-type', 'application/json')
  85. .send({ name: 'category1' })
  86. res.should.be.json
  87. res.should.have.status(401)
  88. res.body.errors.should.contain.something.that.deep.equals(Errors.requestNotAuthorized)
  89. } catch (res) {
  90. res.should.have.status(401)
  91. JSON.parse(res.response.text).errors.should.contain.something.that.deep.equals(Errors.requestNotAuthorized)
  92. }
  93. })
  94. it('should return an error if not logged', async () => {
  95. try {
  96. await chai.request(server)
  97. .post('/api/v1/category')
  98. .set('content-type', 'application/json')
  99. .send({ name: 'category1' })
  100. res.should.be.json
  101. res.should.have.status(401)
  102. res.body.errors.should.contain.something.that.deep.equals(Errors.requestNotAuthorized)
  103. } catch (res) {
  104. res.should.have.status(401)
  105. JSON.parse(res.response.text).errors.should.contain.something.that.deep.equals(Errors.requestNotAuthorized)
  106. }
  107. })
  108. })
  109. describe('GET /category', () => {
  110. before(async () => {
  111. let agent = chai.request.agent(server)
  112. await agent
  113. .post('/api/v1/user/adminaccount/login')
  114. .set('content-type', 'application/json')
  115. .send({ password: 'password' })
  116. await agent
  117. .post('/api/v1/category')
  118. .set('content-type', 'application/json')
  119. .send({ name: 'another_category' })
  120. })
  121. it('should return all categories', async () => {
  122. let res = await chai.request(server)
  123. .get('/api/v1/category')
  124. res.should.be.json
  125. res.should.have.status(200)
  126. res.body.should.contain.an.item.with.property('name', 'category')
  127. res.body.should.contain.an.item.with.property('name', 'another_category')
  128. })
  129. })
  130. describe('GET /category/:category', () => {
  131. before(async () => {
  132. let agent = chai.request.agent(server)
  133. await agent
  134. .post('/api/v1/user/adminaccount/login')
  135. .set('content-type', 'application/json')
  136. .send({ password: 'password' })
  137. await agent
  138. .post('/api/v1/thread')
  139. .set('content-type', 'application/json')
  140. .send({ name: 'thread', category: 'category' })
  141. await agent
  142. .post('/api/v1/thread')
  143. .set('content-type', 'application/json')
  144. .send({ name: 'another_thread', category: 'category' })
  145. await agent
  146. .post('/api/v1/post')
  147. .set('content-type', 'application/json')
  148. .send({ content: 'content here', threadId: 1 })
  149. await agent
  150. .post('/api/v1/post')
  151. .set('content-type', 'application/json')
  152. .send({ content: 'content here 2', threadId: 1 })
  153. await agent
  154. .post('/api/v1/post')
  155. .set('content-type', 'application/json')
  156. .send({ content: 'content here 3', threadId: 1 })
  157. await agent
  158. .post('/api/v1/post')
  159. .set('content-type', 'application/json')
  160. .send({ content: 'content here', threadId: 2 })
  161. await agent
  162. .post('/api/v1/post')
  163. .set('content-type', 'application/json')
  164. .send({ content: 'content here 2', threadId: 2 })
  165. await agent
  166. .post('/api/v1/post')
  167. .set('content-type', 'application/json')
  168. .send({ content: 'content here 3', threadId: 2 })
  169. })
  170. it('should return all threads in a category', async () => {
  171. let res = await chai.request(server)
  172. .get('/api/v1/category/category')
  173. res.should.be.json
  174. res.should.have.status(200)
  175. res.body.should.have.property('name', 'category')
  176. res.body.Threads.should.have.property('length', 2)
  177. res.body.Threads.should.contain.an.item.with.deep.property('User.username', 'adminaccount')
  178. res.body.Threads.should.contain.an.item.with.deep.property('Posts.0.content', '<p>content here</p>\n')
  179. res.body.Threads.should.contain.an.item.with.deep.property('Posts.1.content', '<p>content here 3</p>\n')
  180. res.body.Threads.should.contain.an.item.with.deep.property('Posts.0.User.username', 'adminaccount')
  181. })
  182. it('should return all threads in all categories', async () => {
  183. let res = await chai.request(server)
  184. .get('/api/v1/category/ALL')
  185. res.should.be.json
  186. res.should.have.status(200)
  187. res.body.should.have.property('name', 'All')
  188. res.body.Threads.should.have.property('length', 2)
  189. res.body.Threads.should.contain.an.item.with.deep.property('User.username', 'adminaccount')
  190. res.body.Threads.should.contain.an.item.with.deep.property('Posts.0.content', '<p>content here</p>\n')
  191. res.body.Threads.should.contain.an.item.with.deep.property('Posts.1.content', '<p>content here 3</p>\n')
  192. res.body.Threads.should.contain.an.item.with.deep.property('Posts.0.User.username', 'adminaccount')
  193. })
  194. it('should return an error if category does not exist', async () => {
  195. try {
  196. let res = await chai.request(server)
  197. .get('/api/v1/category/not_real')
  198. res.should.be.json
  199. res.should.have.status(400)
  200. res.body.errors.should.contain.something.that.deep.equals(Errors.invalidParameter('id', 'thread does not exist'))
  201. } catch (res) {
  202. let body = JSON.parse(res.response.text)
  203. res.should.have.status(400)
  204. body.errors.should.contain.something.that.deep.equals(Errors.invalidParameter('id', 'thread does not exist'))
  205. }
  206. })
  207. })
  208. })