]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/tests/api/videos/video-nsfw.ts
Introduce user 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 6 cleanupTests,
7c3b7976 7 flushAndRunServer,
6b738c7a 8 getAccountVideos,
23a3a882 9 getMyVideos,
3cd0734f 10 getVideoChannelVideos,
23a3a882 11 getVideosList,
0883b324 12 getVideosListWithToken,
23a3a882
C
13 ServerInfo,
14 setAccessTokensToServers,
41d1d075 15 uploadVideo
23a3a882 16} from '@shared/extra-utils'
7926c5f9 17import { BooleanBothQuery, CustomConfig, ResultList, Video, VideosOverview } from '@shared/models'
0883b324
C
18
19const expect = chai.expect
20
23a3a882 21function createOverviewRes (overview: VideosOverview) {
764a9657 22 const videos = overview.categories[0].videos
dd0ebb71 23 return { data: videos, total: videos.length }
764a9657
C
24}
25
0883b324
C
26describe('Test video NSFW policy', function () {
27 let server: ServerInfo
28 let userAccessToken: string
29 let customConfig: CustomConfig
30
dd0ebb71 31 async function getVideosFunctions (token?: string, query: { nsfw?: BooleanBothQuery } = {}) {
7926c5f9 32 const user = await server.usersCommand.getMyInfo()
dd0ebb71
C
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),
0d8ecb75 41 server.searchCommand.advancedVideoSearch({ token, search: { search: 'n', sort: '-publishedAt', ...query } }),
dd0ebb71
C
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),
0d8ecb75 58 server.searchCommand.searchVideos({ search: 'n', sort: '-publishedAt' }),
dd0ebb71
C
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)
6b738c7a
C
71 }
72
0883b324
C
73 before(async function () {
74 this.timeout(50000)
210feb6c 75 server = await flushAndRunServer(1)
0883b324
C
76
77 // Get the access tokens
78 await setAccessTokensToServers([ server ])
79
80 {
764a9657 81 const attributes = { name: 'nsfw', nsfw: true, category: 1 }
0883b324
C
82 await uploadVideo(server.url, server.accessToken, attributes)
83 }
84
85 {
764a9657 86 const attributes = { name: 'normal', nsfw: false, category: 1 }
0883b324
C
87 await uploadVideo(server.url, server.accessToken, attributes)
88 }
89
65e6e260 90 customConfig = await server.configCommand.getCustomConfig()
0883b324
C
91 })
92
93 describe('Instance default NSFW policy', function () {
94 it('Should display NSFW videos with display default NSFW policy', async function () {
65e6e260 95 const serverConfig = await server.configCommand.getConfig()
0883b324
C
96 expect(serverConfig.instance.defaultNSFWPolicy).to.equal('display')
97
dd0ebb71
C
98 for (const body of await getVideosFunctions()) {
99 expect(body.total).to.equal(2)
0883b324 100
dd0ebb71 101 const videos = body.data
0883b324 102 expect(videos).to.have.lengthOf(2)
a1587156
C
103 expect(videos[0].name).to.equal('normal')
104 expect(videos[1].name).to.equal('nsfw')
0883b324
C
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'
65e6e260 110 await server.configCommand.updateCustomConfig({ newCustomConfig: customConfig })
0883b324 111
65e6e260 112 const serverConfig = await server.configCommand.getConfig()
0883b324
C
113 expect(serverConfig.instance.defaultNSFWPolicy).to.equal('do_not_list')
114
dd0ebb71
C
115 for (const body of await getVideosFunctions()) {
116 expect(body.total).to.equal(1)
0883b324 117
dd0ebb71 118 const videos = body.data
0883b324 119 expect(videos).to.have.lengthOf(1)
a1587156 120 expect(videos[0].name).to.equal('normal')
0883b324
C
121 }
122 })
123
124 it('Should display NSFW videos with blur default NSFW policy', async function () {
125 customConfig.instance.defaultNSFWPolicy = 'blur'
65e6e260 126 await server.configCommand.updateCustomConfig({ newCustomConfig: customConfig })
0883b324 127
65e6e260 128 const serverConfig = await server.configCommand.getConfig()
0883b324
C
129 expect(serverConfig.instance.defaultNSFWPolicy).to.equal('blur')
130
dd0ebb71
C
131 for (const body of await getVideosFunctions()) {
132 expect(body.total).to.equal(2)
0883b324 133
dd0ebb71 134 const videos = body.data
0883b324 135 expect(videos).to.have.lengthOf(2)
a1587156
C
136 expect(videos[0].name).to.equal('normal')
137 expect(videos[1].name).to.equal('nsfw')
0883b324
C
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'
7926c5f9 147 await server.usersCommand.create({ username: username, password: password })
0883b324 148
41d1d075 149 userAccessToken = await server.loginCommand.getAccessToken({ username, password })
0883b324 150
7926c5f9 151 const user = await server.usersCommand.getMyInfo({ token: userAccessToken })
0883b324
C
152 expect(user.nsfwPolicy).to.equal('blur')
153 })
154
155 it('Should display NSFW videos with blur user NSFW policy', async function () {
eb87f9a4 156 customConfig.instance.defaultNSFWPolicy = 'do_not_list'
65e6e260 157 await server.configCommand.updateCustomConfig({ newCustomConfig: customConfig })
eb87f9a4 158
dd0ebb71
C
159 for (const body of await getVideosFunctions(userAccessToken)) {
160 expect(body.total).to.equal(2)
0883b324 161
dd0ebb71 162 const videos = body.data
0883b324 163 expect(videos).to.have.lengthOf(2)
a1587156
C
164 expect(videos[0].name).to.equal('normal')
165 expect(videos[1].name).to.equal('nsfw')
0883b324
C
166 }
167 })
168
169 it('Should display NSFW videos with display user NSFW policy', async function () {
7926c5f9 170 await server.usersCommand.updateMe({ nsfwPolicy: 'display' })
0883b324 171
dd0ebb71
C
172 for (const body of await getVideosFunctions(server.accessToken)) {
173 expect(body.total).to.equal(2)
0883b324 174
dd0ebb71 175 const videos = body.data
0883b324 176 expect(videos).to.have.lengthOf(2)
a1587156
C
177 expect(videos[0].name).to.equal('normal')
178 expect(videos[1].name).to.equal('nsfw')
0883b324
C
179 }
180 })
181
182 it('Should not display NSFW videos with do_not_list user NSFW policy', async function () {
7926c5f9 183 await server.usersCommand.updateMe({ nsfwPolicy: 'do_not_list' })
0883b324 184
dd0ebb71
C
185 for (const body of await getVideosFunctions(server.accessToken)) {
186 expect(body.total).to.equal(1)
0883b324 187
dd0ebb71 188 const videos = body.data
0883b324 189 expect(videos).to.have.lengthOf(1)
a1587156 190 expect(videos[0].name).to.equal('normal')
0883b324
C
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)
a1587156
C
200 expect(videos[0].name).to.equal('normal')
201 expect(videos[1].name).to.equal('nsfw')
0883b324 202 })
d525fc39
C
203
204 it('Should display NSFW videos when the nsfw param === true', async function () {
dd0ebb71
C
205 for (const body of await getVideosFunctions(server.accessToken, { nsfw: 'true' })) {
206 expect(body.total).to.equal(1)
d525fc39 207
dd0ebb71 208 const videos = body.data
d525fc39 209 expect(videos).to.have.lengthOf(1)
a1587156 210 expect(videos[0].name).to.equal('nsfw')
d525fc39
C
211 }
212 })
213
214 it('Should hide NSFW videos when the nsfw param === true', async function () {
dd0ebb71
C
215 for (const body of await getVideosFunctions(server.accessToken, { nsfw: 'false' })) {
216 expect(body.total).to.equal(1)
d525fc39 217
dd0ebb71 218 const videos = body.data
d525fc39 219 expect(videos).to.have.lengthOf(1)
a1587156 220 expect(videos[0].name).to.equal('normal')
d525fc39
C
221 }
222 })
0b18f4aa
C
223
224 it('Should display both videos when the nsfw param === both', async function () {
dd0ebb71
C
225 for (const body of await getVideosFunctions(server.accessToken, { nsfw: 'both' })) {
226 expect(body.total).to.equal(2)
0b18f4aa 227
dd0ebb71 228 const videos = body.data
0b18f4aa 229 expect(videos).to.have.lengthOf(2)
a1587156
C
230 expect(videos[0].name).to.equal('normal')
231 expect(videos[1].name).to.equal('nsfw')
0b18f4aa
C
232 }
233 })
0883b324
C
234 })
235
7c3b7976
C
236 after(async function () {
237 await cleanupTests([ server ])
0883b324
C
238 })
239})