PeerTube/server/tests/api/videos/video-nsfw.ts

228 lines
7.9 KiB
TypeScript
Raw Normal View History

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