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