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