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