1
0
Fork 0
peertube/server/tests/api/videos/videos-history.ts

241 lines
6.5 KiB
TypeScript
Raw Normal View History

2020-01-31 10:56:52 -05:00
/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
2018-10-05 05:15:06 -04:00
import 'mocha'
2021-07-06 09:22:51 -04:00
import * as chai from 'chai'
import { wait } from '@shared/core-utils'
import { HttpStatusCode, Video } from '@shared/models'
2018-10-05 05:15:06 -04:00
import {
2019-04-24 09:10:37 -04:00
cleanupTests,
2021-07-16 03:47:51 -04:00
createSingleServer,
2021-07-08 10:21:42 -04:00
HistoryCommand,
2019-04-24 09:10:37 -04:00
killallServers,
2021-07-16 03:47:51 -04:00
PeerTubeServer,
setAccessTokensToServers
} from '@shared/server-commands'
2018-10-05 05:15:06 -04:00
const expect = chai.expect
describe('Test videos history', function () {
2021-07-16 03:47:51 -04:00
let server: PeerTubeServer = null
2022-01-18 05:23:41 -05:00
let video1Id: number
2018-10-05 05:15:06 -04:00
let video1UUID: string
let video2UUID: string
let video3UUID: string
let video3WatchedDate: Date
let userAccessToken: string
2021-07-08 10:21:42 -04:00
let command: HistoryCommand
2018-10-05 05:15:06 -04:00
before(async function () {
this.timeout(30000)
2021-07-16 03:47:51 -04:00
server = await createSingleServer(1)
2018-10-05 05:15:06 -04:00
await setAccessTokensToServers([ server ])
2021-07-16 03:04:35 -04:00
command = server.history
2021-07-08 10:21:42 -04:00
2018-10-05 05:15:06 -04:00
{
2022-01-18 05:23:41 -05:00
const { id, uuid } = await server.videos.upload({ attributes: { name: 'video 1' } })
2021-07-15 04:02:54 -04:00
video1UUID = uuid
2022-01-18 05:23:41 -05:00
video1Id = id
2018-10-05 05:15:06 -04:00
}
{
2021-07-16 03:04:35 -04:00
const { uuid } = await server.videos.upload({ attributes: { name: 'video 2' } })
2021-07-15 04:02:54 -04:00
video2UUID = uuid
2018-10-05 05:15:06 -04:00
}
{
2021-07-16 03:04:35 -04:00
const { uuid } = await server.videos.upload({ attributes: { name: 'video 3' } })
2021-07-15 04:02:54 -04:00
video3UUID = uuid
2018-10-05 05:15:06 -04:00
}
const user = {
username: 'user_1',
password: 'super password'
}
2021-07-16 03:04:35 -04:00
await server.users.create({ username: user.username, password: user.password })
userAccessToken = await server.login.getAccessToken(user)
2018-10-05 05:15:06 -04:00
})
it('Should get videos, without watching history', async function () {
2021-07-16 03:04:35 -04:00
const { data } = await server.videos.listWithToken()
2018-10-05 05:15:06 -04:00
2021-07-15 04:02:54 -04:00
for (const video of data) {
2021-07-16 03:04:35 -04:00
const videoDetails = await server.videos.getWithToken({ id: video.id })
2018-10-05 05:15:06 -04:00
expect(video.userHistory).to.be.undefined
expect(videoDetails.userHistory).to.be.undefined
}
})
it('Should watch the first and second video', async function () {
2022-01-18 05:23:41 -05:00
await command.watchVideo({ videoId: video2UUID, currentTime: 8 })
await command.watchVideo({ videoId: video1UUID, currentTime: 3 })
2018-10-05 05:15:06 -04:00
})
it('Should return the correct history when listing, searching and getting videos', async function () {
const videosOfVideos: Video[][] = []
{
2021-07-16 03:04:35 -04:00
const { data } = await server.videos.listWithToken()
2021-07-15 04:02:54 -04:00
videosOfVideos.push(data)
2018-10-05 05:15:06 -04:00
}
{
2021-07-16 03:04:35 -04:00
const body = await server.search.searchVideos({ token: server.accessToken, search: 'video' })
2021-07-06 09:22:51 -04:00
videosOfVideos.push(body.data)
2018-10-05 05:15:06 -04: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 03:04:35 -04:00
const videoDetails = await server.videos.getWithToken({ id: video1UUID })
2018-10-05 05:15:06 -04:00
expect(videoDetails.userHistory).to.not.be.undefined
expect(videoDetails.userHistory.currentTime).to.equal(3)
}
{
2021-07-16 03:04:35 -04:00
const videoDetails = await server.videos.getWithToken({ id: video2UUID })
2018-10-05 05:15:06 -04:00
expect(videoDetails.userHistory).to.not.be.undefined
expect(videoDetails.userHistory.currentTime).to.equal(8)
}
{
2021-07-16 03:04:35 -04:00
const videoDetails = await server.videos.getWithToken({ id: video3UUID })
2018-10-05 05:15:06 -04:00
expect(videoDetails.userHistory).to.be.undefined
}
})
it('Should have these videos when listing my history', async function () {
video3WatchedDate = new Date()
2022-01-18 05:23:41 -05:00
await command.watchVideo({ videoId: video3UUID, currentTime: 2 })
2021-07-08 10:21:42 -04:00
const body = await command.list()
2021-07-08 10:21:42 -04:00
expect(body.total).to.equal(3)
2021-07-08 10:21:42 -04:00
const videos = body.data
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 () {
2021-07-08 10:21:42 -04:00
const body = await command.list({ token: userAccessToken })
2021-07-08 10:21:42 -04:00
expect(body.total).to.equal(0)
expect(body.data).to.have.lengthOf(0)
})
it('Should be able to search through videos in my history', async function () {
2021-07-08 10:21:42 -04:00
const body = await command.list({ search: '2' })
expect(body.total).to.equal(1)
2021-07-08 10:21:42 -04:00
const videos = body.data
expect(videos[0].name).to.equal('video 2')
})
it('Should clear my history', async function () {
2022-01-18 05:23:41 -05:00
await command.removeAll({ beforeDate: video3WatchedDate.toISOString() })
})
it('Should have my history cleared', async function () {
2021-07-08 10:21:42 -04:00
const body = await command.list()
expect(body.total).to.equal(1)
2021-07-08 10:21:42 -04:00
const videos = body.data
expect(videos[0].name).to.equal('video 3')
})
it('Should disable videos history', async function () {
2021-07-16 03:04:35 -04:00
await server.users.updateMe({
videosHistoryEnabled: false
})
2022-01-18 05:23:41 -05:00
await command.watchVideo({ videoId: video2UUID, currentTime: 8, expectedStatus: HttpStatusCode.CONFLICT_409 })
})
it('Should re-enable videos history', async function () {
2021-07-16 03:04:35 -04:00
await server.users.updateMe({
videosHistoryEnabled: true
})
2022-01-18 05:23:41 -05:00
await command.watchVideo({ videoId: video1UUID, currentTime: 8 })
2021-07-08 10:21:42 -04:00
const body = await command.list()
expect(body.total).to.equal(2)
2021-07-08 10:21:42 -04:00
const videos = body.data
expect(videos[0].name).to.equal('video 1')
expect(videos[1].name).to.equal('video 3')
})
it('Should not clean old history', async function () {
this.timeout(50000)
2021-07-09 09:37:43 -04:00
await killallServers([ server ])
2021-07-16 03:47:51 -04:00
await server.run({ history: { videos: { max_age: '10 days' } } })
await wait(6000)
// Should still have history
2021-07-08 10:21:42 -04:00
const body = await command.list()
expect(body.total).to.equal(2)
})
it('Should clean old history', async function () {
this.timeout(50000)
2021-07-09 09:37:43 -04:00
await killallServers([ server ])
2021-07-16 03:47:51 -04:00
await server.run({ history: { videos: { max_age: '5 seconds' } } })
await wait(6000)
2021-07-08 10:21:42 -04:00
const body = await command.list()
expect(body.total).to.equal(0)
})
2022-01-18 05:23:41 -05:00
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)
}
})
2019-04-24 09:10:37 -04:00
after(async function () {
await cleanupTests([ server ])
2018-10-05 05:15:06 -04:00
})
})