follows.ts 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  1. /* tslint:disable:no-unused-expression */
  2. import * as chai from 'chai'
  3. import 'mocha'
  4. import { Video, VideoPrivacy } from '../../../../shared/models/videos'
  5. import { VideoComment, VideoCommentThreadTree } from '../../../../shared/models/videos/video-comment.model'
  6. import { cleanupTests, completeVideoCheck } from '../../../../shared/extra-utils'
  7. import {
  8. flushAndRunMultipleServers,
  9. getVideosList,
  10. ServerInfo,
  11. setAccessTokensToServers,
  12. uploadVideo
  13. } from '../../../../shared/extra-utils/index'
  14. import { dateIsValid } from '../../../../shared/extra-utils/miscs/miscs'
  15. import {
  16. follow,
  17. getFollowersListPaginationAndSort,
  18. getFollowingListPaginationAndSort,
  19. unfollow
  20. } from '../../../../shared/extra-utils/server/follows'
  21. import { expectAccountFollows } from '../../../../shared/extra-utils/users/accounts'
  22. import { userLogin } from '../../../../shared/extra-utils/users/login'
  23. import { createUser } from '../../../../shared/extra-utils/users/users'
  24. import {
  25. addVideoCommentReply,
  26. addVideoCommentThread,
  27. getVideoCommentThreads,
  28. getVideoThreadComments
  29. } from '../../../../shared/extra-utils/videos/video-comments'
  30. import { rateVideo } from '../../../../shared/extra-utils/videos/videos'
  31. import { waitJobs } from '../../../../shared/extra-utils/server/jobs'
  32. import { createVideoCaption, listVideoCaptions, testCaptionFile } from '../../../../shared/extra-utils/videos/video-captions'
  33. import { VideoCaption } from '../../../../shared/models/videos/caption/video-caption.model'
  34. const expect = chai.expect
  35. describe('Test follows', function () {
  36. let servers: ServerInfo[] = []
  37. before(async function () {
  38. this.timeout(30000)
  39. servers = await flushAndRunMultipleServers(3)
  40. // Get the access tokens
  41. await setAccessTokensToServers(servers)
  42. })
  43. it('Should not have followers', async function () {
  44. for (const server of servers) {
  45. const res = await getFollowersListPaginationAndSort(server.url, 0, 5, 'createdAt')
  46. const follows = res.body.data
  47. expect(res.body.total).to.equal(0)
  48. expect(follows).to.be.an('array')
  49. expect(follows.length).to.equal(0)
  50. }
  51. })
  52. it('Should not have following', async function () {
  53. for (const server of servers) {
  54. const res = await getFollowingListPaginationAndSort(server.url, 0, 5, 'createdAt')
  55. const follows = res.body.data
  56. expect(res.body.total).to.equal(0)
  57. expect(follows).to.be.an('array')
  58. expect(follows.length).to.equal(0)
  59. }
  60. })
  61. it('Should have server 1 following server 2 and 3', async function () {
  62. this.timeout(30000)
  63. await follow(servers[0].url, [ servers[1].url, servers[2].url ], servers[0].accessToken)
  64. await waitJobs(servers)
  65. })
  66. it('Should have 2 followings on server 1', async function () {
  67. let res = await getFollowingListPaginationAndSort(servers[0].url, 0, 1, 'createdAt')
  68. let follows = res.body.data
  69. expect(res.body.total).to.equal(2)
  70. expect(follows).to.be.an('array')
  71. expect(follows.length).to.equal(1)
  72. res = await getFollowingListPaginationAndSort(servers[0].url, 1, 1, 'createdAt')
  73. follows = follows.concat(res.body.data)
  74. const server2Follow = follows.find(f => f.following.host === 'localhost:' + servers[1].port)
  75. const server3Follow = follows.find(f => f.following.host === 'localhost:' + servers[2].port)
  76. expect(server2Follow).to.not.be.undefined
  77. expect(server3Follow).to.not.be.undefined
  78. expect(server2Follow.state).to.equal('accepted')
  79. expect(server3Follow.state).to.equal('accepted')
  80. })
  81. it('Should search followings on server 1', async function () {
  82. {
  83. const res = await getFollowingListPaginationAndSort(servers[ 0 ].url, 0, 1, 'createdAt', ':' + servers[1].port)
  84. const follows = res.body.data
  85. expect(res.body.total).to.equal(1)
  86. expect(follows.length).to.equal(1)
  87. expect(follows[ 0 ].following.host).to.equal('localhost:' + servers[1].port)
  88. }
  89. {
  90. const res = await getFollowingListPaginationAndSort(servers[ 0 ].url, 0, 1, 'createdAt', 'bla')
  91. const follows = res.body.data
  92. expect(res.body.total).to.equal(0)
  93. expect(follows.length).to.equal(0)
  94. }
  95. })
  96. it('Should have 0 followings on server 2 and 3', async function () {
  97. for (const server of [ servers[1], servers[2] ]) {
  98. const res = await getFollowingListPaginationAndSort(server.url, 0, 5, 'createdAt')
  99. const follows = res.body.data
  100. expect(res.body.total).to.equal(0)
  101. expect(follows).to.be.an('array')
  102. expect(follows.length).to.equal(0)
  103. }
  104. })
  105. it('Should have 1 followers on server 2 and 3', async function () {
  106. for (const server of [ servers[1], servers[2] ]) {
  107. let res = await getFollowersListPaginationAndSort(server.url, 0, 1, 'createdAt')
  108. let follows = res.body.data
  109. expect(res.body.total).to.equal(1)
  110. expect(follows).to.be.an('array')
  111. expect(follows.length).to.equal(1)
  112. expect(follows[0].follower.host).to.equal('localhost:' + servers[0].port)
  113. }
  114. })
  115. it('Should search followers on server 2', async function () {
  116. {
  117. const res = await getFollowersListPaginationAndSort(servers[ 2 ].url, 0, 5, 'createdAt', servers[0].port + '')
  118. const follows = res.body.data
  119. expect(res.body.total).to.equal(1)
  120. expect(follows.length).to.equal(1)
  121. expect(follows[ 0 ].following.host).to.equal('localhost:' + servers[2].port)
  122. }
  123. {
  124. const res = await getFollowersListPaginationAndSort(servers[ 2 ].url, 0, 5, 'createdAt', 'bla')
  125. const follows = res.body.data
  126. expect(res.body.total).to.equal(0)
  127. expect(follows.length).to.equal(0)
  128. }
  129. })
  130. it('Should have 0 followers on server 1', async function () {
  131. const res = await getFollowersListPaginationAndSort(servers[0].url, 0, 5, 'createdAt')
  132. const follows = res.body.data
  133. expect(res.body.total).to.equal(0)
  134. expect(follows).to.be.an('array')
  135. expect(follows.length).to.equal(0)
  136. })
  137. it('Should have the correct follows counts', async function () {
  138. await expectAccountFollows(servers[0].url, 'peertube@localhost:' + servers[0].port, 0, 2)
  139. await expectAccountFollows(servers[0].url, 'peertube@localhost:' + servers[1].port, 1, 0)
  140. await expectAccountFollows(servers[0].url, 'peertube@localhost:' + servers[2].port, 1, 0)
  141. // Server 2 and 3 does not know server 1 follow another server (there was not a refresh)
  142. await expectAccountFollows(servers[1].url, 'peertube@localhost:' + servers[0].port, 0, 1)
  143. await expectAccountFollows(servers[1].url, 'peertube@localhost:' + servers[1].port, 1, 0)
  144. await expectAccountFollows(servers[2].url, 'peertube@localhost:' + servers[0].port, 0, 1)
  145. await expectAccountFollows(servers[2].url, 'peertube@localhost:' + servers[2].port, 1, 0)
  146. })
  147. it('Should unfollow server 3 on server 1', async function () {
  148. this.timeout(5000)
  149. await unfollow(servers[0].url, servers[0].accessToken, servers[2])
  150. await waitJobs(servers)
  151. })
  152. it('Should not follow server 3 on server 1 anymore', async function () {
  153. const res = await getFollowingListPaginationAndSort(servers[0].url, 0, 2, 'createdAt')
  154. let follows = res.body.data
  155. expect(res.body.total).to.equal(1)
  156. expect(follows).to.be.an('array')
  157. expect(follows.length).to.equal(1)
  158. expect(follows[0].following.host).to.equal('localhost:' + servers[1].port)
  159. })
  160. it('Should not have server 1 as follower on server 3 anymore', async function () {
  161. const res = await getFollowersListPaginationAndSort(servers[2].url, 0, 1, 'createdAt')
  162. let follows = res.body.data
  163. expect(res.body.total).to.equal(0)
  164. expect(follows).to.be.an('array')
  165. expect(follows.length).to.equal(0)
  166. })
  167. it('Should have the correct follows counts 2', async function () {
  168. await expectAccountFollows(servers[0].url, 'peertube@localhost:' + servers[0].port, 0, 1)
  169. await expectAccountFollows(servers[0].url, 'peertube@localhost:' + servers[1].port, 1, 0)
  170. await expectAccountFollows(servers[1].url, 'peertube@localhost:' + servers[0].port, 0, 1)
  171. await expectAccountFollows(servers[1].url, 'peertube@localhost:' + servers[1].port, 1, 0)
  172. await expectAccountFollows(servers[2].url, 'peertube@localhost:' + servers[0].port, 0, 0)
  173. await expectAccountFollows(servers[2].url, 'peertube@localhost:' + servers[2].port, 0, 0)
  174. })
  175. it('Should upload a video on server 2 and 3 and propagate only the video of server 2', async function () {
  176. this.timeout(35000)
  177. await uploadVideo(servers[1].url, servers[1].accessToken, { name: 'server2' })
  178. await uploadVideo(servers[2].url, servers[2].accessToken, { name: 'server3' })
  179. await waitJobs(servers)
  180. let res = await getVideosList(servers[0].url)
  181. expect(res.body.total).to.equal(1)
  182. expect(res.body.data[0].name).to.equal('server2')
  183. res = await getVideosList(servers[1].url)
  184. expect(res.body.total).to.equal(1)
  185. expect(res.body.data[0].name).to.equal('server2')
  186. res = await getVideosList(servers[2].url)
  187. expect(res.body.total).to.equal(1)
  188. expect(res.body.data[0].name).to.equal('server3')
  189. })
  190. describe('Should propagate data on a new following', function () {
  191. let video4: Video
  192. before(async function () {
  193. this.timeout(20000)
  194. const video4Attributes = {
  195. name: 'server3-4',
  196. category: 2,
  197. nsfw: true,
  198. licence: 6,
  199. tags: [ 'tag1', 'tag2', 'tag3' ]
  200. }
  201. await uploadVideo(servers[ 2 ].url, servers[ 2 ].accessToken, { name: 'server3-2' })
  202. await uploadVideo(servers[ 2 ].url, servers[ 2 ].accessToken, { name: 'server3-3' })
  203. await uploadVideo(servers[ 2 ].url, servers[ 2 ].accessToken, video4Attributes)
  204. await uploadVideo(servers[ 2 ].url, servers[ 2 ].accessToken, { name: 'server3-5' })
  205. await uploadVideo(servers[ 2 ].url, servers[ 2 ].accessToken, { name: 'server3-6' })
  206. {
  207. const user = { username: 'captain', password: 'password' }
  208. await createUser({ url: servers[ 2 ].url, accessToken: servers[ 2 ].accessToken, username: user.username, password: user.password })
  209. const userAccessToken = await userLogin(servers[ 2 ], user)
  210. const resVideos = await getVideosList(servers[ 2 ].url)
  211. video4 = resVideos.body.data.find(v => v.name === 'server3-4')
  212. {
  213. await rateVideo(servers[ 2 ].url, servers[ 2 ].accessToken, video4.id, 'like')
  214. await rateVideo(servers[ 2 ].url, userAccessToken, video4.id, 'dislike')
  215. }
  216. {
  217. const text = 'my super first comment'
  218. const res = await addVideoCommentThread(servers[ 2 ].url, servers[ 2 ].accessToken, video4.id, text)
  219. const threadId = res.body.comment.id
  220. const text1 = 'my super answer to thread 1'
  221. const childCommentRes = await addVideoCommentReply(servers[ 2 ].url, servers[ 2 ].accessToken, video4.id, threadId, text1)
  222. const childCommentId = childCommentRes.body.comment.id
  223. const text2 = 'my super answer to answer of thread 1'
  224. await addVideoCommentReply(servers[ 2 ].url, servers[ 2 ].accessToken, video4.id, childCommentId, text2)
  225. const text3 = 'my second answer to thread 1'
  226. await addVideoCommentReply(servers[ 2 ].url, servers[ 2 ].accessToken, video4.id, threadId, text3)
  227. }
  228. {
  229. await createVideoCaption({
  230. url: servers[2].url,
  231. accessToken: servers[2].accessToken,
  232. language: 'ar',
  233. videoId: video4.id,
  234. fixture: 'subtitle-good2.vtt'
  235. })
  236. }
  237. }
  238. await waitJobs(servers)
  239. // Server 1 follows server 3
  240. await follow(servers[ 0 ].url, [ servers[ 2 ].url ], servers[ 0 ].accessToken)
  241. await waitJobs(servers)
  242. })
  243. it('Should have the correct follows counts 3', async function () {
  244. await expectAccountFollows(servers[0].url, 'peertube@localhost:' + servers[0].port, 0, 2)
  245. await expectAccountFollows(servers[0].url, 'peertube@localhost:' + servers[1].port, 1, 0)
  246. await expectAccountFollows(servers[0].url, 'peertube@localhost:' + servers[2].port, 1, 0)
  247. await expectAccountFollows(servers[1].url, 'peertube@localhost:' + servers[0].port, 0, 1)
  248. await expectAccountFollows(servers[1].url, 'peertube@localhost:' + servers[1].port, 1, 0)
  249. await expectAccountFollows(servers[2].url, 'peertube@localhost:' + servers[0].port, 0, 2)
  250. await expectAccountFollows(servers[2].url, 'peertube@localhost:' + servers[2].port, 1, 0)
  251. })
  252. it('Should have propagated videos', async function () {
  253. const res = await getVideosList(servers[ 0 ].url)
  254. expect(res.body.total).to.equal(7)
  255. const video2 = res.body.data.find(v => v.name === 'server3-2')
  256. video4 = res.body.data.find(v => v.name === 'server3-4')
  257. const video6 = res.body.data.find(v => v.name === 'server3-6')
  258. expect(video2).to.not.be.undefined
  259. expect(video4).to.not.be.undefined
  260. expect(video6).to.not.be.undefined
  261. const isLocal = false
  262. const checkAttributes = {
  263. name: 'server3-4',
  264. category: 2,
  265. licence: 6,
  266. language: 'zh',
  267. nsfw: true,
  268. description: 'my super description',
  269. support: 'my super support text',
  270. account: {
  271. name: 'root',
  272. host: 'localhost:' + servers[2].port
  273. },
  274. isLocal,
  275. commentsEnabled: true,
  276. downloadEnabled: true,
  277. duration: 5,
  278. tags: [ 'tag1', 'tag2', 'tag3' ],
  279. privacy: VideoPrivacy.PUBLIC,
  280. likes: 1,
  281. dislikes: 1,
  282. channel: {
  283. displayName: 'Main root channel',
  284. name: 'root_channel',
  285. description: '',
  286. isLocal
  287. },
  288. fixture: 'video_short.webm',
  289. files: [
  290. {
  291. resolution: 720,
  292. size: 218910
  293. }
  294. ]
  295. }
  296. await completeVideoCheck(servers[ 0 ].url, video4, checkAttributes)
  297. })
  298. it('Should have propagated comments', async function () {
  299. const res1 = await getVideoCommentThreads(servers[0].url, video4.id, 0, 5)
  300. expect(res1.body.total).to.equal(1)
  301. expect(res1.body.data).to.be.an('array')
  302. expect(res1.body.data).to.have.lengthOf(1)
  303. const comment: VideoComment = res1.body.data[0]
  304. expect(comment.inReplyToCommentId).to.be.null
  305. expect(comment.text).equal('my super first comment')
  306. expect(comment.videoId).to.equal(video4.id)
  307. expect(comment.id).to.equal(comment.threadId)
  308. expect(comment.account.name).to.equal('root')
  309. expect(comment.account.host).to.equal('localhost:' + servers[2].port)
  310. expect(comment.totalReplies).to.equal(3)
  311. expect(dateIsValid(comment.createdAt as string)).to.be.true
  312. expect(dateIsValid(comment.updatedAt as string)).to.be.true
  313. const threadId = comment.threadId
  314. const res2 = await getVideoThreadComments(servers[0].url, video4.id, threadId)
  315. const tree: VideoCommentThreadTree = res2.body
  316. expect(tree.comment.text).equal('my super first comment')
  317. expect(tree.children).to.have.lengthOf(2)
  318. const firstChild = tree.children[0]
  319. expect(firstChild.comment.text).to.equal('my super answer to thread 1')
  320. expect(firstChild.children).to.have.lengthOf(1)
  321. const childOfFirstChild = firstChild.children[0]
  322. expect(childOfFirstChild.comment.text).to.equal('my super answer to answer of thread 1')
  323. expect(childOfFirstChild.children).to.have.lengthOf(0)
  324. const secondChild = tree.children[1]
  325. expect(secondChild.comment.text).to.equal('my second answer to thread 1')
  326. expect(secondChild.children).to.have.lengthOf(0)
  327. })
  328. it('Should have propagated captions', async function () {
  329. const res = await listVideoCaptions(servers[0].url, video4.id)
  330. expect(res.body.total).to.equal(1)
  331. expect(res.body.data).to.have.lengthOf(1)
  332. const caption1: VideoCaption = res.body.data[0]
  333. expect(caption1.language.id).to.equal('ar')
  334. expect(caption1.language.label).to.equal('Arabic')
  335. expect(caption1.captionPath).to.equal('/static/video-captions/' + video4.uuid + '-ar.vtt')
  336. await testCaptionFile(servers[0].url, caption1.captionPath, 'Subtitle good 2.')
  337. })
  338. it('Should unfollow server 3 on server 1 and does not list server 3 videos', async function () {
  339. this.timeout(5000)
  340. await unfollow(servers[0].url, servers[0].accessToken, servers[2])
  341. await waitJobs(servers)
  342. let res = await getVideosList(servers[ 0 ].url)
  343. expect(res.body.total).to.equal(1)
  344. })
  345. })
  346. after(async function () {
  347. await cleanupTests(servers)
  348. })
  349. })