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 () {
104 password: 'super password'
106 await servers[0].users.create({ username: user.username, password: user.password })
108 anotherUserToken = await servers[0].login.getAccessToken(user)
110 await servers[0].videos.getWithToken({
111 token: anotherUserToken,
112 id: privateVideoUUID,
113 expectedStatus: HttpStatusCode.FORBIDDEN_403
117 it('Should be able to watch the internal video with another user', async function () {
118 await servers[0].videos.getWithToken({ token: anotherUserToken, id: internalVideoUUID })
121 it('Should be able to watch the private video with the correct user', async function () {
122 await servers[0].videos.getWithToken({ id: privateVideoUUID })
126 describe('Unlisted videos', function () {
128 it('Should upload an unlisted video on server 2', async function () {
132 name: 'unlisted video',
133 privacy: VideoPrivacy.UNLISTED
135 await servers[1].videos.upload({ attributes })
137 // Server 2 has transcoding enabled
138 await waitJobs(servers)
141 it('Should not have this unlisted video listed on server 1 and 2', async function () {
142 for (const server of servers) {
143 const { total, data } = await server.videos.list()
145 expect(total).to.equal(0)
146 expect(data).to.have.lengthOf(0)
150 it('Should list my (unlisted) videos', async function () {
151 const { total, data } = await servers[1].videos.listMyVideos()
153 expect(total).to.equal(1)
154 expect(data).to.have.lengthOf(1)
156 unlistedVideo = data[0]
159 it('Should not be able to get this unlisted video using its id', async function () {
160 await servers[1].videos.get({ id: unlistedVideo.id, expectedStatus: HttpStatusCode.UNAUTHORIZED_401 })
163 it('Should be able to get this unlisted video using its uuid/shortUUID', async function () {
164 for (const server of servers) {
165 for (const id of [ unlistedVideo.uuid, unlistedVideo.shortUUID ]) {
166 const video = await server.videos.get({ id })
168 expect(video.name).to.equal('unlisted video')
173 it('Should upload a non-federating unlisted video to server 1', async function () {
177 name: 'unlisted video',
178 privacy: VideoPrivacy.UNLISTED
180 await servers[0].videos.upload({ attributes })
182 await waitJobs(servers)
185 it('Should list my new unlisted video', async function () {
186 const { total, data } = await servers[0].videos.listMyVideos()
188 expect(total).to.equal(3)
189 expect(data).to.have.lengthOf(3)
191 nonFederatedUnlistedVideoUUID = data[0].uuid
194 it('Should be able to get non-federated unlisted video from origin', async function () {
195 const video = await servers[0].videos.get({ id: nonFederatedUnlistedVideoUUID })
197 expect(video.name).to.equal('unlisted video')
200 it('Should not be able to get non-federated unlisted video from federated server', async function () {
201 await servers[1].videos.get({ id: nonFederatedUnlistedVideoUUID, expectedStatus: HttpStatusCode.NOT_FOUND_404 })
205 describe('Privacy update', function () {
207 it('Should update the private and internal videos to public on server 1', async function () {
214 name: 'private video becomes public',
215 privacy: VideoPrivacy.PUBLIC
218 await servers[0].videos.update({ id: privateVideoId, attributes })
223 name: 'internal video becomes public',
224 privacy: VideoPrivacy.PUBLIC
226 await servers[0].videos.update({ id: internalVideoId, attributes })
230 await waitJobs(servers)
233 it('Should have this new public video listed on server 1 and 2', async function () {
234 for (const server of servers) {
235 const { total, data } = await server.videos.list()
236 expect(total).to.equal(2)
237 expect(data).to.have.lengthOf(2)
239 const privateVideo = data.find(v => v.name === 'private video becomes public')
240 const internalVideo = data.find(v => v.name === 'internal video becomes public')
242 expect(privateVideo).to.not.be.undefined
243 expect(internalVideo).to.not.be.undefined
245 expect(new Date(privateVideo.publishedAt).getTime()).to.be.at.least(now)
246 // We don't change the publish date of internal videos
247 expect(new Date(internalVideo.publishedAt).getTime()).to.be.below(now)
249 expect(privateVideo.privacy.id).to.equal(VideoPrivacy.PUBLIC)
250 expect(internalVideo.privacy.id).to.equal(VideoPrivacy.PUBLIC)
254 it('Should set these videos as private and internal', async function () {
255 await servers[0].videos.update({ id: internalVideoId, attributes: { privacy: VideoPrivacy.PRIVATE } })
256 await servers[0].videos.update({ id: privateVideoId, attributes: { privacy: VideoPrivacy.INTERNAL } })
258 await waitJobs(servers)
260 for (const server of servers) {
261 const { total, data } = await server.videos.list()
263 expect(total).to.equal(0)
264 expect(data).to.have.lengthOf(0)
268 const { total, data } = await servers[0].videos.listMyVideos()
269 expect(total).to.equal(3)
270 expect(data).to.have.lengthOf(3)
272 const privateVideo = data.find(v => v.name === 'private video becomes public')
273 const internalVideo = data.find(v => v.name === 'internal video becomes public')
275 expect(privateVideo).to.not.be.undefined
276 expect(internalVideo).to.not.be.undefined
278 expect(privateVideo.privacy.id).to.equal(VideoPrivacy.INTERNAL)
279 expect(internalVideo.privacy.id).to.equal(VideoPrivacy.PRIVATE)
284 after(async function () {
285 await cleanupTests(servers)