From df0b219d36bf6852cdf2a7ad09ed4a41c6bccefa Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Tue, 5 Mar 2019 10:58:44 +0100 Subject: Add playlist rest tests --- shared/utils/videos/video-channels.ts | 18 ++++- shared/utils/videos/video-playlists.ts | 117 +++++++++++++++++++++++++++++++-- shared/utils/videos/videos.ts | 29 ++++++-- 3 files changed, 151 insertions(+), 13 deletions(-) (limited to 'shared/utils/videos') diff --git a/shared/utils/videos/video-channels.ts b/shared/utils/videos/video-channels.ts index 3935c261e..93a257bf9 100644 --- a/shared/utils/videos/video-channels.ts +++ b/shared/utils/videos/video-channels.ts @@ -1,6 +1,8 @@ import * as request from 'supertest' import { VideoChannelCreate, VideoChannelUpdate } from '../../models/videos' import { updateAvatarRequest } from '../requests/requests' +import { getMyUserInformation, ServerInfo } from '..' +import { User } from '../..' function getVideoChannelsList (url: string, start: number, count: number, sort?: string) { const path = '/api/v1/video-channels' @@ -105,6 +107,19 @@ function updateVideoChannelAvatar (options: { return updateAvatarRequest(Object.assign(options, { path })) } +function setDefaultVideoChannel (servers: ServerInfo[]) { + const tasks: Promise[] = [] + + for (const server of servers) { + const p = getMyUserInformation(server.url, server.accessToken) + .then(res => server.videoChannel = (res.body as User).videoChannels[0]) + + tasks.push(p) + } + + return Promise.all(tasks) +} + // --------------------------------------------------------------------------- export { @@ -114,5 +129,6 @@ export { addVideoChannel, updateVideoChannel, deleteVideoChannel, - getVideoChannel + getVideoChannel, + setDefaultVideoChannel } diff --git a/shared/utils/videos/video-playlists.ts b/shared/utils/videos/video-playlists.ts index 21285688a..4af52ec0f 100644 --- a/shared/utils/videos/video-playlists.ts +++ b/shared/utils/videos/video-playlists.ts @@ -4,6 +4,12 @@ import { omit } from 'lodash' import { VideoPlaylistUpdate } from '../../models/videos/playlist/video-playlist-update.model' import { VideoPlaylistElementCreate } from '../../models/videos/playlist/video-playlist-element-create.model' import { VideoPlaylistElementUpdate } from '../../models/videos/playlist/video-playlist-element-update.model' +import { videoUUIDToId } from './videos' +import { join } from 'path' +import { root } from '..' +import { readdir } from 'fs-extra' +import { expect } from 'chai' +import { VideoPlaylistType } from '../../models/videos/playlist/video-playlist-type.model' function getVideoPlaylistsList (url: string, start: number, count: number, sort?: string) { const path = '/api/v1/video-playlists' @@ -17,7 +23,67 @@ function getVideoPlaylistsList (url: string, start: number, count: number, sort? return makeGetRequest({ url, path, - query + query, + statusCodeExpected: 200 + }) +} + +function getVideoChannelPlaylistsList (url: string, videoChannelName: string, start: number, count: number, sort?: string) { + const path = '/api/v1/video-channels/' + videoChannelName + '/video-playlists' + + const query = { + start, + count, + sort + } + + return makeGetRequest({ + url, + path, + query, + statusCodeExpected: 200 + }) +} + +function getAccountPlaylistsList (url: string, accountName: string, start: number, count: number, sort?: string) { + const path = '/api/v1/accounts/' + accountName + '/video-playlists' + + const query = { + start, + count, + sort + } + + return makeGetRequest({ + url, + path, + query, + statusCodeExpected: 200 + }) +} + +function getAccountPlaylistsListWithToken ( + url: string, + token: string, + accountName: string, + start: number, + count: number, + playlistType?: VideoPlaylistType +) { + const path = '/api/v1/accounts/' + accountName + '/video-playlists' + + const query = { + start, + count, + playlistType + } + + return makeGetRequest({ + url, + token, + path, + query, + statusCodeExpected: 200 }) } @@ -31,6 +97,17 @@ function getVideoPlaylist (url: string, playlistId: number | string, statusCodeE }) } +function getVideoPlaylistWithToken (url: string, token: string, playlistId: number | string, statusCodeExpected = 200) { + const path = '/api/v1/video-playlists/' + playlistId + + return makeGetRequest({ + url, + token, + path, + statusCodeExpected + }) +} + function deleteVideoPlaylist (url: string, token: string, playlistId: number | string, statusCodeExpected = 204) { const path = '/api/v1/video-playlists/' + playlistId @@ -93,13 +170,15 @@ function updateVideoPlaylist (options: { }) } -function addVideoInPlaylist (options: { +async function addVideoInPlaylist (options: { url: string, token: string, playlistId: number | string, - elementAttrs: VideoPlaylistElementCreate + elementAttrs: VideoPlaylistElementCreate | { videoId: string } expectedStatus?: number }) { + options.elementAttrs.videoId = await videoUUIDToId(options.url, options.elementAttrs.videoId) + const path = '/api/v1/video-playlists/' + options.playlistId + '/videos' return makePostBodyRequest({ @@ -135,7 +214,7 @@ function removeVideoFromPlaylist (options: { token: string, playlistId: number | string, videoId: number | string, - expectedStatus: number + expectedStatus?: number }) { const path = '/api/v1/video-playlists/' + options.playlistId + '/videos/' + options.videoId @@ -156,7 +235,7 @@ function reorderVideosPlaylist (options: { insertAfterPosition: number, reorderLength?: number }, - expectedStatus: number + expectedStatus?: number }) { const path = '/api/v1/video-playlists/' + options.playlistId + '/videos/reorder' @@ -165,15 +244,37 @@ function reorderVideosPlaylist (options: { path, token: options.token, fields: options.elementAttrs, - statusCodeExpected: options.expectedStatus + statusCodeExpected: options.expectedStatus || 204 }) } +async function checkPlaylistFilesWereRemoved ( + playlistUUID: string, + serverNumber: number, + directories = [ 'thumbnails' ] +) { + const testDirectory = 'test' + serverNumber + + for (const directory of directories) { + const directoryPath = join(root(), testDirectory, directory) + + const files = await readdir(directoryPath) + for (const file of files) { + expect(file).to.not.contain(playlistUUID) + } + } +} + // --------------------------------------------------------------------------- export { getVideoPlaylistsList, + getVideoChannelPlaylistsList, + getAccountPlaylistsList, + getAccountPlaylistsListWithToken, + getVideoPlaylist, + getVideoPlaylistWithToken, createVideoPlaylist, updateVideoPlaylist, @@ -183,5 +284,7 @@ export { updateVideoPlaylistElement, removeVideoFromPlaylist, - reorderVideosPlaylist + reorderVideosPlaylist, + + checkPlaylistFilesWereRemoved } diff --git a/shared/utils/videos/videos.ts b/shared/utils/videos/videos.ts index 2c09f0086..16b5165f1 100644 --- a/shared/utils/videos/videos.ts +++ b/shared/utils/videos/videos.ts @@ -1,7 +1,7 @@ /* tslint:disable:no-unused-expression */ import { expect } from 'chai' -import { existsSync, readdir, readFile } from 'fs-extra' +import { pathExists, readdir, readFile } from 'fs-extra' import * as parseTorrent from 'parse-torrent' import { extname, join } from 'path' import * as request from 'supertest' @@ -16,7 +16,7 @@ import { ServerInfo, testImage } from '../' - +import * as validator from 'validator' import { VideoDetails, VideoPrivacy } from '../../models/videos' import { VIDEO_CATEGORIES, VIDEO_LANGUAGES, VIDEO_LICENCES, VIDEO_PRIVACIES } from '../../../server/initializers/constants' import { dateIsValid, webtorrentAdd } from '../miscs/miscs' @@ -311,8 +311,8 @@ async function checkVideoFilesWereRemoved ( for (const directory of directories) { const directoryPath = join(root(), testDirectory, directory) - const directoryExists = existsSync(directoryPath) - if (!directoryExists) continue + const directoryExists = await pathExists(directoryPath) + if (directoryExists === false) continue const files = await readdir(directoryPath) for (const file of files) { @@ -597,12 +597,30 @@ async function completeVideoCheck ( } } +async function videoUUIDToId (url: string, id: number | string) { + if (validator.isUUID('' + id) === false) return id + + const res = await getVideo(url, id) + return res.body.id +} + +async function uploadVideoAndGetId (options: { server: ServerInfo, videoName: string, nsfw?: boolean, token?: string }) { + const videoAttrs: any = { name: options.videoName } + if (options.nsfw) videoAttrs.nsfw = options.nsfw + + + const res = await uploadVideo(options.server.url, options.token || options.server.accessToken, videoAttrs) + + return { id: res.body.video.id, uuid: res.body.video.uuid } +} + // --------------------------------------------------------------------------- export { getVideoDescription, getVideoCategories, getVideoLicences, + videoUUIDToId, getVideoPrivacies, getVideoLanguages, getMyVideos, @@ -624,5 +642,6 @@ export { getLocalVideos, completeVideoCheck, checkVideoFilesWereRemoved, - getPlaylistVideos + getPlaylistVideos, + uploadVideoAndGetId } -- cgit v1.2.3