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