1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
4 import * as chai from 'chai'
5 import { cleanupTests, createSingleServer, PeerTubeServer, 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: PeerTubeServer
17 let userAccessToken: string
18 let customConfig: CustomConfig
20 async function getVideosFunctions (token?: string, query: { nsfw?: BooleanBothQuery } = {}) {
21 const user = await server.users.getMyInfo()
23 const channelName = user.videoChannels[0].name
24 const accountName = user.account.name + '@' + user.account.host
26 const hasQuery = Object.keys(query).length !== 0
27 let promises: Promise<ResultList<Video>>[]
31 server.search.advancedVideoSearch({ token, search: { search: 'n', sort: '-publishedAt', ...query } }),
32 server.videos.listWithToken({ token, ...query }),
33 server.videos.listByAccount({ token, handle: accountName, ...query }),
34 server.videos.listByChannel({ token, handle: channelName, ...query })
37 // Overviews do not support video filters
39 const p = server.overviews.getVideos({ page: 1, token })
40 .then(res => createOverviewRes(res))
44 return Promise.all(promises)
48 server.search.searchVideos({ search: 'n', sort: '-publishedAt' }),
50 server.videos.listByAccount({ token: null, handle: accountName }),
51 server.videos.listByChannel({ token: null, handle: channelName })
54 // Overviews do not support video filters
56 const p = server.overviews.getVideos({ page: 1 })
57 .then(res => createOverviewRes(res))
61 return Promise.all(promises)
64 before(async function () {
66 server = await createSingleServer(1)
68 // Get the access tokens
69 await setAccessTokensToServers([ server ])
72 const attributes = { name: 'nsfw', nsfw: true, category: 1 }
73 await server.videos.upload({ attributes })
77 const attributes = { name: 'normal', nsfw: false, category: 1 }
78 await server.videos.upload({ attributes })
81 customConfig = await server.config.getCustomConfig()
84 describe('Instance default NSFW policy', function () {
86 it('Should display NSFW videos with display default NSFW policy', async function () {
87 const serverConfig = await server.config.getConfig()
88 expect(serverConfig.instance.defaultNSFWPolicy).to.equal('display')
90 for (const body of await getVideosFunctions()) {
91 expect(body.total).to.equal(2)
93 const videos = body.data
94 expect(videos).to.have.lengthOf(2)
95 expect(videos[0].name).to.equal('normal')
96 expect(videos[1].name).to.equal('nsfw')
100 it('Should not display NSFW videos with do_not_list default NSFW policy', async function () {
101 customConfig.instance.defaultNSFWPolicy = 'do_not_list'
102 await server.config.updateCustomConfig({ newCustomConfig: customConfig })
104 const serverConfig = await server.config.getConfig()
105 expect(serverConfig.instance.defaultNSFWPolicy).to.equal('do_not_list')
107 for (const body of await getVideosFunctions()) {
108 expect(body.total).to.equal(1)
110 const videos = body.data
111 expect(videos).to.have.lengthOf(1)
112 expect(videos[0].name).to.equal('normal')
116 it('Should display NSFW videos with blur default NSFW policy', async function () {
117 customConfig.instance.defaultNSFWPolicy = 'blur'
118 await server.config.updateCustomConfig({ newCustomConfig: customConfig })
120 const serverConfig = await server.config.getConfig()
121 expect(serverConfig.instance.defaultNSFWPolicy).to.equal('blur')
123 for (const body of await getVideosFunctions()) {
124 expect(body.total).to.equal(2)
126 const videos = body.data
127 expect(videos).to.have.lengthOf(2)
128 expect(videos[0].name).to.equal('normal')
129 expect(videos[1].name).to.equal('nsfw')
134 describe('User NSFW policy', function () {
136 it('Should create a user having the default nsfw policy', async function () {
137 const username = 'user1'
138 const password = 'my super password'
139 await server.users.create({ username: username, password: password })
141 userAccessToken = await server.login.getAccessToken({ username, password })
143 const user = await server.users.getMyInfo({ token: userAccessToken })
144 expect(user.nsfwPolicy).to.equal('blur')
147 it('Should display NSFW videos with blur user NSFW policy', async function () {
148 customConfig.instance.defaultNSFWPolicy = 'do_not_list'
149 await server.config.updateCustomConfig({ newCustomConfig: customConfig })
151 for (const body of await getVideosFunctions(userAccessToken)) {
152 expect(body.total).to.equal(2)
154 const videos = body.data
155 expect(videos).to.have.lengthOf(2)
156 expect(videos[0].name).to.equal('normal')
157 expect(videos[1].name).to.equal('nsfw')
161 it('Should display NSFW videos with display user NSFW policy', async function () {
162 await server.users.updateMe({ nsfwPolicy: 'display' })
164 for (const body of await getVideosFunctions(server.accessToken)) {
165 expect(body.total).to.equal(2)
167 const videos = body.data
168 expect(videos).to.have.lengthOf(2)
169 expect(videos[0].name).to.equal('normal')
170 expect(videos[1].name).to.equal('nsfw')
174 it('Should not display NSFW videos with do_not_list user NSFW policy', async function () {
175 await server.users.updateMe({ nsfwPolicy: 'do_not_list' })
177 for (const body of await getVideosFunctions(server.accessToken)) {
178 expect(body.total).to.equal(1)
180 const videos = body.data
181 expect(videos).to.have.lengthOf(1)
182 expect(videos[0].name).to.equal('normal')
186 it('Should be able to see my NSFW videos even with do_not_list user NSFW policy', async function () {
187 const { total, data } = await server.videos.listMyVideos()
188 expect(total).to.equal(2)
190 expect(data).to.have.lengthOf(2)
191 expect(data[0].name).to.equal('normal')
192 expect(data[1].name).to.equal('nsfw')
195 it('Should display NSFW videos when the nsfw param === true', async function () {
196 for (const body of await getVideosFunctions(server.accessToken, { nsfw: 'true' })) {
197 expect(body.total).to.equal(1)
199 const videos = body.data
200 expect(videos).to.have.lengthOf(1)
201 expect(videos[0].name).to.equal('nsfw')
205 it('Should hide NSFW videos when the nsfw param === true', async function () {
206 for (const body of await getVideosFunctions(server.accessToken, { nsfw: 'false' })) {
207 expect(body.total).to.equal(1)
209 const videos = body.data
210 expect(videos).to.have.lengthOf(1)
211 expect(videos[0].name).to.equal('normal')
215 it('Should display both videos when the nsfw param === both', async function () {
216 for (const body of await getVideosFunctions(server.accessToken, { nsfw: 'both' })) {
217 expect(body.total).to.equal(2)
219 const videos = body.data
220 expect(videos).to.have.lengthOf(2)
221 expect(videos[0].name).to.equal('normal')
222 expect(videos[1].name).to.equal('nsfw')
227 after(async function () {
228 await cleanupTests([ server ])