1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
3 import * as chai from 'chai'
5 import { VideoPrivacy } from '../../../../shared/models/videos/video-privacy.enum'
10 getVideosListWithToken,
12 setAccessTokensToServers,
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'
22 const expect = chai.expect
24 describe('Test video privacy', function () {
25 const servers: ServerInfo[] = []
26 let anotherUserToken: string
28 let privateVideoId: number
29 let privateVideoUUID: string
31 let internalVideoId: number
32 let internalVideoUUID: string
34 let unlistedVideoUUID: string
35 let nonFederatedUnlistedVideoUUID: string
39 const dontFederateUnlistedConfig = {
42 federate_unlisted: false
47 before(async function () {
51 servers.push(await flushAndRunServer(1, dontFederateUnlistedConfig))
52 servers.push(await flushAndRunServer(2))
54 // Get the access tokens
55 await setAccessTokensToServers(servers)
57 // Server 1 and server 2 follow each other
58 await doubleFollow(servers[0], servers[1])
61 it('Should upload a private and internal videos on server 1', async function () {
64 for (const privacy of [ VideoPrivacy.PRIVATE, VideoPrivacy.INTERNAL ]) {
65 const attributes = { privacy }
66 await uploadVideo(servers[0].url, servers[0].accessToken, attributes)
69 await waitJobs(servers)
72 it('Should not have these private and internal videos on server 2', async function () {
73 const res = await getVideosList(servers[1].url)
75 expect(res.body.total).to.equal(0)
76 expect(res.body.data).to.have.lengthOf(0)
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)
82 expect(res.body.total).to.equal(0)
83 expect(res.body.data).to.have.lengthOf(0)
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)
89 expect(res.body.total).to.equal(1)
90 expect(res.body.data).to.have.lengthOf(1)
92 expect(res.body.data[0].privacy.id).to.equal(VideoPrivacy.INTERNAL)
95 it('Should list my (private and internal) videos', async function () {
96 const res = await getMyVideos(servers[0].url, servers[0].accessToken, 0, 10)
98 expect(res.body.total).to.equal(2)
99 expect(res.body.data).to.have.lengthOf(2)
101 const videos: Video[] = res.body.data
103 const privateVideo = videos.find(v => v.privacy.id === VideoPrivacy.PRIVATE)
104 privateVideoId = privateVideo.id
105 privateVideoUUID = privateVideo.uuid
107 const internalVideo = videos.find(v => v.privacy.id === VideoPrivacy.INTERNAL)
108 internalVideoId = internalVideo.id
109 internalVideoUUID = internalVideo.uuid
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)
117 it('Should not be able to watch the private video with another user', async function () {
122 password: 'super password'
124 await createUser({ url: servers[0].url, accessToken: servers[0].accessToken, username: user.username, password: user.password })
126 anotherUserToken = await userLogin(servers[0], user)
127 await getVideoWithToken(servers[0].url, anotherUserToken, privateVideoUUID, 403)
130 it('Should be able to watch the internal video with another user', async function () {
131 await getVideoWithToken(servers[0].url, anotherUserToken, internalVideoUUID, 200)
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)
138 it('Should upload an unlisted video on server 2', async function () {
142 name: 'unlisted video',
143 privacy: VideoPrivacy.UNLISTED
145 await uploadVideo(servers[1].url, servers[1].accessToken, attributes)
147 // Server 2 has transcoding enabled
148 await waitJobs(servers)
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)
155 expect(res.body.total).to.equal(0)
156 expect(res.body.data).to.have.lengthOf(0)
160 it('Should list my (unlisted) videos', async function () {
161 const res = await getMyVideos(servers[1].url, servers[1].accessToken, 0, 1)
163 expect(res.body.total).to.equal(1)
164 expect(res.body.data).to.have.lengthOf(1)
166 unlistedVideoUUID = res.body.data[0].uuid
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)
173 expect(res.body.name).to.equal('unlisted video')
177 it('Should upload a non-federating unlisted video to server 1', async function () {
181 name: 'unlisted video',
182 privacy: VideoPrivacy.UNLISTED
184 await uploadVideo(servers[0].url, servers[0].accessToken, attributes)
186 await waitJobs(servers)
189 it('Should list my new unlisted video', async function () {
190 const res = await getMyVideos(servers[0].url, servers[0].accessToken, 0, 3)
192 expect(res.body.total).to.equal(3)
193 expect(res.body.data).to.have.lengthOf(3)
195 nonFederatedUnlistedVideoUUID = res.body.data[0].uuid
198 it('Should be able to get non-federated unlisted video from origin', async function () {
199 const res = await getVideo(servers[0].url, nonFederatedUnlistedVideoUUID)
201 expect(res.body.name).to.equal('unlisted video')
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)
208 it('Should update the private and internal videos to public on server 1', async function () {
215 name: 'private video becomes public',
216 privacy: VideoPrivacy.PUBLIC
219 await updateVideo(servers[0].url, servers[0].accessToken, privateVideoId, attribute)
224 name: 'internal video becomes public',
225 privacy: VideoPrivacy.PUBLIC
227 await updateVideo(servers[0].url, servers[0].accessToken, internalVideoId, attribute)
230 await waitJobs(servers)
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)
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')
243 expect(privateVideo).to.not.be.undefined
244 expect(internalVideo).to.not.be.undefined
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)
250 expect(privateVideo.privacy.id).to.equal(VideoPrivacy.PUBLIC)
251 expect(internalVideo.privacy.id).to.equal(VideoPrivacy.PUBLIC)
255 it('Should set these videos as private and internal', async function () {
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 })
261 await waitJobs(servers)
263 for (const server of servers) {
264 const res = await getVideosList(server.url)
266 expect(res.body.total).to.equal(0)
267 expect(res.body.data).to.have.lengthOf(0)
271 const res = await getMyVideos(servers[0].url, servers[0].accessToken, 0, 5)
272 const videos = res.body.data
274 expect(res.body.total).to.equal(3)
275 expect(videos).to.have.lengthOf(3)
277 const privateVideo = videos.find(v => v.name === 'private video becomes public')
278 const internalVideo = videos.find(v => v.name === 'internal video becomes public')
280 expect(privateVideo).to.not.be.undefined
281 expect(internalVideo).to.not.be.undefined
283 expect(privateVideo.privacy.id).to.equal(VideoPrivacy.INTERNAL)
284 expect(internalVideo.privacy.id).to.equal(VideoPrivacy.PRIVATE)
288 after(async function () {
289 await cleanupTests(servers)