1 /* tslint:disable:no-unused-expression */
3 import * as chai from 'chai'
5 import { VideoPrivacy } from '../../../../shared/models/videos/video-privacy.enum'
8 flushAndRunMultipleServers,
9 getVideosList, getVideosListWithToken,
11 setAccessTokensToServers,
13 } from '../../../../shared/extra-utils/index'
14 import { doubleFollow } from '../../../../shared/extra-utils/server/follows'
15 import { userLogin } from '../../../../shared/extra-utils/users/login'
16 import { createUser } from '../../../../shared/extra-utils/users/users'
17 import { getMyVideos, getVideo, getVideoWithToken, updateVideo } from '../../../../shared/extra-utils/videos/videos'
18 import { waitJobs } from '../../../../shared/extra-utils/server/jobs'
19 import { Video } from '@shared/models'
21 const expect = chai.expect
23 describe('Test video privacy', function () {
24 let servers: ServerInfo[] = []
25 let anotherUserToken: string
27 let privateVideoId: number
28 let privateVideoUUID: string
30 let internalVideoId: number
31 let internalVideoUUID: string
33 let unlistedVideoUUID: string
37 before(async function () {
41 servers = await flushAndRunMultipleServers(2)
43 // Get the access tokens
44 await setAccessTokensToServers(servers)
46 // Server 1 and server 2 follow each other
47 await doubleFollow(servers[0], servers[1])
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 uploadVideo(servers[0].url, servers[0].accessToken, attributes)
58 await waitJobs(servers)
61 it('Should not have these private and internal videos on server 2', async function () {
62 const res = await getVideosList(servers[1].url)
64 expect(res.body.total).to.equal(0)
65 expect(res.body.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 res = await getVideosList(servers[0].url)
71 expect(res.body.total).to.equal(0)
72 expect(res.body.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 res = await getVideosListWithToken(servers[0].url, servers[0].accessToken)
78 expect(res.body.total).to.equal(1)
79 expect(res.body.data).to.have.lengthOf(1)
81 expect(res.body.data[0].privacy.id).to.equal(VideoPrivacy.INTERNAL)
84 it('Should list my (private and internal) videos', async function () {
85 const res = await getMyVideos(servers[0].url, servers[0].accessToken, 0, 10)
87 expect(res.body.total).to.equal(2)
88 expect(res.body.data).to.have.lengthOf(2)
90 const videos: Video[] = res.body.data
92 const privateVideo = videos.find(v => v.privacy.id === VideoPrivacy.PRIVATE)
93 privateVideoId = privateVideo.id
94 privateVideoUUID = privateVideo.uuid
96 const internalVideo = videos.find(v => v.privacy.id === VideoPrivacy.INTERNAL)
97 internalVideoId = internalVideo.id
98 internalVideoUUID = internalVideo.uuid
101 it('Should not be able to watch the private/internal video with non authenticated user', async function () {
102 await getVideo(servers[0].url, privateVideoUUID, 401)
103 await getVideo(servers[0].url, internalVideoUUID, 401)
106 it('Should not be able to watch the private video with another user', async function () {
111 password: 'super password'
113 await createUser({ url: servers[ 0 ].url, accessToken: servers[ 0 ].accessToken, username: user.username, password: user.password })
115 anotherUserToken = await userLogin(servers[0], user)
116 await getVideoWithToken(servers[0].url, anotherUserToken, privateVideoUUID, 403)
119 it('Should be able to watch the internal video with another user', async function () {
120 await getVideoWithToken(servers[0].url, anotherUserToken, internalVideoUUID, 200)
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)
127 it('Should upload an unlisted video on server 2', async function () {
131 name: 'unlisted video',
132 privacy: VideoPrivacy.UNLISTED
134 await uploadVideo(servers[1].url, servers[1].accessToken, attributes)
136 // Server 2 has transcoding enabled
137 await waitJobs(servers)
140 it('Should not have this unlisted video listed on server 1 and 2', async function () {
141 for (const server of servers) {
142 const res = await getVideosList(server.url)
144 expect(res.body.total).to.equal(0)
145 expect(res.body.data).to.have.lengthOf(0)
149 it('Should list my (unlisted) videos', async function () {
150 const res = await getMyVideos(servers[1].url, servers[1].accessToken, 0, 1)
152 expect(res.body.total).to.equal(1)
153 expect(res.body.data).to.have.lengthOf(1)
155 unlistedVideoUUID = res.body.data[0].uuid
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)
162 expect(res.body.name).to.equal('unlisted video')
166 it('Should update the private and internal videos to public on server 1', async function () {
173 name: 'private video becomes public',
174 privacy: VideoPrivacy.PUBLIC
177 await updateVideo(servers[ 0 ].url, servers[ 0 ].accessToken, privateVideoId, attribute)
182 name: 'internal video becomes public',
183 privacy: VideoPrivacy.PUBLIC
185 await updateVideo(servers[ 0 ].url, servers[ 0 ].accessToken, internalVideoId, attribute)
188 await waitJobs(servers)
191 it('Should have this new public video listed on server 1 and 2', async function () {
192 for (const server of servers) {
193 const res = await getVideosList(server.url)
194 expect(res.body.total).to.equal(2)
195 expect(res.body.data).to.have.lengthOf(2)
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')
201 expect(privateVideo).to.not.be.undefined
202 expect(internalVideo).to.not.be.undefined
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)
208 expect(privateVideo.privacy.id).to.equal(VideoPrivacy.PUBLIC)
209 expect(internalVideo.privacy.id).to.equal(VideoPrivacy.PUBLIC)
213 it('Should set these videos as private and internal', async function () {
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 })
219 await waitJobs(servers)
221 for (const server of servers) {
222 const res = await getVideosList(server.url)
224 expect(res.body.total).to.equal(0)
225 expect(res.body.data).to.have.lengthOf(0)
229 const res = await getMyVideos(servers[0].url, servers[0].accessToken, 0, 5)
230 const videos = res.body.data
232 expect(res.body.total).to.equal(2)
233 expect(videos).to.have.lengthOf(2)
235 const privateVideo = videos.find(v => v.name === 'private video becomes public')
236 const internalVideo = videos.find(v => v.name === 'internal video becomes public')
238 expect(privateVideo).to.not.be.undefined
239 expect(internalVideo).to.not.be.undefined
241 expect(privateVideo.privacy.id).to.equal(VideoPrivacy.INTERNAL)
242 expect(internalVideo.privacy.id).to.equal(VideoPrivacy.PRIVATE)
246 after(async function () {
247 await cleanupTests(servers)