1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
3 import { HttpStatusCode, VideoCreateResult, VideoPlaylistCreateResult, VideoPlaylistPrivacy, VideoPrivacy } from '@shared/models'
9 setAccessTokensToServers,
10 setDefaultVideoChannel
11 } from '@shared/server-commands'
13 describe('Test services API validators', function () {
14 let server: PeerTubeServer
15 let playlistUUID: string
17 let privateVideo: VideoCreateResult
18 let unlistedVideo: VideoCreateResult
20 let privatePlaylist: VideoPlaylistCreateResult
21 let unlistedPlaylist: VideoPlaylistCreateResult
23 // ---------------------------------------------------------------
25 before(async function () {
28 server = await createSingleServer(1)
29 await setAccessTokensToServers([ server ])
30 await setDefaultVideoChannel([ server ])
32 server.store.videoCreated = await server.videos.upload({ attributes: { name: 'my super name' } })
34 privateVideo = await server.videos.quickUpload({ name: 'private', privacy: VideoPrivacy.PRIVATE })
35 unlistedVideo = await server.videos.quickUpload({ name: 'unlisted', privacy: VideoPrivacy.UNLISTED })
38 const created = await server.playlists.create({
40 displayName: 'super playlist',
41 privacy: VideoPlaylistPrivacy.PUBLIC,
42 videoChannelId: server.store.channel.id
46 playlistUUID = created.uuid
48 privatePlaylist = await server.playlists.create({
50 displayName: 'private',
51 privacy: VideoPlaylistPrivacy.PRIVATE,
52 videoChannelId: server.store.channel.id
56 unlistedPlaylist = await server.playlists.create({
58 displayName: 'unlisted',
59 privacy: VideoPlaylistPrivacy.UNLISTED,
60 videoChannelId: server.store.channel.id
66 describe('Test oEmbed API validators', function () {
68 it('Should fail with an invalid url', async function () {
69 const embedUrl = 'hello.com'
70 await checkParamEmbed(server, embedUrl)
73 it('Should fail with an invalid host', async function () {
74 const embedUrl = 'http://hello.com/videos/watch/' + server.store.videoCreated.uuid
75 await checkParamEmbed(server, embedUrl)
78 it('Should fail with an invalid element id', async function () {
79 const embedUrl = `http://localhost:${server.port}/videos/watch/blabla`
80 await checkParamEmbed(server, embedUrl)
83 it('Should fail with an unknown element', async function () {
84 const embedUrl = `http://localhost:${server.port}/videos/watch/88fc0165-d1f0-4a35-a51a-3b47f668689c`
85 await checkParamEmbed(server, embedUrl, HttpStatusCode.NOT_FOUND_404)
88 it('Should fail with an invalid path', async function () {
89 const embedUrl = `http://localhost:${server.port}/videos/watchs/${server.store.videoCreated.uuid}`
91 await checkParamEmbed(server, embedUrl)
94 it('Should fail with an invalid max height', async function () {
95 const embedUrl = `http://localhost:${server.port}/videos/watch/${server.store.videoCreated.uuid}`
97 await checkParamEmbed(server, embedUrl, HttpStatusCode.BAD_REQUEST_400, { maxheight: 'hello' })
100 it('Should fail with an invalid max width', async function () {
101 const embedUrl = `http://localhost:${server.port}/videos/watch/${server.store.videoCreated.uuid}`
103 await checkParamEmbed(server, embedUrl, HttpStatusCode.BAD_REQUEST_400, { maxwidth: 'hello' })
106 it('Should fail with an invalid format', async function () {
107 const embedUrl = `http://localhost:${server.port}/videos/watch/${server.store.videoCreated.uuid}`
109 await checkParamEmbed(server, embedUrl, HttpStatusCode.BAD_REQUEST_400, { format: 'blabla' })
112 it('Should fail with a non supported format', async function () {
113 const embedUrl = `http://localhost:${server.port}/videos/watch/${server.store.videoCreated.uuid}`
115 await checkParamEmbed(server, embedUrl, HttpStatusCode.NOT_IMPLEMENTED_501, { format: 'xml' })
118 it('Should fail with a private video', async function () {
119 const embedUrl = `http://localhost:${server.port}/videos/watch/${privateVideo.uuid}`
121 await checkParamEmbed(server, embedUrl, HttpStatusCode.FORBIDDEN_403)
124 it('Should fail with an unlisted video with the int id', async function () {
125 const embedUrl = `http://localhost:${server.port}/videos/watch/${unlistedVideo.id}`
127 await checkParamEmbed(server, embedUrl, HttpStatusCode.FORBIDDEN_403)
130 it('Should succeed with an unlisted video using the uuid id', async function () {
131 for (const uuid of [ unlistedVideo.uuid, unlistedVideo.shortUUID ]) {
132 const embedUrl = `http://localhost:${server.port}/videos/watch/${uuid}`
134 await checkParamEmbed(server, embedUrl, HttpStatusCode.OK_200)
138 it('Should fail with a private playlist', async function () {
139 const embedUrl = `http://localhost:${server.port}/videos/watch/playlist/${privatePlaylist.uuid}`
141 await checkParamEmbed(server, embedUrl, HttpStatusCode.FORBIDDEN_403)
144 it('Should fail with an unlisted playlist using the int id', async function () {
145 const embedUrl = `http://localhost:${server.port}/videos/watch/playlist/${unlistedPlaylist.id}`
147 await checkParamEmbed(server, embedUrl, HttpStatusCode.FORBIDDEN_403)
150 it('Should succeed with an unlisted playlist using the uuid id', async function () {
151 for (const uuid of [ unlistedPlaylist.uuid, unlistedPlaylist.shortUUID ]) {
152 const embedUrl = `http://localhost:${server.port}/videos/watch/playlist/${uuid}`
154 await checkParamEmbed(server, embedUrl, HttpStatusCode.OK_200)
158 it('Should succeed with the correct params with a video', async function () {
159 const embedUrl = `http://localhost:${server.port}/videos/watch/${server.store.videoCreated.uuid}`
166 await checkParamEmbed(server, embedUrl, HttpStatusCode.OK_200, query)
169 it('Should succeed with the correct params with a playlist', async function () {
170 const embedUrl = `http://localhost:${server.port}/videos/watch/playlist/${playlistUUID}`
177 await checkParamEmbed(server, embedUrl, HttpStatusCode.OK_200, query)
181 after(async function () {
182 await cleanupTests([ server ])
186 function checkParamEmbed (server: PeerTubeServer, embedUrl: string, expectedStatus = HttpStatusCode.BAD_REQUEST_400, query = {}) {
187 const path = '/services/oembed'
189 return makeGetRequest({
192 query: Object.assign(query, { url: embedUrl }),