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