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