videos-history.ts 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. /* tslint:disable:no-unused-expression */
  2. import * as chai from 'chai'
  3. import 'mocha'
  4. import {
  5. createUser,
  6. flushTests,
  7. getVideosListWithToken,
  8. getVideoWithToken,
  9. killallServers, reRunServer,
  10. runServer,
  11. searchVideoWithToken,
  12. ServerInfo,
  13. setAccessTokensToServers,
  14. updateMyUser,
  15. uploadVideo,
  16. userLogin,
  17. wait
  18. } from '../../../../shared/utils'
  19. import { Video, VideoDetails } from '../../../../shared/models/videos'
  20. import { listMyVideosHistory, removeMyVideosHistory, userWatchVideo } from '../../../../shared/utils/videos/video-history'
  21. const expect = chai.expect
  22. describe('Test videos history', function () {
  23. let server: ServerInfo = null
  24. let video1UUID: string
  25. let video2UUID: string
  26. let video3UUID: string
  27. let video3WatchedDate: Date
  28. let userAccessToken: string
  29. before(async function () {
  30. this.timeout(30000)
  31. await flushTests()
  32. server = await runServer(1)
  33. await setAccessTokensToServers([ server ])
  34. {
  35. const res = await uploadVideo(server.url, server.accessToken, { name: 'video 1' })
  36. video1UUID = res.body.video.uuid
  37. }
  38. {
  39. const res = await uploadVideo(server.url, server.accessToken, { name: 'video 2' })
  40. video2UUID = res.body.video.uuid
  41. }
  42. {
  43. const res = await uploadVideo(server.url, server.accessToken, { name: 'video 3' })
  44. video3UUID = res.body.video.uuid
  45. }
  46. const user = {
  47. username: 'user_1',
  48. password: 'super password'
  49. }
  50. await createUser(server.url, server.accessToken, user.username, user.password)
  51. userAccessToken = await userLogin(server, user)
  52. })
  53. it('Should get videos, without watching history', async function () {
  54. const res = await getVideosListWithToken(server.url, server.accessToken)
  55. const videos: Video[] = res.body.data
  56. for (const video of videos) {
  57. const resDetail = await getVideoWithToken(server.url, server.accessToken, video.id)
  58. const videoDetails: VideoDetails = resDetail.body
  59. expect(video.userHistory).to.be.undefined
  60. expect(videoDetails.userHistory).to.be.undefined
  61. }
  62. })
  63. it('Should watch the first and second video', async function () {
  64. await userWatchVideo(server.url, server.accessToken, video2UUID, 8)
  65. await userWatchVideo(server.url, server.accessToken, video1UUID, 3)
  66. })
  67. it('Should return the correct history when listing, searching and getting videos', async function () {
  68. const videosOfVideos: Video[][] = []
  69. {
  70. const res = await getVideosListWithToken(server.url, server.accessToken)
  71. videosOfVideos.push(res.body.data)
  72. }
  73. {
  74. const res = await searchVideoWithToken(server.url, 'video', server.accessToken)
  75. videosOfVideos.push(res.body.data)
  76. }
  77. for (const videos of videosOfVideos) {
  78. const video1 = videos.find(v => v.uuid === video1UUID)
  79. const video2 = videos.find(v => v.uuid === video2UUID)
  80. const video3 = videos.find(v => v.uuid === video3UUID)
  81. expect(video1.userHistory).to.not.be.undefined
  82. expect(video1.userHistory.currentTime).to.equal(3)
  83. expect(video2.userHistory).to.not.be.undefined
  84. expect(video2.userHistory.currentTime).to.equal(8)
  85. expect(video3.userHistory).to.be.undefined
  86. }
  87. {
  88. const resDetail = await getVideoWithToken(server.url, server.accessToken, video1UUID)
  89. const videoDetails: VideoDetails = resDetail.body
  90. expect(videoDetails.userHistory).to.not.be.undefined
  91. expect(videoDetails.userHistory.currentTime).to.equal(3)
  92. }
  93. {
  94. const resDetail = await getVideoWithToken(server.url, server.accessToken, video2UUID)
  95. const videoDetails: VideoDetails = resDetail.body
  96. expect(videoDetails.userHistory).to.not.be.undefined
  97. expect(videoDetails.userHistory.currentTime).to.equal(8)
  98. }
  99. {
  100. const resDetail = await getVideoWithToken(server.url, server.accessToken, video3UUID)
  101. const videoDetails: VideoDetails = resDetail.body
  102. expect(videoDetails.userHistory).to.be.undefined
  103. }
  104. })
  105. it('Should have these videos when listing my history', async function () {
  106. video3WatchedDate = new Date()
  107. await userWatchVideo(server.url, server.accessToken, video3UUID, 2)
  108. const res = await listMyVideosHistory(server.url, server.accessToken)
  109. expect(res.body.total).to.equal(3)
  110. const videos: Video[] = res.body.data
  111. expect(videos[0].name).to.equal('video 3')
  112. expect(videos[1].name).to.equal('video 1')
  113. expect(videos[2].name).to.equal('video 2')
  114. })
  115. it('Should not have videos history on another user', async function () {
  116. const res = await listMyVideosHistory(server.url, userAccessToken)
  117. expect(res.body.total).to.equal(0)
  118. expect(res.body.data).to.have.lengthOf(0)
  119. })
  120. it('Should clear my history', async function () {
  121. await removeMyVideosHistory(server.url, server.accessToken, video3WatchedDate.toISOString())
  122. })
  123. it('Should have my history cleared', async function () {
  124. const res = await listMyVideosHistory(server.url, server.accessToken)
  125. expect(res.body.total).to.equal(1)
  126. const videos: Video[] = res.body.data
  127. expect(videos[0].name).to.equal('video 3')
  128. })
  129. it('Should disable videos history', async function () {
  130. await updateMyUser({
  131. url: server.url,
  132. accessToken: server.accessToken,
  133. videosHistoryEnabled: false
  134. })
  135. await userWatchVideo(server.url, server.accessToken, video2UUID, 8, 409)
  136. })
  137. it('Should re-enable videos history', async function () {
  138. await updateMyUser({
  139. url: server.url,
  140. accessToken: server.accessToken,
  141. videosHistoryEnabled: true
  142. })
  143. await userWatchVideo(server.url, server.accessToken, video1UUID, 8)
  144. const res = await listMyVideosHistory(server.url, server.accessToken)
  145. expect(res.body.total).to.equal(2)
  146. const videos: Video[] = res.body.data
  147. expect(videos[0].name).to.equal('video 1')
  148. expect(videos[1].name).to.equal('video 3')
  149. })
  150. it('Should not clean old history', async function () {
  151. this.timeout(50000)
  152. killallServers([ server ])
  153. await reRunServer(server, { history: { videos: { max_age: '10 days' } } })
  154. await wait(6000)
  155. // Should still have history
  156. const res = await listMyVideosHistory(server.url, server.accessToken)
  157. expect(res.body.total).to.equal(2)
  158. })
  159. it('Should clean old history', async function () {
  160. this.timeout(50000)
  161. killallServers([ server ])
  162. await reRunServer(server, { history: { videos: { max_age: '5 seconds' } } })
  163. await wait(6000)
  164. const res = await listMyVideosHistory(server.url, server.accessToken)
  165. expect(res.body.total).to.equal(0)
  166. })
  167. after(async function () {
  168. killallServers([ server ])
  169. // Keep the logs if the test failed
  170. if (this['ok']) {
  171. await flushTests()
  172. }
  173. })
  174. })