follows.ts 16 KB

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