2020-01-31 16:56:52 +01:00
|
|
|
/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
|
2018-04-19 11:01:34 +02:00
|
|
|
|
|
|
|
import 'mocha'
|
2021-07-06 14:30:20 +02:00
|
|
|
import * as chai from 'chai'
|
2021-07-16 09:47:51 +02:00
|
|
|
import { cleanupTests, createSingleServer, PeerTubeServer, setAccessTokensToServers } from '@shared/extra-utils'
|
2021-07-13 14:23:01 +02:00
|
|
|
import { BooleanBothQuery, CustomConfig, ResultList, Video, VideosOverview } from '@shared/models'
|
2018-04-19 11:01:34 +02:00
|
|
|
|
|
|
|
const expect = chai.expect
|
|
|
|
|
2021-07-06 14:30:20 +02:00
|
|
|
function createOverviewRes (overview: VideosOverview) {
|
2020-03-11 14:39:28 +01:00
|
|
|
const videos = overview.categories[0].videos
|
2021-07-09 16:23:01 +02:00
|
|
|
return { data: videos, total: videos.length }
|
2020-03-11 14:39:28 +01:00
|
|
|
}
|
|
|
|
|
2018-04-19 11:01:34 +02:00
|
|
|
describe('Test video NSFW policy', function () {
|
2021-07-16 09:47:51 +02:00
|
|
|
let server: PeerTubeServer
|
2018-04-19 11:01:34 +02:00
|
|
|
let userAccessToken: string
|
|
|
|
let customConfig: CustomConfig
|
|
|
|
|
2021-07-09 16:23:01 +02:00
|
|
|
async function getVideosFunctions (token?: string, query: { nsfw?: BooleanBothQuery } = {}) {
|
2021-07-16 09:04:35 +02:00
|
|
|
const user = await server.users.getMyInfo()
|
2021-07-09 16:23:01 +02:00
|
|
|
const videoChannelName = user.videoChannels[0].name
|
|
|
|
const accountName = user.account.name + '@' + user.account.host
|
|
|
|
const hasQuery = Object.keys(query).length !== 0
|
|
|
|
let promises: Promise<ResultList<Video>>[]
|
|
|
|
|
|
|
|
if (token) {
|
|
|
|
promises = [
|
2021-07-16 09:04:35 +02:00
|
|
|
server.search.advancedVideoSearch({ token, search: { search: 'n', sort: '-publishedAt', ...query } }),
|
|
|
|
server.videos.listWithToken({ token, ...query }),
|
|
|
|
server.videos.listByAccount({ token, accountName, ...query }),
|
|
|
|
server.videos.listByChannel({ token, videoChannelName, ...query })
|
2021-07-09 16:23:01 +02:00
|
|
|
]
|
|
|
|
|
|
|
|
// Overviews do not support video filters
|
|
|
|
if (!hasQuery) {
|
2021-07-16 09:04:35 +02:00
|
|
|
const p = server.overviews.getVideos({ page: 1, token })
|
2021-07-09 16:23:01 +02:00
|
|
|
.then(res => createOverviewRes(res))
|
|
|
|
promises.push(p)
|
|
|
|
}
|
|
|
|
|
|
|
|
return Promise.all(promises)
|
|
|
|
}
|
|
|
|
|
|
|
|
promises = [
|
2021-07-16 09:04:35 +02:00
|
|
|
server.search.searchVideos({ search: 'n', sort: '-publishedAt' }),
|
|
|
|
server.videos.list(),
|
|
|
|
server.videos.listByAccount({ accountName }),
|
|
|
|
server.videos.listByChannel({ videoChannelName })
|
2021-07-09 16:23:01 +02:00
|
|
|
]
|
|
|
|
|
|
|
|
// Overviews do not support video filters
|
|
|
|
if (!hasQuery) {
|
2021-07-16 09:04:35 +02:00
|
|
|
const p = server.overviews.getVideos({ page: 1 })
|
2021-07-09 16:23:01 +02:00
|
|
|
.then(res => createOverviewRes(res))
|
|
|
|
promises.push(p)
|
|
|
|
}
|
|
|
|
|
|
|
|
return Promise.all(promises)
|
2018-04-25 10:21:38 +02:00
|
|
|
}
|
|
|
|
|
2018-04-19 11:01:34 +02:00
|
|
|
before(async function () {
|
|
|
|
this.timeout(50000)
|
2021-07-16 09:47:51 +02:00
|
|
|
server = await createSingleServer(1)
|
2018-04-19 11:01:34 +02:00
|
|
|
|
|
|
|
// Get the access tokens
|
|
|
|
await setAccessTokensToServers([ server ])
|
|
|
|
|
|
|
|
{
|
2020-03-11 14:39:28 +01:00
|
|
|
const attributes = { name: 'nsfw', nsfw: true, category: 1 }
|
2021-07-16 09:04:35 +02:00
|
|
|
await server.videos.upload({ attributes })
|
2018-04-19 11:01:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2020-03-11 14:39:28 +01:00
|
|
|
const attributes = { name: 'normal', nsfw: false, category: 1 }
|
2021-07-16 09:04:35 +02:00
|
|
|
await server.videos.upload({ attributes })
|
2018-04-19 11:01:34 +02:00
|
|
|
}
|
|
|
|
|
2021-07-16 09:04:35 +02:00
|
|
|
customConfig = await server.config.getCustomConfig()
|
2018-04-19 11:01:34 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
describe('Instance default NSFW policy', function () {
|
|
|
|
it('Should display NSFW videos with display default NSFW policy', async function () {
|
2021-07-16 09:04:35 +02:00
|
|
|
const serverConfig = await server.config.getConfig()
|
2018-04-19 11:01:34 +02:00
|
|
|
expect(serverConfig.instance.defaultNSFWPolicy).to.equal('display')
|
|
|
|
|
2021-07-09 16:23:01 +02:00
|
|
|
for (const body of await getVideosFunctions()) {
|
|
|
|
expect(body.total).to.equal(2)
|
2018-04-19 11:01:34 +02:00
|
|
|
|
2021-07-09 16:23:01 +02:00
|
|
|
const videos = body.data
|
2018-04-19 11:01:34 +02:00
|
|
|
expect(videos).to.have.lengthOf(2)
|
2020-01-31 16:56:52 +01:00
|
|
|
expect(videos[0].name).to.equal('normal')
|
|
|
|
expect(videos[1].name).to.equal('nsfw')
|
2018-04-19 11:01:34 +02:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
it('Should not display NSFW videos with do_not_list default NSFW policy', async function () {
|
|
|
|
customConfig.instance.defaultNSFWPolicy = 'do_not_list'
|
2021-07-16 09:04:35 +02:00
|
|
|
await server.config.updateCustomConfig({ newCustomConfig: customConfig })
|
2018-04-19 11:01:34 +02:00
|
|
|
|
2021-07-16 09:04:35 +02:00
|
|
|
const serverConfig = await server.config.getConfig()
|
2018-04-19 11:01:34 +02:00
|
|
|
expect(serverConfig.instance.defaultNSFWPolicy).to.equal('do_not_list')
|
|
|
|
|
2021-07-09 16:23:01 +02:00
|
|
|
for (const body of await getVideosFunctions()) {
|
|
|
|
expect(body.total).to.equal(1)
|
2018-04-19 11:01:34 +02:00
|
|
|
|
2021-07-09 16:23:01 +02:00
|
|
|
const videos = body.data
|
2018-04-19 11:01:34 +02:00
|
|
|
expect(videos).to.have.lengthOf(1)
|
2020-01-31 16:56:52 +01:00
|
|
|
expect(videos[0].name).to.equal('normal')
|
2018-04-19 11:01:34 +02:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
it('Should display NSFW videos with blur default NSFW policy', async function () {
|
|
|
|
customConfig.instance.defaultNSFWPolicy = 'blur'
|
2021-07-16 09:04:35 +02:00
|
|
|
await server.config.updateCustomConfig({ newCustomConfig: customConfig })
|
2018-04-19 11:01:34 +02:00
|
|
|
|
2021-07-16 09:04:35 +02:00
|
|
|
const serverConfig = await server.config.getConfig()
|
2018-04-19 11:01:34 +02:00
|
|
|
expect(serverConfig.instance.defaultNSFWPolicy).to.equal('blur')
|
|
|
|
|
2021-07-09 16:23:01 +02:00
|
|
|
for (const body of await getVideosFunctions()) {
|
|
|
|
expect(body.total).to.equal(2)
|
2018-04-19 11:01:34 +02:00
|
|
|
|
2021-07-09 16:23:01 +02:00
|
|
|
const videos = body.data
|
2018-04-19 11:01:34 +02:00
|
|
|
expect(videos).to.have.lengthOf(2)
|
2020-01-31 16:56:52 +01:00
|
|
|
expect(videos[0].name).to.equal('normal')
|
|
|
|
expect(videos[1].name).to.equal('nsfw')
|
2018-04-19 11:01:34 +02:00
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('User NSFW policy', function () {
|
|
|
|
|
|
|
|
it('Should create a user having the default nsfw policy', async function () {
|
|
|
|
const username = 'user1'
|
|
|
|
const password = 'my super password'
|
2021-07-16 09:04:35 +02:00
|
|
|
await server.users.create({ username: username, password: password })
|
2018-04-19 11:01:34 +02:00
|
|
|
|
2021-07-16 09:04:35 +02:00
|
|
|
userAccessToken = await server.login.getAccessToken({ username, password })
|
2018-04-19 11:01:34 +02:00
|
|
|
|
2021-07-16 09:04:35 +02:00
|
|
|
const user = await server.users.getMyInfo({ token: userAccessToken })
|
2018-04-19 11:01:34 +02:00
|
|
|
expect(user.nsfwPolicy).to.equal('blur')
|
|
|
|
})
|
|
|
|
|
|
|
|
it('Should display NSFW videos with blur user NSFW policy', async function () {
|
2018-09-04 11:19:19 +02:00
|
|
|
customConfig.instance.defaultNSFWPolicy = 'do_not_list'
|
2021-07-16 09:04:35 +02:00
|
|
|
await server.config.updateCustomConfig({ newCustomConfig: customConfig })
|
2018-09-04 11:19:19 +02:00
|
|
|
|
2021-07-09 16:23:01 +02:00
|
|
|
for (const body of await getVideosFunctions(userAccessToken)) {
|
|
|
|
expect(body.total).to.equal(2)
|
2018-04-19 11:01:34 +02:00
|
|
|
|
2021-07-09 16:23:01 +02:00
|
|
|
const videos = body.data
|
2018-04-19 11:01:34 +02:00
|
|
|
expect(videos).to.have.lengthOf(2)
|
2020-01-31 16:56:52 +01:00
|
|
|
expect(videos[0].name).to.equal('normal')
|
|
|
|
expect(videos[1].name).to.equal('nsfw')
|
2018-04-19 11:01:34 +02:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
it('Should display NSFW videos with display user NSFW policy', async function () {
|
2021-07-16 09:04:35 +02:00
|
|
|
await server.users.updateMe({ nsfwPolicy: 'display' })
|
2018-04-19 11:01:34 +02:00
|
|
|
|
2021-07-09 16:23:01 +02:00
|
|
|
for (const body of await getVideosFunctions(server.accessToken)) {
|
|
|
|
expect(body.total).to.equal(2)
|
2018-04-19 11:01:34 +02:00
|
|
|
|
2021-07-09 16:23:01 +02:00
|
|
|
const videos = body.data
|
2018-04-19 11:01:34 +02:00
|
|
|
expect(videos).to.have.lengthOf(2)
|
2020-01-31 16:56:52 +01:00
|
|
|
expect(videos[0].name).to.equal('normal')
|
|
|
|
expect(videos[1].name).to.equal('nsfw')
|
2018-04-19 11:01:34 +02:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
it('Should not display NSFW videos with do_not_list user NSFW policy', async function () {
|
2021-07-16 09:04:35 +02:00
|
|
|
await server.users.updateMe({ nsfwPolicy: 'do_not_list' })
|
2018-04-19 11:01:34 +02:00
|
|
|
|
2021-07-09 16:23:01 +02:00
|
|
|
for (const body of await getVideosFunctions(server.accessToken)) {
|
|
|
|
expect(body.total).to.equal(1)
|
2018-04-19 11:01:34 +02:00
|
|
|
|
2021-07-09 16:23:01 +02:00
|
|
|
const videos = body.data
|
2018-04-19 11:01:34 +02:00
|
|
|
expect(videos).to.have.lengthOf(1)
|
2020-01-31 16:56:52 +01:00
|
|
|
expect(videos[0].name).to.equal('normal')
|
2018-04-19 11:01:34 +02:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
it('Should be able to see my NSFW videos even with do_not_list user NSFW policy', async function () {
|
2021-07-16 09:04:35 +02:00
|
|
|
const { total, data } = await server.videos.listMyVideos()
|
2021-07-15 10:02:54 +02:00
|
|
|
expect(total).to.equal(2)
|
2018-04-19 11:01:34 +02:00
|
|
|
|
2021-07-15 10:02:54 +02:00
|
|
|
expect(data).to.have.lengthOf(2)
|
|
|
|
expect(data[0].name).to.equal('normal')
|
|
|
|
expect(data[1].name).to.equal('nsfw')
|
2018-04-19 11:01:34 +02:00
|
|
|
})
|
2018-07-20 14:35:18 +02:00
|
|
|
|
|
|
|
it('Should display NSFW videos when the nsfw param === true', async function () {
|
2021-07-09 16:23:01 +02:00
|
|
|
for (const body of await getVideosFunctions(server.accessToken, { nsfw: 'true' })) {
|
|
|
|
expect(body.total).to.equal(1)
|
2018-07-20 14:35:18 +02:00
|
|
|
|
2021-07-09 16:23:01 +02:00
|
|
|
const videos = body.data
|
2018-07-20 14:35:18 +02:00
|
|
|
expect(videos).to.have.lengthOf(1)
|
2020-01-31 16:56:52 +01:00
|
|
|
expect(videos[0].name).to.equal('nsfw')
|
2018-07-20 14:35:18 +02:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
it('Should hide NSFW videos when the nsfw param === true', async function () {
|
2021-07-09 16:23:01 +02:00
|
|
|
for (const body of await getVideosFunctions(server.accessToken, { nsfw: 'false' })) {
|
|
|
|
expect(body.total).to.equal(1)
|
2018-07-20 14:35:18 +02:00
|
|
|
|
2021-07-09 16:23:01 +02:00
|
|
|
const videos = body.data
|
2018-07-20 14:35:18 +02:00
|
|
|
expect(videos).to.have.lengthOf(1)
|
2020-01-31 16:56:52 +01:00
|
|
|
expect(videos[0].name).to.equal('normal')
|
2018-07-20 14:35:18 +02:00
|
|
|
}
|
|
|
|
})
|
2018-07-20 18:31:49 +02:00
|
|
|
|
|
|
|
it('Should display both videos when the nsfw param === both', async function () {
|
2021-07-09 16:23:01 +02:00
|
|
|
for (const body of await getVideosFunctions(server.accessToken, { nsfw: 'both' })) {
|
|
|
|
expect(body.total).to.equal(2)
|
2018-07-20 18:31:49 +02:00
|
|
|
|
2021-07-09 16:23:01 +02:00
|
|
|
const videos = body.data
|
2018-07-20 18:31:49 +02:00
|
|
|
expect(videos).to.have.lengthOf(2)
|
2020-01-31 16:56:52 +01:00
|
|
|
expect(videos[0].name).to.equal('normal')
|
|
|
|
expect(videos[1].name).to.equal('nsfw')
|
2018-07-20 18:31:49 +02:00
|
|
|
}
|
|
|
|
})
|
2018-04-19 11:01:34 +02:00
|
|
|
})
|
|
|
|
|
2019-04-24 15:10:37 +02:00
|
|
|
after(async function () {
|
|
|
|
await cleanupTests([ server ])
|
2018-04-19 11:01:34 +02:00
|
|
|
})
|
|
|
|
})
|