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