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, User, VideosOverview } from '@shared/models'
23 const expect = chai.expect
25 function createOverviewRes (overview: VideosOverview) {
26 const videos = overview.categories[0].videos
27 return { body: { 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 function getVideosFunctions (token?: string, query: { nsfw?: BooleanBothQuery } = {}) {
36 return getMyUserInformation(server.url, server.accessToken)
38 const user: User = res.body
39 const videoChannelName = user.videoChannels[0].name
40 const accountName = user.account.name + '@' + user.account.host
41 const hasQuery = Object.keys(query).length !== 0
42 let promises: Promise<any>[]
46 getVideosListWithToken(server.url, token, query),
47 server.searchCommand.advancedVideoSearch({ token, search: { search: 'n', ...query } }),
48 getAccountVideos(server.url, token, accountName, 0, 5, undefined, query),
49 getVideoChannelVideos(server.url, token, videoChannelName, 0, 5, undefined, query)
52 // Overviews do not support video filters
54 const p = server.overviewsCommand.getVideos({ page: 1, token })
55 .then(res => createOverviewRes(res))
59 return Promise.all(promises)
63 getVideosList(server.url),
64 server.searchCommand.searchVideos({ search: 'n' }),
65 getAccountVideos(server.url, undefined, accountName, 0, 5),
66 getVideoChannelVideos(server.url, undefined, videoChannelName, 0, 5)
69 // Overviews do not support video filters
71 const p = server.overviewsCommand.getVideos({ page: 1 })
72 .then(res => createOverviewRes(res))
76 return Promise.all(promises)
80 before(async function () {
82 server = await flushAndRunServer(1)
84 // Get the access tokens
85 await setAccessTokensToServers([ server ])
88 const attributes = { name: 'nsfw', nsfw: true, category: 1 }
89 await uploadVideo(server.url, server.accessToken, attributes)
93 const attributes = { name: 'normal', nsfw: false, category: 1 }
94 await uploadVideo(server.url, server.accessToken, attributes)
97 customConfig = await server.configCommand.getCustomConfig()
100 describe('Instance default NSFW policy', function () {
101 it('Should display NSFW videos with display default NSFW policy', async function () {
102 const serverConfig = await server.configCommand.getConfig()
103 expect(serverConfig.instance.defaultNSFWPolicy).to.equal('display')
105 for (const res of await getVideosFunctions()) {
106 expect(res.body.total).to.equal(2)
108 const videos = res.body.data
109 expect(videos).to.have.lengthOf(2)
110 expect(videos[0].name).to.equal('normal')
111 expect(videos[1].name).to.equal('nsfw')
115 it('Should not display NSFW videos with do_not_list default NSFW policy', async function () {
116 customConfig.instance.defaultNSFWPolicy = 'do_not_list'
117 await server.configCommand.updateCustomConfig({ newCustomConfig: customConfig })
119 const serverConfig = await server.configCommand.getConfig()
120 expect(serverConfig.instance.defaultNSFWPolicy).to.equal('do_not_list')
122 for (const res of await getVideosFunctions()) {
123 expect(res.body.total).to.equal(1)
125 const videos = res.body.data
126 expect(videos).to.have.lengthOf(1)
127 expect(videos[0].name).to.equal('normal')
131 it('Should display NSFW videos with blur default NSFW policy', async function () {
132 customConfig.instance.defaultNSFWPolicy = 'blur'
133 await server.configCommand.updateCustomConfig({ newCustomConfig: customConfig })
135 const serverConfig = await server.configCommand.getConfig()
136 expect(serverConfig.instance.defaultNSFWPolicy).to.equal('blur')
138 for (const res of await getVideosFunctions()) {
139 expect(res.body.total).to.equal(2)
141 const videos = res.body.data
142 expect(videos).to.have.lengthOf(2)
143 expect(videos[0].name).to.equal('normal')
144 expect(videos[1].name).to.equal('nsfw')
149 describe('User NSFW policy', function () {
151 it('Should create a user having the default nsfw policy', async function () {
152 const username = 'user1'
153 const password = 'my super password'
154 await createUser({ url: server.url, accessToken: server.accessToken, username: username, password: password })
156 userAccessToken = await userLogin(server, { username, password })
158 const res = await getMyUserInformation(server.url, userAccessToken)
159 const user = res.body
161 expect(user.nsfwPolicy).to.equal('blur')
164 it('Should display NSFW videos with blur user NSFW policy', async function () {
165 customConfig.instance.defaultNSFWPolicy = 'do_not_list'
166 await server.configCommand.updateCustomConfig({ newCustomConfig: customConfig })
168 for (const res of await getVideosFunctions(userAccessToken)) {
169 expect(res.body.total).to.equal(2)
171 const videos = res.body.data
172 expect(videos).to.have.lengthOf(2)
173 expect(videos[0].name).to.equal('normal')
174 expect(videos[1].name).to.equal('nsfw')
178 it('Should display NSFW videos with display user NSFW policy', async function () {
181 accessToken: server.accessToken,
182 nsfwPolicy: 'display'
185 for (const res of await getVideosFunctions(server.accessToken)) {
186 expect(res.body.total).to.equal(2)
188 const videos = res.body.data
189 expect(videos).to.have.lengthOf(2)
190 expect(videos[0].name).to.equal('normal')
191 expect(videos[1].name).to.equal('nsfw')
195 it('Should not display NSFW videos with do_not_list user NSFW policy', async function () {
198 accessToken: server.accessToken,
199 nsfwPolicy: 'do_not_list'
202 for (const res of await getVideosFunctions(server.accessToken)) {
203 expect(res.body.total).to.equal(1)
205 const videos = res.body.data
206 expect(videos).to.have.lengthOf(1)
207 expect(videos[0].name).to.equal('normal')
211 it('Should be able to see my NSFW videos even with do_not_list user NSFW policy', async function () {
212 const res = await getMyVideos(server.url, server.accessToken, 0, 5)
213 expect(res.body.total).to.equal(2)
215 const videos = res.body.data
216 expect(videos).to.have.lengthOf(2)
217 expect(videos[0].name).to.equal('normal')
218 expect(videos[1].name).to.equal('nsfw')
221 it('Should display NSFW videos when the nsfw param === true', async function () {
222 for (const res of await getVideosFunctions(server.accessToken, { nsfw: 'true' })) {
223 expect(res.body.total).to.equal(1)
225 const videos = res.body.data
226 expect(videos).to.have.lengthOf(1)
227 expect(videos[0].name).to.equal('nsfw')
231 it('Should hide NSFW videos when the nsfw param === true', async function () {
232 for (const res of await getVideosFunctions(server.accessToken, { nsfw: 'false' })) {
233 expect(res.body.total).to.equal(1)
235 const videos = res.body.data
236 expect(videos).to.have.lengthOf(1)
237 expect(videos[0].name).to.equal('normal')
241 it('Should display both videos when the nsfw param === both', async function () {
242 for (const res of await getVideosFunctions(server.accessToken, { nsfw: 'both' })) {
243 expect(res.body.total).to.equal(2)
245 const videos = res.body.data
246 expect(videos).to.have.lengthOf(2)
247 expect(videos[0].name).to.equal('normal')
248 expect(videos[1].name).to.equal('nsfw')
253 after(async function () {
254 await cleanupTests([ server ])