]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/tests/api/check-params/services.ts
Allow oembed to fetch unlisted videos
[github/Chocobozzz/PeerTube.git] / server / tests / api / check-params / services.ts
1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
2
3 import 'mocha'
4 import { HttpStatusCode, VideoCreateResult, VideoPlaylistCreateResult, VideoPlaylistPrivacy, VideoPrivacy } from '@shared/models'
5 import {
6 cleanupTests,
7 createSingleServer,
8 makeGetRequest,
9 PeerTubeServer,
10 setAccessTokensToServers,
11 setDefaultVideoChannel
12 } from '@shared/server-commands'
13
14 describe('Test services API validators', function () {
15 let server: PeerTubeServer
16 let playlistUUID: string
17
18 let privateVideo: VideoCreateResult
19 let unlistedVideo: VideoCreateResult
20
21 let privatePlaylist: VideoPlaylistCreateResult
22 let unlistedPlaylist: VideoPlaylistCreateResult
23
24 // ---------------------------------------------------------------
25
26 before(async function () {
27 this.timeout(60000)
28
29 server = await createSingleServer(1)
30 await setAccessTokensToServers([ server ])
31 await setDefaultVideoChannel([ server ])
32
33 server.store.videoCreated = await server.videos.upload({ attributes: { name: 'my super name' } })
34
35 privateVideo = await server.videos.quickUpload({ name: 'private', privacy: VideoPrivacy.PRIVATE })
36 unlistedVideo = await server.videos.quickUpload({ name: 'unlisted', privacy: VideoPrivacy.UNLISTED })
37
38 {
39 const created = await server.playlists.create({
40 attributes: {
41 displayName: 'super playlist',
42 privacy: VideoPlaylistPrivacy.PUBLIC,
43 videoChannelId: server.store.channel.id
44 }
45 })
46
47 playlistUUID = created.uuid
48
49 privatePlaylist = await server.playlists.create({
50 attributes: {
51 displayName: 'private',
52 privacy: VideoPlaylistPrivacy.PRIVATE,
53 videoChannelId: server.store.channel.id
54 }
55 })
56
57 unlistedPlaylist = await server.playlists.create({
58 attributes: {
59 displayName: 'unlisted',
60 privacy: VideoPlaylistPrivacy.UNLISTED,
61 videoChannelId: server.store.channel.id
62 }
63 })
64 }
65 })
66
67 describe('Test oEmbed API validators', function () {
68
69 it('Should fail with an invalid url', async function () {
70 const embedUrl = 'hello.com'
71 await checkParamEmbed(server, embedUrl)
72 })
73
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)
77 })
78
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)
82 })
83
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)
87 })
88
89 it('Should fail with an invalid path', async function () {
90 const embedUrl = `http://localhost:${server.port}/videos/watchs/${server.store.videoCreated.uuid}`
91
92 await checkParamEmbed(server, embedUrl)
93 })
94
95 it('Should fail with an invalid max height', async function () {
96 const embedUrl = `http://localhost:${server.port}/videos/watch/${server.store.videoCreated.uuid}`
97
98 await checkParamEmbed(server, embedUrl, HttpStatusCode.BAD_REQUEST_400, { maxheight: 'hello' })
99 })
100
101 it('Should fail with an invalid max width', async function () {
102 const embedUrl = `http://localhost:${server.port}/videos/watch/${server.store.videoCreated.uuid}`
103
104 await checkParamEmbed(server, embedUrl, HttpStatusCode.BAD_REQUEST_400, { maxwidth: 'hello' })
105 })
106
107 it('Should fail with an invalid format', async function () {
108 const embedUrl = `http://localhost:${server.port}/videos/watch/${server.store.videoCreated.uuid}`
109
110 await checkParamEmbed(server, embedUrl, HttpStatusCode.BAD_REQUEST_400, { format: 'blabla' })
111 })
112
113 it('Should fail with a non supported format', async function () {
114 const embedUrl = `http://localhost:${server.port}/videos/watch/${server.store.videoCreated.uuid}`
115
116 await checkParamEmbed(server, embedUrl, HttpStatusCode.NOT_IMPLEMENTED_501, { format: 'xml' })
117 })
118
119 it('Should fail with a private video', async function () {
120 const embedUrl = `http://localhost:${server.port}/videos/watch/${privateVideo.uuid}`
121
122 await checkParamEmbed(server, embedUrl, HttpStatusCode.FORBIDDEN_403)
123 })
124
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}`
127
128 await checkParamEmbed(server, embedUrl, HttpStatusCode.FORBIDDEN_403)
129 })
130
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}`
134
135 await checkParamEmbed(server, embedUrl, HttpStatusCode.OK_200)
136 }
137 })
138
139 it('Should fail with a private playlist', async function () {
140 const embedUrl = `http://localhost:${server.port}/videos/watch/playlist/${privatePlaylist.uuid}`
141
142 await checkParamEmbed(server, embedUrl, HttpStatusCode.FORBIDDEN_403)
143 })
144
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}`
147
148 await checkParamEmbed(server, embedUrl, HttpStatusCode.FORBIDDEN_403)
149 })
150
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}`
154
155 await checkParamEmbed(server, embedUrl, HttpStatusCode.OK_200)
156 }
157 })
158
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}`
161 const query = {
162 format: 'json',
163 maxheight: 400,
164 maxwidth: 400
165 }
166
167 await checkParamEmbed(server, embedUrl, HttpStatusCode.OK_200, query)
168 })
169
170 it('Should succeed with the correct params with a playlist', async function () {
171 const embedUrl = `http://localhost:${server.port}/videos/watch/playlist/${playlistUUID}`
172 const query = {
173 format: 'json',
174 maxheight: 400,
175 maxwidth: 400
176 }
177
178 await checkParamEmbed(server, embedUrl, HttpStatusCode.OK_200, query)
179 })
180 })
181
182 after(async function () {
183 await cleanupTests([ server ])
184 })
185 })
186
187 function checkParamEmbed (server: PeerTubeServer, embedUrl: string, expectedStatus = HttpStatusCode.BAD_REQUEST_400, query = {}) {
188 const path = '/services/oembed'
189
190 return makeGetRequest({
191 url: server.url,
192 path,
193 query: Object.assign(query, { url: embedUrl }),
194 expectedStatus
195 })
196 }