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