X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Ftests%2Fapi%2Fvideos%2Fvideos-history.ts;h=89e74a309143ea6182fc37e76c907835c1df540b;hb=9295c68b74fe1f1e2e9f72009205d7f0379844c5;hp=40ae94f797f198e1bdc9e79c833c82d252dd84a6;hpb=ae28cdf327d782e629379eee1999096ca2a5d74b;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/tests/api/videos/videos-history.ts b/server/tests/api/videos/videos-history.ts index 40ae94f79..89e74a309 100644 --- a/server/tests/api/videos/videos-history.ts +++ b/server/tests/api/videos/videos-history.ts @@ -1,60 +1,56 @@ -/* tslint:disable:no-unused-expression */ +/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */ -import * as chai from 'chai' import 'mocha' -import { - flushTests, - getVideosListWithToken, - getVideoWithToken, - killallServers, makePutBodyRequest, - runServer, searchVideoWithToken, - ServerInfo, - setAccessTokensToServers, - uploadVideo -} from '../../../../shared/utils' -import { Video, VideoDetails } from '../../../../shared/models/videos' -import { userWatchVideo } from '../../../../shared/utils/videos/video-history' +import * as chai from 'chai' +import { wait } from '@shared/core-utils' +import { Video } from '@shared/models' +import { cleanupTests, createSingleServer, killallServers, 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 + let video3WatchedDate: Date + let userAccessToken: string before(async function () { this.timeout(30000) - await flushTests() - - server = await runServer(1) + server = await createSingleServer(1) await setAccessTokensToServers([ server ]) + // 10 seconds long + const fixture = 'video_short1.webm' + { - 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', fixture } }) + 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', fixture } }) + 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', fixture } }) + video3UUID = uuid } + + userAccessToken = await server.users.generateUserAndToken('user_1') }) 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 @@ -62,21 +58,21 @@ describe('Test videos history', function () { }) it('Should watch the first and second video', async function () { - await userWatchVideo(server.url, server.accessToken, video1UUID, 3) - await userWatchVideo(server.url, server.accessToken, video2UUID, 8) + await server.views.view({ id: video2UUID, token: server.accessToken, currentTime: 8 }) + await server.views.view({ id: video1UUID, token: server.accessToken, 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 res = await searchVideoWithToken(server.url, 'video', server.accessToken) - videosOfVideos.push(res.body.data) + const body = await server.search.searchVideos({ token: server.accessToken, search: 'video' }) + videosOfVideos.push(body.data) } for (const videos of videosOfVideos) { @@ -94,35 +90,138 @@ 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 } }) - after(async function () { - killallServers([ server ]) + it('Should have these videos when listing my history', async function () { + video3WatchedDate = new Date() + await server.views.view({ id: video3UUID, token: server.accessToken, currentTime: 2 }) + + const body = await server.history.list() + + expect(body.total).to.equal(3) + + 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 () { + const body = await server.history.list({ token: userAccessToken }) + + 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 () { + const body = await server.history.list({ search: '2' }) + expect(body.total).to.equal(1) + + const videos = body.data + expect(videos[0].name).to.equal('video 2') + }) + + it('Should clear my history', async function () { + await server.history.removeAll({ beforeDate: video3WatchedDate.toISOString() }) + }) + + it('Should have my history cleared', async function () { + const body = await server.history.list() + expect(body.total).to.equal(1) + + const videos = body.data + expect(videos[0].name).to.equal('video 3') + }) + + it('Should disable videos history', async function () { + await server.users.updateMe({ + videosHistoryEnabled: false + }) + + 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') + }) + + it('Should re-enable videos history', async function () { + await server.users.updateMe({ + videosHistoryEnabled: true + }) + + await server.views.view({ id: video2UUID, token: server.accessToken, currentTime: 8 }) - // Keep the logs if the test failed - if (this['ok']) { - await flushTests() + const { data } = await server.history.list() + expect(data[0].name).to.equal('video 2') + }) + + it('Should not clean old history', async function () { + this.timeout(50000) + + await killallServers([ server ]) + + await server.run({ history: { videos: { max_age: '10 days' } } }) + + await wait(6000) + + // Should still have history + + const body = await server.history.list() + expect(body.total).to.equal(2) + }) + + it('Should clean old history', async function () { + this.timeout(50000) + + await killallServers([ server ]) + + await server.run({ history: { videos: { max_age: '5 seconds' } } }) + + await wait(6000) + + const body = await server.history.list() + expect(body.total).to.equal(0) + }) + + it('Should delete a specific history element', async function () { + { + await server.views.view({ id: video1UUID, token: server.accessToken, currentTime: 4 }) + await server.views.view({ id: video2UUID, token: server.accessToken, currentTime: 8 }) } + + { + const body = await server.history.list() + expect(body.total).to.equal(2) + } + + { + await server.history.removeElement({ videoId: video1Id }) + + const body = await server.history.list() + expect(body.total).to.equal(1) + expect(body.data[0].uuid).to.equal(video2UUID) + } + }) + + after(async function () { + await cleanupTests([ server ]) }) })