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