Merge from upstream
[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   flushAndRunMultipleServers,
8   getVideosList,
9   killallServers,
10   ServerInfo,
11   setAccessTokensToServers,
12   uploadVideo
13 } from '../../../../shared/utils/index'
14 import { doubleFollow } from '../../../../shared/utils/server/follows'
15 import { userLogin } from '../../../../shared/utils/users/login'
16 import { createUser } from '../../../../shared/utils/users/users'
17 import { getMyVideos, getVideo, getVideoWithToken, updateVideo } from '../../../../shared/utils/videos/videos'
18 import { waitJobs } from '../../../../shared/utils/server/jobs'
19
20 const expect = chai.expect
21
22 describe('Test video privacy', function () {
23   let servers: ServerInfo[] = []
24   let privateVideoId: number
25   let privateVideoUUID: string
26   let unlistedVideoUUID: string
27   let now: number
28
29   before(async function () {
30     this.timeout(50000)
31
32     // Run servers
33     servers = await flushAndRunMultipleServers(2)
34
35     // Get the access tokens
36     await setAccessTokensToServers(servers)
37
38     // Server 1 and server 2 follow each other
39     await doubleFollow(servers[0], servers[1])
40   })
41
42   it('Should upload a private video on server 1', async function () {
43     this.timeout(10000)
44
45     const attributes = {
46       privacy: VideoPrivacy.PRIVATE
47     }
48     await uploadVideo(servers[0].url, servers[0].accessToken, attributes)
49
50     await waitJobs(servers)
51   })
52
53   it('Should not have this private video on server 2', async function () {
54     const res = await getVideosList(servers[1].url)
55
56     expect(res.body.total).to.equal(0)
57     expect(res.body.data).to.have.lengthOf(0)
58   })
59
60   it('Should list my (private) videos', async function () {
61     const res = await getMyVideos(servers[0].url, servers[0].accessToken, 0, 1)
62
63     expect(res.body.total).to.equal(1)
64     expect(res.body.data).to.have.lengthOf(1)
65
66     privateVideoId = res.body.data[0].id
67     privateVideoUUID = res.body.data[0].uuid
68   })
69
70   it('Should not be able to watch this video with non authenticated user', async function () {
71     await getVideo(servers[0].url, privateVideoUUID, 401)
72   })
73
74   it('Should not be able to watch this private video with another user', async function () {
75     this.timeout(10000)
76
77     const user = {
78       username: 'hello',
79       password: 'super password'
80     }
81     await createUser(servers[0].url, servers[0].accessToken, user.username, user.password)
82
83     const token = await userLogin(servers[0], user)
84     await getVideoWithToken(servers[0].url, token, privateVideoUUID, 403)
85   })
86
87   it('Should be able to watch this video with the correct user', async function () {
88     await getVideoWithToken(servers[0].url, servers[0].accessToken, privateVideoUUID)
89   })
90
91   it('Should upload an unlisted video on server 2', async function () {
92     this.timeout(30000)
93
94     const attributes = {
95       name: 'unlisted video',
96       privacy: VideoPrivacy.UNLISTED
97     }
98     await uploadVideo(servers[1].url, servers[1].accessToken, attributes)
99
100     // Server 2 has transcoding enabled
101     await waitJobs(servers)
102   })
103
104   it('Should not have this unlisted video listed on server 1 and 2', async function () {
105     for (const server of servers) {
106       const res = await getVideosList(server.url)
107
108       expect(res.body.total).to.equal(0)
109       expect(res.body.data).to.have.lengthOf(0)
110     }
111   })
112
113   it('Should list my (unlisted) videos', async function () {
114     const res = await getMyVideos(servers[1].url, servers[1].accessToken, 0, 1)
115
116     expect(res.body.total).to.equal(1)
117     expect(res.body.data).to.have.lengthOf(1)
118
119     unlistedVideoUUID = res.body.data[0].uuid
120   })
121
122   it('Should be able to get this unlisted video', async function () {
123     for (const server of servers) {
124       const res = await getVideo(server.url, unlistedVideoUUID)
125
126       expect(res.body.name).to.equal('unlisted video')
127     }
128   })
129
130   it('Should update the private video to public on server 1', async function () {
131     this.timeout(10000)
132
133     const attribute = {
134       name: 'super video public',
135       privacy: VideoPrivacy.PUBLIC
136     }
137
138     now = Date.now()
139     await updateVideo(servers[0].url, servers[0].accessToken, privateVideoId, attribute)
140
141     await waitJobs(servers)
142   })
143
144   it('Should have this new public video listed on server 1 and 2', async function () {
145     for (const server of servers) {
146       const res = await getVideosList(server.url)
147
148       expect(res.body.total).to.equal(1)
149       expect(res.body.data).to.have.lengthOf(1)
150       expect(res.body.data[0].name).to.equal('super video public')
151       expect(new Date(res.body.data[0].publishedAt).getTime()).to.be.at.least(now)
152     }
153   })
154
155   after(async function () {
156     killallServers(servers)
157   })
158 })