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