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