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