Tests for totalRepliesFromVideoAuthor
[oweals/peertube.git] / server / tests / api / videos / video-privacy.ts
1 /* tslint:disable:no-unused-expression */
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, getVideosListWithToken,
10   ServerInfo,
11   setAccessTokensToServers,
12   uploadVideo
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'
20
21 const expect = chai.expect
22
23 describe('Test video privacy', function () {
24   let servers: ServerInfo[] = []
25   let anotherUserToken: string
26
27   let privateVideoId: number
28   let privateVideoUUID: string
29
30   let internalVideoId: number
31   let internalVideoUUID: string
32
33   let unlistedVideoUUID: string
34
35   let now: number
36
37   before(async function () {
38     this.timeout(50000)
39
40     // Run servers
41     servers = await flushAndRunMultipleServers(2)
42
43     // Get the access tokens
44     await setAccessTokensToServers(servers)
45
46     // Server 1 and server 2 follow each other
47     await doubleFollow(servers[0], servers[1])
48   })
49
50   it('Should upload a private and internal videos on server 1', async function () {
51     this.timeout(10000)
52
53     for (const privacy of [ VideoPrivacy.PRIVATE, VideoPrivacy.INTERNAL ]) {
54       const attributes = { privacy }
55       await uploadVideo(servers[0].url, servers[0].accessToken, attributes)
56     }
57
58     await waitJobs(servers)
59   })
60
61   it('Should not have these private and internal videos on server 2', async function () {
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
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)
77
78     expect(res.body.total).to.equal(1)
79     expect(res.body.data).to.have.lengthOf(1)
80
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
99   })
100
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)
104   })
105
106   it('Should not be able to watch the private video with another user', async function () {
107     this.timeout(10000)
108
109     const user = {
110       username: 'hello',
111       password: 'super password'
112     }
113     await createUser({ url: servers[ 0 ].url, accessToken: servers[ 0 ].accessToken, username: user.username, password: user.password })
114
115     anotherUserToken = await userLogin(servers[0], user)
116     await getVideoWithToken(servers[0].url, anotherUserToken, privateVideoUUID, 403)
117   })
118
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)
125   })
126
127   it('Should upload an unlisted video on server 2', async function () {
128     this.timeout(30000)
129
130     const attributes = {
131       name: 'unlisted video',
132       privacy: VideoPrivacy.UNLISTED
133     }
134     await uploadVideo(servers[1].url, servers[1].accessToken, attributes)
135
136     // Server 2 has transcoding enabled
137     await waitJobs(servers)
138   })
139
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)
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
166   it('Should update the private and internal videos to public on server 1', async function () {
167     this.timeout(10000)
168
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)
178     }
179
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     }
187
188     await waitJobs(servers)
189   })
190
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)
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)
207
208       expect(privateVideo.privacy.id).to.equal(VideoPrivacy.PUBLIC)
209       expect(internalVideo.privacy.id).to.equal(VideoPrivacy.PUBLIC)
210     }
211   })
212
213   it('Should set these videos as private and internal', async function () {
214     this.timeout(10000)
215
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 })
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)
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
240
241       expect(privateVideo.privacy.id).to.equal(VideoPrivacy.INTERNAL)
242       expect(internalVideo.privacy.id).to.equal(VideoPrivacy.PRIVATE)
243     }
244   })
245
246   after(async function () {
247     await cleanupTests(servers)
248   })
249 })