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'
21 import { HttpStatusCode } from '@shared/core-utils/miscs/http-error-codes'
23 const expect = chai.expect
25 describe('Test video privacy', function () {
26 const servers: ServerInfo[] = []
27 let anotherUserToken: string
29 let privateVideoId: number
30 let privateVideoUUID: string
32 let internalVideoId: number
33 let internalVideoUUID: string
35 let unlistedVideoUUID: string
36 let nonFederatedUnlistedVideoUUID: string
40 const dontFederateUnlistedConfig = {
43 federate_unlisted: false
48 before(async function () {
52 servers.push(await flushAndRunServer(1, dontFederateUnlistedConfig))
53 servers.push(await flushAndRunServer(2))
55 // Get the access tokens
56 await setAccessTokensToServers(servers)
58 // Server 1 and server 2 follow each other
59 await doubleFollow(servers[0], servers[1])
62 it('Should upload a private and internal videos on server 1', async function () {
65 for (const privacy of [ VideoPrivacy.PRIVATE, VideoPrivacy.INTERNAL ]) {
66 const attributes = { privacy }
67 await uploadVideo(servers[0].url, servers[0].accessToken, attributes)
70 await waitJobs(servers)
73 it('Should not have these private and internal videos on server 2', async function () {
74 const res = await getVideosList(servers[1].url)
76 expect(res.body.total).to.equal(0)
77 expect(res.body.data).to.have.lengthOf(0)
80 it('Should not list the private and internal videos for an unauthenticated user on server 1', async function () {
81 const res = await getVideosList(servers[0].url)
83 expect(res.body.total).to.equal(0)
84 expect(res.body.data).to.have.lengthOf(0)
87 it('Should not list the private video and list the internal video for an authenticated user on server 1', async function () {
88 const res = await getVideosListWithToken(servers[0].url, servers[0].accessToken)
90 expect(res.body.total).to.equal(1)
91 expect(res.body.data).to.have.lengthOf(1)
93 expect(res.body.data[0].privacy.id).to.equal(VideoPrivacy.INTERNAL)
96 it('Should list my (private and internal) videos', async function () {
97 const res = await getMyVideos(servers[0].url, servers[0].accessToken, 0, 10)
99 expect(res.body.total).to.equal(2)
100 expect(res.body.data).to.have.lengthOf(2)
102 const videos: Video[] = res.body.data
104 const privateVideo = videos.find(v => v.privacy.id === VideoPrivacy.PRIVATE)
105 privateVideoId = privateVideo.id
106 privateVideoUUID = privateVideo.uuid
108 const internalVideo = videos.find(v => v.privacy.id === VideoPrivacy.INTERNAL)
109 internalVideoId = internalVideo.id
110 internalVideoUUID = internalVideo.uuid
113 it('Should not be able to watch the private/internal video with non authenticated user', async function () {
114 await getVideo(servers[0].url, privateVideoUUID, HttpStatusCode.UNAUTHORIZED_401)
115 await getVideo(servers[0].url, internalVideoUUID, HttpStatusCode.UNAUTHORIZED_401)
118 it('Should not be able to watch the private video with another user', async function () {
123 password: 'super password'
125 await createUser({ url: servers[0].url, accessToken: servers[0].accessToken, username: user.username, password: user.password })
127 anotherUserToken = await userLogin(servers[0], user)
128 await getVideoWithToken(servers[0].url, anotherUserToken, privateVideoUUID, HttpStatusCode.FORBIDDEN_403)
131 it('Should be able to watch the internal video with another user', async function () {
132 await getVideoWithToken(servers[0].url, anotherUserToken, internalVideoUUID, HttpStatusCode.OK_200)
135 it('Should be able to watch the private video with the correct user', async function () {
136 await getVideoWithToken(servers[0].url, servers[0].accessToken, privateVideoUUID, HttpStatusCode.OK_200)
139 it('Should upload an unlisted video on server 2', async function () {
143 name: 'unlisted video',
144 privacy: VideoPrivacy.UNLISTED
146 await uploadVideo(servers[1].url, servers[1].accessToken, attributes)
148 // Server 2 has transcoding enabled
149 await waitJobs(servers)
152 it('Should not have this unlisted video listed on server 1 and 2', async function () {
153 for (const server of servers) {
154 const res = await getVideosList(server.url)
156 expect(res.body.total).to.equal(0)
157 expect(res.body.data).to.have.lengthOf(0)
161 it('Should list my (unlisted) videos', async function () {
162 const res = await getMyVideos(servers[1].url, servers[1].accessToken, 0, 1)
164 expect(res.body.total).to.equal(1)
165 expect(res.body.data).to.have.lengthOf(1)
167 unlistedVideoUUID = res.body.data[0].uuid
170 it('Should be able to get this unlisted video', async function () {
171 for (const server of servers) {
172 const res = await getVideo(server.url, unlistedVideoUUID)
174 expect(res.body.name).to.equal('unlisted video')
178 it('Should upload a non-federating unlisted video to server 1', async function () {
182 name: 'unlisted video',
183 privacy: VideoPrivacy.UNLISTED
185 await uploadVideo(servers[0].url, servers[0].accessToken, attributes)
187 await waitJobs(servers)
190 it('Should list my new unlisted video', async function () {
191 const res = await getMyVideos(servers[0].url, servers[0].accessToken, 0, 3)
193 expect(res.body.total).to.equal(3)
194 expect(res.body.data).to.have.lengthOf(3)
196 nonFederatedUnlistedVideoUUID = res.body.data[0].uuid
199 it('Should be able to get non-federated unlisted video from origin', async function () {
200 const res = await getVideo(servers[0].url, nonFederatedUnlistedVideoUUID)
202 expect(res.body.name).to.equal('unlisted video')
205 it('Should not be able to get non-federated unlisted video from federated server', async function () {
206 await getVideo(servers[1].url, nonFederatedUnlistedVideoUUID, HttpStatusCode.NOT_FOUND_404)
209 it('Should update the private and internal videos to public on server 1', async function () {
216 name: 'private video becomes public',
217 privacy: VideoPrivacy.PUBLIC
220 await updateVideo(servers[0].url, servers[0].accessToken, privateVideoId, attribute)
225 name: 'internal video becomes public',
226 privacy: VideoPrivacy.PUBLIC
228 await updateVideo(servers[0].url, servers[0].accessToken, internalVideoId, attribute)
231 await waitJobs(servers)
234 it('Should have this new public video listed on server 1 and 2', async function () {
235 for (const server of servers) {
236 const res = await getVideosList(server.url)
237 expect(res.body.total).to.equal(2)
238 expect(res.body.data).to.have.lengthOf(2)
240 const videos: Video[] = res.body.data
241 const privateVideo = videos.find(v => v.name === 'private video becomes public')
242 const internalVideo = videos.find(v => v.name === 'internal video becomes public')
244 expect(privateVideo).to.not.be.undefined
245 expect(internalVideo).to.not.be.undefined
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)
251 expect(privateVideo.privacy.id).to.equal(VideoPrivacy.PUBLIC)
252 expect(internalVideo.privacy.id).to.equal(VideoPrivacy.PUBLIC)
256 it('Should set these videos as private and internal', async function () {
259 await updateVideo(servers[0].url, servers[0].accessToken, internalVideoId, { privacy: VideoPrivacy.PRIVATE })
260 await updateVideo(servers[0].url, servers[0].accessToken, privateVideoId, { privacy: VideoPrivacy.INTERNAL })
262 await waitJobs(servers)
264 for (const server of servers) {
265 const res = await getVideosList(server.url)
267 expect(res.body.total).to.equal(0)
268 expect(res.body.data).to.have.lengthOf(0)
272 const res = await getMyVideos(servers[0].url, servers[0].accessToken, 0, 5)
273 const videos = res.body.data
275 expect(res.body.total).to.equal(3)
276 expect(videos).to.have.lengthOf(3)
278 const privateVideo = videos.find(v => v.name === 'private video becomes public')
279 const internalVideo = videos.find(v => v.name === 'internal video becomes public')
281 expect(privateVideo).to.not.be.undefined
282 expect(internalVideo).to.not.be.undefined
284 expect(privateVideo.privacy.id).to.equal(VideoPrivacy.INTERNAL)
285 expect(internalVideo.privacy.id).to.equal(VideoPrivacy.PRIVATE)
289 after(async function () {
290 await cleanupTests(servers)