video-comments.ts 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /* tslint:disable:no-unused-expression */
  2. import * as chai from 'chai'
  3. import 'mocha'
  4. import { VideoComment, VideoCommentThreadTree } from '../../../../shared/models/videos/video-comment.model'
  5. import { testImage } from '../../../../shared/extra-utils'
  6. import {
  7. dateIsValid,
  8. flushTests,
  9. killallServers,
  10. flushAndRunServer,
  11. ServerInfo,
  12. setAccessTokensToServers,
  13. updateMyAvatar,
  14. uploadVideo
  15. } from '../../../../shared/extra-utils/index'
  16. import {
  17. addVideoCommentReply,
  18. addVideoCommentThread,
  19. deleteVideoComment,
  20. getVideoCommentThreads,
  21. getVideoThreadComments
  22. } from '../../../../shared/extra-utils/videos/video-comments'
  23. const expect = chai.expect
  24. describe('Test video comments', function () {
  25. let server: ServerInfo
  26. let videoId
  27. let videoUUID
  28. let threadId
  29. let replyToDeleteId: number
  30. before(async function () {
  31. this.timeout(30000)
  32. server = await flushAndRunServer(1)
  33. await setAccessTokensToServers([ server ])
  34. const res = await uploadVideo(server.url, server.accessToken, {})
  35. videoUUID = res.body.video.uuid
  36. videoId = res.body.video.id
  37. await updateMyAvatar({
  38. url: server.url,
  39. accessToken: server.accessToken,
  40. fixture: 'avatar.png'
  41. })
  42. })
  43. it('Should not have threads on this video', async function () {
  44. const res = await getVideoCommentThreads(server.url, videoUUID, 0, 5)
  45. expect(res.body.total).to.equal(0)
  46. expect(res.body.data).to.be.an('array')
  47. expect(res.body.data).to.have.lengthOf(0)
  48. })
  49. it('Should create a thread in this video', async function () {
  50. const text = 'my super first comment'
  51. const res = await addVideoCommentThread(server.url, server.accessToken, videoUUID, text)
  52. const comment = res.body.comment
  53. expect(comment.inReplyToCommentId).to.be.null
  54. expect(comment.text).equal('my super first comment')
  55. expect(comment.videoId).to.equal(videoId)
  56. expect(comment.id).to.equal(comment.threadId)
  57. expect(comment.account.name).to.equal('root')
  58. expect(comment.account.host).to.equal('localhost:9001')
  59. expect(comment.account.url).to.equal('http://localhost:9001/accounts/root')
  60. expect(comment.totalReplies).to.equal(0)
  61. expect(dateIsValid(comment.createdAt as string)).to.be.true
  62. expect(dateIsValid(comment.updatedAt as string)).to.be.true
  63. })
  64. it('Should list threads of this video', async function () {
  65. const res = await getVideoCommentThreads(server.url, videoUUID, 0, 5)
  66. expect(res.body.total).to.equal(1)
  67. expect(res.body.data).to.be.an('array')
  68. expect(res.body.data).to.have.lengthOf(1)
  69. const comment: VideoComment = res.body.data[0]
  70. expect(comment.inReplyToCommentId).to.be.null
  71. expect(comment.text).equal('my super first comment')
  72. expect(comment.videoId).to.equal(videoId)
  73. expect(comment.id).to.equal(comment.threadId)
  74. expect(comment.account.name).to.equal('root')
  75. expect(comment.account.host).to.equal('localhost:9001')
  76. await testImage(server.url, 'avatar-resized', comment.account.avatar.path, '.png')
  77. expect(comment.totalReplies).to.equal(0)
  78. expect(dateIsValid(comment.createdAt as string)).to.be.true
  79. expect(dateIsValid(comment.updatedAt as string)).to.be.true
  80. threadId = comment.threadId
  81. })
  82. it('Should get all the thread created', async function () {
  83. const res = await getVideoThreadComments(server.url, videoUUID, threadId)
  84. const rootComment = res.body.comment
  85. expect(rootComment.inReplyToCommentId).to.be.null
  86. expect(rootComment.text).equal('my super first comment')
  87. expect(rootComment.videoId).to.equal(videoId)
  88. expect(dateIsValid(rootComment.createdAt as string)).to.be.true
  89. expect(dateIsValid(rootComment.updatedAt as string)).to.be.true
  90. })
  91. it('Should create multiple replies in this thread', async function () {
  92. const text1 = 'my super answer to thread 1'
  93. const childCommentRes = await addVideoCommentReply(server.url, server.accessToken, videoId, threadId, text1)
  94. const childCommentId = childCommentRes.body.comment.id
  95. const text2 = 'my super answer to answer of thread 1'
  96. await addVideoCommentReply(server.url, server.accessToken, videoId, childCommentId, text2)
  97. const text3 = 'my second answer to thread 1'
  98. await addVideoCommentReply(server.url, server.accessToken, videoId, threadId, text3)
  99. })
  100. it('Should get correctly the replies', async function () {
  101. const res = await getVideoThreadComments(server.url, videoUUID, threadId)
  102. const tree: VideoCommentThreadTree = res.body
  103. expect(tree.comment.text).equal('my super first comment')
  104. expect(tree.children).to.have.lengthOf(2)
  105. const firstChild = tree.children[0]
  106. expect(firstChild.comment.text).to.equal('my super answer to thread 1')
  107. expect(firstChild.children).to.have.lengthOf(1)
  108. const childOfFirstChild = firstChild.children[0]
  109. expect(childOfFirstChild.comment.text).to.equal('my super answer to answer of thread 1')
  110. expect(childOfFirstChild.children).to.have.lengthOf(0)
  111. const secondChild = tree.children[1]
  112. expect(secondChild.comment.text).to.equal('my second answer to thread 1')
  113. expect(secondChild.children).to.have.lengthOf(0)
  114. replyToDeleteId = secondChild.comment.id
  115. })
  116. it('Should create other threads', async function () {
  117. const text1 = 'super thread 2'
  118. await addVideoCommentThread(server.url, server.accessToken, videoUUID, text1)
  119. const text2 = 'super thread 3'
  120. await addVideoCommentThread(server.url, server.accessToken, videoUUID, text2)
  121. })
  122. it('Should list the threads', async function () {
  123. const res = await getVideoCommentThreads(server.url, videoUUID, 0, 5, 'createdAt')
  124. expect(res.body.total).to.equal(3)
  125. expect(res.body.data).to.be.an('array')
  126. expect(res.body.data).to.have.lengthOf(3)
  127. expect(res.body.data[0].text).to.equal('my super first comment')
  128. expect(res.body.data[0].totalReplies).to.equal(3)
  129. expect(res.body.data[1].text).to.equal('super thread 2')
  130. expect(res.body.data[1].totalReplies).to.equal(0)
  131. expect(res.body.data[2].text).to.equal('super thread 3')
  132. expect(res.body.data[2].totalReplies).to.equal(0)
  133. })
  134. it('Should delete a reply', async function () {
  135. await deleteVideoComment(server.url, server.accessToken, videoId, replyToDeleteId)
  136. const res = await getVideoThreadComments(server.url, videoUUID, threadId)
  137. const tree: VideoCommentThreadTree = res.body
  138. expect(tree.comment.text).equal('my super first comment')
  139. expect(tree.children).to.have.lengthOf(1)
  140. const firstChild = tree.children[0]
  141. expect(firstChild.comment.text).to.equal('my super answer to thread 1')
  142. expect(firstChild.children).to.have.lengthOf(1)
  143. const childOfFirstChild = firstChild.children[0]
  144. expect(childOfFirstChild.comment.text).to.equal('my super answer to answer of thread 1')
  145. expect(childOfFirstChild.children).to.have.lengthOf(0)
  146. })
  147. it('Should delete a complete thread', async function () {
  148. await deleteVideoComment(server.url, server.accessToken, videoId, threadId)
  149. const res = await getVideoCommentThreads(server.url, videoUUID, 0, 5, 'createdAt')
  150. expect(res.body.total).to.equal(2)
  151. expect(res.body.data).to.be.an('array')
  152. expect(res.body.data).to.have.lengthOf(2)
  153. expect(res.body.data[0].text).to.equal('super thread 2')
  154. expect(res.body.data[0].totalReplies).to.equal(0)
  155. expect(res.body.data[1].text).to.equal('super thread 3')
  156. expect(res.body.data[1].totalReplies).to.equal(0)
  157. })
  158. after(function () {
  159. killallServers([ server ])
  160. })
  161. })