]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/tests/api/videos/video-privacy.ts
Merge branch 'release/2.1.0' into develop
[github/Chocobozzz/PeerTube.git] / server / tests / api / videos / video-privacy.ts
1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
2
3 import * as chai from 'chai'
4 import 'mocha'
5 import { VideoPrivacy } from '../../../../shared/models/videos/video-privacy.enum'
6 import {
7 cleanupTests,
8 flushAndRunMultipleServers,
9 getVideosList,
10 getVideosListWithToken,
11 ServerInfo,
12 setAccessTokensToServers,
13 uploadVideo
14 } from '../../../../shared/extra-utils/index'
15 import { doubleFollow } from '../../../../shared/extra-utils/server/follows'
16 import { userLogin } from '../../../../shared/extra-utils/users/login'
17 import { createUser } from '../../../../shared/extra-utils/users/users'
18 import { getMyVideos, getVideo, getVideoWithToken, updateVideo } from '../../../../shared/extra-utils/videos/videos'
19 import { waitJobs } from '../../../../shared/extra-utils/server/jobs'
20 import { Video } from '@shared/models'
21
22 const expect = chai.expect
23
24 describe('Test video privacy', function () {
25 let servers: ServerInfo[] = []
26 let anotherUserToken: string
27
28 let privateVideoId: number
29 let privateVideoUUID: string
30
31 let internalVideoId: number
32 let internalVideoUUID: string
33
34 let unlistedVideoUUID: string
35
36 let now: number
37
38 before(async function () {
39 this.timeout(50000)
40
41 // Run servers
42 servers = await flushAndRunMultipleServers(2)
43
44 // Get the access tokens
45 await setAccessTokensToServers(servers)
46
47 // Server 1 and server 2 follow each other
48 await doubleFollow(servers[0], servers[1])
49 })
50
51 it('Should upload a private and internal videos on server 1', async function () {
52 this.timeout(10000)
53
54 for (const privacy of [ VideoPrivacy.PRIVATE, VideoPrivacy.INTERNAL ]) {
55 const attributes = { privacy }
56 await uploadVideo(servers[0].url, servers[0].accessToken, attributes)
57 }
58
59 await waitJobs(servers)
60 })
61
62 it('Should not have these private and internal videos on server 2', async function () {
63 const res = await getVideosList(servers[1].url)
64
65 expect(res.body.total).to.equal(0)
66 expect(res.body.data).to.have.lengthOf(0)
67 })
68
69 it('Should not list the private and internal videos for an unauthenticated user on server 1', async function () {
70 const res = await getVideosList(servers[0].url)
71
72 expect(res.body.total).to.equal(0)
73 expect(res.body.data).to.have.lengthOf(0)
74 })
75
76 it('Should not list the private video and list the internal video for an authenticated user on server 1', async function () {
77 const res = await getVideosListWithToken(servers[0].url, servers[0].accessToken)
78
79 expect(res.body.total).to.equal(1)
80 expect(res.body.data).to.have.lengthOf(1)
81
82 expect(res.body.data[0].privacy.id).to.equal(VideoPrivacy.INTERNAL)
83 })
84
85 it('Should list my (private and internal) videos', async function () {
86 const res = await getMyVideos(servers[0].url, servers[0].accessToken, 0, 10)
87
88 expect(res.body.total).to.equal(2)
89 expect(res.body.data).to.have.lengthOf(2)
90
91 const videos: Video[] = res.body.data
92
93 const privateVideo = videos.find(v => v.privacy.id === VideoPrivacy.PRIVATE)
94 privateVideoId = privateVideo.id
95 privateVideoUUID = privateVideo.uuid
96
97 const internalVideo = videos.find(v => v.privacy.id === VideoPrivacy.INTERNAL)
98 internalVideoId = internalVideo.id
99 internalVideoUUID = internalVideo.uuid
100 })
101
102 it('Should not be able to watch the private/internal video with non authenticated user', async function () {
103 await getVideo(servers[0].url, privateVideoUUID, 401)
104 await getVideo(servers[0].url, internalVideoUUID, 401)
105 })
106
107 it('Should not be able to watch the private video with another user', async function () {
108 this.timeout(10000)
109
110 const user = {
111 username: 'hello',
112 password: 'super password'
113 }
114 await createUser({ url: servers[0].url, accessToken: servers[0].accessToken, username: user.username, password: user.password })
115
116 anotherUserToken = await userLogin(servers[0], user)
117 await getVideoWithToken(servers[0].url, anotherUserToken, privateVideoUUID, 403)
118 })
119
120 it('Should be able to watch the internal video with another user', async function () {
121 await getVideoWithToken(servers[0].url, anotherUserToken, internalVideoUUID, 200)
122 })
123
124 it('Should be able to watch the private video with the correct user', async function () {
125 await getVideoWithToken(servers[0].url, servers[0].accessToken, privateVideoUUID, 200)
126 })
127
128 it('Should upload an unlisted video on server 2', async function () {
129 this.timeout(30000)
130
131 const attributes = {
132 name: 'unlisted video',
133 privacy: VideoPrivacy.UNLISTED
134 }
135 await uploadVideo(servers[1].url, servers[1].accessToken, attributes)
136
137 // Server 2 has transcoding enabled
138 await waitJobs(servers)
139 })
140
141 it('Should not have this unlisted video listed on server 1 and 2', async function () {
142 for (const server of servers) {
143 const res = await getVideosList(server.url)
144
145 expect(res.body.total).to.equal(0)
146 expect(res.body.data).to.have.lengthOf(0)
147 }
148 })
149
150 it('Should list my (unlisted) videos', async function () {
151 const res = await getMyVideos(servers[1].url, servers[1].accessToken, 0, 1)
152
153 expect(res.body.total).to.equal(1)
154 expect(res.body.data).to.have.lengthOf(1)
155
156 unlistedVideoUUID = res.body.data[0].uuid
157 })
158
159 it('Should be able to get this unlisted video', async function () {
160 for (const server of servers) {
161 const res = await getVideo(server.url, unlistedVideoUUID)
162
163 expect(res.body.name).to.equal('unlisted video')
164 }
165 })
166
167 it('Should update the private and internal videos to public on server 1', async function () {
168 this.timeout(10000)
169
170 now = Date.now()
171
172 {
173 const attribute = {
174 name: 'private video becomes public',
175 privacy: VideoPrivacy.PUBLIC
176 }
177
178 await updateVideo(servers[0].url, servers[0].accessToken, privateVideoId, attribute)
179 }
180
181 {
182 const attribute = {
183 name: 'internal video becomes public',
184 privacy: VideoPrivacy.PUBLIC
185 }
186 await updateVideo(servers[0].url, servers[0].accessToken, internalVideoId, attribute)
187 }
188
189 await waitJobs(servers)
190 })
191
192 it('Should have this new public video listed on server 1 and 2', async function () {
193 for (const server of servers) {
194 const res = await getVideosList(server.url)
195 expect(res.body.total).to.equal(2)
196 expect(res.body.data).to.have.lengthOf(2)
197
198 const videos: Video[] = res.body.data
199 const privateVideo = videos.find(v => v.name === 'private video becomes public')
200 const internalVideo = videos.find(v => v.name === 'internal video becomes public')
201
202 expect(privateVideo).to.not.be.undefined
203 expect(internalVideo).to.not.be.undefined
204
205 expect(new Date(privateVideo.publishedAt).getTime()).to.be.at.least(now)
206 // We don't change the publish date of internal videos
207 expect(new Date(internalVideo.publishedAt).getTime()).to.be.below(now)
208
209 expect(privateVideo.privacy.id).to.equal(VideoPrivacy.PUBLIC)
210 expect(internalVideo.privacy.id).to.equal(VideoPrivacy.PUBLIC)
211 }
212 })
213
214 it('Should set these videos as private and internal', async function () {
215 this.timeout(10000)
216
217 await updateVideo(servers[0].url, servers[0].accessToken, internalVideoId, { privacy: VideoPrivacy.PRIVATE })
218 await updateVideo(servers[0].url, servers[0].accessToken, privateVideoId, { privacy: VideoPrivacy.INTERNAL })
219
220 await waitJobs(servers)
221
222 for (const server of servers) {
223 const res = await getVideosList(server.url)
224
225 expect(res.body.total).to.equal(0)
226 expect(res.body.data).to.have.lengthOf(0)
227 }
228
229 {
230 const res = await getMyVideos(servers[0].url, servers[0].accessToken, 0, 5)
231 const videos = res.body.data
232
233 expect(res.body.total).to.equal(2)
234 expect(videos).to.have.lengthOf(2)
235
236 const privateVideo = videos.find(v => v.name === 'private video becomes public')
237 const internalVideo = videos.find(v => v.name === 'internal video becomes public')
238
239 expect(privateVideo).to.not.be.undefined
240 expect(internalVideo).to.not.be.undefined
241
242 expect(privateVideo.privacy.id).to.equal(VideoPrivacy.INTERNAL)
243 expect(internalVideo.privacy.id).to.equal(VideoPrivacy.PRIVATE)
244 }
245 })
246
247 after(async function () {
248 await cleanupTests(servers)
249 })
250 })