2020-01-31 16:56:52 +01:00
|
|
|
/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
|
2018-10-05 11:15:06 +02:00
|
|
|
|
2022-08-17 15:44:32 +02:00
|
|
|
import { expect } from 'chai'
|
2021-12-17 11:58:15 +01:00
|
|
|
import { wait } from '@shared/core-utils'
|
2022-03-24 13:36:47 +01:00
|
|
|
import { Video } from '@shared/models'
|
|
|
|
import { cleanupTests, createSingleServer, killallServers, PeerTubeServer, setAccessTokensToServers } from '@shared/server-commands'
|
2018-10-05 11:15:06 +02:00
|
|
|
|
|
|
|
describe('Test videos history', function () {
|
2021-07-16 09:47:51 +02:00
|
|
|
let server: PeerTubeServer = null
|
2022-01-18 11:23:41 +01:00
|
|
|
let video1Id: number
|
2018-10-05 11:15:06 +02:00
|
|
|
let video1UUID: string
|
|
|
|
let video2UUID: string
|
|
|
|
let video3UUID: string
|
2018-12-17 15:52:38 +01:00
|
|
|
let video3WatchedDate: Date
|
|
|
|
let userAccessToken: string
|
2018-10-05 11:15:06 +02:00
|
|
|
|
|
|
|
before(async function () {
|
2022-05-19 08:27:04 +02:00
|
|
|
this.timeout(120000)
|
2018-10-05 11:15:06 +02:00
|
|
|
|
2021-07-16 09:47:51 +02:00
|
|
|
server = await createSingleServer(1)
|
2018-10-05 11:15:06 +02:00
|
|
|
|
|
|
|
await setAccessTokensToServers([ server ])
|
|
|
|
|
2022-03-24 13:36:47 +01:00
|
|
|
// 10 seconds long
|
2022-04-05 14:58:59 +02:00
|
|
|
const fixture = 'video_short1.webm'
|
2021-07-08 16:21:42 +02:00
|
|
|
|
2018-10-05 11:15:06 +02:00
|
|
|
{
|
2022-03-24 13:36:47 +01:00
|
|
|
const { id, uuid } = await server.videos.upload({ attributes: { name: 'video 1', fixture } })
|
2021-07-15 10:02:54 +02:00
|
|
|
video1UUID = uuid
|
2022-01-18 11:23:41 +01:00
|
|
|
video1Id = id
|
2018-10-05 11:15:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2022-03-24 13:36:47 +01:00
|
|
|
const { uuid } = await server.videos.upload({ attributes: { name: 'video 2', fixture } })
|
2021-07-15 10:02:54 +02:00
|
|
|
video2UUID = uuid
|
2018-10-05 11:15:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2022-03-24 13:36:47 +01:00
|
|
|
const { uuid } = await server.videos.upload({ attributes: { name: 'video 3', fixture } })
|
2021-07-15 10:02:54 +02:00
|
|
|
video3UUID = uuid
|
2018-10-05 11:15:06 +02:00
|
|
|
}
|
2018-12-17 15:52:38 +01:00
|
|
|
|
2022-03-24 13:36:47 +01:00
|
|
|
userAccessToken = await server.users.generateUserAndToken('user_1')
|
2018-10-05 11:15:06 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
it('Should get videos, without watching history', async function () {
|
2021-07-16 09:04:35 +02:00
|
|
|
const { data } = await server.videos.listWithToken()
|
2018-10-05 11:15:06 +02:00
|
|
|
|
2021-07-15 10:02:54 +02:00
|
|
|
for (const video of data) {
|
2021-07-16 09:04:35 +02:00
|
|
|
const videoDetails = await server.videos.getWithToken({ id: video.id })
|
2018-10-05 11:15:06 +02:00
|
|
|
|
|
|
|
expect(video.userHistory).to.be.undefined
|
|
|
|
expect(videoDetails.userHistory).to.be.undefined
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
it('Should watch the first and second video', async function () {
|
2022-03-24 13:36:47 +01:00
|
|
|
await server.views.view({ id: video2UUID, token: server.accessToken, currentTime: 8 })
|
|
|
|
await server.views.view({ id: video1UUID, token: server.accessToken, currentTime: 3 })
|
2018-10-05 11:15:06 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
it('Should return the correct history when listing, searching and getting videos', async function () {
|
|
|
|
const videosOfVideos: Video[][] = []
|
|
|
|
|
|
|
|
{
|
2021-07-16 09:04:35 +02:00
|
|
|
const { data } = await server.videos.listWithToken()
|
2021-07-15 10:02:54 +02:00
|
|
|
videosOfVideos.push(data)
|
2018-10-05 11:15:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2021-07-16 09:04:35 +02:00
|
|
|
const body = await server.search.searchVideos({ token: server.accessToken, search: 'video' })
|
2021-07-06 15:22:51 +02:00
|
|
|
videosOfVideos.push(body.data)
|
2018-10-05 11:15:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for (const videos of videosOfVideos) {
|
|
|
|
const video1 = videos.find(v => v.uuid === video1UUID)
|
|
|
|
const video2 = videos.find(v => v.uuid === video2UUID)
|
|
|
|
const video3 = videos.find(v => v.uuid === video3UUID)
|
|
|
|
|
|
|
|
expect(video1.userHistory).to.not.be.undefined
|
|
|
|
expect(video1.userHistory.currentTime).to.equal(3)
|
|
|
|
|
|
|
|
expect(video2.userHistory).to.not.be.undefined
|
|
|
|
expect(video2.userHistory.currentTime).to.equal(8)
|
|
|
|
|
|
|
|
expect(video3.userHistory).to.be.undefined
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2021-07-16 09:04:35 +02:00
|
|
|
const videoDetails = await server.videos.getWithToken({ id: video1UUID })
|
2018-10-05 11:15:06 +02:00
|
|
|
|
|
|
|
expect(videoDetails.userHistory).to.not.be.undefined
|
|
|
|
expect(videoDetails.userHistory.currentTime).to.equal(3)
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2021-07-16 09:04:35 +02:00
|
|
|
const videoDetails = await server.videos.getWithToken({ id: video2UUID })
|
2018-10-05 11:15:06 +02:00
|
|
|
|
|
|
|
expect(videoDetails.userHistory).to.not.be.undefined
|
|
|
|
expect(videoDetails.userHistory.currentTime).to.equal(8)
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2021-07-16 09:04:35 +02:00
|
|
|
const videoDetails = await server.videos.getWithToken({ id: video3UUID })
|
2018-10-05 11:15:06 +02:00
|
|
|
|
|
|
|
expect(videoDetails.userHistory).to.be.undefined
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2018-12-17 15:52:38 +01:00
|
|
|
it('Should have these videos when listing my history', async function () {
|
|
|
|
video3WatchedDate = new Date()
|
2022-03-24 13:36:47 +01:00
|
|
|
await server.views.view({ id: video3UUID, token: server.accessToken, currentTime: 2 })
|
2018-12-17 15:52:38 +01:00
|
|
|
|
2022-03-24 13:36:47 +01:00
|
|
|
const body = await server.history.list()
|
2018-12-17 15:52:38 +01:00
|
|
|
|
2021-07-08 16:21:42 +02:00
|
|
|
expect(body.total).to.equal(3)
|
2018-12-17 15:52:38 +01:00
|
|
|
|
2021-07-08 16:21:42 +02:00
|
|
|
const videos = body.data
|
2018-12-17 15:52:38 +01:00
|
|
|
expect(videos[0].name).to.equal('video 3')
|
|
|
|
expect(videos[1].name).to.equal('video 1')
|
|
|
|
expect(videos[2].name).to.equal('video 2')
|
|
|
|
})
|
|
|
|
|
|
|
|
it('Should not have videos history on another user', async function () {
|
2022-03-24 13:36:47 +01:00
|
|
|
const body = await server.history.list({ token: userAccessToken })
|
2018-12-17 15:52:38 +01:00
|
|
|
|
2021-07-08 16:21:42 +02:00
|
|
|
expect(body.total).to.equal(0)
|
|
|
|
expect(body.data).to.have.lengthOf(0)
|
2018-12-17 15:52:38 +01:00
|
|
|
})
|
|
|
|
|
2021-01-13 09:16:15 +01:00
|
|
|
it('Should be able to search through videos in my history', async function () {
|
2022-03-24 13:36:47 +01:00
|
|
|
const body = await server.history.list({ search: '2' })
|
2021-07-08 16:21:42 +02:00
|
|
|
expect(body.total).to.equal(1)
|
2021-01-13 09:16:15 +01:00
|
|
|
|
2021-07-08 16:21:42 +02:00
|
|
|
const videos = body.data
|
2021-01-13 09:16:15 +01:00
|
|
|
expect(videos[0].name).to.equal('video 2')
|
|
|
|
})
|
|
|
|
|
2018-12-17 15:52:38 +01:00
|
|
|
it('Should clear my history', async function () {
|
2022-03-24 13:36:47 +01:00
|
|
|
await server.history.removeAll({ beforeDate: video3WatchedDate.toISOString() })
|
2018-12-17 15:52:38 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
it('Should have my history cleared', async function () {
|
2022-03-24 13:36:47 +01:00
|
|
|
const body = await server.history.list()
|
2021-07-08 16:21:42 +02:00
|
|
|
expect(body.total).to.equal(1)
|
2018-12-17 15:52:38 +01:00
|
|
|
|
2021-07-08 16:21:42 +02:00
|
|
|
const videos = body.data
|
2018-12-17 15:52:38 +01:00
|
|
|
expect(videos[0].name).to.equal('video 3')
|
|
|
|
})
|
|
|
|
|
|
|
|
it('Should disable videos history', async function () {
|
2021-07-16 09:04:35 +02:00
|
|
|
await server.users.updateMe({
|
2018-12-17 15:52:38 +01:00
|
|
|
videosHistoryEnabled: false
|
|
|
|
})
|
|
|
|
|
2022-03-24 13:36:47 +01:00
|
|
|
await server.views.view({ id: video2UUID, token: server.accessToken, currentTime: 8 })
|
|
|
|
|
|
|
|
const { data } = await server.history.list()
|
|
|
|
expect(data[0].name).to.not.equal('video 2')
|
2018-12-17 15:52:38 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
it('Should re-enable videos history', async function () {
|
2021-07-16 09:04:35 +02:00
|
|
|
await server.users.updateMe({
|
2018-12-17 15:52:38 +01:00
|
|
|
videosHistoryEnabled: true
|
|
|
|
})
|
|
|
|
|
2022-03-24 13:36:47 +01:00
|
|
|
await server.views.view({ id: video2UUID, token: server.accessToken, currentTime: 8 })
|
2018-12-17 15:52:38 +01:00
|
|
|
|
2022-03-24 13:36:47 +01:00
|
|
|
const { data } = await server.history.list()
|
|
|
|
expect(data[0].name).to.equal('video 2')
|
2018-12-17 15:52:38 +01:00
|
|
|
})
|
|
|
|
|
2019-04-11 15:38:53 +02:00
|
|
|
it('Should not clean old history', async function () {
|
|
|
|
this.timeout(50000)
|
|
|
|
|
2021-07-09 15:37:43 +02:00
|
|
|
await killallServers([ server ])
|
2019-04-11 15:38:53 +02:00
|
|
|
|
2021-07-16 09:47:51 +02:00
|
|
|
await server.run({ history: { videos: { max_age: '10 days' } } })
|
2019-04-11 15:38:53 +02:00
|
|
|
|
|
|
|
await wait(6000)
|
|
|
|
|
|
|
|
// Should still have history
|
|
|
|
|
2022-03-24 13:36:47 +01:00
|
|
|
const body = await server.history.list()
|
2021-07-08 16:21:42 +02:00
|
|
|
expect(body.total).to.equal(2)
|
2019-04-11 15:38:53 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
it('Should clean old history', async function () {
|
|
|
|
this.timeout(50000)
|
|
|
|
|
2021-07-09 15:37:43 +02:00
|
|
|
await killallServers([ server ])
|
2019-04-11 15:38:53 +02:00
|
|
|
|
2021-07-16 09:47:51 +02:00
|
|
|
await server.run({ history: { videos: { max_age: '5 seconds' } } })
|
2019-04-11 15:38:53 +02:00
|
|
|
|
|
|
|
await wait(6000)
|
|
|
|
|
2022-03-24 13:36:47 +01:00
|
|
|
const body = await server.history.list()
|
2021-07-08 16:21:42 +02:00
|
|
|
expect(body.total).to.equal(0)
|
2019-04-11 15:38:53 +02:00
|
|
|
})
|
|
|
|
|
2022-01-18 11:23:41 +01:00
|
|
|
it('Should delete a specific history element', async function () {
|
|
|
|
{
|
2022-03-24 13:36:47 +01:00
|
|
|
await server.views.view({ id: video1UUID, token: server.accessToken, currentTime: 4 })
|
|
|
|
await server.views.view({ id: video2UUID, token: server.accessToken, currentTime: 8 })
|
2022-01-18 11:23:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2022-03-24 13:36:47 +01:00
|
|
|
const body = await server.history.list()
|
2022-01-18 11:23:41 +01:00
|
|
|
expect(body.total).to.equal(2)
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2022-03-24 13:36:47 +01:00
|
|
|
await server.history.removeElement({ videoId: video1Id })
|
2022-01-18 11:23:41 +01:00
|
|
|
|
2022-03-24 13:36:47 +01:00
|
|
|
const body = await server.history.list()
|
2022-01-18 11:23:41 +01:00
|
|
|
expect(body.total).to.equal(1)
|
|
|
|
expect(body.data[0].uuid).to.equal(video2UUID)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2019-04-24 15:10:37 +02:00
|
|
|
after(async function () {
|
|
|
|
await cleanupTests([ server ])
|
2018-10-05 11:15:06 +02:00
|
|
|
})
|
|
|
|
})
|