1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
3 import { expect } from 'chai'
4 import { wait } from '@shared/core-utils'
5 import { HttpStatusCode, VideoCreateResult, VideoPrivacy } from '@shared/models'
6 import { cleanupTests, createSingleServer, doubleFollow, PeerTubeServer, setAccessTokensToServers, waitJobs } from '@shared/server-commands'
8 describe('Test video privacy', function () {
9 const servers: PeerTubeServer[] = []
10 let anotherUserToken: string
12 let privateVideoId: number
13 let privateVideoUUID: string
15 let internalVideoId: number
16 let internalVideoUUID: string
18 let unlistedVideo: VideoCreateResult
19 let nonFederatedUnlistedVideoUUID: string
23 const dontFederateUnlistedConfig = {
26 federate_unlisted: false
31 before(async function () {
35 servers.push(await createSingleServer(1, dontFederateUnlistedConfig))
36 servers.push(await createSingleServer(2))
38 // Get the access tokens
39 await setAccessTokensToServers(servers)
41 // Server 1 and server 2 follow each other
42 await doubleFollow(servers[0], servers[1])
45 describe('Private and internal videos', function () {
47 it('Should upload a private and internal videos on server 1', async function () {
50 for (const privacy of [ VideoPrivacy.PRIVATE, VideoPrivacy.INTERNAL ]) {
51 const attributes = { privacy }
52 await servers[0].videos.upload({ attributes })
55 await waitJobs(servers)
58 it('Should not have these private and internal videos on server 2', async function () {
59 const { total, data } = await servers[1].videos.list()
61 expect(total).to.equal(0)
62 expect(data).to.have.lengthOf(0)
65 it('Should not list the private and internal videos for an unauthenticated user on server 1', async function () {
66 const { total, data } = await servers[0].videos.list()
68 expect(total).to.equal(0)
69 expect(data).to.have.lengthOf(0)
72 it('Should not list the private video and list the internal video for an authenticated user on server 1', async function () {
73 const { total, data } = await servers[0].videos.listWithToken()
75 expect(total).to.equal(1)
76 expect(data).to.have.lengthOf(1)
78 expect(data[0].privacy.id).to.equal(VideoPrivacy.INTERNAL)
81 it('Should list my (private and internal) videos', async function () {
82 const { total, data } = await servers[0].videos.listMyVideos()
84 expect(total).to.equal(2)
85 expect(data).to.have.lengthOf(2)
87 const privateVideo = data.find(v => v.privacy.id === VideoPrivacy.PRIVATE)
88 privateVideoId = privateVideo.id
89 privateVideoUUID = privateVideo.uuid
91 const internalVideo = data.find(v => v.privacy.id === VideoPrivacy.INTERNAL)
92 internalVideoId = internalVideo.id
93 internalVideoUUID = internalVideo.uuid
96 it('Should not be able to watch the private/internal video with non authenticated user', async function () {
97 await servers[0].videos.get({ id: privateVideoUUID, expectedStatus: HttpStatusCode.UNAUTHORIZED_401 })
98 await servers[0].videos.get({ id: internalVideoUUID, expectedStatus: HttpStatusCode.UNAUTHORIZED_401 })
101 it('Should not be able to watch the private video with another user', async function () {
106 password: 'super password'
108 await servers[0].users.create({ username: user.username, password: user.password })
110 anotherUserToken = await servers[0].login.getAccessToken(user)
112 await servers[0].videos.getWithToken({
113 token: anotherUserToken,
114 id: privateVideoUUID,
115 expectedStatus: HttpStatusCode.FORBIDDEN_403
119 it('Should be able to watch the internal video with another user', async function () {
120 await servers[0].videos.getWithToken({ token: anotherUserToken, id: internalVideoUUID })
123 it('Should be able to watch the private video with the correct user', async function () {
124 await servers[0].videos.getWithToken({ id: privateVideoUUID })
128 describe('Unlisted videos', function () {
130 it('Should upload an unlisted video on server 2', async function () {
134 name: 'unlisted video',
135 privacy: VideoPrivacy.UNLISTED
137 await servers[1].videos.upload({ attributes })
139 // Server 2 has transcoding enabled
140 await waitJobs(servers)
143 it('Should not have this unlisted video listed on server 1 and 2', async function () {
144 for (const server of servers) {
145 const { total, data } = await server.videos.list()
147 expect(total).to.equal(0)
148 expect(data).to.have.lengthOf(0)
152 it('Should list my (unlisted) videos', async function () {
153 const { total, data } = await servers[1].videos.listMyVideos()
155 expect(total).to.equal(1)
156 expect(data).to.have.lengthOf(1)
158 unlistedVideo = data[0]
161 it('Should not be able to get this unlisted video using its id', async function () {
162 await servers[1].videos.get({ id: unlistedVideo.id, expectedStatus: HttpStatusCode.UNAUTHORIZED_401 })
165 it('Should be able to get this unlisted video using its uuid/shortUUID', async function () {
166 for (const server of servers) {
167 for (const id of [ unlistedVideo.uuid, unlistedVideo.shortUUID ]) {
168 const video = await server.videos.get({ id })
170 expect(video.name).to.equal('unlisted video')
175 it('Should upload a non-federating unlisted video to server 1', async function () {
179 name: 'unlisted video',
180 privacy: VideoPrivacy.UNLISTED
182 await servers[0].videos.upload({ attributes })
184 await waitJobs(servers)
187 it('Should list my new unlisted video', async function () {
188 const { total, data } = await servers[0].videos.listMyVideos()
190 expect(total).to.equal(3)
191 expect(data).to.have.lengthOf(3)
193 nonFederatedUnlistedVideoUUID = data[0].uuid
196 it('Should be able to get non-federated unlisted video from origin', async function () {
197 const video = await servers[0].videos.get({ id: nonFederatedUnlistedVideoUUID })
199 expect(video.name).to.equal('unlisted video')
202 it('Should not be able to get non-federated unlisted video from federated server', async function () {
203 await servers[1].videos.get({ id: nonFederatedUnlistedVideoUUID, expectedStatus: HttpStatusCode.NOT_FOUND_404 })
207 describe('Privacy update', function () {
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 servers[0].videos.update({ id: privateVideoId, attributes })
225 name: 'internal video becomes public',
226 privacy: VideoPrivacy.PUBLIC
228 await servers[0].videos.update({ id: internalVideoId, attributes })
232 await waitJobs(servers)
235 it('Should have this new public video listed on server 1 and 2', async function () {
236 for (const server of servers) {
237 const { total, data } = await server.videos.list()
238 expect(total).to.equal(2)
239 expect(data).to.have.lengthOf(2)
241 const privateVideo = data.find(v => v.name === 'private video becomes public')
242 const internalVideo = data.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 servers[0].videos.update({ id: internalVideoId, attributes: { privacy: VideoPrivacy.PRIVATE } })
260 await servers[0].videos.update({ id: privateVideoId, attributes: { privacy: VideoPrivacy.INTERNAL } })
262 await waitJobs(servers)
264 for (const server of servers) {
265 const { total, data } = await server.videos.list()
267 expect(total).to.equal(0)
268 expect(data).to.have.lengthOf(0)
272 const { total, data } = await servers[0].videos.listMyVideos()
273 expect(total).to.equal(3)
274 expect(data).to.have.lengthOf(3)
276 const privateVideo = data.find(v => v.name === 'private video becomes public')
277 const internalVideo = data.find(v => v.name === 'internal video becomes public')
279 expect(privateVideo).to.not.be.undefined
280 expect(internalVideo).to.not.be.undefined
282 expect(privateVideo.privacy.id).to.equal(VideoPrivacy.INTERNAL)
283 expect(internalVideo.privacy.id).to.equal(VideoPrivacy.PRIVATE)
288 after(async function () {
289 await cleanupTests(servers)