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'
14 getVideosListWithToken,
17 setAccessTokensToServers,
21 } from '@shared/extra-utils'
22 import { Video, VideoCreateResult, VideoPrivacy } from '@shared/models'
24 const expect = chai.expect
26 describe('Test video privacy', function () {
27 const servers: ServerInfo[] = []
28 let anotherUserToken: string
30 let privateVideoId: number
31 let privateVideoUUID: string
33 let internalVideoId: number
34 let internalVideoUUID: string
36 let unlistedVideo: VideoCreateResult
37 let nonFederatedUnlistedVideoUUID: string
41 const dontFederateUnlistedConfig = {
44 federate_unlisted: false
49 before(async function () {
53 servers.push(await flushAndRunServer(1, dontFederateUnlistedConfig))
54 servers.push(await flushAndRunServer(2))
56 // Get the access tokens
57 await setAccessTokensToServers(servers)
59 // Server 1 and server 2 follow each other
60 await doubleFollow(servers[0], servers[1])
63 describe('Private and internal videos', function () {
65 it('Should upload a private and internal videos on server 1', async function () {
68 for (const privacy of [ VideoPrivacy.PRIVATE, VideoPrivacy.INTERNAL ]) {
69 const attributes = { privacy }
70 await uploadVideo(servers[0].url, servers[0].accessToken, attributes)
73 await waitJobs(servers)
76 it('Should not have these private and internal videos on server 2', async function () {
77 const res = await getVideosList(servers[1].url)
79 expect(res.body.total).to.equal(0)
80 expect(res.body.data).to.have.lengthOf(0)
83 it('Should not list the private and internal videos for an unauthenticated user on server 1', async function () {
84 const res = await getVideosList(servers[0].url)
86 expect(res.body.total).to.equal(0)
87 expect(res.body.data).to.have.lengthOf(0)
90 it('Should not list the private video and list the internal video for an authenticated user on server 1', async function () {
91 const res = await getVideosListWithToken(servers[0].url, servers[0].accessToken)
93 expect(res.body.total).to.equal(1)
94 expect(res.body.data).to.have.lengthOf(1)
96 expect(res.body.data[0].privacy.id).to.equal(VideoPrivacy.INTERNAL)
99 it('Should list my (private and internal) videos', async function () {
100 const res = await getMyVideos(servers[0].url, servers[0].accessToken, 0, 10)
102 expect(res.body.total).to.equal(2)
103 expect(res.body.data).to.have.lengthOf(2)
105 const videos: Video[] = res.body.data
107 const privateVideo = videos.find(v => v.privacy.id === VideoPrivacy.PRIVATE)
108 privateVideoId = privateVideo.id
109 privateVideoUUID = privateVideo.uuid
111 const internalVideo = videos.find(v => v.privacy.id === VideoPrivacy.INTERNAL)
112 internalVideoId = internalVideo.id
113 internalVideoUUID = internalVideo.uuid
116 it('Should not be able to watch the private/internal video with non authenticated user', async function () {
117 await getVideo(servers[0].url, privateVideoUUID, HttpStatusCode.UNAUTHORIZED_401)
118 await getVideo(servers[0].url, internalVideoUUID, HttpStatusCode.UNAUTHORIZED_401)
121 it('Should not be able to watch the private video with another user', async function () {
126 password: 'super password'
128 await createUser({ url: servers[0].url, accessToken: servers[0].accessToken, username: user.username, password: user.password })
130 anotherUserToken = await servers[0].loginCommand.getAccessToken(user)
131 await getVideoWithToken(servers[0].url, anotherUserToken, privateVideoUUID, HttpStatusCode.FORBIDDEN_403)
134 it('Should be able to watch the internal video with another user', async function () {
135 await getVideoWithToken(servers[0].url, anotherUserToken, internalVideoUUID, HttpStatusCode.OK_200)
138 it('Should be able to watch the private video with the correct user', async function () {
139 await getVideoWithToken(servers[0].url, servers[0].accessToken, privateVideoUUID, HttpStatusCode.OK_200)
143 describe('Unlisted videos', function () {
145 it('Should upload an unlisted video on server 2', async function () {
149 name: 'unlisted video',
150 privacy: VideoPrivacy.UNLISTED
152 await uploadVideo(servers[1].url, servers[1].accessToken, attributes)
154 // Server 2 has transcoding enabled
155 await waitJobs(servers)
158 it('Should not have this unlisted video listed on server 1 and 2', async function () {
159 for (const server of servers) {
160 const res = await getVideosList(server.url)
162 expect(res.body.total).to.equal(0)
163 expect(res.body.data).to.have.lengthOf(0)
167 it('Should list my (unlisted) videos', async function () {
168 const res = await getMyVideos(servers[1].url, servers[1].accessToken, 0, 1)
170 expect(res.body.total).to.equal(1)
171 expect(res.body.data).to.have.lengthOf(1)
173 unlistedVideo = res.body.data[0]
176 it('Should not be able to get this unlisted video using its id', async function () {
177 await getVideo(servers[1].url, unlistedVideo.id, 404)
180 it('Should be able to get this unlisted video using its uuid/shortUUID', async function () {
181 for (const server of servers) {
182 for (const id of [ unlistedVideo.uuid, unlistedVideo.shortUUID ]) {
183 const res = await getVideo(server.url, id)
185 expect(res.body.name).to.equal('unlisted video')
190 it('Should upload a non-federating unlisted video to server 1', async function () {
194 name: 'unlisted video',
195 privacy: VideoPrivacy.UNLISTED
197 await uploadVideo(servers[0].url, servers[0].accessToken, attributes)
199 await waitJobs(servers)
202 it('Should list my new unlisted video', async function () {
203 const res = await getMyVideos(servers[0].url, servers[0].accessToken, 0, 3)
205 expect(res.body.total).to.equal(3)
206 expect(res.body.data).to.have.lengthOf(3)
208 nonFederatedUnlistedVideoUUID = res.body.data[0].uuid
211 it('Should be able to get non-federated unlisted video from origin', async function () {
212 const res = await getVideo(servers[0].url, nonFederatedUnlistedVideoUUID)
214 expect(res.body.name).to.equal('unlisted video')
217 it('Should not be able to get non-federated unlisted video from federated server', async function () {
218 await getVideo(servers[1].url, nonFederatedUnlistedVideoUUID, HttpStatusCode.NOT_FOUND_404)
222 describe('Privacy update', function () {
224 it('Should update the private and internal videos to public on server 1', async function () {
231 name: 'private video becomes public',
232 privacy: VideoPrivacy.PUBLIC
235 await updateVideo(servers[0].url, servers[0].accessToken, privateVideoId, attribute)
240 name: 'internal video becomes public',
241 privacy: VideoPrivacy.PUBLIC
243 await updateVideo(servers[0].url, servers[0].accessToken, internalVideoId, attribute)
246 await waitJobs(servers)
249 it('Should have this new public video listed on server 1 and 2', async function () {
250 for (const server of servers) {
251 const res = await getVideosList(server.url)
252 expect(res.body.total).to.equal(2)
253 expect(res.body.data).to.have.lengthOf(2)
255 const videos: Video[] = res.body.data
256 const privateVideo = videos.find(v => v.name === 'private video becomes public')
257 const internalVideo = videos.find(v => v.name === 'internal video becomes public')
259 expect(privateVideo).to.not.be.undefined
260 expect(internalVideo).to.not.be.undefined
262 expect(new Date(privateVideo.publishedAt).getTime()).to.be.at.least(now)
263 // We don't change the publish date of internal videos
264 expect(new Date(internalVideo.publishedAt).getTime()).to.be.below(now)
266 expect(privateVideo.privacy.id).to.equal(VideoPrivacy.PUBLIC)
267 expect(internalVideo.privacy.id).to.equal(VideoPrivacy.PUBLIC)
271 it('Should set these videos as private and internal', async function () {
274 await updateVideo(servers[0].url, servers[0].accessToken, internalVideoId, { privacy: VideoPrivacy.PRIVATE })
275 await updateVideo(servers[0].url, servers[0].accessToken, privateVideoId, { privacy: VideoPrivacy.INTERNAL })
277 await waitJobs(servers)
279 for (const server of servers) {
280 const res = await getVideosList(server.url)
282 expect(res.body.total).to.equal(0)
283 expect(res.body.data).to.have.lengthOf(0)
287 const res = await getMyVideos(servers[0].url, servers[0].accessToken, 0, 5)
288 const videos = res.body.data
290 expect(res.body.total).to.equal(3)
291 expect(videos).to.have.lengthOf(3)
293 const privateVideo = videos.find(v => v.name === 'private video becomes public')
294 const internalVideo = videos.find(v => v.name === 'internal video becomes public')
296 expect(privateVideo).to.not.be.undefined
297 expect(internalVideo).to.not.be.undefined
299 expect(privateVideo.privacy.id).to.equal(VideoPrivacy.INTERNAL)
300 expect(internalVideo.privacy.id).to.equal(VideoPrivacy.PRIVATE)
305 after(async function () {
306 await cleanupTests(servers)