1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
4 import * as chai from 'chai'
5 import { wait } from '@shared/core-utils'
6 import { HttpStatusCode, VideoCreateResult, VideoPrivacy } from '@shared/models'
7 import { cleanupTests, createSingleServer, doubleFollow, PeerTubeServer, setAccessTokensToServers, waitJobs } from '@shared/server-commands'
9 const expect = chai.expect
11 describe('Test video privacy', function () {
12 const servers: PeerTubeServer[] = []
13 let anotherUserToken: string
15 let privateVideoId: number
16 let privateVideoUUID: string
18 let internalVideoId: number
19 let internalVideoUUID: string
21 let unlistedVideo: VideoCreateResult
22 let nonFederatedUnlistedVideoUUID: string
26 const dontFederateUnlistedConfig = {
29 federate_unlisted: false
34 before(async function () {
38 servers.push(await createSingleServer(1, dontFederateUnlistedConfig))
39 servers.push(await createSingleServer(2))
41 // Get the access tokens
42 await setAccessTokensToServers(servers)
44 // Server 1 and server 2 follow each other
45 await doubleFollow(servers[0], servers[1])
48 describe('Private and internal videos', function () {
50 it('Should upload a private and internal videos on server 1', async function () {
53 for (const privacy of [ VideoPrivacy.PRIVATE, VideoPrivacy.INTERNAL ]) {
54 const attributes = { privacy }
55 await servers[0].videos.upload({ attributes })
58 await waitJobs(servers)
61 it('Should not have these private and internal videos on server 2', async function () {
62 const { total, data } = await servers[1].videos.list()
64 expect(total).to.equal(0)
65 expect(data).to.have.lengthOf(0)
68 it('Should not list the private and internal videos for an unauthenticated user on server 1', async function () {
69 const { total, data } = await servers[0].videos.list()
71 expect(total).to.equal(0)
72 expect(data).to.have.lengthOf(0)
75 it('Should not list the private video and list the internal video for an authenticated user on server 1', async function () {
76 const { total, data } = await servers[0].videos.listWithToken()
78 expect(total).to.equal(1)
79 expect(data).to.have.lengthOf(1)
81 expect(data[0].privacy.id).to.equal(VideoPrivacy.INTERNAL)
84 it('Should list my (private and internal) videos', async function () {
85 const { total, data } = await servers[0].videos.listMyVideos()
87 expect(total).to.equal(2)
88 expect(data).to.have.lengthOf(2)
90 const privateVideo = data.find(v => v.privacy.id === VideoPrivacy.PRIVATE)
91 privateVideoId = privateVideo.id
92 privateVideoUUID = privateVideo.uuid
94 const internalVideo = data.find(v => v.privacy.id === VideoPrivacy.INTERNAL)
95 internalVideoId = internalVideo.id
96 internalVideoUUID = internalVideo.uuid
99 it('Should not be able to watch the private/internal video with non authenticated user', async function () {
100 await servers[0].videos.get({ id: privateVideoUUID, expectedStatus: HttpStatusCode.UNAUTHORIZED_401 })
101 await servers[0].videos.get({ id: internalVideoUUID, expectedStatus: HttpStatusCode.UNAUTHORIZED_401 })
104 it('Should not be able to watch the private video with another user', async function () {
109 password: 'super password'
111 await servers[0].users.create({ username: user.username, password: user.password })
113 anotherUserToken = await servers[0].login.getAccessToken(user)
115 await servers[0].videos.getWithToken({
116 token: anotherUserToken,
117 id: privateVideoUUID,
118 expectedStatus: HttpStatusCode.FORBIDDEN_403
122 it('Should be able to watch the internal video with another user', async function () {
123 await servers[0].videos.getWithToken({ token: anotherUserToken, id: internalVideoUUID })
126 it('Should be able to watch the private video with the correct user', async function () {
127 await servers[0].videos.getWithToken({ id: privateVideoUUID })
131 describe('Unlisted videos', function () {
133 it('Should upload an unlisted video on server 2', async function () {
137 name: 'unlisted video',
138 privacy: VideoPrivacy.UNLISTED
140 await servers[1].videos.upload({ attributes })
142 // Server 2 has transcoding enabled
143 await waitJobs(servers)
146 it('Should not have this unlisted video listed on server 1 and 2', async function () {
147 for (const server of servers) {
148 const { total, data } = await server.videos.list()
150 expect(total).to.equal(0)
151 expect(data).to.have.lengthOf(0)
155 it('Should list my (unlisted) videos', async function () {
156 const { total, data } = await servers[1].videos.listMyVideos()
158 expect(total).to.equal(1)
159 expect(data).to.have.lengthOf(1)
161 unlistedVideo = data[0]
164 it('Should not be able to get this unlisted video using its id', async function () {
165 await servers[1].videos.get({ id: unlistedVideo.id, expectedStatus: HttpStatusCode.NOT_FOUND_404 })
168 it('Should be able to get this unlisted video using its uuid/shortUUID', async function () {
169 for (const server of servers) {
170 for (const id of [ unlistedVideo.uuid, unlistedVideo.shortUUID ]) {
171 const video = await server.videos.get({ id })
173 expect(video.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 servers[0].videos.upload({ attributes })
187 await waitJobs(servers)
190 it('Should list my new unlisted video', async function () {
191 const { total, data } = await servers[0].videos.listMyVideos()
193 expect(total).to.equal(3)
194 expect(data).to.have.lengthOf(3)
196 nonFederatedUnlistedVideoUUID = data[0].uuid
199 it('Should be able to get non-federated unlisted video from origin', async function () {
200 const video = await servers[0].videos.get({ id: nonFederatedUnlistedVideoUUID })
202 expect(video.name).to.equal('unlisted video')
205 it('Should not be able to get non-federated unlisted video from federated server', async function () {
206 await servers[1].videos.get({ id: nonFederatedUnlistedVideoUUID, expectedStatus: HttpStatusCode.NOT_FOUND_404 })
210 describe('Privacy update', function () {
212 it('Should update the private and internal videos to public on server 1', async function () {
219 name: 'private video becomes public',
220 privacy: VideoPrivacy.PUBLIC
223 await servers[0].videos.update({ id: privateVideoId, attributes })
228 name: 'internal video becomes public',
229 privacy: VideoPrivacy.PUBLIC
231 await servers[0].videos.update({ id: internalVideoId, attributes })
235 await waitJobs(servers)
238 it('Should have this new public video listed on server 1 and 2', async function () {
239 for (const server of servers) {
240 const { total, data } = await server.videos.list()
241 expect(total).to.equal(2)
242 expect(data).to.have.lengthOf(2)
244 const privateVideo = data.find(v => v.name === 'private video becomes public')
245 const internalVideo = data.find(v => v.name === 'internal video becomes public')
247 expect(privateVideo).to.not.be.undefined
248 expect(internalVideo).to.not.be.undefined
250 expect(new Date(privateVideo.publishedAt).getTime()).to.be.at.least(now)
251 // We don't change the publish date of internal videos
252 expect(new Date(internalVideo.publishedAt).getTime()).to.be.below(now)
254 expect(privateVideo.privacy.id).to.equal(VideoPrivacy.PUBLIC)
255 expect(internalVideo.privacy.id).to.equal(VideoPrivacy.PUBLIC)
259 it('Should set these videos as private and internal', async function () {
262 await servers[0].videos.update({ id: internalVideoId, attributes: { privacy: VideoPrivacy.PRIVATE } })
263 await servers[0].videos.update({ id: privateVideoId, attributes: { privacy: VideoPrivacy.INTERNAL } })
265 await waitJobs(servers)
267 for (const server of servers) {
268 const { total, data } = await server.videos.list()
270 expect(total).to.equal(0)
271 expect(data).to.have.lengthOf(0)
275 const { total, data } = await servers[0].videos.listMyVideos()
276 expect(total).to.equal(3)
277 expect(data).to.have.lengthOf(3)
279 const privateVideo = data.find(v => v.name === 'private video becomes public')
280 const internalVideo = data.find(v => v.name === 'internal video becomes public')
282 expect(privateVideo).to.not.be.undefined
283 expect(internalVideo).to.not.be.undefined
285 expect(privateVideo.privacy.id).to.equal(VideoPrivacy.INTERNAL)
286 expect(internalVideo.privacy.id).to.equal(VideoPrivacy.PRIVATE)
291 after(async function () {
292 await cleanupTests(servers)