diff options
Diffstat (limited to 'packages/tests/src/api/check-params/services.ts')
-rw-r--r-- | packages/tests/src/api/check-params/services.ts | 207 |
1 files changed, 207 insertions, 0 deletions
diff --git a/packages/tests/src/api/check-params/services.ts b/packages/tests/src/api/check-params/services.ts new file mode 100644 index 000000000..0b0466d84 --- /dev/null +++ b/packages/tests/src/api/check-params/services.ts | |||
@@ -0,0 +1,207 @@ | |||
1 | /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */ | ||
2 | |||
3 | import { | ||
4 | HttpStatusCode, | ||
5 | HttpStatusCodeType, | ||
6 | VideoCreateResult, | ||
7 | VideoPlaylistCreateResult, | ||
8 | VideoPlaylistPrivacy, | ||
9 | VideoPrivacy | ||
10 | } from '@peertube/peertube-models' | ||
11 | import { | ||
12 | cleanupTests, | ||
13 | createSingleServer, | ||
14 | makeGetRequest, | ||
15 | PeerTubeServer, | ||
16 | setAccessTokensToServers, | ||
17 | setDefaultVideoChannel | ||
18 | } from '@peertube/peertube-server-commands' | ||
19 | |||
20 | describe('Test services API validators', function () { | ||
21 | let server: PeerTubeServer | ||
22 | let playlistUUID: string | ||
23 | |||
24 | let privateVideo: VideoCreateResult | ||
25 | let unlistedVideo: VideoCreateResult | ||
26 | |||
27 | let privatePlaylist: VideoPlaylistCreateResult | ||
28 | let unlistedPlaylist: VideoPlaylistCreateResult | ||
29 | |||
30 | // --------------------------------------------------------------- | ||
31 | |||
32 | before(async function () { | ||
33 | this.timeout(60000) | ||
34 | |||
35 | server = await createSingleServer(1) | ||
36 | await setAccessTokensToServers([ server ]) | ||
37 | await setDefaultVideoChannel([ server ]) | ||
38 | |||
39 | server.store.videoCreated = await server.videos.upload({ attributes: { name: 'my super name' } }) | ||
40 | |||
41 | privateVideo = await server.videos.quickUpload({ name: 'private', privacy: VideoPrivacy.PRIVATE }) | ||
42 | unlistedVideo = await server.videos.quickUpload({ name: 'unlisted', privacy: VideoPrivacy.UNLISTED }) | ||
43 | |||
44 | { | ||
45 | const created = await server.playlists.create({ | ||
46 | attributes: { | ||
47 | displayName: 'super playlist', | ||
48 | privacy: VideoPlaylistPrivacy.PUBLIC, | ||
49 | videoChannelId: server.store.channel.id | ||
50 | } | ||
51 | }) | ||
52 | |||
53 | playlistUUID = created.uuid | ||
54 | |||
55 | privatePlaylist = await server.playlists.create({ | ||
56 | attributes: { | ||
57 | displayName: 'private', | ||
58 | privacy: VideoPlaylistPrivacy.PRIVATE, | ||
59 | videoChannelId: server.store.channel.id | ||
60 | } | ||
61 | }) | ||
62 | |||
63 | unlistedPlaylist = await server.playlists.create({ | ||
64 | attributes: { | ||
65 | displayName: 'unlisted', | ||
66 | privacy: VideoPlaylistPrivacy.UNLISTED, | ||
67 | videoChannelId: server.store.channel.id | ||
68 | } | ||
69 | }) | ||
70 | } | ||
71 | }) | ||
72 | |||
73 | describe('Test oEmbed API validators', function () { | ||
74 | |||
75 | it('Should fail with an invalid url', async function () { | ||
76 | const embedUrl = 'hello.com' | ||
77 | await checkParamEmbed(server, embedUrl) | ||
78 | }) | ||
79 | |||
80 | it('Should fail with an invalid host', async function () { | ||
81 | const embedUrl = 'http://hello.com/videos/watch/' + server.store.videoCreated.uuid | ||
82 | await checkParamEmbed(server, embedUrl) | ||
83 | }) | ||
84 | |||
85 | it('Should fail with an invalid element id', async function () { | ||
86 | const embedUrl = `${server.url}/videos/watch/blabla` | ||
87 | await checkParamEmbed(server, embedUrl) | ||
88 | }) | ||
89 | |||
90 | it('Should fail with an unknown element', async function () { | ||
91 | const embedUrl = `${server.url}/videos/watch/88fc0165-d1f0-4a35-a51a-3b47f668689c` | ||
92 | await checkParamEmbed(server, embedUrl, HttpStatusCode.NOT_FOUND_404) | ||
93 | }) | ||
94 | |||
95 | it('Should fail with an invalid path', async function () { | ||
96 | const embedUrl = `${server.url}/videos/watchs/${server.store.videoCreated.uuid}` | ||
97 | |||
98 | await checkParamEmbed(server, embedUrl) | ||
99 | }) | ||
100 | |||
101 | it('Should fail with an invalid max height', async function () { | ||
102 | const embedUrl = `${server.url}/videos/watch/${server.store.videoCreated.uuid}` | ||
103 | |||
104 | await checkParamEmbed(server, embedUrl, HttpStatusCode.BAD_REQUEST_400, { maxheight: 'hello' }) | ||
105 | }) | ||
106 | |||
107 | it('Should fail with an invalid max width', async function () { | ||
108 | const embedUrl = `${server.url}/videos/watch/${server.store.videoCreated.uuid}` | ||
109 | |||
110 | await checkParamEmbed(server, embedUrl, HttpStatusCode.BAD_REQUEST_400, { maxwidth: 'hello' }) | ||
111 | }) | ||
112 | |||
113 | it('Should fail with an invalid format', async function () { | ||
114 | const embedUrl = `${server.url}/videos/watch/${server.store.videoCreated.uuid}` | ||
115 | |||
116 | await checkParamEmbed(server, embedUrl, HttpStatusCode.BAD_REQUEST_400, { format: 'blabla' }) | ||
117 | }) | ||
118 | |||
119 | it('Should fail with a non supported format', async function () { | ||
120 | const embedUrl = `${server.url}/videos/watch/${server.store.videoCreated.uuid}` | ||
121 | |||
122 | await checkParamEmbed(server, embedUrl, HttpStatusCode.NOT_IMPLEMENTED_501, { format: 'xml' }) | ||
123 | }) | ||
124 | |||
125 | it('Should fail with a private video', async function () { | ||
126 | const embedUrl = `${server.url}/videos/watch/${privateVideo.uuid}` | ||
127 | |||
128 | await checkParamEmbed(server, embedUrl, HttpStatusCode.FORBIDDEN_403) | ||
129 | }) | ||
130 | |||
131 | it('Should fail with an unlisted video with the int id', async function () { | ||
132 | const embedUrl = `${server.url}/videos/watch/${unlistedVideo.id}` | ||
133 | |||
134 | await checkParamEmbed(server, embedUrl, HttpStatusCode.FORBIDDEN_403) | ||
135 | }) | ||
136 | |||
137 | it('Should succeed with an unlisted video using the uuid id', async function () { | ||
138 | for (const uuid of [ unlistedVideo.uuid, unlistedVideo.shortUUID ]) { | ||
139 | const embedUrl = `${server.url}/videos/watch/${uuid}` | ||
140 | |||
141 | await checkParamEmbed(server, embedUrl, HttpStatusCode.OK_200) | ||
142 | } | ||
143 | }) | ||
144 | |||
145 | it('Should fail with a private playlist', async function () { | ||
146 | const embedUrl = `${server.url}/videos/watch/playlist/${privatePlaylist.uuid}` | ||
147 | |||
148 | await checkParamEmbed(server, embedUrl, HttpStatusCode.FORBIDDEN_403) | ||
149 | }) | ||
150 | |||
151 | it('Should fail with an unlisted playlist using the int id', async function () { | ||
152 | const embedUrl = `${server.url}/videos/watch/playlist/${unlistedPlaylist.id}` | ||
153 | |||
154 | await checkParamEmbed(server, embedUrl, HttpStatusCode.FORBIDDEN_403) | ||
155 | }) | ||
156 | |||
157 | it('Should succeed with an unlisted playlist using the uuid id', async function () { | ||
158 | for (const uuid of [ unlistedPlaylist.uuid, unlistedPlaylist.shortUUID ]) { | ||
159 | const embedUrl = `${server.url}/videos/watch/playlist/${uuid}` | ||
160 | |||
161 | await checkParamEmbed(server, embedUrl, HttpStatusCode.OK_200) | ||
162 | } | ||
163 | }) | ||
164 | |||
165 | it('Should succeed with the correct params with a video', async function () { | ||
166 | const embedUrl = `${server.url}/videos/watch/${server.store.videoCreated.uuid}` | ||
167 | const query = { | ||
168 | format: 'json', | ||
169 | maxheight: 400, | ||
170 | maxwidth: 400 | ||
171 | } | ||
172 | |||
173 | await checkParamEmbed(server, embedUrl, HttpStatusCode.OK_200, query) | ||
174 | }) | ||
175 | |||
176 | it('Should succeed with the correct params with a playlist', async function () { | ||
177 | const embedUrl = `${server.url}/videos/watch/playlist/${playlistUUID}` | ||
178 | const query = { | ||
179 | format: 'json', | ||
180 | maxheight: 400, | ||
181 | maxwidth: 400 | ||
182 | } | ||
183 | |||
184 | await checkParamEmbed(server, embedUrl, HttpStatusCode.OK_200, query) | ||
185 | }) | ||
186 | }) | ||
187 | |||
188 | after(async function () { | ||
189 | await cleanupTests([ server ]) | ||
190 | }) | ||
191 | }) | ||
192 | |||
193 | function checkParamEmbed ( | ||
194 | server: PeerTubeServer, | ||
195 | embedUrl: string, | ||
196 | expectedStatus: HttpStatusCodeType = HttpStatusCode.BAD_REQUEST_400, | ||
197 | query = {} | ||
198 | ) { | ||
199 | const path = '/services/oembed' | ||
200 | |||
201 | return makeGetRequest({ | ||
202 | url: server.url, | ||
203 | path, | ||
204 | query: Object.assign(query, { url: embedUrl }), | ||
205 | expectedStatus | ||
206 | }) | ||
207 | } | ||