]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/tests/api/videos/video-nsfw.ts
Move AP request in requests file
[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 getConfig,
11 getCustomConfig,
12 getMyUserInformation,
13 getMyVideos,
14 getVideoChannelVideos,
15 getVideosList,
16 getVideosListWithToken,
17 ServerInfo,
18 setAccessTokensToServers,
19 updateCustomConfig,
20 updateMyUser,
21 uploadVideo,
22 userLogin
23 } from '@shared/extra-utils'
24 import { BooleanBothQuery, CustomConfig, ServerConfig, User, VideosOverview } from '@shared/models'
25
26 const expect = chai.expect
27
28 function createOverviewRes (overview: VideosOverview) {
29 const videos = overview.categories[0].videos
30 return { body: { data: videos, total: videos.length } }
31 }
32
33 describe('Test video NSFW policy', function () {
34 let server: ServerInfo
35 let userAccessToken: string
36 let customConfig: CustomConfig
37
38 function getVideosFunctions (token?: string, query: { nsfw?: BooleanBothQuery } = {}) {
39 return getMyUserInformation(server.url, server.accessToken)
40 .then(res => {
41 const user: User = res.body
42 const videoChannelName = user.videoChannels[0].name
43 const accountName = user.account.name + '@' + user.account.host
44 const hasQuery = Object.keys(query).length !== 0
45 let promises: Promise<any>[]
46
47 if (token) {
48 promises = [
49 getVideosListWithToken(server.url, token, query),
50 server.searchCommand.advancedVideoSearch({ token, search: { search: 'n', ...query } }),
51 getAccountVideos(server.url, token, accountName, 0, 5, undefined, query),
52 getVideoChannelVideos(server.url, token, videoChannelName, 0, 5, undefined, query)
53 ]
54
55 // Overviews do not support video filters
56 if (!hasQuery) {
57 const p = server.overviewsCommand.getVideos({ page: 1, token })
58 .then(res => createOverviewRes(res))
59 promises.push(p)
60 }
61
62 return Promise.all(promises)
63 }
64
65 promises = [
66 getVideosList(server.url),
67 server.searchCommand.searchVideos({ search: 'n' }),
68 getAccountVideos(server.url, undefined, accountName, 0, 5),
69 getVideoChannelVideos(server.url, undefined, videoChannelName, 0, 5)
70 ]
71
72 // Overviews do not support video filters
73 if (!hasQuery) {
74 const p = server.overviewsCommand.getVideos({ page: 1 })
75 .then(res => createOverviewRes(res))
76 promises.push(p)
77 }
78
79 return Promise.all(promises)
80 })
81 }
82
83 before(async function () {
84 this.timeout(50000)
85 server = await flushAndRunServer(1)
86
87 // Get the access tokens
88 await setAccessTokensToServers([ server ])
89
90 {
91 const attributes = { name: 'nsfw', nsfw: true, category: 1 }
92 await uploadVideo(server.url, server.accessToken, attributes)
93 }
94
95 {
96 const attributes = { name: 'normal', nsfw: false, category: 1 }
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
112 for (const res of await getVideosFunctions()) {
113 expect(res.body.total).to.equal(2)
114
115 const videos = res.body.data
116 expect(videos).to.have.lengthOf(2)
117 expect(videos[0].name).to.equal('normal')
118 expect(videos[1].name).to.equal('nsfw')
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
130 for (const res of await getVideosFunctions()) {
131 expect(res.body.total).to.equal(1)
132
133 const videos = res.body.data
134 expect(videos).to.have.lengthOf(1)
135 expect(videos[0].name).to.equal('normal')
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
147 for (const res of await getVideosFunctions()) {
148 expect(res.body.total).to.equal(2)
149
150 const videos = res.body.data
151 expect(videos).to.have.lengthOf(2)
152 expect(videos[0].name).to.equal('normal')
153 expect(videos[1].name).to.equal('nsfw')
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'
163 await createUser({ url: server.url, accessToken: server.accessToken, username: username, password: password })
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 () {
174 customConfig.instance.defaultNSFWPolicy = 'do_not_list'
175 await updateCustomConfig(server.url, server.accessToken, customConfig)
176
177 for (const res of await getVideosFunctions(userAccessToken)) {
178 expect(res.body.total).to.equal(2)
179
180 const videos = res.body.data
181 expect(videos).to.have.lengthOf(2)
182 expect(videos[0].name).to.equal('normal')
183 expect(videos[1].name).to.equal('nsfw')
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
194 for (const res of await getVideosFunctions(server.accessToken)) {
195 expect(res.body.total).to.equal(2)
196
197 const videos = res.body.data
198 expect(videos).to.have.lengthOf(2)
199 expect(videos[0].name).to.equal('normal')
200 expect(videos[1].name).to.equal('nsfw')
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
211 for (const res of await getVideosFunctions(server.accessToken)) {
212 expect(res.body.total).to.equal(1)
213
214 const videos = res.body.data
215 expect(videos).to.have.lengthOf(1)
216 expect(videos[0].name).to.equal('normal')
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)
226 expect(videos[0].name).to.equal('normal')
227 expect(videos[1].name).to.equal('nsfw')
228 })
229
230 it('Should display NSFW videos when the nsfw param === true', async function () {
231 for (const res of await getVideosFunctions(server.accessToken, { nsfw: 'true' })) {
232 expect(res.body.total).to.equal(1)
233
234 const videos = res.body.data
235 expect(videos).to.have.lengthOf(1)
236 expect(videos[0].name).to.equal('nsfw')
237 }
238 })
239
240 it('Should hide NSFW videos when the nsfw param === true', async function () {
241 for (const res of await getVideosFunctions(server.accessToken, { nsfw: 'false' })) {
242 expect(res.body.total).to.equal(1)
243
244 const videos = res.body.data
245 expect(videos).to.have.lengthOf(1)
246 expect(videos[0].name).to.equal('normal')
247 }
248 })
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)
256 expect(videos[0].name).to.equal('normal')
257 expect(videos[1].name).to.equal('nsfw')
258 }
259 })
260 })
261
262 after(async function () {
263 await cleanupTests([ server ])
264 })
265 })