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