1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
3 import * as chai from 'chai'
5 import { cleanupTests, getVideosList, ServerInfo, setAccessTokensToServers, uploadVideo } from '../../../../shared/extra-utils/index'
6 import { userLogin } from '../../../../shared/extra-utils/users/login'
7 import { createUser } from '../../../../shared/extra-utils/users/users'
8 import { getMyVideos } from '../../../../shared/extra-utils/videos/videos'
15 getVideoChannelVideos,
16 getVideosListWithToken,
21 } from '../../../../shared/extra-utils'
22 import { ServerConfig, VideosOverview } from '../../../../shared/models'
23 import { CustomConfig } from '../../../../shared/models/server/custom-config.model'
24 import { User } from '../../../../shared/models/users'
25 import { getVideosOverview, getVideosOverviewWithToken } from '@shared/extra-utils/overviews/overviews'
27 const expect = chai.expect
29 function createOverviewRes (res: any) {
30 const overview = res.body as VideosOverview
32 const videos = overview.categories[0].videos
33 return { body: { data: videos, total: videos.length } }
36 describe('Test video NSFW policy', function () {
37 let server: ServerInfo
38 let userAccessToken: string
39 let customConfig: CustomConfig
41 function getVideosFunctions (token?: string, query = {}) {
42 return getMyUserInformation(server.url, server.accessToken)
44 const user: User = res.body
45 const videoChannelName = user.videoChannels[0].name
46 const accountName = user.account.name + '@' + user.account.host
47 const hasQuery = Object.keys(query).length !== 0
48 let promises: Promise<any>[]
52 getVideosListWithToken(server.url, token, query),
53 searchVideoWithToken(server.url, 'n', token, query),
54 getAccountVideos(server.url, token, accountName, 0, 5, undefined, query),
55 getVideoChannelVideos(server.url, token, videoChannelName, 0, 5, undefined, query)
58 // Overviews do not support video filters
60 promises.push(getVideosOverviewWithToken(server.url, 1, token).then(res => createOverviewRes(res)))
63 return Promise.all(promises)
67 getVideosList(server.url),
68 searchVideo(server.url, 'n'),
69 getAccountVideos(server.url, undefined, accountName, 0, 5),
70 getVideoChannelVideos(server.url, undefined, videoChannelName, 0, 5)
73 // Overviews do not support video filters
75 promises.push(getVideosOverview(server.url, 1).then(res => createOverviewRes(res)))
78 return Promise.all(promises)
82 before(async function () {
84 server = await flushAndRunServer(1)
86 // Get the access tokens
87 await setAccessTokensToServers([ server ])
90 const attributes = { name: 'nsfw', nsfw: true, category: 1 }
91 await uploadVideo(server.url, server.accessToken, attributes)
95 const attributes = { name: 'normal', nsfw: false, category: 1 }
96 await uploadVideo(server.url, server.accessToken, attributes)
100 const res = await getCustomConfig(server.url, server.accessToken)
101 customConfig = res.body
105 describe('Instance default NSFW policy', function () {
106 it('Should display NSFW videos with display default NSFW policy', async function () {
107 const resConfig = await getConfig(server.url)
108 const serverConfig: ServerConfig = resConfig.body
109 expect(serverConfig.instance.defaultNSFWPolicy).to.equal('display')
111 for (const res of await getVideosFunctions()) {
112 expect(res.body.total).to.equal(2)
114 const videos = res.body.data
115 expect(videos).to.have.lengthOf(2)
116 expect(videos[0].name).to.equal('normal')
117 expect(videos[1].name).to.equal('nsfw')
121 it('Should not display NSFW videos with do_not_list default NSFW policy', async function () {
122 customConfig.instance.defaultNSFWPolicy = 'do_not_list'
123 await updateCustomConfig(server.url, server.accessToken, customConfig)
125 const resConfig = await getConfig(server.url)
126 const serverConfig: ServerConfig = resConfig.body
127 expect(serverConfig.instance.defaultNSFWPolicy).to.equal('do_not_list')
129 for (const res of await getVideosFunctions()) {
130 expect(res.body.total).to.equal(1)
132 const videos = res.body.data
133 expect(videos).to.have.lengthOf(1)
134 expect(videos[0].name).to.equal('normal')
138 it('Should display NSFW videos with blur default NSFW policy', async function () {
139 customConfig.instance.defaultNSFWPolicy = 'blur'
140 await updateCustomConfig(server.url, server.accessToken, customConfig)
142 const resConfig = await getConfig(server.url)
143 const serverConfig: ServerConfig = resConfig.body
144 expect(serverConfig.instance.defaultNSFWPolicy).to.equal('blur')
146 for (const res of await getVideosFunctions()) {
147 expect(res.body.total).to.equal(2)
149 const videos = res.body.data
150 expect(videos).to.have.lengthOf(2)
151 expect(videos[0].name).to.equal('normal')
152 expect(videos[1].name).to.equal('nsfw')
157 describe('User NSFW policy', function () {
159 it('Should create a user having the default nsfw policy', async function () {
160 const username = 'user1'
161 const password = 'my super password'
162 await createUser({ url: server.url, accessToken: server.accessToken, username: username, password: password })
164 userAccessToken = await userLogin(server, { username, password })
166 const res = await getMyUserInformation(server.url, userAccessToken)
167 const user = res.body
169 expect(user.nsfwPolicy).to.equal('blur')
172 it('Should display NSFW videos with blur user NSFW policy', async function () {
173 customConfig.instance.defaultNSFWPolicy = 'do_not_list'
174 await updateCustomConfig(server.url, server.accessToken, customConfig)
176 for (const res of await getVideosFunctions(userAccessToken)) {
177 expect(res.body.total).to.equal(2)
179 const videos = res.body.data
180 expect(videos).to.have.lengthOf(2)
181 expect(videos[0].name).to.equal('normal')
182 expect(videos[1].name).to.equal('nsfw')
186 it('Should display NSFW videos with display user NSFW policy', async function () {
189 accessToken: server.accessToken,
190 nsfwPolicy: 'display'
193 for (const res of await getVideosFunctions(server.accessToken)) {
194 expect(res.body.total).to.equal(2)
196 const videos = res.body.data
197 expect(videos).to.have.lengthOf(2)
198 expect(videos[0].name).to.equal('normal')
199 expect(videos[1].name).to.equal('nsfw')
203 it('Should not display NSFW videos with do_not_list user NSFW policy', async function () {
206 accessToken: server.accessToken,
207 nsfwPolicy: 'do_not_list'
210 for (const res of await getVideosFunctions(server.accessToken)) {
211 expect(res.body.total).to.equal(1)
213 const videos = res.body.data
214 expect(videos).to.have.lengthOf(1)
215 expect(videos[0].name).to.equal('normal')
219 it('Should be able to see my NSFW videos even with do_not_list user NSFW policy', async function () {
220 const res = await getMyVideos(server.url, server.accessToken, 0, 5)
221 expect(res.body.total).to.equal(2)
223 const videos = res.body.data
224 expect(videos).to.have.lengthOf(2)
225 expect(videos[0].name).to.equal('normal')
226 expect(videos[1].name).to.equal('nsfw')
229 it('Should display NSFW videos when the nsfw param === true', async function () {
230 for (const res of await getVideosFunctions(server.accessToken, { nsfw: true })) {
231 expect(res.body.total).to.equal(1)
233 const videos = res.body.data
234 expect(videos).to.have.lengthOf(1)
235 expect(videos[0].name).to.equal('nsfw')
239 it('Should hide NSFW videos when the nsfw param === true', async function () {
240 for (const res of await getVideosFunctions(server.accessToken, { nsfw: false })) {
241 expect(res.body.total).to.equal(1)
243 const videos = res.body.data
244 expect(videos).to.have.lengthOf(1)
245 expect(videos[0].name).to.equal('normal')
249 it('Should display both videos when the nsfw param === both', async function () {
250 for (const res of await getVideosFunctions(server.accessToken, { nsfw: 'both' })) {
251 expect(res.body.total).to.equal(2)
253 const videos = res.body.data
254 expect(videos).to.have.lengthOf(2)
255 expect(videos[0].name).to.equal('normal')
256 expect(videos[1].name).to.equal('nsfw')
261 after(async function () {
262 await cleanupTests([ server ])