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 --- .../models/activitypub/objects/playlist-object.ts | 3 + .../videos/playlist/video-playlist-type.model.ts | 4 + .../models/videos/playlist/video-playlist.model.ts | 3 + shared/utils/requests/requests.ts | 2 + shared/utils/server/servers.ts | 4 + shared/utils/users/users.ts | 9 ++ shared/utils/videos/video-channels.ts | 18 +++- shared/utils/videos/video-playlists.ts | 117 +++++++++++++++++++-- shared/utils/videos/videos.ts | 29 ++++- 9 files changed, 176 insertions(+), 13 deletions(-) create mode 100644 shared/models/videos/playlist/video-playlist-type.model.ts (limited to 'shared') diff --git a/shared/models/activitypub/objects/playlist-object.ts b/shared/models/activitypub/objects/playlist-object.ts index 5f6733f92..c11a23a69 100644 --- a/shared/models/activitypub/objects/playlist-object.ts +++ b/shared/models/activitypub/objects/playlist-object.ts @@ -13,6 +13,9 @@ export interface PlaylistObject { icon: ActivityIconObject + published: string + updated: string + orderedItems?: string[] partOf?: string diff --git a/shared/models/videos/playlist/video-playlist-type.model.ts b/shared/models/videos/playlist/video-playlist-type.model.ts new file mode 100644 index 000000000..49233b743 --- /dev/null +++ b/shared/models/videos/playlist/video-playlist-type.model.ts @@ -0,0 +1,4 @@ +export enum VideoPlaylistType { + REGULAR = 1, + WATCH_LATER = 2 +} diff --git a/shared/models/videos/playlist/video-playlist.model.ts b/shared/models/videos/playlist/video-playlist.model.ts index 6aa04048c..7fec0e42b 100644 --- a/shared/models/videos/playlist/video-playlist.model.ts +++ b/shared/models/videos/playlist/video-playlist.model.ts @@ -1,6 +1,7 @@ import { AccountSummary } from '../../actors/index' import { VideoChannelSummary, VideoConstant } from '..' import { VideoPlaylistPrivacy } from './video-playlist-privacy.model' +import { VideoPlaylistType } from './video-playlist-type.model' export interface VideoPlaylist { id: number @@ -15,6 +16,8 @@ export interface VideoPlaylist { videosLength: number + type: VideoConstant + createdAt: Date | string updatedAt: Date | string diff --git a/shared/utils/requests/requests.ts b/shared/utils/requests/requests.ts index dc2d4abe5..3532fb429 100644 --- a/shared/utils/requests/requests.ts +++ b/shared/utils/requests/requests.ts @@ -77,6 +77,8 @@ function makeUploadRequest (options: { Object.keys(options.fields).forEach(field => { const value = options.fields[field] + if (value === undefined) return + if (Array.isArray(value)) { for (let i = 0; i < value.length; i++) { req.field(field + '[' + i + ']', value[i]) diff --git a/shared/utils/server/servers.ts b/shared/utils/server/servers.ts index bde7dd5c2..5288d253a 100644 --- a/shared/utils/server/servers.ts +++ b/shared/utils/server/servers.ts @@ -6,6 +6,7 @@ import { root, wait } from '../miscs/miscs' import { readdir, readFile } from 'fs-extra' import { existsSync } from 'fs' import { expect } from 'chai' +import { VideoChannel } from '../../models/videos' interface ServerInfo { app: ChildProcess, @@ -25,6 +26,7 @@ interface ServerInfo { } accessToken?: string + videoChannel?: VideoChannel video?: { id: number @@ -39,6 +41,8 @@ interface ServerInfo { id: number uuid: string } + + videos?: { id: number, uuid: string }[] } function flushAndRunMultipleServers (totalServers: number, configOverride?: Object) { diff --git a/shared/utils/users/users.ts b/shared/utils/users/users.ts index 7191b263e..e3c14a4a3 100644 --- a/shared/utils/users/users.ts +++ b/shared/utils/users/users.ts @@ -3,6 +3,7 @@ import { makePostBodyRequest, makePutBodyRequest, updateAvatarRequest } from '.. import { UserRole } from '../../index' import { NSFWPolicyType } from '../../models/videos/nsfw-policy.type' +import { ServerInfo, userLogin } from '..' function createUser ( url: string, @@ -32,6 +33,13 @@ function createUser ( .expect(specialStatus) } +async function generateUserAccessToken (server: ServerInfo, username: string) { + const password = 'my super password' + await createUser(server.url, server.accessToken, username, password) + + return userLogin(server, { username, password }) +} + function registerUser (url: string, username: string, password: string, specialStatus = 204) { const path = '/api/v1/users/register' const body = { @@ -300,5 +308,6 @@ export { resetPassword, updateMyAvatar, askSendVerifyEmail, + generateUserAccessToken, verifyEmail } 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