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: channelName, ...query }),
34 server.videos.listByChannel({ token, handle: accountName, ...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({ handle: channelName }),
51 server.videos.listByChannel({ handle: accountName })
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 () {
85 it('Should display NSFW videos with display default NSFW policy', async function () {
86 const serverConfig = await server.config.getConfig()
87 expect(serverConfig.instance.defaultNSFWPolicy).to.equal('display')
89 for (const body of await getVideosFunctions()) {
90 expect(body.total).to.equal(2)
92 const videos = body.data
93 expect(videos).to.have.lengthOf(2)
94 expect(videos[0].name).to.equal('normal')
95 expect(videos[1].name).to.equal('nsfw')
99 it('Should not display NSFW videos with do_not_list default NSFW policy', async function () {
100 customConfig.instance.defaultNSFWPolicy = 'do_not_list'
101 await server.config.updateCustomConfig({ newCustomConfig: customConfig })
103 const serverConfig = await server.config.getConfig()
104 expect(serverConfig.instance.defaultNSFWPolicy).to.equal('do_not_list')
106 for (const body of await getVideosFunctions()) {
107 expect(body.total).to.equal(1)
109 const videos = body.data
110 expect(videos).to.have.lengthOf(1)
111 expect(videos[0].name).to.equal('normal')
115 it('Should display NSFW videos with blur default NSFW policy', async function () {
116 customConfig.instance.defaultNSFWPolicy = 'blur'
117 await server.config.updateCustomConfig({ newCustomConfig: customConfig })
119 const serverConfig = await server.config.getConfig()
120 expect(serverConfig.instance.defaultNSFWPolicy).to.equal('blur')
122 for (const body of await getVideosFunctions()) {
123 expect(body.total).to.equal(2)
125 const videos = body.data
126 expect(videos).to.have.lengthOf(2)
127 expect(videos[0].name).to.equal('normal')
128 expect(videos[1].name).to.equal('nsfw')
133 describe('User NSFW policy', function () {
135 it('Should create a user having the default nsfw policy', async function () {
136 const username = 'user1'
137 const password = 'my super password'
138 await server.users.create({ username: username, password: password })
140 userAccessToken = await server.login.getAccessToken({ username, password })
142 const user = await server.users.getMyInfo({ token: userAccessToken })
143 expect(user.nsfwPolicy).to.equal('blur')
146 it('Should display NSFW videos with blur user NSFW policy', async function () {
147 customConfig.instance.defaultNSFWPolicy = 'do_not_list'
148 await server.config.updateCustomConfig({ newCustomConfig: customConfig })
150 for (const body of await getVideosFunctions(userAccessToken)) {
151 expect(body.total).to.equal(2)
153 const videos = body.data
154 expect(videos).to.have.lengthOf(2)
155 expect(videos[0].name).to.equal('normal')
156 expect(videos[1].name).to.equal('nsfw')
160 it('Should display NSFW videos with display user NSFW policy', async function () {
161 await server.users.updateMe({ nsfwPolicy: 'display' })
163 for (const body of await getVideosFunctions(server.accessToken)) {
164 expect(body.total).to.equal(2)
166 const videos = body.data
167 expect(videos).to.have.lengthOf(2)
168 expect(videos[0].name).to.equal('normal')
169 expect(videos[1].name).to.equal('nsfw')
173 it('Should not display NSFW videos with do_not_list user NSFW policy', async function () {
174 await server.users.updateMe({ nsfwPolicy: 'do_not_list' })
176 for (const body of await getVideosFunctions(server.accessToken)) {
177 expect(body.total).to.equal(1)
179 const videos = body.data
180 expect(videos).to.have.lengthOf(1)
181 expect(videos[0].name).to.equal('normal')
185 it('Should be able to see my NSFW videos even with do_not_list user NSFW policy', async function () {
186 const { total, data } = await server.videos.listMyVideos()
187 expect(total).to.equal(2)
189 expect(data).to.have.lengthOf(2)
190 expect(data[0].name).to.equal('normal')
191 expect(data[1].name).to.equal('nsfw')
194 it('Should display NSFW videos when the nsfw param === true', async function () {
195 for (const body of await getVideosFunctions(server.accessToken, { nsfw: 'true' })) {
196 expect(body.total).to.equal(1)
198 const videos = body.data
199 expect(videos).to.have.lengthOf(1)
200 expect(videos[0].name).to.equal('nsfw')
204 it('Should hide NSFW videos when the nsfw param === true', async function () {
205 for (const body of await getVideosFunctions(server.accessToken, { nsfw: 'false' })) {
206 expect(body.total).to.equal(1)
208 const videos = body.data
209 expect(videos).to.have.lengthOf(1)
210 expect(videos[0].name).to.equal('normal')
214 it('Should display both videos when the nsfw param === both', async function () {
215 for (const body of await getVideosFunctions(server.accessToken, { nsfw: 'both' })) {
216 expect(body.total).to.equal(2)
218 const videos = body.data
219 expect(videos).to.have.lengthOf(2)
220 expect(videos[0].name).to.equal('normal')
221 expect(videos[1].name).to.equal('nsfw')
226 after(async function () {
227 await cleanupTests([ server ])