Merge branch 'develop' of https://github.com/Chocobozzz/PeerTube into move-utils...
[oweals/peertube.git] / server / tests / api / videos / video-nsfw.ts
1 /* tslint:disable:no-unused-expression */
2
3 import * as chai from 'chai'
4 import 'mocha'
5 import {
6   flushTests,
7   getVideosList,
8   killallServers,
9   ServerInfo,
10   setAccessTokensToServers,
11   uploadVideo
12 } from '../../../../shared/utils/index'
13 import { userLogin } from '../../../../shared/utils/users/login'
14 import { createUser } from '../../../../shared/utils/users/users'
15 import { getMyVideos } from '../../../../shared/utils/videos/videos'
16 import {
17   getAccountVideos,
18   getConfig,
19   getCustomConfig,
20   getMyUserInformation,
21   getVideoChannelVideos,
22   getVideosListWithToken,
23   runServer,
24   searchVideo,
25   searchVideoWithToken,
26   updateCustomConfig,
27   updateMyUser
28 } from '../../../../shared/utils'
29 import { ServerConfig } from '../../../../shared/models'
30 import { CustomConfig } from '../../../../shared/models/server/custom-config.model'
31 import { User } from '../../../../shared/models/users'
32
33 const expect = chai.expect
34
35 describe('Test video NSFW policy', function () {
36   let server: ServerInfo
37   let userAccessToken: string
38   let customConfig: CustomConfig
39
40   function getVideosFunctions (token?: string, query = {}) {
41     return getMyUserInformation(server.url, server.accessToken)
42       .then(res => {
43         const user: User = res.body
44         const videoChannelName = user.videoChannels[0].name
45         const accountName = user.account.name + '@' + user.account.host
46
47         if (token) {
48           return Promise.all([
49             getVideosListWithToken(server.url, token, query),
50             searchVideoWithToken(server.url, 'n', token, query),
51             getAccountVideos(server.url, token, accountName, 0, 5, undefined, query),
52             getVideoChannelVideos(server.url, token, videoChannelName, 0, 5, undefined, query)
53           ])
54         }
55
56         return Promise.all([
57           getVideosList(server.url),
58           searchVideo(server.url, 'n'),
59           getAccountVideos(server.url, undefined, accountName, 0, 5),
60           getVideoChannelVideos(server.url, undefined, videoChannelName, 0, 5)
61         ])
62       })
63   }
64
65   before(async function () {
66     this.timeout(50000)
67
68     await flushTests()
69     server = await runServer(1)
70
71     // Get the access tokens
72     await setAccessTokensToServers([ server ])
73
74     {
75       const attributes = { name: 'nsfw', nsfw: true }
76       await uploadVideo(server.url, server.accessToken, attributes)
77     }
78
79     {
80       const attributes = { name: 'normal', nsfw: false }
81       await uploadVideo(server.url, server.accessToken, attributes)
82     }
83
84     {
85       const res = await getCustomConfig(server.url, server.accessToken)
86       customConfig = res.body
87     }
88   })
89
90   describe('Instance default NSFW policy', function () {
91     it('Should display NSFW videos with display default NSFW policy', async function () {
92       const resConfig = await getConfig(server.url)
93       const serverConfig: ServerConfig = resConfig.body
94       expect(serverConfig.instance.defaultNSFWPolicy).to.equal('display')
95
96       for (const res of await getVideosFunctions()) {
97         expect(res.body.total).to.equal(2)
98
99         const videos = res.body.data
100         expect(videos).to.have.lengthOf(2)
101         expect(videos[ 0 ].name).to.equal('normal')
102         expect(videos[ 1 ].name).to.equal('nsfw')
103       }
104     })
105
106     it('Should not display NSFW videos with do_not_list default NSFW policy', async function () {
107       customConfig.instance.defaultNSFWPolicy = 'do_not_list'
108       await updateCustomConfig(server.url, server.accessToken, customConfig)
109
110       const resConfig = await getConfig(server.url)
111       const serverConfig: ServerConfig = resConfig.body
112       expect(serverConfig.instance.defaultNSFWPolicy).to.equal('do_not_list')
113
114       for (const res of await getVideosFunctions()) {
115         expect(res.body.total).to.equal(1)
116
117         const videos = res.body.data
118         expect(videos).to.have.lengthOf(1)
119         expect(videos[ 0 ].name).to.equal('normal')
120       }
121     })
122
123     it('Should display NSFW videos with blur default NSFW policy', async function () {
124       customConfig.instance.defaultNSFWPolicy = 'blur'
125       await updateCustomConfig(server.url, server.accessToken, customConfig)
126
127       const resConfig = await getConfig(server.url)
128       const serverConfig: ServerConfig = resConfig.body
129       expect(serverConfig.instance.defaultNSFWPolicy).to.equal('blur')
130
131       for (const res of await getVideosFunctions()) {
132         expect(res.body.total).to.equal(2)
133
134         const videos = res.body.data
135         expect(videos).to.have.lengthOf(2)
136         expect(videos[ 0 ].name).to.equal('normal')
137         expect(videos[ 1 ].name).to.equal('nsfw')
138       }
139     })
140   })
141
142   describe('User NSFW policy', function () {
143
144     it('Should create a user having the default nsfw policy', async function () {
145       const username = 'user1'
146       const password = 'my super password'
147       await createUser(server.url, server.accessToken, username, password)
148
149       userAccessToken = await userLogin(server, { username, password })
150
151       const res = await getMyUserInformation(server.url, userAccessToken)
152       const user = res.body
153
154       expect(user.nsfwPolicy).to.equal('blur')
155     })
156
157     it('Should display NSFW videos with blur user NSFW policy', async function () {
158       customConfig.instance.defaultNSFWPolicy = 'do_not_list'
159       await updateCustomConfig(server.url, server.accessToken, customConfig)
160
161       for (const res of await getVideosFunctions(userAccessToken)) {
162         expect(res.body.total).to.equal(2)
163
164         const videos = res.body.data
165         expect(videos).to.have.lengthOf(2)
166         expect(videos[ 0 ].name).to.equal('normal')
167         expect(videos[ 1 ].name).to.equal('nsfw')
168       }
169     })
170
171     it('Should display NSFW videos with display user NSFW policy', async function () {
172       await updateMyUser({
173         url: server.url,
174         accessToken: server.accessToken,
175         nsfwPolicy: 'display'
176       })
177
178       for (const res of await getVideosFunctions(server.accessToken)) {
179         expect(res.body.total).to.equal(2)
180
181         const videos = res.body.data
182         expect(videos).to.have.lengthOf(2)
183         expect(videos[ 0 ].name).to.equal('normal')
184         expect(videos[ 1 ].name).to.equal('nsfw')
185       }
186     })
187
188     it('Should not display NSFW videos with do_not_list user NSFW policy', async function () {
189       await updateMyUser({
190         url: server.url,
191         accessToken: server.accessToken,
192         nsfwPolicy: 'do_not_list'
193       })
194
195       for (const res of await getVideosFunctions(server.accessToken)) {
196         expect(res.body.total).to.equal(1)
197
198         const videos = res.body.data
199         expect(videos).to.have.lengthOf(1)
200         expect(videos[ 0 ].name).to.equal('normal')
201       }
202     })
203
204     it('Should be able to see my NSFW videos even with do_not_list user NSFW policy', async function () {
205       const res = await getMyVideos(server.url, server.accessToken, 0, 5)
206       expect(res.body.total).to.equal(2)
207
208       const videos = res.body.data
209       expect(videos).to.have.lengthOf(2)
210       expect(videos[ 0 ].name).to.equal('normal')
211       expect(videos[ 1 ].name).to.equal('nsfw')
212     })
213
214     it('Should display NSFW videos when the nsfw param === true', async function () {
215       for (const res of await getVideosFunctions(server.accessToken, { nsfw: true })) {
216         expect(res.body.total).to.equal(1)
217
218         const videos = res.body.data
219         expect(videos).to.have.lengthOf(1)
220         expect(videos[ 0 ].name).to.equal('nsfw')
221       }
222     })
223
224     it('Should hide NSFW videos when the nsfw param === true', async function () {
225       for (const res of await getVideosFunctions(server.accessToken, { nsfw: false })) {
226         expect(res.body.total).to.equal(1)
227
228         const videos = res.body.data
229         expect(videos).to.have.lengthOf(1)
230         expect(videos[ 0 ].name).to.equal('normal')
231       }
232     })
233
234     it('Should display both videos when the nsfw param === both', async function () {
235       for (const res of await getVideosFunctions(server.accessToken, { nsfw: 'both' })) {
236         expect(res.body.total).to.equal(2)
237
238         const videos = res.body.data
239         expect(videos).to.have.lengthOf(2)
240         expect(videos[ 0 ].name).to.equal('normal')
241         expect(videos[ 1 ].name).to.equal('nsfw')
242       }
243     })
244   })
245
246   after(async function () {
247     killallServers([ server ])
248   })
249 })