1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
4 import * as chai from 'chai'
12 getVideoChannelVideos,
14 getVideosListWithToken,
16 setAccessTokensToServers,
20 } from '@shared/extra-utils'
21 import { BooleanBothQuery, CustomConfig, ResultList, User, Video, VideosOverview } from '@shared/models'
23 const expect = chai.expect
25 function createOverviewRes (overview: VideosOverview) {
26 const videos = overview.categories[0].videos
27 return { data: videos, total: videos.length }
30 describe('Test video NSFW policy', function () {
31 let server: ServerInfo
32 let userAccessToken: string
33 let customConfig: CustomConfig
35 async function getVideosFunctions (token?: string, query: { nsfw?: BooleanBothQuery } = {}) {
36 const res = await getMyUserInformation(server.url, server.accessToken)
37 const user: User = res.body
38 const videoChannelName = user.videoChannels[0].name
39 const accountName = user.account.name + '@' + user.account.host
40 const hasQuery = Object.keys(query).length !== 0
41 let promises: Promise<ResultList<Video>>[]
45 getVideosListWithToken(server.url, token, query).then(res => res.body),
46 server.searchCommand.advancedVideoSearch({ token, search: { search: 'n', sort: '-publishedAt', ...query } }),
47 getAccountVideos(server.url, token, accountName, 0, 5, undefined, query).then(res => res.body),
48 getVideoChannelVideos(server.url, token, videoChannelName, 0, 5, undefined, query).then(res => res.body)
51 // Overviews do not support video filters
53 const p = server.overviewsCommand.getVideos({ page: 1, token })
54 .then(res => createOverviewRes(res))
58 return Promise.all(promises)
62 getVideosList(server.url).then(res => res.body),
63 server.searchCommand.searchVideos({ search: 'n', sort: '-publishedAt' }),
64 getAccountVideos(server.url, undefined, accountName, 0, 5).then(res => res.body),
65 getVideoChannelVideos(server.url, undefined, videoChannelName, 0, 5).then(res => res.body)
68 // Overviews do not support video filters
70 const p = server.overviewsCommand.getVideos({ page: 1 })
71 .then(res => createOverviewRes(res))
75 return Promise.all(promises)
78 before(async function () {
80 server = await flushAndRunServer(1)
82 // Get the access tokens
83 await setAccessTokensToServers([ server ])
86 const attributes = { name: 'nsfw', nsfw: true, category: 1 }
87 await uploadVideo(server.url, server.accessToken, attributes)
91 const attributes = { name: 'normal', nsfw: false, category: 1 }
92 await uploadVideo(server.url, server.accessToken, attributes)
95 customConfig = await server.configCommand.getCustomConfig()
98 describe('Instance default NSFW policy', function () {
99 it('Should display NSFW videos with display default NSFW policy', async function () {
100 const serverConfig = await server.configCommand.getConfig()
101 expect(serverConfig.instance.defaultNSFWPolicy).to.equal('display')
103 for (const body of await getVideosFunctions()) {
104 expect(body.total).to.equal(2)
106 const videos = body.data
107 expect(videos).to.have.lengthOf(2)
108 expect(videos[0].name).to.equal('normal')
109 expect(videos[1].name).to.equal('nsfw')
113 it('Should not display NSFW videos with do_not_list default NSFW policy', async function () {
114 customConfig.instance.defaultNSFWPolicy = 'do_not_list'
115 await server.configCommand.updateCustomConfig({ newCustomConfig: customConfig })
117 const serverConfig = await server.configCommand.getConfig()
118 expect(serverConfig.instance.defaultNSFWPolicy).to.equal('do_not_list')
120 for (const body of await getVideosFunctions()) {
121 expect(body.total).to.equal(1)
123 const videos = body.data
124 expect(videos).to.have.lengthOf(1)
125 expect(videos[0].name).to.equal('normal')
129 it('Should display NSFW videos with blur default NSFW policy', async function () {
130 customConfig.instance.defaultNSFWPolicy = 'blur'
131 await server.configCommand.updateCustomConfig({ newCustomConfig: customConfig })
133 const serverConfig = await server.configCommand.getConfig()
134 expect(serverConfig.instance.defaultNSFWPolicy).to.equal('blur')
136 for (const body of await getVideosFunctions()) {
137 expect(body.total).to.equal(2)
139 const videos = body.data
140 expect(videos).to.have.lengthOf(2)
141 expect(videos[0].name).to.equal('normal')
142 expect(videos[1].name).to.equal('nsfw')
147 describe('User NSFW policy', function () {
149 it('Should create a user having the default nsfw policy', async function () {
150 const username = 'user1'
151 const password = 'my super password'
152 await createUser({ url: server.url, accessToken: server.accessToken, username: username, password: password })
154 userAccessToken = await userLogin(server, { username, password })
156 const res = await getMyUserInformation(server.url, userAccessToken)
157 const user = res.body
159 expect(user.nsfwPolicy).to.equal('blur')
162 it('Should display NSFW videos with blur user NSFW policy', async function () {
163 customConfig.instance.defaultNSFWPolicy = 'do_not_list'
164 await server.configCommand.updateCustomConfig({ newCustomConfig: customConfig })
166 for (const body of await getVideosFunctions(userAccessToken)) {
167 expect(body.total).to.equal(2)
169 const videos = body.data
170 expect(videos).to.have.lengthOf(2)
171 expect(videos[0].name).to.equal('normal')
172 expect(videos[1].name).to.equal('nsfw')
176 it('Should display NSFW videos with display user NSFW policy', async function () {
179 accessToken: server.accessToken,
180 nsfwPolicy: 'display'
183 for (const body of await getVideosFunctions(server.accessToken)) {
184 expect(body.total).to.equal(2)
186 const videos = body.data
187 expect(videos).to.have.lengthOf(2)
188 expect(videos[0].name).to.equal('normal')
189 expect(videos[1].name).to.equal('nsfw')
193 it('Should not display NSFW videos with do_not_list user NSFW policy', async function () {
196 accessToken: server.accessToken,
197 nsfwPolicy: 'do_not_list'
200 for (const body of await getVideosFunctions(server.accessToken)) {
201 expect(body.total).to.equal(1)
203 const videos = body.data
204 expect(videos).to.have.lengthOf(1)
205 expect(videos[0].name).to.equal('normal')
209 it('Should be able to see my NSFW videos even with do_not_list user NSFW policy', async function () {
210 const res = await getMyVideos(server.url, server.accessToken, 0, 5)
211 expect(res.body.total).to.equal(2)
213 const videos = res.body.data
214 expect(videos).to.have.lengthOf(2)
215 expect(videos[0].name).to.equal('normal')
216 expect(videos[1].name).to.equal('nsfw')
219 it('Should display NSFW videos when the nsfw param === true', async function () {
220 for (const body of await getVideosFunctions(server.accessToken, { nsfw: 'true' })) {
221 expect(body.total).to.equal(1)
223 const videos = body.data
224 expect(videos).to.have.lengthOf(1)
225 expect(videos[0].name).to.equal('nsfw')
229 it('Should hide NSFW videos when the nsfw param === true', async function () {
230 for (const body of await getVideosFunctions(server.accessToken, { nsfw: 'false' })) {
231 expect(body.total).to.equal(1)
233 const videos = body.data
234 expect(videos).to.have.lengthOf(1)
235 expect(videos[0].name).to.equal('normal')
239 it('Should display both videos when the nsfw param === both', async function () {
240 for (const body of await getVideosFunctions(server.accessToken, { nsfw: 'both' })) {
241 expect(body.total).to.equal(2)
243 const videos = body.data
244 expect(videos).to.have.lengthOf(2)
245 expect(videos[0].name).to.equal('normal')
246 expect(videos[1].name).to.equal('nsfw')
251 after(async function () {
252 await cleanupTests([ server ])