From 6e46de095d7169355dd83030f6ce4a582304153a Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Fri, 5 Oct 2018 11:15:06 +0200 Subject: Add user history and resume videos --- server/tests/api/check-params/index.ts | 1 + server/tests/api/check-params/videos-history.ts | 79 +++++++++++++++ server/tests/api/videos/index.ts | 1 + server/tests/api/videos/videos-history.ts | 128 ++++++++++++++++++++++++ server/tests/utils/videos/video-history.ts | 14 +++ 5 files changed, 223 insertions(+) create mode 100644 server/tests/api/check-params/videos-history.ts create mode 100644 server/tests/api/videos/videos-history.ts create mode 100644 server/tests/utils/videos/video-history.ts (limited to 'server/tests') diff --git a/server/tests/api/check-params/index.ts b/server/tests/api/check-params/index.ts index 44460a167..71a217649 100644 --- a/server/tests/api/check-params/index.ts +++ b/server/tests/api/check-params/index.ts @@ -15,3 +15,4 @@ import './video-channels' import './video-comments' import './video-imports' import './videos' +import './videos-history' diff --git a/server/tests/api/check-params/videos-history.ts b/server/tests/api/check-params/videos-history.ts new file mode 100644 index 000000000..808c3b616 --- /dev/null +++ b/server/tests/api/check-params/videos-history.ts @@ -0,0 +1,79 @@ +/* tslint:disable:no-unused-expression */ + +import * as chai from 'chai' +import 'mocha' +import { + flushTests, + killallServers, + makePostBodyRequest, + makePutBodyRequest, + runServer, + ServerInfo, + setAccessTokensToServers, + uploadVideo +} from '../../utils' + +const expect = chai.expect + +describe('Test videos history API validator', function () { + let path: string + let server: ServerInfo + + // --------------------------------------------------------------- + + before(async function () { + this.timeout(30000) + + await flushTests() + + server = await runServer(1) + + await setAccessTokensToServers([ server ]) + + const res = await uploadVideo(server.url, server.accessToken, {}) + const videoUUID = res.body.video.uuid + + path = '/api/v1/videos/' + videoUUID + '/watching' + }) + + describe('When notifying a user is watching a video', function () { + + it('Should fail with an unauthenticated user', async function () { + const fields = { currentTime: 5 } + await makePutBodyRequest({ url: server.url, path, fields, statusCodeExpected: 401 }) + }) + + it('Should fail with an incorrect video id', async function () { + const fields = { currentTime: 5 } + const path = '/api/v1/videos/blabla/watching' + await makePutBodyRequest({ url: server.url, path, fields, token: server.accessToken, statusCodeExpected: 400 }) + }) + + it('Should fail with an unknown video', async function () { + const fields = { currentTime: 5 } + const path = '/api/v1/videos/d91fff41-c24d-4508-8e13-3bd5902c3b02/watching' + + await makePutBodyRequest({ url: server.url, path, fields, token: server.accessToken, statusCodeExpected: 404 }) + }) + + it('Should fail with a bad current time', async function () { + const fields = { currentTime: 'hello' } + await makePutBodyRequest({ url: server.url, path, fields, token: server.accessToken, statusCodeExpected: 400 }) + }) + + it('Should succeed with the correct parameters', async function () { + const fields = { currentTime: 5 } + + await makePutBodyRequest({ url: server.url, path, fields, token: server.accessToken, statusCodeExpected: 204 }) + }) + }) + + after(async function () { + killallServers([ server ]) + + // Keep the logs if the test failed + if (this['ok']) { + await flushTests() + } + }) +}) diff --git a/server/tests/api/videos/index.ts b/server/tests/api/videos/index.ts index bf58f9c79..09bb62a8d 100644 --- a/server/tests/api/videos/index.ts +++ b/server/tests/api/videos/index.ts @@ -14,4 +14,5 @@ import './video-nsfw' import './video-privacy' import './video-schedule-update' import './video-transcoder' +import './videos-history' import './videos-overview' diff --git a/server/tests/api/videos/videos-history.ts b/server/tests/api/videos/videos-history.ts new file mode 100644 index 000000000..6d289b288 --- /dev/null +++ b/server/tests/api/videos/videos-history.ts @@ -0,0 +1,128 @@ +/* tslint:disable:no-unused-expression */ + +import * as chai from 'chai' +import 'mocha' +import { + flushTests, + getVideosListWithToken, + getVideoWithToken, + killallServers, makePutBodyRequest, + runServer, searchVideoWithToken, + ServerInfo, + setAccessTokensToServers, + uploadVideo +} from '../../utils' +import { Video, VideoDetails } from '../../../../shared/models/videos' +import { userWatchVideo } from '../../utils/videos/video-history' + +const expect = chai.expect + +describe('Test videos history', function () { + let server: ServerInfo = null + let video1UUID: string + let video2UUID: string + let video3UUID: string + + before(async function () { + this.timeout(30000) + + await flushTests() + + server = await runServer(1) + + await setAccessTokensToServers([ server ]) + + { + const res = await uploadVideo(server.url, server.accessToken, { name: 'video 1' }) + video1UUID = res.body.video.uuid + } + + { + const res = await uploadVideo(server.url, server.accessToken, { name: 'video 2' }) + video2UUID = res.body.video.uuid + } + + { + const res = await uploadVideo(server.url, server.accessToken, { name: 'video 3' }) + video3UUID = res.body.video.uuid + } + }) + + it('Should get videos, without watching history', async function () { + const res = await getVideosListWithToken(server.url, server.accessToken) + const videos: Video[] = res.body.data + + for (const video of videos) { + const resDetail = await getVideoWithToken(server.url, server.accessToken, video.id) + const videoDetails: VideoDetails = resDetail.body + + expect(video.userHistory).to.be.undefined + expect(videoDetails.userHistory).to.be.undefined + } + }) + + 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) + }) + + 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 res = await searchVideoWithToken(server.url, 'video', server.accessToken) + videosOfVideos.push(res.body.data) + } + + 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 + } + + { + const resDetail = await getVideoWithToken(server.url, server.accessToken, video1UUID) + const videoDetails: VideoDetails = resDetail.body + + 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 + + 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 + + expect(videoDetails.userHistory).to.be.undefined + } + }) + + after(async function () { + killallServers([ server ]) + + // Keep the logs if the test failed + if (this['ok']) { + await flushTests() + } + }) +}) diff --git a/server/tests/utils/videos/video-history.ts b/server/tests/utils/videos/video-history.ts new file mode 100644 index 000000000..7635478f7 --- /dev/null +++ b/server/tests/utils/videos/video-history.ts @@ -0,0 +1,14 @@ +import { makePutBodyRequest } from '../requests/requests' + +function userWatchVideo (url: string, token: string, videoId: number | string, currentTime: number) { + const path = '/api/v1/videos/' + videoId + '/watching' + const fields = { currentTime } + + return makePutBodyRequest({ url, path, token, fields, statusCodeExpected: 204 }) +} + +// --------------------------------------------------------------------------- + +export { + userWatchVideo +} -- cgit v1.2.3