1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
4 import * as chai from 'chai'
10 getVideoChannelVideos,
12 getVideosListWithToken,
14 setAccessTokensToServers,
16 } from '@shared/extra-utils'
17 import { BooleanBothQuery, CustomConfig, ResultList, Video, VideosOverview } from '@shared/models'
19 const expect = chai.expect
21 function createOverviewRes (overview: VideosOverview) {
22 const videos = overview.categories[0].videos
23 return { data: videos, total: videos.length }
26 describe('Test video NSFW policy', function () {
27 let server: ServerInfo
28 let userAccessToken: string
29 let customConfig: CustomConfig
31 async function getVideosFunctions (token?: string, query: { nsfw?: BooleanBothQuery } = {}) {
32 const user = await server.usersCommand.getMyInfo()
33 const videoChannelName = user.videoChannels[0].name
34 const accountName = user.account.name + '@' + user.account.host
35 const hasQuery = Object.keys(query).length !== 0
36 let promises: Promise<ResultList<Video>>[]
40 getVideosListWithToken(server.url, token, query).then(res => res.body),
41 server.searchCommand.advancedVideoSearch({ token, search: { search: 'n', sort: '-publishedAt', ...query } }),
42 getAccountVideos(server.url, token, accountName, 0, 5, undefined, query).then(res => res.body),
43 getVideoChannelVideos(server.url, token, videoChannelName, 0, 5, undefined, query).then(res => res.body)
46 // Overviews do not support video filters
48 const p = server.overviewsCommand.getVideos({ page: 1, token })
49 .then(res => createOverviewRes(res))
53 return Promise.all(promises)
57 getVideosList(server.url).then(res => res.body),
58 server.searchCommand.searchVideos({ search: 'n', sort: '-publishedAt' }),
59 getAccountVideos(server.url, undefined, accountName, 0, 5).then(res => res.body),
60 getVideoChannelVideos(server.url, undefined, videoChannelName, 0, 5).then(res => res.body)
63 // Overviews do not support video filters
65 const p = server.overviewsCommand.getVideos({ page: 1 })
66 .then(res => createOverviewRes(res))
70 return Promise.all(promises)
73 before(async function () {
75 server = await flushAndRunServer(1)
77 // Get the access tokens
78 await setAccessTokensToServers([ server ])
81 const attributes = { name: 'nsfw', nsfw: true, category: 1 }
82 await uploadVideo(server.url, server.accessToken, attributes)
86 const attributes = { name: 'normal', nsfw: false, category: 1 }
87 await uploadVideo(server.url, server.accessToken, attributes)
90 customConfig = await server.configCommand.getCustomConfig()
93 describe('Instance default NSFW policy', function () {
94 it('Should display NSFW videos with display default NSFW policy', async function () {
95 const serverConfig = await server.configCommand.getConfig()
96 expect(serverConfig.instance.defaultNSFWPolicy).to.equal('display')
98 for (const body of await getVideosFunctions()) {
99 expect(body.total).to.equal(2)
101 const videos = body.data
102 expect(videos).to.have.lengthOf(2)
103 expect(videos[0].name).to.equal('normal')
104 expect(videos[1].name).to.equal('nsfw')
108 it('Should not display NSFW videos with do_not_list default NSFW policy', async function () {
109 customConfig.instance.defaultNSFWPolicy = 'do_not_list'
110 await server.configCommand.updateCustomConfig({ newCustomConfig: customConfig })
112 const serverConfig = await server.configCommand.getConfig()
113 expect(serverConfig.instance.defaultNSFWPolicy).to.equal('do_not_list')
115 for (const body of await getVideosFunctions()) {
116 expect(body.total).to.equal(1)
118 const videos = body.data
119 expect(videos).to.have.lengthOf(1)
120 expect(videos[0].name).to.equal('normal')
124 it('Should display NSFW videos with blur default NSFW policy', async function () {
125 customConfig.instance.defaultNSFWPolicy = 'blur'
126 await server.configCommand.updateCustomConfig({ newCustomConfig: customConfig })
128 const serverConfig = await server.configCommand.getConfig()
129 expect(serverConfig.instance.defaultNSFWPolicy).to.equal('blur')
131 for (const body of await getVideosFunctions()) {
132 expect(body.total).to.equal(2)
134 const videos = body.data
135 expect(videos).to.have.lengthOf(2)
136 expect(videos[0].name).to.equal('normal')
137 expect(videos[1].name).to.equal('nsfw')
142 describe('User NSFW policy', function () {
144 it('Should create a user having the default nsfw policy', async function () {
145 const username = 'user1'
146 const password = 'my super password'
147 await server.usersCommand.create({ username: username, password: password })
149 userAccessToken = await server.loginCommand.getAccessToken({ username, password })
151 const user = await server.usersCommand.getMyInfo({ token: userAccessToken })
152 expect(user.nsfwPolicy).to.equal('blur')
155 it('Should display NSFW videos with blur user NSFW policy', async function () {
156 customConfig.instance.defaultNSFWPolicy = 'do_not_list'
157 await server.configCommand.updateCustomConfig({ newCustomConfig: customConfig })
159 for (const body of await getVideosFunctions(userAccessToken)) {
160 expect(body.total).to.equal(2)
162 const videos = body.data
163 expect(videos).to.have.lengthOf(2)
164 expect(videos[0].name).to.equal('normal')
165 expect(videos[1].name).to.equal('nsfw')
169 it('Should display NSFW videos with display user NSFW policy', async function () {
170 await server.usersCommand.updateMe({ nsfwPolicy: 'display' })
172 for (const body of await getVideosFunctions(server.accessToken)) {
173 expect(body.total).to.equal(2)
175 const videos = body.data
176 expect(videos).to.have.lengthOf(2)
177 expect(videos[0].name).to.equal('normal')
178 expect(videos[1].name).to.equal('nsfw')
182 it('Should not display NSFW videos with do_not_list user NSFW policy', async function () {
183 await server.usersCommand.updateMe({ nsfwPolicy: 'do_not_list' })
185 for (const body of await getVideosFunctions(server.accessToken)) {
186 expect(body.total).to.equal(1)
188 const videos = body.data
189 expect(videos).to.have.lengthOf(1)
190 expect(videos[0].name).to.equal('normal')
194 it('Should be able to see my NSFW videos even with do_not_list user NSFW policy', async function () {
195 const res = await getMyVideos(server.url, server.accessToken, 0, 5)
196 expect(res.body.total).to.equal(2)
198 const videos = res.body.data
199 expect(videos).to.have.lengthOf(2)
200 expect(videos[0].name).to.equal('normal')
201 expect(videos[1].name).to.equal('nsfw')
204 it('Should display NSFW videos when the nsfw param === true', async function () {
205 for (const body of await getVideosFunctions(server.accessToken, { nsfw: 'true' })) {
206 expect(body.total).to.equal(1)
208 const videos = body.data
209 expect(videos).to.have.lengthOf(1)
210 expect(videos[0].name).to.equal('nsfw')
214 it('Should hide NSFW videos when the nsfw param === true', async function () {
215 for (const body of await getVideosFunctions(server.accessToken, { nsfw: 'false' })) {
216 expect(body.total).to.equal(1)
218 const videos = body.data
219 expect(videos).to.have.lengthOf(1)
220 expect(videos[0].name).to.equal('normal')
224 it('Should display both videos when the nsfw param === both', async function () {
225 for (const body of await getVideosFunctions(server.accessToken, { nsfw: 'both' })) {
226 expect(body.total).to.equal(2)
228 const videos = body.data
229 expect(videos).to.have.lengthOf(2)
230 expect(videos[0].name).to.equal('normal')
231 expect(videos[1].name).to.equal('nsfw')
236 after(async function () {
237 await cleanupTests([ server ])