1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
4 import { HttpStatusCode, VideoCreateResult, VideoPlaylistCreateResult, VideoPlaylistPrivacy, VideoPrivacy } from '@shared/models'
10 setAccessTokensToServers,
11 setDefaultVideoChannel
12 } from '@shared/server-commands'
14 describe('Test services API validators', function () {
15 let server: PeerTubeServer
16 let playlistUUID: string
18 let privateVideo: VideoCreateResult
19 let unlistedVideo: VideoCreateResult
21 let privatePlaylist: VideoPlaylistCreateResult
22 let unlistedPlaylist: VideoPlaylistCreateResult
24 // ---------------------------------------------------------------
26 before(async function () {
29 server = await createSingleServer(1)
30 await setAccessTokensToServers([ server ])
31 await setDefaultVideoChannel([ server ])
33 server.store.videoCreated = await server.videos.upload({ attributes: { name: 'my super name' } })
35 privateVideo = await server.videos.quickUpload({ name: 'private', privacy: VideoPrivacy.PRIVATE })
36 unlistedVideo = await server.videos.quickUpload({ name: 'unlisted', privacy: VideoPrivacy.UNLISTED })
39 const created = await server.playlists.create({
41 displayName: 'super playlist',
42 privacy: VideoPlaylistPrivacy.PUBLIC,
43 videoChannelId: server.store.channel.id
47 playlistUUID = created.uuid
49 privatePlaylist = await server.playlists.create({
51 displayName: 'private',
52 privacy: VideoPlaylistPrivacy.PRIVATE,
53 videoChannelId: server.store.channel.id
57 unlistedPlaylist = await server.playlists.create({
59 displayName: 'unlisted',
60 privacy: VideoPlaylistPrivacy.UNLISTED,
61 videoChannelId: server.store.channel.id
67 describe('Test oEmbed API validators', function () {
69 it('Should fail with an invalid url', async function () {
70 const embedUrl = 'hello.com'
71 await checkParamEmbed(server, embedUrl)
74 it('Should fail with an invalid host', async function () {
75 const embedUrl = 'http://hello.com/videos/watch/' + server.store.videoCreated.uuid
76 await checkParamEmbed(server, embedUrl)
79 it('Should fail with an invalid element id', async function () {
80 const embedUrl = `http://localhost:${server.port}/videos/watch/blabla`
81 await checkParamEmbed(server, embedUrl)
84 it('Should fail with an unknown element', async function () {
85 const embedUrl = `http://localhost:${server.port}/videos/watch/88fc0165-d1f0-4a35-a51a-3b47f668689c`
86 await checkParamEmbed(server, embedUrl, HttpStatusCode.NOT_FOUND_404)
89 it('Should fail with an invalid path', async function () {
90 const embedUrl = `http://localhost:${server.port}/videos/watchs/${server.store.videoCreated.uuid}`
92 await checkParamEmbed(server, embedUrl)
95 it('Should fail with an invalid max height', async function () {
96 const embedUrl = `http://localhost:${server.port}/videos/watch/${server.store.videoCreated.uuid}`
98 await checkParamEmbed(server, embedUrl, HttpStatusCode.BAD_REQUEST_400, { maxheight: 'hello' })
101 it('Should fail with an invalid max width', async function () {
102 const embedUrl = `http://localhost:${server.port}/videos/watch/${server.store.videoCreated.uuid}`
104 await checkParamEmbed(server, embedUrl, HttpStatusCode.BAD_REQUEST_400, { maxwidth: 'hello' })
107 it('Should fail with an invalid format', async function () {
108 const embedUrl = `http://localhost:${server.port}/videos/watch/${server.store.videoCreated.uuid}`
110 await checkParamEmbed(server, embedUrl, HttpStatusCode.BAD_REQUEST_400, { format: 'blabla' })
113 it('Should fail with a non supported format', async function () {
114 const embedUrl = `http://localhost:${server.port}/videos/watch/${server.store.videoCreated.uuid}`
116 await checkParamEmbed(server, embedUrl, HttpStatusCode.NOT_IMPLEMENTED_501, { format: 'xml' })
119 it('Should fail with a private video', async function () {
120 const embedUrl = `http://localhost:${server.port}/videos/watch/${privateVideo.uuid}`
122 await checkParamEmbed(server, embedUrl, HttpStatusCode.FORBIDDEN_403)
125 it('Should fail with an unlisted video with the int id', async function () {
126 const embedUrl = `http://localhost:${server.port}/videos/watch/${unlistedVideo.id}`
128 await checkParamEmbed(server, embedUrl, HttpStatusCode.FORBIDDEN_403)
131 it('Should succeed with an unlisted video using the uuid id', async function () {
132 for (const uuid of [ unlistedVideo.uuid, unlistedVideo.shortUUID ]) {
133 const embedUrl = `http://localhost:${server.port}/videos/watch/${uuid}`
135 await checkParamEmbed(server, embedUrl, HttpStatusCode.OK_200)
139 it('Should fail with a private playlist', async function () {
140 const embedUrl = `http://localhost:${server.port}/videos/watch/playlist/${privatePlaylist.uuid}`
142 await checkParamEmbed(server, embedUrl, HttpStatusCode.FORBIDDEN_403)
145 it('Should fail with an unlisted playlist using the int id', async function () {
146 const embedUrl = `http://localhost:${server.port}/videos/watch/playlist/${unlistedPlaylist.id}`
148 await checkParamEmbed(server, embedUrl, HttpStatusCode.FORBIDDEN_403)
151 it('Should succeed with an unlisted playlist using the uuid id', async function () {
152 for (const uuid of [ unlistedPlaylist.uuid, unlistedPlaylist.shortUUID ]) {
153 const embedUrl = `http://localhost:${server.port}/videos/watch/playlist/${uuid}`
155 await checkParamEmbed(server, embedUrl, HttpStatusCode.OK_200)
159 it('Should succeed with the correct params with a video', async function () {
160 const embedUrl = `http://localhost:${server.port}/videos/watch/${server.store.videoCreated.uuid}`
167 await checkParamEmbed(server, embedUrl, HttpStatusCode.OK_200, query)
170 it('Should succeed with the correct params with a playlist', async function () {
171 const embedUrl = `http://localhost:${server.port}/videos/watch/playlist/${playlistUUID}`
178 await checkParamEmbed(server, embedUrl, HttpStatusCode.OK_200, query)
182 after(async function () {
183 await cleanupTests([ server ])
187 function checkParamEmbed (server: PeerTubeServer, embedUrl: string, expectedStatus = HttpStatusCode.BAD_REQUEST_400, query = {}) {
188 const path = '/services/oembed'
190 return makeGetRequest({
193 query: Object.assign(query, { url: embedUrl }),