like.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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('Like', () => {
  10. let admin = chai.request.agent(server)
  11. let user = chai.request.agent(server)
  12. let user2 = chai.request.agent(server)
  13. //Wait for app to start before commencing
  14. before((done) => {
  15. function serverStarted () {
  16. admin
  17. .post('/api/v1/user')
  18. .set('content-type', 'application/json')
  19. .send({
  20. username: 'adminaccount',
  21. password: 'password',
  22. admin: true
  23. })
  24. .then(() => {
  25. return user
  26. .post('/api/v1/user')
  27. .set('content-type', 'application/json')
  28. .send({
  29. username: 'useraccount',
  30. password: 'password',
  31. })
  32. })
  33. .then(() => {
  34. return user2
  35. .post('/api/v1/user')
  36. .set('content-type', 'application/json')
  37. .send({
  38. username: 'useraccount2',
  39. password: 'password',
  40. })
  41. })
  42. .then(() => {
  43. return admin
  44. .post('/api/v1/category')
  45. .set('content-type', 'application/json')
  46. .send({ name: 'category' })
  47. })
  48. .then(() => {
  49. return admin
  50. .post('/api/v1/thread')
  51. .set('content-type', 'application/json')
  52. .send({ category: 'CATEGORY', name: 'thread' })
  53. })
  54. .then(() => {
  55. return admin
  56. .post('/api/v1/post')
  57. .set('content-type', 'application/json')
  58. .send({ threadId: 1, content: 'POST 1' })
  59. })
  60. .then(() => {
  61. return admin
  62. .post('/api/v1/post')
  63. .set('content-type', 'application/json')
  64. .send({ threadId: 1, content: 'POST 2' })
  65. })
  66. .then(() => {
  67. return user
  68. .post('/api/v1/post')
  69. .set('content-type', 'application/json')
  70. .send({ threadId: 1, content: 'POST 3' })
  71. })
  72. .then(_ => {
  73. done()
  74. })
  75. .catch(done)
  76. }
  77. if(server.locals.appStarted) serverStarted()
  78. server.on('appStarted', () => {
  79. serverStarted()
  80. })
  81. })
  82. after(() => sequelize.sync({ force: true }))
  83. it('should allow a user to like a post', async () => {
  84. let likeRes = await user.put('/api/v1/post/1/like')
  85. //Should be idempotent, hence put twice
  86. await user.put('/api/v1/post/1/like')
  87. likeRes.should.have.status(200)
  88. likeRes.should.be.json
  89. likeRes.body.should.have.property('success', true)
  90. let postRes = await user.get('/api/v1/post/1')
  91. postRes.should.have.status(200)
  92. postRes.should.be.json
  93. postRes.body.should.have.property('Likes')
  94. postRes.body.Likes.should.have.property('length', 1)
  95. postRes.body.Likes[0].should.have.deep.property('username', 'useraccount')
  96. })
  97. it('should return an error if not logged in', done => {
  98. chai.request(server)
  99. .put('/api/v1/post/3/like')
  100. .end((err, res) => {
  101. res.body.errors.should.contain.something.that.deep.equals(Errors.requestNotAuthorized)
  102. done()
  103. })
  104. })
  105. it('should return an error if liking own post', done => {
  106. admin
  107. .put('/api/v1/post/1/like')
  108. .end((err, res) => {
  109. res.body.errors.should.contain.something.that.deep.equals(Errors.cannotLikeOwnPost)
  110. done()
  111. })
  112. })
  113. it('should return all likes on a post', async () => {
  114. await admin.put('/api/v1/post/3/like')
  115. await user2.put('/api/v1/post/3/like')
  116. let postRes = await admin.get('/api/v1/post/3')
  117. postRes.should.have.status(200)
  118. postRes.should.be.json
  119. postRes.body.should.have.property('Likes')
  120. postRes.body.Likes.should.have.property('length', 2)
  121. })
  122. it('should return all likes on a thread', async () => {
  123. let thread = await admin.get('/api/v1/thread/1')
  124. thread.should.have.status(200)
  125. thread.should.be.json
  126. thread.body.Posts[0].Likes.should.have.property('length', 1)
  127. thread.body.Posts[2].Likes.should.have.property('length', 2)
  128. })
  129. it('should remove a like', async () => {
  130. await admin.delete('/api/v1/post/3/like')
  131. let postRes = await admin.get('/api/v1/post/3')
  132. postRes.should.have.status(200)
  133. postRes.should.be.json
  134. postRes.body.Likes.should.have.length(1)
  135. postRes.body.Likes.should.not.contain.something.that.has.property('username', 'adminaccount')
  136. })
  137. it('should do nothing if removing a like on something never liked', async () => {
  138. await admin.delete('/api/v1/post/2/like')
  139. let postRes = await admin.get('/api/v1/post/2')
  140. postRes.should.have.status(200)
  141. postRes.should.be.json
  142. postRes.body.Likes.should.have.length(0)
  143. })
  144. })