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