]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/tests/api/videos/video-nsfw.ts
Refactor requests
[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/extra-utils'
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: channelName, ...query }),
34 server.videos.listByChannel({ token, handle: accountName, ...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({ handle: channelName }),
51 server.videos.listByChannel({ handle: accountName })
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 it('Should display NSFW videos with display default NSFW policy', async function () {
86 const serverConfig = await server.config.getConfig()
87 expect(serverConfig.instance.defaultNSFWPolicy).to.equal('display')
88
89 for (const body of await getVideosFunctions()) {
90 expect(body.total).to.equal(2)
91
92 const videos = body.data
93 expect(videos).to.have.lengthOf(2)
94 expect(videos[0].name).to.equal('normal')
95 expect(videos[1].name).to.equal('nsfw')
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'
101 await server.config.updateCustomConfig({ newCustomConfig: customConfig })
102
103 const serverConfig = await server.config.getConfig()
104 expect(serverConfig.instance.defaultNSFWPolicy).to.equal('do_not_list')
105
106 for (const body of await getVideosFunctions()) {
107 expect(body.total).to.equal(1)
108
109 const videos = body.data
110 expect(videos).to.have.lengthOf(1)
111 expect(videos[0].name).to.equal('normal')
112 }
113 })
114
115 it('Should display NSFW videos with blur default NSFW policy', async function () {
116 customConfig.instance.defaultNSFWPolicy = 'blur'
117 await server.config.updateCustomConfig({ newCustomConfig: customConfig })
118
119 const serverConfig = await server.config.getConfig()
120 expect(serverConfig.instance.defaultNSFWPolicy).to.equal('blur')
121
122 for (const body of await getVideosFunctions()) {
123 expect(body.total).to.equal(2)
124
125 const videos = body.data
126 expect(videos).to.have.lengthOf(2)
127 expect(videos[0].name).to.equal('normal')
128 expect(videos[1].name).to.equal('nsfw')
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'
138 await server.users.create({ username: username, password: password })
139
140 userAccessToken = await server.login.getAccessToken({ username, password })
141
142 const user = await server.users.getMyInfo({ token: userAccessToken })
143 expect(user.nsfwPolicy).to.equal('blur')
144 })
145
146 it('Should display NSFW videos with blur user NSFW policy', async function () {
147 customConfig.instance.defaultNSFWPolicy = 'do_not_list'
148 await server.config.updateCustomConfig({ newCustomConfig: customConfig })
149
150 for (const body of await getVideosFunctions(userAccessToken)) {
151 expect(body.total).to.equal(2)
152
153 const videos = body.data
154 expect(videos).to.have.lengthOf(2)
155 expect(videos[0].name).to.equal('normal')
156 expect(videos[1].name).to.equal('nsfw')
157 }
158 })
159
160 it('Should display NSFW videos with display user NSFW policy', async function () {
161 await server.users.updateMe({ nsfwPolicy: 'display' })
162
163 for (const body of await getVideosFunctions(server.accessToken)) {
164 expect(body.total).to.equal(2)
165
166 const videos = body.data
167 expect(videos).to.have.lengthOf(2)
168 expect(videos[0].name).to.equal('normal')
169 expect(videos[1].name).to.equal('nsfw')
170 }
171 })
172
173 it('Should not display NSFW videos with do_not_list user NSFW policy', async function () {
174 await server.users.updateMe({ nsfwPolicy: 'do_not_list' })
175
176 for (const body of await getVideosFunctions(server.accessToken)) {
177 expect(body.total).to.equal(1)
178
179 const videos = body.data
180 expect(videos).to.have.lengthOf(1)
181 expect(videos[0].name).to.equal('normal')
182 }
183 })
184
185 it('Should be able to see my NSFW videos even with do_not_list user NSFW policy', async function () {
186 const { total, data } = await server.videos.listMyVideos()
187 expect(total).to.equal(2)
188
189 expect(data).to.have.lengthOf(2)
190 expect(data[0].name).to.equal('normal')
191 expect(data[1].name).to.equal('nsfw')
192 })
193
194 it('Should display NSFW videos when the nsfw param === true', async function () {
195 for (const body of await getVideosFunctions(server.accessToken, { nsfw: 'true' })) {
196 expect(body.total).to.equal(1)
197
198 const videos = body.data
199 expect(videos).to.have.lengthOf(1)
200 expect(videos[0].name).to.equal('nsfw')
201 }
202 })
203
204 it('Should hide NSFW videos when the nsfw param === true', async function () {
205 for (const body of await getVideosFunctions(server.accessToken, { nsfw: 'false' })) {
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('normal')
211 }
212 })
213
214 it('Should display both videos when the nsfw param === both', async function () {
215 for (const body of await getVideosFunctions(server.accessToken, { nsfw: 'both' })) {
216 expect(body.total).to.equal(2)
217
218 const videos = body.data
219 expect(videos).to.have.lengthOf(2)
220 expect(videos[0].name).to.equal('normal')
221 expect(videos[1].name).to.equal('nsfw')
222 }
223 })
224 })
225
226 after(async function () {
227 await cleanupTests([ server ])
228 })
229 })