Introduce history command

This commit is contained in:
Chocobozzz 2021-07-08 16:21:42 +02:00
parent e6346d59e6
commit 313228e9c3
No known key found for this signature in database
GPG Key ID: 583A612D890159BE
5 changed files with 92 additions and 81 deletions

View File

@ -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')
const body = await command.list({ search: '2' })
expect(body.total).to.equal(1)
expect(res.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)
const body = await command.list()
expect(body.total).to.equal(1)
expect(res.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)
await command.wathVideo({ videoId: video1UUID, currentTime: 8 })
const res = await listMyVideosHistory(server.url, server.accessToken)
const body = await command.list()
expect(body.total).to.equal(2)
expect(res.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 () {

View File

@ -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)
})

View File

@ -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<ResultList<Video>>({
...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
})
}
}

View File

@ -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'

View File

@ -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
}