]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/tests/api/videos/video-privacy.ts
Adapt CLI to new commands
[github/Chocobozzz/PeerTube.git] / server / tests / api / videos / video-privacy.ts
CommitLineData
a1587156 1/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
11474c3c 2
975e6e0e 3import 'mocha'
d4a8e7a6 4import * as chai from 'chai'
41d1d075 5import { HttpStatusCode } from '@shared/core-utils'
11474c3c 6import {
7c3b7976 7 cleanupTests,
41d1d075
C
8 createUser,
9 doubleFollow,
3092e9bb 10 flushAndRunServer,
41d1d075
C
11 getMyVideos,
12 getVideo,
a1587156
C
13 getVideosList,
14 getVideosListWithToken,
41d1d075 15 getVideoWithToken,
975e6e0e 16 ServerInfo,
11474c3c 17 setAccessTokensToServers,
41d1d075
C
18 updateVideo,
19 uploadVideo,
20 waitJobs
21} from '@shared/extra-utils'
22import { Video, VideoCreateResult, VideoPrivacy } from '@shared/models'
975e6e0e
C
23
24const expect = chai.expect
11474c3c
C
25
26describe('Test video privacy', function () {
3092e9bb 27 const servers: ServerInfo[] = []
22a73cb8
C
28 let anotherUserToken: string
29
c49db162
C
30 let privateVideoId: number
31 let privateVideoUUID: string
22a73cb8
C
32
33 let internalVideoId: number
34 let internalVideoUUID: string
35
d4a8e7a6 36 let unlistedVideo: VideoCreateResult
3092e9bb 37 let nonFederatedUnlistedVideoUUID: string
22a73cb8 38
c49db162 39 let now: number
11474c3c 40
3092e9bb
LB
41 const dontFederateUnlistedConfig = {
42 federation: {
43 videos: {
44 federate_unlisted: false
45 }
46 }
47 }
48
11474c3c 49 before(async function () {
572f8d3d 50 this.timeout(50000)
11474c3c
C
51
52 // Run servers
3092e9bb
LB
53 servers.push(await flushAndRunServer(1, dontFederateUnlistedConfig))
54 servers.push(await flushAndRunServer(2))
11474c3c
C
55
56 // Get the access tokens
57 await setAccessTokensToServers(servers)
58
975e6e0e
C
59 // Server 1 and server 2 follow each other
60 await doubleFollow(servers[0], servers[1])
11474c3c
C
61 })
62
d4a8e7a6 63 describe('Private and internal videos', function () {
11474c3c 64
d4a8e7a6
C
65 it('Should upload a private and internal videos on server 1', async function () {
66 this.timeout(10000)
11474c3c 67
d4a8e7a6
C
68 for (const privacy of [ VideoPrivacy.PRIVATE, VideoPrivacy.INTERNAL ]) {
69 const attributes = { privacy }
70 await uploadVideo(servers[0].url, servers[0].accessToken, attributes)
71 }
11474c3c 72
d4a8e7a6
C
73 await waitJobs(servers)
74 })
11474c3c 75
d4a8e7a6
C
76 it('Should not have these private and internal videos on server 2', async function () {
77 const res = await getVideosList(servers[1].url)
11474c3c 78
d4a8e7a6
C
79 expect(res.body.total).to.equal(0)
80 expect(res.body.data).to.have.lengthOf(0)
81 })
22a73cb8 82
d4a8e7a6
C
83 it('Should not list the private and internal videos for an unauthenticated user on server 1', async function () {
84 const res = await getVideosList(servers[0].url)
85
86 expect(res.body.total).to.equal(0)
87 expect(res.body.data).to.have.lengthOf(0)
88 })
22a73cb8 89
d4a8e7a6
C
90 it('Should not list the private video and list the internal video for an authenticated user on server 1', async function () {
91 const res = await getVideosListWithToken(servers[0].url, servers[0].accessToken)
11474c3c 92
d4a8e7a6
C
93 expect(res.body.total).to.equal(1)
94 expect(res.body.data).to.have.lengthOf(1)
11474c3c 95
d4a8e7a6
C
96 expect(res.body.data[0].privacy.id).to.equal(VideoPrivacy.INTERNAL)
97 })
22a73cb8 98
d4a8e7a6
C
99 it('Should list my (private and internal) videos', async function () {
100 const res = await getMyVideos(servers[0].url, servers[0].accessToken, 0, 10)
22a73cb8 101
d4a8e7a6
C
102 expect(res.body.total).to.equal(2)
103 expect(res.body.data).to.have.lengthOf(2)
22a73cb8 104
d4a8e7a6 105 const videos: Video[] = res.body.data
22a73cb8 106
d4a8e7a6
C
107 const privateVideo = videos.find(v => v.privacy.id === VideoPrivacy.PRIVATE)
108 privateVideoId = privateVideo.id
109 privateVideoUUID = privateVideo.uuid
22a73cb8 110
d4a8e7a6
C
111 const internalVideo = videos.find(v => v.privacy.id === VideoPrivacy.INTERNAL)
112 internalVideoId = internalVideo.id
113 internalVideoUUID = internalVideo.uuid
114 })
11474c3c 115
d4a8e7a6
C
116 it('Should not be able to watch the private/internal video with non authenticated user', async function () {
117 await getVideo(servers[0].url, privateVideoUUID, HttpStatusCode.UNAUTHORIZED_401)
118 await getVideo(servers[0].url, internalVideoUUID, HttpStatusCode.UNAUTHORIZED_401)
119 })
11474c3c 120
d4a8e7a6
C
121 it('Should not be able to watch the private video with another user', async function () {
122 this.timeout(10000)
77a87fec 123
d4a8e7a6
C
124 const user = {
125 username: 'hello',
126 password: 'super password'
127 }
128 await createUser({ url: servers[0].url, accessToken: servers[0].accessToken, username: user.username, password: user.password })
11474c3c 129
41d1d075 130 anotherUserToken = await servers[0].loginCommand.getAccessToken(user)
d4a8e7a6
C
131 await getVideoWithToken(servers[0].url, anotherUserToken, privateVideoUUID, HttpStatusCode.FORBIDDEN_403)
132 })
11474c3c 133
d4a8e7a6
C
134 it('Should be able to watch the internal video with another user', async function () {
135 await getVideoWithToken(servers[0].url, anotherUserToken, internalVideoUUID, HttpStatusCode.OK_200)
136 })
22a73cb8 137
d4a8e7a6
C
138 it('Should be able to watch the private video with the correct user', async function () {
139 await getVideoWithToken(servers[0].url, servers[0].accessToken, privateVideoUUID, HttpStatusCode.OK_200)
140 })
11474c3c
C
141 })
142
d4a8e7a6 143 describe('Unlisted videos', function () {
11474c3c 144
d4a8e7a6
C
145 it('Should upload an unlisted video on server 2', async function () {
146 this.timeout(60000)
11474c3c 147
d4a8e7a6
C
148 const attributes = {
149 name: 'unlisted video',
150 privacy: VideoPrivacy.UNLISTED
151 }
152 await uploadVideo(servers[1].url, servers[1].accessToken, attributes)
11474c3c 153
d4a8e7a6
C
154 // Server 2 has transcoding enabled
155 await waitJobs(servers)
156 })
11474c3c 157
d4a8e7a6
C
158 it('Should not have this unlisted video listed on server 1 and 2', async function () {
159 for (const server of servers) {
160 const res = await getVideosList(server.url)
11474c3c 161
d4a8e7a6
C
162 expect(res.body.total).to.equal(0)
163 expect(res.body.data).to.have.lengthOf(0)
164 }
165 })
11474c3c 166
d4a8e7a6
C
167 it('Should list my (unlisted) videos', async function () {
168 const res = await getMyVideos(servers[1].url, servers[1].accessToken, 0, 1)
11474c3c 169
d4a8e7a6
C
170 expect(res.body.total).to.equal(1)
171 expect(res.body.data).to.have.lengthOf(1)
11474c3c 172
d4a8e7a6
C
173 unlistedVideo = res.body.data[0]
174 })
11474c3c 175
d4a8e7a6
C
176 it('Should not be able to get this unlisted video using its id', async function () {
177 await getVideo(servers[1].url, unlistedVideo.id, 404)
178 })
11474c3c 179
d4a8e7a6
C
180 it('Should be able to get this unlisted video using its uuid/shortUUID', async function () {
181 for (const server of servers) {
182 for (const id of [ unlistedVideo.uuid, unlistedVideo.shortUUID ]) {
183 const res = await getVideo(server.url, id)
3092e9bb 184
d4a8e7a6
C
185 expect(res.body.name).to.equal('unlisted video')
186 }
187 }
188 })
3092e9bb 189
d4a8e7a6
C
190 it('Should upload a non-federating unlisted video to server 1', async function () {
191 this.timeout(30000)
192
193 const attributes = {
194 name: 'unlisted video',
195 privacy: VideoPrivacy.UNLISTED
196 }
197 await uploadVideo(servers[0].url, servers[0].accessToken, attributes)
3092e9bb 198
d4a8e7a6
C
199 await waitJobs(servers)
200 })
3092e9bb 201
d4a8e7a6
C
202 it('Should list my new unlisted video', async function () {
203 const res = await getMyVideos(servers[0].url, servers[0].accessToken, 0, 3)
3092e9bb 204
d4a8e7a6
C
205 expect(res.body.total).to.equal(3)
206 expect(res.body.data).to.have.lengthOf(3)
3092e9bb 207
d4a8e7a6
C
208 nonFederatedUnlistedVideoUUID = res.body.data[0].uuid
209 })
3092e9bb 210
d4a8e7a6
C
211 it('Should be able to get non-federated unlisted video from origin', async function () {
212 const res = await getVideo(servers[0].url, nonFederatedUnlistedVideoUUID)
3092e9bb 213
d4a8e7a6
C
214 expect(res.body.name).to.equal('unlisted video')
215 })
216
217 it('Should not be able to get non-federated unlisted video from federated server', async function () {
218 await getVideo(servers[1].url, nonFederatedUnlistedVideoUUID, HttpStatusCode.NOT_FOUND_404)
219 })
3092e9bb
LB
220 })
221
d4a8e7a6 222 describe('Privacy update', function () {
11474c3c 223
d4a8e7a6
C
224 it('Should update the private and internal videos to public on server 1', async function () {
225 this.timeout(10000)
22a73cb8 226
d4a8e7a6 227 now = Date.now()
22a73cb8 228
d4a8e7a6
C
229 {
230 const attribute = {
231 name: 'private video becomes public',
232 privacy: VideoPrivacy.PUBLIC
233 }
11474c3c 234
d4a8e7a6 235 await updateVideo(servers[0].url, servers[0].accessToken, privateVideoId, attribute)
22a73cb8 236 }
11474c3c 237
d4a8e7a6
C
238 {
239 const attribute = {
240 name: 'internal video becomes public',
241 privacy: VideoPrivacy.PUBLIC
242 }
243 await updateVideo(servers[0].url, servers[0].accessToken, internalVideoId, attribute)
244 }
11474c3c 245
d4a8e7a6
C
246 await waitJobs(servers)
247 })
22a73cb8 248
d4a8e7a6
C
249 it('Should have this new public video listed on server 1 and 2', async function () {
250 for (const server of servers) {
251 const res = await getVideosList(server.url)
252 expect(res.body.total).to.equal(2)
253 expect(res.body.data).to.have.lengthOf(2)
22a73cb8 254
d4a8e7a6
C
255 const videos: Video[] = res.body.data
256 const privateVideo = videos.find(v => v.name === 'private video becomes public')
257 const internalVideo = videos.find(v => v.name === 'internal video becomes public')
22a73cb8 258
d4a8e7a6
C
259 expect(privateVideo).to.not.be.undefined
260 expect(internalVideo).to.not.be.undefined
11474c3c 261
d4a8e7a6
C
262 expect(new Date(privateVideo.publishedAt).getTime()).to.be.at.least(now)
263 // We don't change the publish date of internal videos
264 expect(new Date(internalVideo.publishedAt).getTime()).to.be.below(now)
11474c3c 265
d4a8e7a6
C
266 expect(privateVideo.privacy.id).to.equal(VideoPrivacy.PUBLIC)
267 expect(internalVideo.privacy.id).to.equal(VideoPrivacy.PUBLIC)
268 }
269 })
46a6db24 270
d4a8e7a6
C
271 it('Should set these videos as private and internal', async function () {
272 this.timeout(10000)
46a6db24 273
d4a8e7a6
C
274 await updateVideo(servers[0].url, servers[0].accessToken, internalVideoId, { privacy: VideoPrivacy.PRIVATE })
275 await updateVideo(servers[0].url, servers[0].accessToken, privateVideoId, { privacy: VideoPrivacy.INTERNAL })
46a6db24 276
d4a8e7a6 277 await waitJobs(servers)
46a6db24 278
d4a8e7a6
C
279 for (const server of servers) {
280 const res = await getVideosList(server.url)
281
282 expect(res.body.total).to.equal(0)
283 expect(res.body.data).to.have.lengthOf(0)
284 }
46a6db24 285
d4a8e7a6
C
286 {
287 const res = await getMyVideos(servers[0].url, servers[0].accessToken, 0, 5)
288 const videos = res.body.data
22a73cb8 289
d4a8e7a6
C
290 expect(res.body.total).to.equal(3)
291 expect(videos).to.have.lengthOf(3)
22a73cb8 292
d4a8e7a6
C
293 const privateVideo = videos.find(v => v.name === 'private video becomes public')
294 const internalVideo = videos.find(v => v.name === 'internal video becomes public')
22a73cb8 295
d4a8e7a6
C
296 expect(privateVideo).to.not.be.undefined
297 expect(internalVideo).to.not.be.undefined
46a6db24 298
d4a8e7a6
C
299 expect(privateVideo.privacy.id).to.equal(VideoPrivacy.INTERNAL)
300 expect(internalVideo.privacy.id).to.equal(VideoPrivacy.PRIVATE)
301 }
302 })
46a6db24
C
303 })
304
7c3b7976
C
305 after(async function () {
306 await cleanupTests(servers)
11474c3c
C
307 })
308})