]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/tests/api/videos/video-nsfw.ts
Introduce blacklist command
[github/Chocobozzz/PeerTube.git] / server / tests / api / videos / video-nsfw.ts
CommitLineData
a1587156 1/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
0883b324 2
0883b324 3import 'mocha'
23a3a882 4import * as chai from 'chai'
0883b324 5import {
23a3a882
C
6 cleanupTests,
7 createUser,
7c3b7976 8 flushAndRunServer,
6b738c7a 9 getAccountVideos,
3cd0734f 10 getMyUserInformation,
23a3a882 11 getMyVideos,
3cd0734f 12 getVideoChannelVideos,
23a3a882 13 getVideosList,
0883b324 14 getVideosListWithToken,
23a3a882
C
15 ServerInfo,
16 setAccessTokensToServers,
23a3a882
C
17 updateMyUser,
18 uploadVideo,
19 userLogin
20} from '@shared/extra-utils'
65e6e260 21import { BooleanBothQuery, CustomConfig, User, VideosOverview } from '@shared/models'
0883b324
C
22
23const expect = chai.expect
24
23a3a882 25function createOverviewRes (overview: VideosOverview) {
764a9657
C
26 const videos = overview.categories[0].videos
27 return { body: { data: videos, total: videos.length } }
28}
29
0883b324
C
30describe('Test video NSFW policy', function () {
31 let server: ServerInfo
32 let userAccessToken: string
33 let customConfig: CustomConfig
34
af971e06 35 function getVideosFunctions (token?: string, query: { nsfw?: BooleanBothQuery } = {}) {
6b738c7a
C
36 return getMyUserInformation(server.url, server.accessToken)
37 .then(res => {
38 const user: User = res.body
240085d0 39 const videoChannelName = user.videoChannels[0].name
ad9e39fb 40 const accountName = user.account.name + '@' + user.account.host
764a9657
C
41 const hasQuery = Object.keys(query).length !== 0
42 let promises: Promise<any>[]
6b738c7a
C
43
44 if (token) {
764a9657 45 promises = [
d525fc39 46 getVideosListWithToken(server.url, token, query),
af971e06 47 server.searchCommand.advancedVideoSearch({ token, search: { search: 'n', ...query } }),
d525fc39 48 getAccountVideos(server.url, token, accountName, 0, 5, undefined, query),
240085d0 49 getVideoChannelVideos(server.url, token, videoChannelName, 0, 5, undefined, query)
764a9657
C
50 ]
51
52 // Overviews do not support video filters
53 if (!hasQuery) {
23a3a882
C
54 const p = server.overviewsCommand.getVideos({ page: 1, token })
55 .then(res => createOverviewRes(res))
56 promises.push(p)
764a9657
C
57 }
58
59 return Promise.all(promises)
6b738c7a
C
60 }
61
764a9657 62 promises = [
6b738c7a 63 getVideosList(server.url),
af971e06 64 server.searchCommand.searchVideos({ search: 'n' }),
ad9e39fb 65 getAccountVideos(server.url, undefined, accountName, 0, 5),
240085d0 66 getVideoChannelVideos(server.url, undefined, videoChannelName, 0, 5)
764a9657
C
67 ]
68
69 // Overviews do not support video filters
70 if (!hasQuery) {
23a3a882
C
71 const p = server.overviewsCommand.getVideos({ page: 1 })
72 .then(res => createOverviewRes(res))
73 promises.push(p)
764a9657
C
74 }
75
76 return Promise.all(promises)
6b738c7a
C
77 })
78 }
79
0883b324
C
80 before(async function () {
81 this.timeout(50000)
210feb6c 82 server = await flushAndRunServer(1)
0883b324
C
83
84 // Get the access tokens
85 await setAccessTokensToServers([ server ])
86
87 {
764a9657 88 const attributes = { name: 'nsfw', nsfw: true, category: 1 }
0883b324
C
89 await uploadVideo(server.url, server.accessToken, attributes)
90 }
91
92 {
764a9657 93 const attributes = { name: 'normal', nsfw: false, category: 1 }
0883b324
C
94 await uploadVideo(server.url, server.accessToken, attributes)
95 }
96
65e6e260 97 customConfig = await server.configCommand.getCustomConfig()
0883b324
C
98 })
99
100 describe('Instance default NSFW policy', function () {
101 it('Should display NSFW videos with display default NSFW policy', async function () {
65e6e260 102 const serverConfig = await server.configCommand.getConfig()
0883b324
C
103 expect(serverConfig.instance.defaultNSFWPolicy).to.equal('display')
104
6b738c7a 105 for (const res of await getVideosFunctions()) {
0883b324
C
106 expect(res.body.total).to.equal(2)
107
108 const videos = res.body.data
109 expect(videos).to.have.lengthOf(2)
a1587156
C
110 expect(videos[0].name).to.equal('normal')
111 expect(videos[1].name).to.equal('nsfw')
0883b324
C
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'
65e6e260 117 await server.configCommand.updateCustomConfig({ newCustomConfig: customConfig })
0883b324 118
65e6e260 119 const serverConfig = await server.configCommand.getConfig()
0883b324
C
120 expect(serverConfig.instance.defaultNSFWPolicy).to.equal('do_not_list')
121
6b738c7a 122 for (const res of await getVideosFunctions()) {
0883b324
C
123 expect(res.body.total).to.equal(1)
124
125 const videos = res.body.data
126 expect(videos).to.have.lengthOf(1)
a1587156 127 expect(videos[0].name).to.equal('normal')
0883b324
C
128 }
129 })
130
131 it('Should display NSFW videos with blur default NSFW policy', async function () {
132 customConfig.instance.defaultNSFWPolicy = 'blur'
65e6e260 133 await server.configCommand.updateCustomConfig({ newCustomConfig: customConfig })
0883b324 134
65e6e260 135 const serverConfig = await server.configCommand.getConfig()
0883b324
C
136 expect(serverConfig.instance.defaultNSFWPolicy).to.equal('blur')
137
6b738c7a 138 for (const res of await getVideosFunctions()) {
0883b324
C
139 expect(res.body.total).to.equal(2)
140
141 const videos = res.body.data
142 expect(videos).to.have.lengthOf(2)
a1587156
C
143 expect(videos[0].name).to.equal('normal')
144 expect(videos[1].name).to.equal('nsfw')
0883b324
C
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'
1eddc9a7 154 await createUser({ url: server.url, accessToken: server.accessToken, username: username, password: password })
0883b324
C
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 () {
eb87f9a4 165 customConfig.instance.defaultNSFWPolicy = 'do_not_list'
65e6e260 166 await server.configCommand.updateCustomConfig({ newCustomConfig: customConfig })
eb87f9a4 167
6b738c7a 168 for (const res of await getVideosFunctions(userAccessToken)) {
0883b324
C
169 expect(res.body.total).to.equal(2)
170
171 const videos = res.body.data
172 expect(videos).to.have.lengthOf(2)
a1587156
C
173 expect(videos[0].name).to.equal('normal')
174 expect(videos[1].name).to.equal('nsfw')
0883b324
C
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
6b738c7a 185 for (const res of await getVideosFunctions(server.accessToken)) {
0883b324
C
186 expect(res.body.total).to.equal(2)
187
188 const videos = res.body.data
189 expect(videos).to.have.lengthOf(2)
a1587156
C
190 expect(videos[0].name).to.equal('normal')
191 expect(videos[1].name).to.equal('nsfw')
0883b324
C
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
6b738c7a 202 for (const res of await getVideosFunctions(server.accessToken)) {
0883b324
C
203 expect(res.body.total).to.equal(1)
204
205 const videos = res.body.data
206 expect(videos).to.have.lengthOf(1)
a1587156 207 expect(videos[0].name).to.equal('normal')
0883b324
C
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)
a1587156
C
217 expect(videos[0].name).to.equal('normal')
218 expect(videos[1].name).to.equal('nsfw')
0883b324 219 })
d525fc39
C
220
221 it('Should display NSFW videos when the nsfw param === true', async function () {
af971e06 222 for (const res of await getVideosFunctions(server.accessToken, { nsfw: 'true' })) {
d525fc39
C
223 expect(res.body.total).to.equal(1)
224
225 const videos = res.body.data
226 expect(videos).to.have.lengthOf(1)
a1587156 227 expect(videos[0].name).to.equal('nsfw')
d525fc39
C
228 }
229 })
230
231 it('Should hide NSFW videos when the nsfw param === true', async function () {
af971e06 232 for (const res of await getVideosFunctions(server.accessToken, { nsfw: 'false' })) {
d525fc39
C
233 expect(res.body.total).to.equal(1)
234
235 const videos = res.body.data
236 expect(videos).to.have.lengthOf(1)
a1587156 237 expect(videos[0].name).to.equal('normal')
d525fc39
C
238 }
239 })
0b18f4aa
C
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)
a1587156
C
247 expect(videos[0].name).to.equal('normal')
248 expect(videos[1].name).to.equal('nsfw')
0b18f4aa
C
249 }
250 })
0883b324
C
251 })
252
7c3b7976
C
253 after(async function () {
254 await cleanupTests([ server ])
0883b324
C
255 })
256})