video-channels.ts 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551
  1. /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
  2. import { expect } from 'chai'
  3. import { basename } from 'path'
  4. import { ACTOR_IMAGES_SIZE } from '@server/initializers/constants'
  5. import { testFileExistsOrNot, testImage } from '@server/tests/shared'
  6. import { wait } from '@shared/core-utils'
  7. import { ActorImageType, User, VideoChannel } from '@shared/models'
  8. import {
  9. cleanupTests,
  10. createMultipleServers,
  11. doubleFollow,
  12. PeerTubeServer,
  13. setAccessTokensToServers,
  14. setDefaultAccountAvatar,
  15. setDefaultVideoChannel,
  16. waitJobs
  17. } from '@shared/server-commands'
  18. async function findChannel (server: PeerTubeServer, channelId: number) {
  19. const body = await server.channels.list({ sort: '-name' })
  20. return body.data.find(c => c.id === channelId)
  21. }
  22. describe('Test video channels', function () {
  23. let servers: PeerTubeServer[]
  24. let userInfo: User
  25. let secondVideoChannelId: number
  26. let totoChannel: number
  27. let videoUUID: string
  28. let accountName: string
  29. let secondUserChannelName: string
  30. const avatarPaths: { [ port: number ]: string } = {}
  31. const bannerPaths: { [ port: number ]: string } = {}
  32. before(async function () {
  33. this.timeout(60000)
  34. servers = await createMultipleServers(2)
  35. await setAccessTokensToServers(servers)
  36. await setDefaultVideoChannel(servers)
  37. await setDefaultAccountAvatar(servers)
  38. await doubleFollow(servers[0], servers[1])
  39. })
  40. it('Should have one video channel (created with root)', async () => {
  41. const body = await servers[0].channels.list({ start: 0, count: 2 })
  42. expect(body.total).to.equal(1)
  43. expect(body.data).to.be.an('array')
  44. expect(body.data).to.have.lengthOf(1)
  45. })
  46. it('Should create another video channel', async function () {
  47. this.timeout(30000)
  48. {
  49. const videoChannel = {
  50. name: 'second_video_channel',
  51. displayName: 'second video channel',
  52. description: 'super video channel description',
  53. support: 'super video channel support text'
  54. }
  55. const created = await servers[0].channels.create({ attributes: videoChannel })
  56. secondVideoChannelId = created.id
  57. }
  58. // The channel is 1 is propagated to servers 2
  59. {
  60. const attributes = { name: 'my video name', channelId: secondVideoChannelId, support: 'video support field' }
  61. const { uuid } = await servers[0].videos.upload({ attributes })
  62. videoUUID = uuid
  63. }
  64. await waitJobs(servers)
  65. })
  66. it('Should have two video channels when getting my information', async () => {
  67. userInfo = await servers[0].users.getMyInfo()
  68. expect(userInfo.videoChannels).to.be.an('array')
  69. expect(userInfo.videoChannels).to.have.lengthOf(2)
  70. const videoChannels = userInfo.videoChannels
  71. expect(videoChannels[0].name).to.equal('root_channel')
  72. expect(videoChannels[0].displayName).to.equal('Main root channel')
  73. expect(videoChannels[1].name).to.equal('second_video_channel')
  74. expect(videoChannels[1].displayName).to.equal('second video channel')
  75. expect(videoChannels[1].description).to.equal('super video channel description')
  76. expect(videoChannels[1].support).to.equal('super video channel support text')
  77. accountName = userInfo.account.name + '@' + userInfo.account.host
  78. })
  79. it('Should have two video channels when getting account channels on server 1', async function () {
  80. const body = await servers[0].channels.listByAccount({ accountName })
  81. expect(body.total).to.equal(2)
  82. const videoChannels = body.data
  83. expect(videoChannels).to.be.an('array')
  84. expect(videoChannels).to.have.lengthOf(2)
  85. expect(videoChannels[0].name).to.equal('root_channel')
  86. expect(videoChannels[0].displayName).to.equal('Main root channel')
  87. expect(videoChannels[1].name).to.equal('second_video_channel')
  88. expect(videoChannels[1].displayName).to.equal('second video channel')
  89. expect(videoChannels[1].description).to.equal('super video channel description')
  90. expect(videoChannels[1].support).to.equal('super video channel support text')
  91. })
  92. it('Should paginate and sort account channels', async function () {
  93. {
  94. const body = await servers[0].channels.listByAccount({
  95. accountName,
  96. start: 0,
  97. count: 1,
  98. sort: 'createdAt'
  99. })
  100. expect(body.total).to.equal(2)
  101. expect(body.data).to.have.lengthOf(1)
  102. const videoChannel: VideoChannel = body.data[0]
  103. expect(videoChannel.name).to.equal('root_channel')
  104. }
  105. {
  106. const body = await servers[0].channels.listByAccount({
  107. accountName,
  108. start: 0,
  109. count: 1,
  110. sort: '-createdAt'
  111. })
  112. expect(body.total).to.equal(2)
  113. expect(body.data).to.have.lengthOf(1)
  114. expect(body.data[0].name).to.equal('second_video_channel')
  115. }
  116. {
  117. const body = await servers[0].channels.listByAccount({
  118. accountName,
  119. start: 1,
  120. count: 1,
  121. sort: '-createdAt'
  122. })
  123. expect(body.total).to.equal(2)
  124. expect(body.data).to.have.lengthOf(1)
  125. expect(body.data[0].name).to.equal('root_channel')
  126. }
  127. })
  128. it('Should have one video channel when getting account channels on server 2', async function () {
  129. const body = await servers[1].channels.listByAccount({ accountName })
  130. expect(body.total).to.equal(1)
  131. expect(body.data).to.be.an('array')
  132. expect(body.data).to.have.lengthOf(1)
  133. const videoChannel = body.data[0]
  134. expect(videoChannel.name).to.equal('second_video_channel')
  135. expect(videoChannel.displayName).to.equal('second video channel')
  136. expect(videoChannel.description).to.equal('super video channel description')
  137. expect(videoChannel.support).to.equal('super video channel support text')
  138. })
  139. it('Should list video channels', async function () {
  140. const body = await servers[0].channels.list({ start: 1, count: 1, sort: '-name' })
  141. expect(body.total).to.equal(2)
  142. expect(body.data).to.be.an('array')
  143. expect(body.data).to.have.lengthOf(1)
  144. expect(body.data[0].name).to.equal('root_channel')
  145. expect(body.data[0].displayName).to.equal('Main root channel')
  146. })
  147. it('Should update video channel', async function () {
  148. this.timeout(15000)
  149. const videoChannelAttributes = {
  150. displayName: 'video channel updated',
  151. description: 'video channel description updated',
  152. support: 'support updated'
  153. }
  154. await servers[0].channels.update({ channelName: 'second_video_channel', attributes: videoChannelAttributes })
  155. await waitJobs(servers)
  156. })
  157. it('Should have video channel updated', async function () {
  158. for (const server of servers) {
  159. const body = await server.channels.list({ start: 0, count: 1, sort: '-name' })
  160. expect(body.total).to.equal(2)
  161. expect(body.data).to.be.an('array')
  162. expect(body.data).to.have.lengthOf(1)
  163. expect(body.data[0].name).to.equal('second_video_channel')
  164. expect(body.data[0].displayName).to.equal('video channel updated')
  165. expect(body.data[0].description).to.equal('video channel description updated')
  166. expect(body.data[0].support).to.equal('support updated')
  167. }
  168. })
  169. it('Should not have updated the video support field', async function () {
  170. for (const server of servers) {
  171. const video = await server.videos.get({ id: videoUUID })
  172. expect(video.support).to.equal('video support field')
  173. }
  174. })
  175. it('Should update another accounts video channel', async function () {
  176. this.timeout(15000)
  177. const result = await servers[0].users.generate('second_user')
  178. secondUserChannelName = result.userChannelName
  179. await servers[0].videos.quickUpload({ name: 'video', token: result.token })
  180. const videoChannelAttributes = {
  181. displayName: 'video channel updated',
  182. description: 'video channel description updated',
  183. support: 'support updated'
  184. }
  185. await servers[0].channels.update({ channelName: secondUserChannelName, attributes: videoChannelAttributes })
  186. await waitJobs(servers)
  187. })
  188. it('Should have another accounts video channel updated', async function () {
  189. for (const server of servers) {
  190. const body = await server.channels.get({ channelName: `${secondUserChannelName}@${servers[0].host}` })
  191. expect(body.displayName).to.equal('video channel updated')
  192. expect(body.description).to.equal('video channel description updated')
  193. expect(body.support).to.equal('support updated')
  194. }
  195. })
  196. it('Should update the channel support field and update videos too', async function () {
  197. this.timeout(35000)
  198. const videoChannelAttributes = {
  199. support: 'video channel support text updated',
  200. bulkVideosSupportUpdate: true
  201. }
  202. await servers[0].channels.update({ channelName: 'second_video_channel', attributes: videoChannelAttributes })
  203. await waitJobs(servers)
  204. for (const server of servers) {
  205. const video = await server.videos.get({ id: videoUUID })
  206. expect(video.support).to.equal(videoChannelAttributes.support)
  207. }
  208. })
  209. it('Should update video channel avatar', async function () {
  210. this.timeout(15000)
  211. const fixture = 'avatar.png'
  212. await servers[0].channels.updateImage({
  213. channelName: 'second_video_channel',
  214. fixture,
  215. type: 'avatar'
  216. })
  217. await waitJobs(servers)
  218. for (const server of servers) {
  219. const videoChannel = await findChannel(server, secondVideoChannelId)
  220. const expectedSizes = ACTOR_IMAGES_SIZE[ActorImageType.AVATAR]
  221. expect(videoChannel.avatars.length).to.equal(expectedSizes.length, 'Expected avatars to be generated in all sizes')
  222. for (const avatar of videoChannel.avatars) {
  223. avatarPaths[server.port] = avatar.path
  224. await testImage(server.url, `avatar-resized-${avatar.width}x${avatar.width}`, avatarPaths[server.port], '.png')
  225. await testFileExistsOrNot(server, 'avatars', basename(avatarPaths[server.port]), true)
  226. const row = await server.sql.getActorImage(basename(avatarPaths[server.port]))
  227. expect(expectedSizes.some(({ height, width }) => row.height === height && row.width === width)).to.equal(true)
  228. }
  229. }
  230. })
  231. it('Should update video channel banner', async function () {
  232. this.timeout(15000)
  233. const fixture = 'banner.jpg'
  234. await servers[0].channels.updateImage({
  235. channelName: 'second_video_channel',
  236. fixture,
  237. type: 'banner'
  238. })
  239. await waitJobs(servers)
  240. for (const server of servers) {
  241. const videoChannel = await server.channels.get({ channelName: 'second_video_channel@' + servers[0].host })
  242. bannerPaths[server.port] = videoChannel.banners[0].path
  243. await testImage(server.url, 'banner-resized', bannerPaths[server.port])
  244. await testFileExistsOrNot(server, 'avatars', basename(bannerPaths[server.port]), true)
  245. const row = await server.sql.getActorImage(basename(bannerPaths[server.port]))
  246. expect(row.height).to.equal(ACTOR_IMAGES_SIZE[ActorImageType.BANNER][0].height)
  247. expect(row.width).to.equal(ACTOR_IMAGES_SIZE[ActorImageType.BANNER][0].width)
  248. }
  249. })
  250. it('Should still correctly list channels', async function () {
  251. {
  252. const body = await servers[0].channels.list({ start: 1, count: 1, sort: 'createdAt' })
  253. expect(body.total).to.equal(3)
  254. expect(body.data).to.have.lengthOf(1)
  255. expect(body.data[0].name).to.equal('second_video_channel')
  256. }
  257. {
  258. const body = await servers[0].channels.listByAccount({ accountName, start: 1, count: 1, sort: 'createdAt' })
  259. expect(body.total).to.equal(2)
  260. expect(body.data).to.have.lengthOf(1)
  261. expect(body.data[0].name).to.equal('second_video_channel')
  262. }
  263. })
  264. it('Should delete the video channel avatar', async function () {
  265. this.timeout(15000)
  266. await servers[0].channels.deleteImage({ channelName: 'second_video_channel', type: 'avatar' })
  267. await waitJobs(servers)
  268. for (const server of servers) {
  269. const videoChannel = await findChannel(server, secondVideoChannelId)
  270. await testFileExistsOrNot(server, 'avatars', basename(avatarPaths[server.port]), false)
  271. expect(videoChannel.avatars).to.be.empty
  272. }
  273. })
  274. it('Should delete the video channel banner', async function () {
  275. this.timeout(15000)
  276. await servers[0].channels.deleteImage({ channelName: 'second_video_channel', type: 'banner' })
  277. await waitJobs(servers)
  278. for (const server of servers) {
  279. const videoChannel = await findChannel(server, secondVideoChannelId)
  280. await testFileExistsOrNot(server, 'avatars', basename(bannerPaths[server.port]), false)
  281. expect(videoChannel.banners).to.be.empty
  282. }
  283. })
  284. it('Should list the second video channel videos', async function () {
  285. this.timeout(10000)
  286. for (const server of servers) {
  287. const channelURI = 'second_video_channel@localhost:' + servers[0].port
  288. const { total, data } = await server.videos.listByChannel({ handle: channelURI })
  289. expect(total).to.equal(1)
  290. expect(data).to.be.an('array')
  291. expect(data).to.have.lengthOf(1)
  292. expect(data[0].name).to.equal('my video name')
  293. }
  294. })
  295. it('Should change the video channel of a video', async function () {
  296. this.timeout(10000)
  297. await servers[0].videos.update({ id: videoUUID, attributes: { channelId: servers[0].store.channel.id } })
  298. await waitJobs(servers)
  299. })
  300. it('Should list the first video channel videos', async function () {
  301. this.timeout(10000)
  302. for (const server of servers) {
  303. {
  304. const secondChannelURI = 'second_video_channel@localhost:' + servers[0].port
  305. const { total } = await server.videos.listByChannel({ handle: secondChannelURI })
  306. expect(total).to.equal(0)
  307. }
  308. {
  309. const channelURI = 'root_channel@localhost:' + servers[0].port
  310. const { total, data } = await server.videos.listByChannel({ handle: channelURI })
  311. expect(total).to.equal(1)
  312. expect(data).to.be.an('array')
  313. expect(data).to.have.lengthOf(1)
  314. expect(data[0].name).to.equal('my video name')
  315. }
  316. }
  317. })
  318. it('Should delete video channel', async function () {
  319. await servers[0].channels.delete({ channelName: 'second_video_channel' })
  320. })
  321. it('Should have video channel deleted', async function () {
  322. const body = await servers[0].channels.list({ start: 0, count: 10, sort: 'createdAt' })
  323. expect(body.total).to.equal(2)
  324. expect(body.data).to.be.an('array')
  325. expect(body.data).to.have.lengthOf(2)
  326. expect(body.data[0].displayName).to.equal('Main root channel')
  327. expect(body.data[1].displayName).to.equal('video channel updated')
  328. })
  329. it('Should create the main channel with a suffix if there is a conflict', async function () {
  330. {
  331. const videoChannel = { name: 'toto_channel', displayName: 'My toto channel' }
  332. const created = await servers[0].channels.create({ attributes: videoChannel })
  333. totoChannel = created.id
  334. }
  335. {
  336. await servers[0].users.create({ username: 'toto', password: 'password' })
  337. const accessToken = await servers[0].login.getAccessToken({ username: 'toto', password: 'password' })
  338. const { videoChannels } = await servers[0].users.getMyInfo({ token: accessToken })
  339. expect(videoChannels[0].name).to.equal('toto_channel-1')
  340. }
  341. })
  342. it('Should report correct channel views per days', async function () {
  343. this.timeout(10000)
  344. {
  345. const { data } = await servers[0].channels.listByAccount({ accountName, withStats: true })
  346. for (const channel of data) {
  347. expect(channel).to.haveOwnProperty('viewsPerDay')
  348. expect(channel.viewsPerDay).to.have.length(30 + 1) // daysPrior + today
  349. for (const v of channel.viewsPerDay) {
  350. expect(v.date).to.be.an('string')
  351. expect(v.views).to.equal(0)
  352. }
  353. }
  354. }
  355. {
  356. // video has been posted on channel servers[0].store.videoChannel.id since last update
  357. await servers[0].views.simulateView({ id: videoUUID, xForwardedFor: '0.0.0.1,127.0.0.1' })
  358. await servers[0].views.simulateView({ id: videoUUID, xForwardedFor: '0.0.0.2,127.0.0.1' })
  359. // Wait the repeatable job
  360. await wait(8000)
  361. const { data } = await servers[0].channels.listByAccount({ accountName, withStats: true })
  362. const channelWithView = data.find(channel => channel.id === servers[0].store.channel.id)
  363. expect(channelWithView.viewsPerDay.slice(-1)[0].views).to.equal(2)
  364. }
  365. })
  366. it('Should report correct total views count', async function () {
  367. // check if there's the property
  368. {
  369. const { data } = await servers[0].channels.listByAccount({ accountName, withStats: true })
  370. for (const channel of data) {
  371. expect(channel).to.haveOwnProperty('totalViews')
  372. expect(channel.totalViews).to.be.a('number')
  373. }
  374. }
  375. // Check if the totalViews count can be updated
  376. {
  377. const { data } = await servers[0].channels.listByAccount({ accountName, withStats: true })
  378. const channelWithView = data.find(channel => channel.id === servers[0].store.channel.id)
  379. expect(channelWithView.totalViews).to.equal(2)
  380. }
  381. })
  382. it('Should report correct videos count', async function () {
  383. const { data } = await servers[0].channels.listByAccount({ accountName, withStats: true })
  384. const totoChannel = data.find(c => c.name === 'toto_channel')
  385. const rootChannel = data.find(c => c.name === 'root_channel')
  386. expect(rootChannel.videosCount).to.equal(1)
  387. expect(totoChannel.videosCount).to.equal(0)
  388. })
  389. it('Should search among account video channels', async function () {
  390. {
  391. const body = await servers[0].channels.listByAccount({ accountName, search: 'root' })
  392. expect(body.total).to.equal(1)
  393. const channels = body.data
  394. expect(channels).to.have.lengthOf(1)
  395. }
  396. {
  397. const body = await servers[0].channels.listByAccount({ accountName, search: 'does not exist' })
  398. expect(body.total).to.equal(0)
  399. const channels = body.data
  400. expect(channels).to.have.lengthOf(0)
  401. }
  402. })
  403. it('Should list channels by updatedAt desc if a video has been uploaded', async function () {
  404. this.timeout(30000)
  405. await servers[0].videos.upload({ attributes: { channelId: totoChannel } })
  406. await waitJobs(servers)
  407. for (const server of servers) {
  408. const { data } = await server.channels.listByAccount({ accountName, sort: '-updatedAt' })
  409. expect(data[0].name).to.equal('toto_channel')
  410. expect(data[1].name).to.equal('root_channel')
  411. }
  412. await servers[0].videos.upload({ attributes: { channelId: servers[0].store.channel.id } })
  413. await waitJobs(servers)
  414. for (const server of servers) {
  415. const { data } = await server.channels.listByAccount({ accountName, sort: '-updatedAt' })
  416. expect(data[0].name).to.equal('root_channel')
  417. expect(data[1].name).to.equal('toto_channel')
  418. }
  419. })
  420. after(async function () {
  421. await cleanupTests(servers)
  422. })
  423. })