]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/tests/api/videos/videos-history.ts
Display avatar in embed poster
[github/Chocobozzz/PeerTube.git] / server / tests / api / videos / videos-history.ts
index 7314471354424840fe0c90d8f2fea6fda89dbfaa..8648c97f092a8158ad9cf4449024f795a421e200 100644 (file)
@@ -2,29 +2,22 @@
 
 import 'mocha'
 import * as chai from 'chai'
-import { HttpStatusCode } from '@shared/core-utils'
+import { wait } from '@shared/core-utils'
+import { HttpStatusCode, Video } from '@shared/models'
 import {
   cleanupTests,
-  createUser,
-  flushAndRunServer,
-  getVideosListWithToken,
-  getVideoWithToken,
+  createSingleServer,
   HistoryCommand,
   killallServers,
-  reRunServer,
-  ServerInfo,
-  setAccessTokensToServers,
-  updateMyUser,
-  uploadVideo,
-  userLogin,
-  wait
-} from '@shared/extra-utils'
-import { Video, VideoDetails } from '@shared/models'
+  PeerTubeServer,
+  setAccessTokensToServers
+} from '@shared/server-commands'
 
 const expect = chai.expect
 
 describe('Test videos history', function () {
-  let server: ServerInfo = null
+  let server: PeerTubeServer = null
+  let video1Id: number
   let video1UUID: string
   let video2UUID: string
   let video3UUID: string
@@ -35,42 +28,41 @@ describe('Test videos history', function () {
   before(async function () {
     this.timeout(30000)
 
-    server = await flushAndRunServer(1)
+    server = await createSingleServer(1)
 
     await setAccessTokensToServers([ server ])
 
-    command = server.historyCommand
+    command = server.history
 
     {
-      const res = await uploadVideo(server.url, server.accessToken, { name: 'video 1' })
-      video1UUID = res.body.video.uuid
+      const { id, uuid } = await server.videos.upload({ attributes: { name: 'video 1' } })
+      video1UUID = uuid
+      video1Id = id
     }
 
     {
-      const res = await uploadVideo(server.url, server.accessToken, { name: 'video 2' })
-      video2UUID = res.body.video.uuid
+      const { uuid } = await server.videos.upload({ attributes: { name: 'video 2' } })
+      video2UUID = uuid
     }
 
     {
-      const res = await uploadVideo(server.url, server.accessToken, { name: 'video 3' })
-      video3UUID = res.body.video.uuid
+      const { uuid } = await server.videos.upload({ attributes: { name: 'video 3' } })
+      video3UUID = uuid
     }
 
     const user = {
       username: 'user_1',
       password: 'super password'
     }
-    await createUser({ url: server.url, accessToken: server.accessToken, username: user.username, password: user.password })
-    userAccessToken = await userLogin(server, user)
+    await server.users.create({ username: user.username, password: user.password })
+    userAccessToken = await server.login.getAccessToken(user)
   })
 
   it('Should get videos, without watching history', async function () {
-    const res = await getVideosListWithToken(server.url, server.accessToken)
-    const videos: Video[] = res.body.data
+    const { data } = await server.videos.listWithToken()
 
-    for (const video of videos) {
-      const resDetail = await getVideoWithToken(server.url, server.accessToken, video.id)
-      const videoDetails: VideoDetails = resDetail.body
+    for (const video of data) {
+      const videoDetails = await server.videos.getWithToken({ id: video.id })
 
       expect(video.userHistory).to.be.undefined
       expect(videoDetails.userHistory).to.be.undefined
@@ -78,20 +70,20 @@ describe('Test videos history', function () {
   })
 
   it('Should watch the first and second video', async function () {
-    await command.wathVideo({ videoId: video2UUID, currentTime: 8 })
-    await command.wathVideo({ videoId: video1UUID, currentTime: 3 })
+    await command.watchVideo({ videoId: video2UUID, currentTime: 8 })
+    await command.watchVideo({ videoId: video1UUID, currentTime: 3 })
   })
 
   it('Should return the correct history when listing, searching and getting videos', async function () {
     const videosOfVideos: Video[][] = []
 
     {
-      const res = await getVideosListWithToken(server.url, server.accessToken)
-      videosOfVideos.push(res.body.data)
+      const { data } = await server.videos.listWithToken()
+      videosOfVideos.push(data)
     }
 
     {
-      const body = await server.searchCommand.searchVideos({ token: server.accessToken, search: 'video' })
+      const body = await server.search.searchVideos({ token: server.accessToken, search: 'video' })
       videosOfVideos.push(body.data)
     }
 
@@ -110,24 +102,21 @@ describe('Test videos history', function () {
     }
 
     {
-      const resDetail = await getVideoWithToken(server.url, server.accessToken, video1UUID)
-      const videoDetails: VideoDetails = resDetail.body
+      const videoDetails = await server.videos.getWithToken({ id: video1UUID })
 
       expect(videoDetails.userHistory).to.not.be.undefined
       expect(videoDetails.userHistory.currentTime).to.equal(3)
     }
 
     {
-      const resDetail = await getVideoWithToken(server.url, server.accessToken, video2UUID)
-      const videoDetails: VideoDetails = resDetail.body
+      const videoDetails = await server.videos.getWithToken({ id: video2UUID })
 
       expect(videoDetails.userHistory).to.not.be.undefined
       expect(videoDetails.userHistory.currentTime).to.equal(8)
     }
 
     {
-      const resDetail = await getVideoWithToken(server.url, server.accessToken, video3UUID)
-      const videoDetails: VideoDetails = resDetail.body
+      const videoDetails = await server.videos.getWithToken({ id: video3UUID })
 
       expect(videoDetails.userHistory).to.be.undefined
     }
@@ -135,7 +124,7 @@ describe('Test videos history', function () {
 
   it('Should have these videos when listing my history', async function () {
     video3WatchedDate = new Date()
-    await command.wathVideo({ videoId: video3UUID, currentTime: 2 })
+    await command.watchVideo({ videoId: video3UUID, currentTime: 2 })
 
     const body = await command.list()
 
@@ -163,7 +152,7 @@ describe('Test videos history', function () {
   })
 
   it('Should clear my history', async function () {
-    await command.remove({ beforeDate: video3WatchedDate.toISOString() })
+    await command.removeAll({ beforeDate: video3WatchedDate.toISOString() })
   })
 
   it('Should have my history cleared', async function () {
@@ -175,23 +164,19 @@ describe('Test videos history', function () {
   })
 
   it('Should disable videos history', async function () {
-    await updateMyUser({
-      url: server.url,
-      accessToken: server.accessToken,
+    await server.users.updateMe({
       videosHistoryEnabled: false
     })
 
-    await command.wathVideo({ videoId: video2UUID, currentTime: 8, expectedStatus: HttpStatusCode.CONFLICT_409 })
+    await command.watchVideo({ videoId: video2UUID, currentTime: 8, expectedStatus: HttpStatusCode.CONFLICT_409 })
   })
 
   it('Should re-enable videos history', async function () {
-    await updateMyUser({
-      url: server.url,
-      accessToken: server.accessToken,
+    await server.users.updateMe({
       videosHistoryEnabled: true
     })
 
-    await command.wathVideo({ videoId: video1UUID, currentTime: 8 })
+    await command.watchVideo({ videoId: video1UUID, currentTime: 8 })
 
     const body = await command.list()
     expect(body.total).to.equal(2)
@@ -204,9 +189,9 @@ describe('Test videos history', function () {
   it('Should not clean old history', async function () {
     this.timeout(50000)
 
-    killallServers([ server ])
+    await killallServers([ server ])
 
-    await reRunServer(server, { history: { videos: { max_age: '10 days' } } })
+    await server.run({ history: { videos: { max_age: '10 days' } } })
 
     await wait(6000)
 
@@ -219,9 +204,9 @@ describe('Test videos history', function () {
   it('Should clean old history', async function () {
     this.timeout(50000)
 
-    killallServers([ server ])
+    await killallServers([ server ])
 
-    await reRunServer(server, { history: { videos: { max_age: '5 seconds' } } })
+    await server.run({ history: { videos: { max_age: '5 seconds' } } })
 
     await wait(6000)
 
@@ -229,6 +214,26 @@ describe('Test videos history', function () {
     expect(body.total).to.equal(0)
   })
 
+  it('Should delete a specific history element', async function () {
+    {
+      await command.watchVideo({ videoId: video1UUID, currentTime: 4 })
+      await command.watchVideo({ videoId: video2UUID, currentTime: 8 })
+    }
+
+    {
+      const body = await command.list()
+      expect(body.total).to.equal(2)
+    }
+
+    {
+      await command.removeElement({ videoId: video1Id })
+
+      const body = await command.list()
+      expect(body.total).to.equal(1)
+      expect(body.data[0].uuid).to.equal(video2UUID)
+    }
+  })
+
   after(async function () {
     await cleanupTests([ server ])
   })