1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
4 import * as chai from 'chai'
5 import { cleanupTests, flushAndRunServer, ServerInfo, setAccessTokensToServers } from '@shared/extra-utils'
6 import { BooleanBothQuery, CustomConfig, ResultList, Video, VideosOverview } from '@shared/models'
8 const expect = chai.expect
10 function createOverviewRes (overview: VideosOverview) {
11 const videos = overview.categories[0].videos
12 return { data: videos, total: videos.length }
15 describe('Test video NSFW policy', function () {
16 let server: ServerInfo
17 let userAccessToken: string
18 let customConfig: CustomConfig
20 async function getVideosFunctions (token?: string, query: { nsfw?: BooleanBothQuery } = {}) {
21 const user = await server.users.getMyInfo()
22 const videoChannelName = user.videoChannels[0].name
23 const accountName = user.account.name + '@' + user.account.host
24 const hasQuery = Object.keys(query).length !== 0
25 let promises: Promise<ResultList<Video>>[]
29 server.search.advancedVideoSearch({ token, search: { search: 'n', sort: '-publishedAt', ...query } }),
30 server.videos.listWithToken({ token, ...query }),
31 server.videos.listByAccount({ token, accountName, ...query }),
32 server.videos.listByChannel({ token, videoChannelName, ...query })
35 // Overviews do not support video filters
37 const p = server.overviews.getVideos({ page: 1, token })
38 .then(res => createOverviewRes(res))
42 return Promise.all(promises)
46 server.search.searchVideos({ search: 'n', sort: '-publishedAt' }),
48 server.videos.listByAccount({ accountName }),
49 server.videos.listByChannel({ videoChannelName })
52 // Overviews do not support video filters
54 const p = server.overviews.getVideos({ page: 1 })
55 .then(res => createOverviewRes(res))
59 return Promise.all(promises)
62 before(async function () {
64 server = await flushAndRunServer(1)
66 // Get the access tokens
67 await setAccessTokensToServers([ server ])
70 const attributes = { name: 'nsfw', nsfw: true, category: 1 }
71 await server.videos.upload({ attributes })
75 const attributes = { name: 'normal', nsfw: false, category: 1 }
76 await server.videos.upload({ attributes })
79 customConfig = await server.config.getCustomConfig()
82 describe('Instance default NSFW policy', function () {
83 it('Should display NSFW videos with display default NSFW policy', async function () {
84 const serverConfig = await server.config.getConfig()
85 expect(serverConfig.instance.defaultNSFWPolicy).to.equal('display')
87 for (const body of await getVideosFunctions()) {
88 expect(body.total).to.equal(2)
90 const videos = body.data
91 expect(videos).to.have.lengthOf(2)
92 expect(videos[0].name).to.equal('normal')
93 expect(videos[1].name).to.equal('nsfw')
97 it('Should not display NSFW videos with do_not_list default NSFW policy', async function () {
98 customConfig.instance.defaultNSFWPolicy = 'do_not_list'
99 await server.config.updateCustomConfig({ newCustomConfig: customConfig })
101 const serverConfig = await server.config.getConfig()
102 expect(serverConfig.instance.defaultNSFWPolicy).to.equal('do_not_list')
104 for (const body of await getVideosFunctions()) {
105 expect(body.total).to.equal(1)
107 const videos = body.data
108 expect(videos).to.have.lengthOf(1)
109 expect(videos[0].name).to.equal('normal')
113 it('Should display NSFW videos with blur default NSFW policy', async function () {
114 customConfig.instance.defaultNSFWPolicy = 'blur'
115 await server.config.updateCustomConfig({ newCustomConfig: customConfig })
117 const serverConfig = await server.config.getConfig()
118 expect(serverConfig.instance.defaultNSFWPolicy).to.equal('blur')
120 for (const body of await getVideosFunctions()) {
121 expect(body.total).to.equal(2)
123 const videos = body.data
124 expect(videos).to.have.lengthOf(2)
125 expect(videos[0].name).to.equal('normal')
126 expect(videos[1].name).to.equal('nsfw')
131 describe('User NSFW policy', function () {
133 it('Should create a user having the default nsfw policy', async function () {
134 const username = 'user1'
135 const password = 'my super password'
136 await server.users.create({ username: username, password: password })
138 userAccessToken = await server.login.getAccessToken({ username, password })
140 const user = await server.users.getMyInfo({ token: userAccessToken })
141 expect(user.nsfwPolicy).to.equal('blur')
144 it('Should display NSFW videos with blur user NSFW policy', async function () {
145 customConfig.instance.defaultNSFWPolicy = 'do_not_list'
146 await server.config.updateCustomConfig({ newCustomConfig: customConfig })
148 for (const body of await getVideosFunctions(userAccessToken)) {
149 expect(body.total).to.equal(2)
151 const videos = body.data
152 expect(videos).to.have.lengthOf(2)
153 expect(videos[0].name).to.equal('normal')
154 expect(videos[1].name).to.equal('nsfw')
158 it('Should display NSFW videos with display user NSFW policy', async function () {
159 await server.users.updateMe({ nsfwPolicy: 'display' })
161 for (const body of await getVideosFunctions(server.accessToken)) {
162 expect(body.total).to.equal(2)
164 const videos = body.data
165 expect(videos).to.have.lengthOf(2)
166 expect(videos[0].name).to.equal('normal')
167 expect(videos[1].name).to.equal('nsfw')
171 it('Should not display NSFW videos with do_not_list user NSFW policy', async function () {
172 await server.users.updateMe({ nsfwPolicy: 'do_not_list' })
174 for (const body of await getVideosFunctions(server.accessToken)) {
175 expect(body.total).to.equal(1)
177 const videos = body.data
178 expect(videos).to.have.lengthOf(1)
179 expect(videos[0].name).to.equal('normal')
183 it('Should be able to see my NSFW videos even with do_not_list user NSFW policy', async function () {
184 const { total, data } = await server.videos.listMyVideos()
185 expect(total).to.equal(2)
187 expect(data).to.have.lengthOf(2)
188 expect(data[0].name).to.equal('normal')
189 expect(data[1].name).to.equal('nsfw')
192 it('Should display NSFW videos when the nsfw param === true', async function () {
193 for (const body of await getVideosFunctions(server.accessToken, { nsfw: 'true' })) {
194 expect(body.total).to.equal(1)
196 const videos = body.data
197 expect(videos).to.have.lengthOf(1)
198 expect(videos[0].name).to.equal('nsfw')
202 it('Should hide NSFW videos when the nsfw param === true', async function () {
203 for (const body of await getVideosFunctions(server.accessToken, { nsfw: 'false' })) {
204 expect(body.total).to.equal(1)
206 const videos = body.data
207 expect(videos).to.have.lengthOf(1)
208 expect(videos[0].name).to.equal('normal')
212 it('Should display both videos when the nsfw param === both', async function () {
213 for (const body of await getVideosFunctions(server.accessToken, { nsfw: 'both' })) {
214 expect(body.total).to.equal(2)
216 const videos = body.data
217 expect(videos).to.have.lengthOf(2)
218 expect(videos[0].name).to.equal('normal')
219 expect(videos[1].name).to.equal('nsfw')
224 after(async function () {
225 await cleanupTests([ server ])