From 313228e9c3b5bcef5391228c9b949d05d32ad7bb Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Thu, 8 Jul 2021 16:21:42 +0200 Subject: [PATCH] Introduce history command --- server/tests/api/videos/videos-history.ts | 59 ++++++++++---------- shared/extra-utils/server/servers.ts | 4 +- shared/extra-utils/videos/history-command.ts | 59 ++++++++++++++++++++ shared/extra-utils/videos/index.ts | 2 +- shared/extra-utils/videos/video-history.ts | 49 ---------------- 5 files changed, 92 insertions(+), 81 deletions(-) create mode 100644 shared/extra-utils/videos/history-command.ts delete mode 100644 shared/extra-utils/videos/video-history.ts diff --git a/server/tests/api/videos/videos-history.ts b/server/tests/api/videos/videos-history.ts index 209b93014..731447135 100644 --- a/server/tests/api/videos/videos-history.ts +++ b/server/tests/api/videos/videos-history.ts @@ -9,6 +9,7 @@ import { flushAndRunServer, getVideosListWithToken, getVideoWithToken, + HistoryCommand, killallServers, reRunServer, ServerInfo, @@ -18,7 +19,6 @@ import { userLogin, wait } from '@shared/extra-utils' -import { listMyVideosHistory, removeMyVideosHistory, userWatchVideo } from '@shared/extra-utils/videos/video-history' import { Video, VideoDetails } from '@shared/models' const expect = chai.expect @@ -30,6 +30,7 @@ describe('Test videos history', function () { let video3UUID: string let video3WatchedDate: Date let userAccessToken: string + let command: HistoryCommand before(async function () { this.timeout(30000) @@ -38,6 +39,8 @@ describe('Test videos history', function () { await setAccessTokensToServers([ server ]) + command = server.historyCommand + { const res = await uploadVideo(server.url, server.accessToken, { name: 'video 1' }) video1UUID = res.body.video.uuid @@ -75,8 +78,8 @@ describe('Test videos history', function () { }) it('Should watch the first and second video', async function () { - await userWatchVideo(server.url, server.accessToken, video2UUID, 8) - await userWatchVideo(server.url, server.accessToken, video1UUID, 3) + await command.wathVideo({ videoId: video2UUID, currentTime: 8 }) + await command.wathVideo({ videoId: video1UUID, currentTime: 3 }) }) it('Should return the correct history when listing, searching and getting videos', async function () { @@ -132,44 +135,42 @@ describe('Test videos history', function () { it('Should have these videos when listing my history', async function () { video3WatchedDate = new Date() - await userWatchVideo(server.url, server.accessToken, video3UUID, 2) + await command.wathVideo({ videoId: video3UUID, currentTime: 2 }) - const res = await listMyVideosHistory(server.url, server.accessToken) + const body = await command.list() - expect(res.body.total).to.equal(3) + expect(body.total).to.equal(3) - const videos: Video[] = res.body.data + 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 res = await listMyVideosHistory(server.url, userAccessToken) + const body = await command.list({ token: userAccessToken }) - expect(res.body.total).to.equal(0) - expect(res.body.data).to.have.lengthOf(0) + 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 res = await listMyVideosHistory(server.url, server.accessToken, '2') - - expect(res.body.total).to.equal(1) + const body = await command.list({ search: '2' }) + expect(body.total).to.equal(1) - const videos: Video[] = res.body.data + const videos = body.data expect(videos[0].name).to.equal('video 2') }) it('Should clear my history', async function () { - await removeMyVideosHistory(server.url, server.accessToken, video3WatchedDate.toISOString()) + await command.remove({ beforeDate: video3WatchedDate.toISOString() }) }) it('Should have my history cleared', async function () { - const res = await listMyVideosHistory(server.url, server.accessToken) - - expect(res.body.total).to.equal(1) + const body = await command.list() + expect(body.total).to.equal(1) - const videos: Video[] = res.body.data + const videos = body.data expect(videos[0].name).to.equal('video 3') }) @@ -180,7 +181,7 @@ describe('Test videos history', function () { videosHistoryEnabled: false }) - await userWatchVideo(server.url, server.accessToken, video2UUID, 8, HttpStatusCode.CONFLICT_409) + await command.wathVideo({ videoId: video2UUID, currentTime: 8, expectedStatus: HttpStatusCode.CONFLICT_409 }) }) it('Should re-enable videos history', async function () { @@ -190,13 +191,12 @@ describe('Test videos history', function () { videosHistoryEnabled: true }) - await userWatchVideo(server.url, server.accessToken, video1UUID, 8) - - const res = await listMyVideosHistory(server.url, server.accessToken) + await command.wathVideo({ videoId: video1UUID, currentTime: 8 }) - expect(res.body.total).to.equal(2) + const body = await command.list() + expect(body.total).to.equal(2) - const videos: Video[] = res.body.data + const videos = body.data expect(videos[0].name).to.equal('video 1') expect(videos[1].name).to.equal('video 3') }) @@ -212,9 +212,8 @@ describe('Test videos history', function () { // Should still have history - const res = await listMyVideosHistory(server.url, server.accessToken) - - expect(res.body.total).to.equal(2) + const body = await command.list() + expect(body.total).to.equal(2) }) it('Should clean old history', async function () { @@ -226,8 +225,8 @@ describe('Test videos history', function () { await wait(6000) - const res = await listMyVideosHistory(server.url, server.accessToken) - expect(res.body.total).to.equal(0) + const body = await command.list() + expect(body.total).to.equal(0) }) after(async function () { diff --git a/shared/extra-utils/server/servers.ts b/shared/extra-utils/server/servers.ts index 78b3be9c7..bd5c29e51 100644 --- a/shared/extra-utils/server/servers.ts +++ b/shared/extra-utils/server/servers.ts @@ -18,7 +18,7 @@ import { makeGetRequest } from '../requests/requests' import { SearchCommand } from '../search' import { SocketIOCommand } from '../socket' import { AccountsCommand, BlocklistCommand, SubscriptionsCommand } from '../users' -import { BlacklistCommand, CaptionsCommand, ChangeOwnershipCommand, LiveCommand, PlaylistsCommand, ServicesCommand } from '../videos' +import { BlacklistCommand, CaptionsCommand, ChangeOwnershipCommand, HistoryCommand, LiveCommand, PlaylistsCommand, ServicesCommand } from '../videos' import { ConfigCommand } from './config-command' import { ContactFormCommand } from './contact-form-command' import { DebugCommand } from './debug-command' @@ -106,6 +106,7 @@ interface ServerInfo { captionsCommand?: CaptionsCommand changeOwnershipCommand?: ChangeOwnershipCommand playlistsCommand?: PlaylistsCommand + historyCommand?: HistoryCommand } function parallelTests () { @@ -337,6 +338,7 @@ async function runServer (server: ServerInfo, configOverrideArg?: any, args = [] server.captionsCommand = new CaptionsCommand(server) server.changeOwnershipCommand = new ChangeOwnershipCommand(server) server.playlistsCommand = new PlaylistsCommand(server) + server.historyCommand = new HistoryCommand(server) res(server) }) diff --git a/shared/extra-utils/videos/history-command.ts b/shared/extra-utils/videos/history-command.ts new file mode 100644 index 000000000..8a144a312 --- /dev/null +++ b/shared/extra-utils/videos/history-command.ts @@ -0,0 +1,59 @@ +import { ResultList, Video } from '@shared/models' +import { HttpStatusCode } from '../../core-utils/miscs/http-error-codes' +import { AbstractCommand, OverrideCommandOptions } from '../shared' + +export class HistoryCommand extends AbstractCommand { + + wathVideo (options: OverrideCommandOptions & { + videoId: number | string + currentTime: number + }) { + const { videoId, currentTime } = options + + const path = '/api/v1/videos/' + videoId + '/watching' + const fields = { currentTime } + + return this.putBodyRequest({ + ...options, + + path, + fields, + implicitToken: true, + defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204 + }) + } + + list (options: OverrideCommandOptions & { + search?: string + } = {}) { + const { search } = options + const path = '/api/v1/users/me/history/videos' + + return this.getRequestBody>({ + ...options, + + path, + query: { + search + }, + implicitToken: true, + defaultExpectedStatus: HttpStatusCode.OK_200 + }) + } + + remove (options: OverrideCommandOptions & { + beforeDate?: string + } = {}) { + const { beforeDate } = options + const path = '/api/v1/users/me/history/videos/remove' + + return this.postBodyRequest({ + ...options, + + path, + fields: { beforeDate }, + implicitToken: true, + defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204 + }) + } +} diff --git a/shared/extra-utils/videos/index.ts b/shared/extra-utils/videos/index.ts index 1f6241d7e..74667fc06 100644 --- a/shared/extra-utils/videos/index.ts +++ b/shared/extra-utils/videos/index.ts @@ -2,6 +2,7 @@ export * from './blacklist-command' export * from './captions' export * from './captions-command' export * from './change-ownership-command' +export * from './history-command' export * from './live-command' export * from './live' export * from './playlists-command' @@ -9,7 +10,6 @@ export * from './playlists' export * from './services-command' export * from './video-channels' export * from './video-comments' -export * from './video-history' export * from './video-imports' export * from './video-streaming-playlists' export * from './videos' diff --git a/shared/extra-utils/videos/video-history.ts b/shared/extra-utils/videos/video-history.ts deleted file mode 100644 index b989e14dc..000000000 --- a/shared/extra-utils/videos/video-history.ts +++ /dev/null @@ -1,49 +0,0 @@ -import { makeGetRequest, makePostBodyRequest, makePutBodyRequest } from '../requests/requests' -import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes' - -function userWatchVideo ( - url: string, - token: string, - videoId: number | string, - currentTime: number, - statusCodeExpected = HttpStatusCode.NO_CONTENT_204 -) { - const path = '/api/v1/videos/' + videoId + '/watching' - const fields = { currentTime } - - return makePutBodyRequest({ url, path, token, fields, statusCodeExpected }) -} - -function listMyVideosHistory (url: string, token: string, search?: string) { - const path = '/api/v1/users/me/history/videos' - - return makeGetRequest({ - url, - path, - token, - query: { - search - }, - statusCodeExpected: HttpStatusCode.OK_200 - }) -} - -function removeMyVideosHistory (url: string, token: string, beforeDate?: string) { - const path = '/api/v1/users/me/history/videos/remove' - - return makePostBodyRequest({ - url, - path, - token, - fields: beforeDate ? { beforeDate } : {}, - statusCodeExpected: HttpStatusCode.NO_CONTENT_204 - }) -} - -// --------------------------------------------------------------------------- - -export { - userWatchVideo, - listMyVideosHistory, - removeMyVideosHistory -} -- 2.41.0