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