1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
4 import * as chai from 'chai'
5 import { HttpStatusCode } from '@shared/core-utils/miscs/http-error-codes'
6 import { Video, VideoCreateResult } from '@shared/models'
11 getVideosListWithToken,
13 setAccessTokensToServers,
15 } from '../../../../shared/extra-utils/index'
16 import { doubleFollow } from '../../../../shared/extra-utils/server/follows'
17 import { waitJobs } from '../../../../shared/extra-utils/server/jobs'
18 import { userLogin } from '../../../../shared/extra-utils/users/login'
19 import { createUser } from '../../../../shared/extra-utils/users/users'
20 import { getMyVideos, getVideo, getVideoWithToken, updateVideo } from '../../../../shared/extra-utils/videos/videos'
21 import { VideoPrivacy } from '../../../../shared/models/videos/video-privacy.enum'
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 unlistedVideo: VideoCreateResult
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 describe('Private and internal videos', function () {
64 it('Should upload a private and internal videos on server 1', async function () {
67 for (const privacy of [ VideoPrivacy.PRIVATE, VideoPrivacy.INTERNAL ]) {
68 const attributes = { privacy }
69 await uploadVideo(servers[0].url, servers[0].accessToken, attributes)
72 await waitJobs(servers)
75 it('Should not have these private and internal videos on server 2', async function () {
76 const res = await getVideosList(servers[1].url)
78 expect(res.body.total).to.equal(0)
79 expect(res.body.data).to.have.lengthOf(0)
82 it('Should not list the private and internal videos for an unauthenticated user on server 1', async function () {
83 const res = await getVideosList(servers[0].url)
85 expect(res.body.total).to.equal(0)
86 expect(res.body.data).to.have.lengthOf(0)
89 it('Should not list the private video and list the internal video for an authenticated user on server 1', async function () {
90 const res = await getVideosListWithToken(servers[0].url, servers[0].accessToken)
92 expect(res.body.total).to.equal(1)
93 expect(res.body.data).to.have.lengthOf(1)
95 expect(res.body.data[0].privacy.id).to.equal(VideoPrivacy.INTERNAL)
98 it('Should list my (private and internal) videos', async function () {
99 const res = await getMyVideos(servers[0].url, servers[0].accessToken, 0, 10)
101 expect(res.body.total).to.equal(2)
102 expect(res.body.data).to.have.lengthOf(2)
104 const videos: Video[] = res.body.data
106 const privateVideo = videos.find(v => v.privacy.id === VideoPrivacy.PRIVATE)
107 privateVideoId = privateVideo.id
108 privateVideoUUID = privateVideo.uuid
110 const internalVideo = videos.find(v => v.privacy.id === VideoPrivacy.INTERNAL)
111 internalVideoId = internalVideo.id
112 internalVideoUUID = internalVideo.uuid
115 it('Should not be able to watch the private/internal video with non authenticated user', async function () {
116 await getVideo(servers[0].url, privateVideoUUID, HttpStatusCode.UNAUTHORIZED_401)
117 await getVideo(servers[0].url, internalVideoUUID, HttpStatusCode.UNAUTHORIZED_401)
120 it('Should not be able to watch the private video with another user', async function () {
125 password: 'super password'
127 await createUser({ url: servers[0].url, accessToken: servers[0].accessToken, username: user.username, password: user.password })
129 anotherUserToken = await userLogin(servers[0], user)
130 await getVideoWithToken(servers[0].url, anotherUserToken, privateVideoUUID, HttpStatusCode.FORBIDDEN_403)
133 it('Should be able to watch the internal video with another user', async function () {
134 await getVideoWithToken(servers[0].url, anotherUserToken, internalVideoUUID, HttpStatusCode.OK_200)
137 it('Should be able to watch the private video with the correct user', async function () {
138 await getVideoWithToken(servers[0].url, servers[0].accessToken, privateVideoUUID, HttpStatusCode.OK_200)
142 describe('Unlisted videos', function () {
144 it('Should upload an unlisted video on server 2', async function () {
148 name: 'unlisted video',
149 privacy: VideoPrivacy.UNLISTED
151 await uploadVideo(servers[1].url, servers[1].accessToken, attributes)
153 // Server 2 has transcoding enabled
154 await waitJobs(servers)
157 it('Should not have this unlisted video listed on server 1 and 2', async function () {
158 for (const server of servers) {
159 const res = await getVideosList(server.url)
161 expect(res.body.total).to.equal(0)
162 expect(res.body.data).to.have.lengthOf(0)
166 it('Should list my (unlisted) videos', async function () {
167 const res = await getMyVideos(servers[1].url, servers[1].accessToken, 0, 1)
169 expect(res.body.total).to.equal(1)
170 expect(res.body.data).to.have.lengthOf(1)
172 unlistedVideo = res.body.data[0]
175 it('Should not be able to get this unlisted video using its id', async function () {
176 await getVideo(servers[1].url, unlistedVideo.id, 404)
179 it('Should be able to get this unlisted video using its uuid/shortUUID', async function () {
180 for (const server of servers) {
181 for (const id of [ unlistedVideo.uuid, unlistedVideo.shortUUID ]) {
182 const res = await getVideo(server.url, id)
184 expect(res.body.name).to.equal('unlisted video')
189 it('Should upload a non-federating unlisted video to server 1', async function () {
193 name: 'unlisted video',
194 privacy: VideoPrivacy.UNLISTED
196 await uploadVideo(servers[0].url, servers[0].accessToken, attributes)
198 await waitJobs(servers)
201 it('Should list my new unlisted video', async function () {
202 const res = await getMyVideos(servers[0].url, servers[0].accessToken, 0, 3)
204 expect(res.body.total).to.equal(3)
205 expect(res.body.data).to.have.lengthOf(3)
207 nonFederatedUnlistedVideoUUID = res.body.data[0].uuid
210 it('Should be able to get non-federated unlisted video from origin', async function () {
211 const res = await getVideo(servers[0].url, nonFederatedUnlistedVideoUUID)
213 expect(res.body.name).to.equal('unlisted video')
216 it('Should not be able to get non-federated unlisted video from federated server', async function () {
217 await getVideo(servers[1].url, nonFederatedUnlistedVideoUUID, HttpStatusCode.NOT_FOUND_404)
221 describe('Privacy update', function () {
223 it('Should update the private and internal videos to public on server 1', async function () {
230 name: 'private video becomes public',
231 privacy: VideoPrivacy.PUBLIC
234 await updateVideo(servers[0].url, servers[0].accessToken, privateVideoId, attribute)
239 name: 'internal video becomes public',
240 privacy: VideoPrivacy.PUBLIC
242 await updateVideo(servers[0].url, servers[0].accessToken, internalVideoId, attribute)
245 await waitJobs(servers)
248 it('Should have this new public video listed on server 1 and 2', async function () {
249 for (const server of servers) {
250 const res = await getVideosList(server.url)
251 expect(res.body.total).to.equal(2)
252 expect(res.body.data).to.have.lengthOf(2)
254 const videos: Video[] = res.body.data
255 const privateVideo = videos.find(v => v.name === 'private video becomes public')
256 const internalVideo = videos.find(v => v.name === 'internal video becomes public')
258 expect(privateVideo).to.not.be.undefined
259 expect(internalVideo).to.not.be.undefined
261 expect(new Date(privateVideo.publishedAt).getTime()).to.be.at.least(now)
262 // We don't change the publish date of internal videos
263 expect(new Date(internalVideo.publishedAt).getTime()).to.be.below(now)
265 expect(privateVideo.privacy.id).to.equal(VideoPrivacy.PUBLIC)
266 expect(internalVideo.privacy.id).to.equal(VideoPrivacy.PUBLIC)
270 it('Should set these videos as private and internal', async function () {
273 await updateVideo(servers[0].url, servers[0].accessToken, internalVideoId, { privacy: VideoPrivacy.PRIVATE })
274 await updateVideo(servers[0].url, servers[0].accessToken, privateVideoId, { privacy: VideoPrivacy.INTERNAL })
276 await waitJobs(servers)
278 for (const server of servers) {
279 const res = await getVideosList(server.url)
281 expect(res.body.total).to.equal(0)
282 expect(res.body.data).to.have.lengthOf(0)
286 const res = await getMyVideos(servers[0].url, servers[0].accessToken, 0, 5)
287 const videos = res.body.data
289 expect(res.body.total).to.equal(3)
290 expect(videos).to.have.lengthOf(3)
292 const privateVideo = videos.find(v => v.name === 'private video becomes public')
293 const internalVideo = videos.find(v => v.name === 'internal video becomes public')
295 expect(privateVideo).to.not.be.undefined
296 expect(internalVideo).to.not.be.undefined
298 expect(privateVideo.privacy.id).to.equal(VideoPrivacy.INTERNAL)
299 expect(internalVideo.privacy.id).to.equal(VideoPrivacy.PRIVATE)
304 after(async function () {
305 await cleanupTests(servers)