1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
4 import * as chai from 'chai'
12 getVideoChannelVideos,
14 getVideosListWithToken,
16 setAccessTokensToServers,
19 } from '@shared/extra-utils'
20 import { BooleanBothQuery, CustomConfig, ResultList, User, Video, VideosOverview } from '@shared/models'
22 const expect = chai.expect
24 function createOverviewRes (overview: VideosOverview) {
25 const videos = overview.categories[0].videos
26 return { data: videos, total: videos.length }
29 describe('Test video NSFW policy', function () {
30 let server: ServerInfo
31 let userAccessToken: string
32 let customConfig: CustomConfig
34 async function getVideosFunctions (token?: string, query: { nsfw?: BooleanBothQuery } = {}) {
35 const res = await getMyUserInformation(server.url, server.accessToken)
36 const user: User = res.body
37 const videoChannelName = user.videoChannels[0].name
38 const accountName = user.account.name + '@' + user.account.host
39 const hasQuery = Object.keys(query).length !== 0
40 let promises: Promise<ResultList<Video>>[]
44 getVideosListWithToken(server.url, token, query).then(res => res.body),
45 server.searchCommand.advancedVideoSearch({ token, search: { search: 'n', sort: '-publishedAt', ...query } }),
46 getAccountVideos(server.url, token, accountName, 0, 5, undefined, query).then(res => res.body),
47 getVideoChannelVideos(server.url, token, videoChannelName, 0, 5, undefined, query).then(res => res.body)
50 // Overviews do not support video filters
52 const p = server.overviewsCommand.getVideos({ page: 1, token })
53 .then(res => createOverviewRes(res))
57 return Promise.all(promises)
61 getVideosList(server.url).then(res => res.body),
62 server.searchCommand.searchVideos({ search: 'n', sort: '-publishedAt' }),
63 getAccountVideos(server.url, undefined, accountName, 0, 5).then(res => res.body),
64 getVideoChannelVideos(server.url, undefined, videoChannelName, 0, 5).then(res => res.body)
67 // Overviews do not support video filters
69 const p = server.overviewsCommand.getVideos({ page: 1 })
70 .then(res => createOverviewRes(res))
74 return Promise.all(promises)
77 before(async function () {
79 server = await flushAndRunServer(1)
81 // Get the access tokens
82 await setAccessTokensToServers([ server ])
85 const attributes = { name: 'nsfw', nsfw: true, category: 1 }
86 await uploadVideo(server.url, server.accessToken, attributes)
90 const attributes = { name: 'normal', nsfw: false, category: 1 }
91 await uploadVideo(server.url, server.accessToken, attributes)
94 customConfig = await server.configCommand.getCustomConfig()
97 describe('Instance default NSFW policy', function () {
98 it('Should display NSFW videos with display default NSFW policy', async function () {
99 const serverConfig = await server.configCommand.getConfig()
100 expect(serverConfig.instance.defaultNSFWPolicy).to.equal('display')
102 for (const body of await getVideosFunctions()) {
103 expect(body.total).to.equal(2)
105 const videos = body.data
106 expect(videos).to.have.lengthOf(2)
107 expect(videos[0].name).to.equal('normal')
108 expect(videos[1].name).to.equal('nsfw')
112 it('Should not display NSFW videos with do_not_list default NSFW policy', async function () {
113 customConfig.instance.defaultNSFWPolicy = 'do_not_list'
114 await server.configCommand.updateCustomConfig({ newCustomConfig: customConfig })
116 const serverConfig = await server.configCommand.getConfig()
117 expect(serverConfig.instance.defaultNSFWPolicy).to.equal('do_not_list')
119 for (const body of await getVideosFunctions()) {
120 expect(body.total).to.equal(1)
122 const videos = body.data
123 expect(videos).to.have.lengthOf(1)
124 expect(videos[0].name).to.equal('normal')
128 it('Should display NSFW videos with blur default NSFW policy', async function () {
129 customConfig.instance.defaultNSFWPolicy = 'blur'
130 await server.configCommand.updateCustomConfig({ newCustomConfig: customConfig })
132 const serverConfig = await server.configCommand.getConfig()
133 expect(serverConfig.instance.defaultNSFWPolicy).to.equal('blur')
135 for (const body of await getVideosFunctions()) {
136 expect(body.total).to.equal(2)
138 const videos = body.data
139 expect(videos).to.have.lengthOf(2)
140 expect(videos[0].name).to.equal('normal')
141 expect(videos[1].name).to.equal('nsfw')
146 describe('User NSFW policy', function () {
148 it('Should create a user having the default nsfw policy', async function () {
149 const username = 'user1'
150 const password = 'my super password'
151 await createUser({ url: server.url, accessToken: server.accessToken, username: username, password: password })
153 userAccessToken = await server.loginCommand.getAccessToken({ username, password })
155 const res = await getMyUserInformation(server.url, userAccessToken)
156 const user = res.body
158 expect(user.nsfwPolicy).to.equal('blur')
161 it('Should display NSFW videos with blur user NSFW policy', async function () {
162 customConfig.instance.defaultNSFWPolicy = 'do_not_list'
163 await server.configCommand.updateCustomConfig({ newCustomConfig: customConfig })
165 for (const body of await getVideosFunctions(userAccessToken)) {
166 expect(body.total).to.equal(2)
168 const videos = body.data
169 expect(videos).to.have.lengthOf(2)
170 expect(videos[0].name).to.equal('normal')
171 expect(videos[1].name).to.equal('nsfw')
175 it('Should display NSFW videos with display user NSFW policy', async function () {
178 accessToken: server.accessToken,
179 nsfwPolicy: 'display'
182 for (const body of await getVideosFunctions(server.accessToken)) {
183 expect(body.total).to.equal(2)
185 const videos = body.data
186 expect(videos).to.have.lengthOf(2)
187 expect(videos[0].name).to.equal('normal')
188 expect(videos[1].name).to.equal('nsfw')
192 it('Should not display NSFW videos with do_not_list user NSFW policy', async function () {
195 accessToken: server.accessToken,
196 nsfwPolicy: 'do_not_list'
199 for (const body of await getVideosFunctions(server.accessToken)) {
200 expect(body.total).to.equal(1)
202 const videos = body.data
203 expect(videos).to.have.lengthOf(1)
204 expect(videos[0].name).to.equal('normal')
208 it('Should be able to see my NSFW videos even with do_not_list user NSFW policy', async function () {
209 const res = await getMyVideos(server.url, server.accessToken, 0, 5)
210 expect(res.body.total).to.equal(2)
212 const videos = res.body.data
213 expect(videos).to.have.lengthOf(2)
214 expect(videos[0].name).to.equal('normal')
215 expect(videos[1].name).to.equal('nsfw')
218 it('Should display NSFW videos when the nsfw param === true', async function () {
219 for (const body of await getVideosFunctions(server.accessToken, { nsfw: 'true' })) {
220 expect(body.total).to.equal(1)
222 const videos = body.data
223 expect(videos).to.have.lengthOf(1)
224 expect(videos[0].name).to.equal('nsfw')
228 it('Should hide NSFW videos when the nsfw param === true', async function () {
229 for (const body of await getVideosFunctions(server.accessToken, { nsfw: 'false' })) {
230 expect(body.total).to.equal(1)
232 const videos = body.data
233 expect(videos).to.have.lengthOf(1)
234 expect(videos[0].name).to.equal('normal')
238 it('Should display both videos when the nsfw param === both', async function () {
239 for (const body of await getVideosFunctions(server.accessToken, { nsfw: 'both' })) {
240 expect(body.total).to.equal(2)
242 const videos = body.data
243 expect(videos).to.have.lengthOf(2)
244 expect(videos[0].name).to.equal('normal')
245 expect(videos[1].name).to.equal('nsfw')
250 after(async function () {
251 await cleanupTests([ server ])