user-subscriptions.ts 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  1. /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
  2. import * as chai from 'chai'
  3. import 'mocha'
  4. import {
  5. cleanupTests,
  6. createUser,
  7. doubleFollow,
  8. flushAndRunMultipleServers,
  9. follow,
  10. getVideosList,
  11. unfollow,
  12. updateVideo,
  13. userLogin
  14. } from '../../../../shared/extra-utils'
  15. import { ServerInfo, uploadVideo } from '../../../../shared/extra-utils/index'
  16. import { setAccessTokensToServers } from '../../../../shared/extra-utils/users/login'
  17. import { Video, VideoChannel } from '../../../../shared/models/videos'
  18. import { waitJobs } from '../../../../shared/extra-utils/server/jobs'
  19. import {
  20. addUserSubscription,
  21. areSubscriptionsExist,
  22. getUserSubscription,
  23. listUserSubscriptions,
  24. listUserSubscriptionVideos,
  25. removeUserSubscription
  26. } from '../../../../shared/extra-utils/users/user-subscriptions'
  27. const expect = chai.expect
  28. describe('Test users subscriptions', function () {
  29. let servers: ServerInfo[] = []
  30. const users: { accessToken: string }[] = []
  31. let video3UUID: string
  32. before(async function () {
  33. this.timeout(120000)
  34. servers = await flushAndRunMultipleServers(3)
  35. // Get the access tokens
  36. await setAccessTokensToServers(servers)
  37. // Server 1 and server 2 follow each other
  38. await doubleFollow(servers[0], servers[1])
  39. {
  40. for (const server of servers) {
  41. const user = { username: 'user' + server.serverNumber, password: 'password' }
  42. await createUser({ url: server.url, accessToken: server.accessToken, username: user.username, password: user.password })
  43. const accessToken = await userLogin(server, user)
  44. users.push({ accessToken })
  45. const videoName1 = 'video 1-' + server.serverNumber
  46. await uploadVideo(server.url, accessToken, { name: videoName1 })
  47. const videoName2 = 'video 2-' + server.serverNumber
  48. await uploadVideo(server.url, accessToken, { name: videoName2 })
  49. }
  50. }
  51. await waitJobs(servers)
  52. })
  53. it('Should display videos of server 2 on server 1', async function () {
  54. const res = await getVideosList(servers[0].url)
  55. expect(res.body.total).to.equal(4)
  56. })
  57. it('User of server 1 should follow user of server 3 and root of server 1', async function () {
  58. this.timeout(60000)
  59. await addUserSubscription(servers[0].url, users[0].accessToken, 'user3_channel@localhost:' + servers[2].port)
  60. await addUserSubscription(servers[0].url, users[0].accessToken, 'root_channel@localhost:' + servers[0].port)
  61. await waitJobs(servers)
  62. const res = await uploadVideo(servers[2].url, users[2].accessToken, { name: 'video server 3 added after follow' })
  63. video3UUID = res.body.video.uuid
  64. await waitJobs(servers)
  65. })
  66. it('Should not display videos of server 3 on server 1', async function () {
  67. const res = await getVideosList(servers[0].url)
  68. expect(res.body.total).to.equal(4)
  69. for (const video of res.body.data) {
  70. expect(video.name).to.not.contain('1-3')
  71. expect(video.name).to.not.contain('2-3')
  72. expect(video.name).to.not.contain('video server 3 added after follow')
  73. }
  74. })
  75. it('Should list subscriptions', async function () {
  76. {
  77. const res = await listUserSubscriptions({ url: servers[0].url, token: servers[0].accessToken })
  78. expect(res.body.total).to.equal(0)
  79. expect(res.body.data).to.be.an('array')
  80. expect(res.body.data).to.have.lengthOf(0)
  81. }
  82. {
  83. const res = await listUserSubscriptions({ url: servers[0].url, token: users[0].accessToken, sort: 'createdAt' })
  84. expect(res.body.total).to.equal(2)
  85. const subscriptions: VideoChannel[] = res.body.data
  86. expect(subscriptions).to.be.an('array')
  87. expect(subscriptions).to.have.lengthOf(2)
  88. expect(subscriptions[0].name).to.equal('user3_channel')
  89. expect(subscriptions[1].name).to.equal('root_channel')
  90. }
  91. })
  92. it('Should get subscription', async function () {
  93. {
  94. const res = await getUserSubscription(servers[0].url, users[0].accessToken, 'user3_channel@localhost:' + servers[2].port)
  95. const videoChannel: VideoChannel = res.body
  96. expect(videoChannel.name).to.equal('user3_channel')
  97. expect(videoChannel.host).to.equal('localhost:' + servers[2].port)
  98. expect(videoChannel.displayName).to.equal('Main user3 channel')
  99. expect(videoChannel.followingCount).to.equal(0)
  100. expect(videoChannel.followersCount).to.equal(1)
  101. }
  102. {
  103. const res = await getUserSubscription(servers[0].url, users[0].accessToken, 'root_channel@localhost:' + servers[0].port)
  104. const videoChannel: VideoChannel = res.body
  105. expect(videoChannel.name).to.equal('root_channel')
  106. expect(videoChannel.host).to.equal('localhost:' + servers[0].port)
  107. expect(videoChannel.displayName).to.equal('Main root channel')
  108. expect(videoChannel.followingCount).to.equal(0)
  109. expect(videoChannel.followersCount).to.equal(1)
  110. }
  111. })
  112. it('Should return the existing subscriptions', async function () {
  113. const uris = [
  114. 'user3_channel@localhost:' + servers[2].port,
  115. 'root2_channel@localhost:' + servers[0].port,
  116. 'root_channel@localhost:' + servers[0].port,
  117. 'user3_channel@localhost:' + servers[0].port
  118. ]
  119. const res = await areSubscriptionsExist(servers[0].url, users[0].accessToken, uris)
  120. const body = res.body
  121. expect(body['user3_channel@localhost:' + servers[2].port]).to.be.true
  122. expect(body['root2_channel@localhost:' + servers[0].port]).to.be.false
  123. expect(body['root_channel@localhost:' + servers[0].port]).to.be.true
  124. expect(body['user3_channel@localhost:' + servers[0].port]).to.be.false
  125. })
  126. it('Should search among subscriptions', async function () {
  127. {
  128. const res = await listUserSubscriptions({
  129. url: servers[0].url,
  130. token: users[0].accessToken,
  131. sort: '-createdAt',
  132. search: 'user3_channel'
  133. })
  134. expect(res.body.total).to.equal(1)
  135. const subscriptions = res.body.data
  136. expect(subscriptions).to.have.lengthOf(1)
  137. }
  138. {
  139. const res = await listUserSubscriptions({
  140. url: servers[0].url,
  141. token: users[0].accessToken,
  142. sort: '-createdAt',
  143. search: 'toto'
  144. })
  145. expect(res.body.total).to.equal(0)
  146. const subscriptions = res.body.data
  147. expect(subscriptions).to.have.lengthOf(0)
  148. }
  149. })
  150. it('Should list subscription videos', async function () {
  151. {
  152. const res = await listUserSubscriptionVideos(servers[0].url, servers[0].accessToken)
  153. expect(res.body.total).to.equal(0)
  154. expect(res.body.data).to.be.an('array')
  155. expect(res.body.data).to.have.lengthOf(0)
  156. }
  157. {
  158. const res = await listUserSubscriptionVideos(servers[0].url, users[0].accessToken, 'createdAt')
  159. expect(res.body.total).to.equal(3)
  160. const videos: Video[] = res.body.data
  161. expect(videos).to.be.an('array')
  162. expect(videos).to.have.lengthOf(3)
  163. expect(videos[0].name).to.equal('video 1-3')
  164. expect(videos[1].name).to.equal('video 2-3')
  165. expect(videos[2].name).to.equal('video server 3 added after follow')
  166. }
  167. })
  168. it('Should upload a video by root on server 1 and see it in the subscription videos', async function () {
  169. this.timeout(60000)
  170. const videoName = 'video server 1 added after follow'
  171. await uploadVideo(servers[0].url, servers[0].accessToken, { name: videoName })
  172. await waitJobs(servers)
  173. {
  174. const res = await listUserSubscriptionVideos(servers[0].url, servers[0].accessToken)
  175. expect(res.body.total).to.equal(0)
  176. expect(res.body.data).to.be.an('array')
  177. expect(res.body.data).to.have.lengthOf(0)
  178. }
  179. {
  180. const res = await listUserSubscriptionVideos(servers[0].url, users[0].accessToken, 'createdAt')
  181. expect(res.body.total).to.equal(4)
  182. const videos: Video[] = res.body.data
  183. expect(videos).to.be.an('array')
  184. expect(videos).to.have.lengthOf(4)
  185. expect(videos[0].name).to.equal('video 1-3')
  186. expect(videos[1].name).to.equal('video 2-3')
  187. expect(videos[2].name).to.equal('video server 3 added after follow')
  188. expect(videos[3].name).to.equal('video server 1 added after follow')
  189. }
  190. {
  191. const res = await getVideosList(servers[0].url)
  192. expect(res.body.total).to.equal(5)
  193. for (const video of res.body.data) {
  194. expect(video.name).to.not.contain('1-3')
  195. expect(video.name).to.not.contain('2-3')
  196. expect(video.name).to.not.contain('video server 3 added after follow')
  197. }
  198. }
  199. })
  200. it('Should have server 1 follow server 3 and display server 3 videos', async function () {
  201. this.timeout(60000)
  202. await follow(servers[0].url, [ servers[2].url ], servers[0].accessToken)
  203. await waitJobs(servers)
  204. const res = await getVideosList(servers[0].url)
  205. expect(res.body.total).to.equal(8)
  206. const names = [ '1-3', '2-3', 'video server 3 added after follow' ]
  207. for (const name of names) {
  208. const video = res.body.data.find(v => v.name.indexOf(name) === -1)
  209. expect(video).to.not.be.undefined
  210. }
  211. })
  212. it('Should remove follow server 1 -> server 3 and hide server 3 videos', async function () {
  213. this.timeout(60000)
  214. await unfollow(servers[0].url, servers[0].accessToken, servers[2])
  215. await waitJobs(servers)
  216. const res = await getVideosList(servers[0].url)
  217. expect(res.body.total).to.equal(5)
  218. for (const video of res.body.data) {
  219. expect(video.name).to.not.contain('1-3')
  220. expect(video.name).to.not.contain('2-3')
  221. expect(video.name).to.not.contain('video server 3 added after follow')
  222. }
  223. })
  224. it('Should still list subscription videos', async function () {
  225. {
  226. const res = await listUserSubscriptionVideos(servers[0].url, servers[0].accessToken)
  227. expect(res.body.total).to.equal(0)
  228. expect(res.body.data).to.be.an('array')
  229. expect(res.body.data).to.have.lengthOf(0)
  230. }
  231. {
  232. const res = await listUserSubscriptionVideos(servers[0].url, users[0].accessToken, 'createdAt')
  233. expect(res.body.total).to.equal(4)
  234. const videos: Video[] = res.body.data
  235. expect(videos).to.be.an('array')
  236. expect(videos).to.have.lengthOf(4)
  237. expect(videos[0].name).to.equal('video 1-3')
  238. expect(videos[1].name).to.equal('video 2-3')
  239. expect(videos[2].name).to.equal('video server 3 added after follow')
  240. expect(videos[3].name).to.equal('video server 1 added after follow')
  241. }
  242. })
  243. it('Should update a video of server 3 and see the updated video on server 1', async function () {
  244. this.timeout(30000)
  245. await updateVideo(servers[2].url, users[2].accessToken, video3UUID, { name: 'video server 3 added after follow updated' })
  246. await waitJobs(servers)
  247. const res = await listUserSubscriptionVideos(servers[0].url, users[0].accessToken, 'createdAt')
  248. const videos: Video[] = res.body.data
  249. expect(videos[2].name).to.equal('video server 3 added after follow updated')
  250. })
  251. it('Should remove user of server 3 subscription', async function () {
  252. this.timeout(30000)
  253. await removeUserSubscription(servers[0].url, users[0].accessToken, 'user3_channel@localhost:' + servers[2].port)
  254. await waitJobs(servers)
  255. })
  256. it('Should not display its videos anymore', async function () {
  257. {
  258. const res = await listUserSubscriptionVideos(servers[0].url, users[0].accessToken, 'createdAt')
  259. expect(res.body.total).to.equal(1)
  260. const videos: Video[] = res.body.data
  261. expect(videos).to.be.an('array')
  262. expect(videos).to.have.lengthOf(1)
  263. expect(videos[0].name).to.equal('video server 1 added after follow')
  264. }
  265. })
  266. it('Should remove the root subscription and not display the videos anymore', async function () {
  267. this.timeout(30000)
  268. await removeUserSubscription(servers[0].url, users[0].accessToken, 'root_channel@localhost:' + servers[0].port)
  269. await waitJobs(servers)
  270. {
  271. const res = await listUserSubscriptionVideos(servers[0].url, users[0].accessToken, 'createdAt')
  272. expect(res.body.total).to.equal(0)
  273. const videos: Video[] = res.body.data
  274. expect(videos).to.be.an('array')
  275. expect(videos).to.have.lengthOf(0)
  276. }
  277. })
  278. it('Should correctly display public videos on server 1', async function () {
  279. const res = await getVideosList(servers[0].url)
  280. expect(res.body.total).to.equal(5)
  281. for (const video of res.body.data) {
  282. expect(video.name).to.not.contain('1-3')
  283. expect(video.name).to.not.contain('2-3')
  284. expect(video.name).to.not.contain('video server 3 added after follow updated')
  285. }
  286. })
  287. it('Should follow user of server 3 again', async function () {
  288. this.timeout(60000)
  289. await addUserSubscription(servers[0].url, users[0].accessToken, 'user3_channel@localhost:' + servers[2].port)
  290. await waitJobs(servers)
  291. {
  292. const res = await listUserSubscriptionVideos(servers[0].url, users[0].accessToken, 'createdAt')
  293. expect(res.body.total).to.equal(3)
  294. const videos: Video[] = res.body.data
  295. expect(videos).to.be.an('array')
  296. expect(videos).to.have.lengthOf(3)
  297. expect(videos[0].name).to.equal('video 1-3')
  298. expect(videos[1].name).to.equal('video 2-3')
  299. expect(videos[2].name).to.equal('video server 3 added after follow updated')
  300. }
  301. {
  302. const res = await getVideosList(servers[0].url)
  303. expect(res.body.total).to.equal(5)
  304. for (const video of res.body.data) {
  305. expect(video.name).to.not.contain('1-3')
  306. expect(video.name).to.not.contain('2-3')
  307. expect(video.name).to.not.contain('video server 3 added after follow updated')
  308. }
  309. }
  310. })
  311. after(async function () {
  312. await cleanupTests(servers)
  313. })
  314. })