From 0883b3245bf0deb9106c4041e9afbd3521b79280 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Thu, 19 Apr 2018 11:01:34 +0200 Subject: Add ability to choose what policy we have for NSFW videos There is a global instance setting and a per user setting --- server/tests/api/check-params/config.ts | 19 ++- server/tests/api/check-params/users.ts | 6 +- server/tests/api/server/config.ts | 5 + server/tests/api/users/users.ts | 26 ++--- server/tests/api/videos/video-nsfw.ts | 197 ++++++++++++++++++++++++++++++++ server/tests/utils/users/users.ts | 5 +- server/tests/utils/videos/videos.ts | 26 +++++ 7 files changed, 265 insertions(+), 19 deletions(-) create mode 100644 server/tests/api/videos/video-nsfw.ts (limited to 'server/tests') diff --git a/server/tests/api/check-params/config.ts b/server/tests/api/check-params/config.ts index 3fe517fad..58b780f38 100644 --- a/server/tests/api/check-params/config.ts +++ b/server/tests/api/check-params/config.ts @@ -6,7 +6,7 @@ import { CustomConfig } from '../../../../shared/models/server/custom-config.mod import { createUser, flushTests, killallServers, makeDeleteRequest, makeGetRequest, makePutBodyRequest, runServer, ServerInfo, - setAccessTokensToServers, userLogin + setAccessTokensToServers, userLogin, immutableAssign } from '../../utils' describe('Test config API validators', function () { @@ -20,6 +20,7 @@ describe('Test config API validators', function () { description: 'my super description', terms: 'my super terms', defaultClientRoute: '/videos/recently-added', + defaultNSFWPolicy: 'blur', customizations: { javascript: 'alert("coucou")', css: 'body { background-color: red; }' @@ -122,6 +123,22 @@ describe('Test config API validators', function () { }) }) + it('Should fail with a bad default NSFW policy', async function () { + const newUpdateParams = immutableAssign(updateParams, { + instance: { + defaultNSFWPolicy: 'hello' + } + }) + + await makePutBodyRequest({ + url: server.url, + path, + fields: newUpdateParams, + token: server.accessToken, + statusCodeExpected: 400 + }) + }) + it('Should success with the correct parameters', async function () { await makePutBodyRequest({ url: server.url, diff --git a/server/tests/api/check-params/users.ts b/server/tests/api/check-params/users.ts index a3e415b94..e8a6ffd19 100644 --- a/server/tests/api/check-params/users.ts +++ b/server/tests/api/check-params/users.ts @@ -231,9 +231,9 @@ describe('Test users API validators', function () { await makePutBodyRequest({ url: server.url, path: path + 'me', token: userAccessToken, fields }) }) - it('Should fail with an invalid display NSFW attribute', async function () { + it('Should fail with an invalid NSFW policy attribute', async function () { const fields = { - displayNSFW: -1 + nsfwPolicy: 'hello' } await makePutBodyRequest({ url: server.url, path: path + 'me', token: userAccessToken, fields }) @@ -266,7 +266,7 @@ describe('Test users API validators', function () { it('Should succeed with the correct params', async function () { const fields = { password: 'my super password', - displayNSFW: true, + nsfwPolicy: 'blur', autoPlayVideo: false, email: 'super_email@example.com' } diff --git a/server/tests/api/server/config.ts b/server/tests/api/server/config.ts index e17588142..3f1b1532c 100644 --- a/server/tests/api/server/config.ts +++ b/server/tests/api/server/config.ts @@ -59,6 +59,7 @@ describe('Test config', function () { expect(data.instance.description).to.equal('Welcome to this PeerTube instance!') expect(data.instance.terms).to.equal('No terms for now.') expect(data.instance.defaultClientRoute).to.equal('/videos/trending') + expect(data.instance.defaultNSFWPolicy).to.equal('display') expect(data.instance.customizations.css).to.be.empty expect(data.instance.customizations.javascript).to.be.empty expect(data.cache.previews.size).to.equal(1) @@ -83,6 +84,7 @@ describe('Test config', function () { description: 'my super description', terms: 'my super terms', defaultClientRoute: '/videos/recently-added', + defaultNSFWPolicy: 'blur' as 'blur', customizations: { javascript: 'alert("coucou")', css: 'body { background-color: red; }' @@ -125,6 +127,7 @@ describe('Test config', function () { expect(data.instance.description).to.equal('my super description') expect(data.instance.terms).to.equal('my super terms') expect(data.instance.defaultClientRoute).to.equal('/videos/recently-added') + expect(data.instance.defaultNSFWPolicy).to.equal('blur') expect(data.instance.customizations.javascript).to.equal('alert("coucou")') expect(data.instance.customizations.css).to.equal('body { background-color: red; }') expect(data.cache.previews.size).to.equal(2) @@ -156,6 +159,7 @@ describe('Test config', function () { expect(data.instance.description).to.equal('my super description') expect(data.instance.terms).to.equal('my super terms') expect(data.instance.defaultClientRoute).to.equal('/videos/recently-added') + expect(data.instance.defaultNSFWPolicy).to.equal('blur') expect(data.instance.customizations.javascript).to.equal('alert("coucou")') expect(data.instance.customizations.css).to.equal('body { background-color: red; }') expect(data.cache.previews.size).to.equal(2) @@ -198,6 +202,7 @@ describe('Test config', function () { expect(data.instance.description).to.equal('Welcome to this PeerTube instance!') expect(data.instance.terms).to.equal('No terms for now.') expect(data.instance.defaultClientRoute).to.equal('/videos/trending') + expect(data.instance.defaultNSFWPolicy).to.equal('display') expect(data.instance.customizations.css).to.be.empty expect(data.instance.customizations.javascript).to.be.empty expect(data.cache.previews.size).to.equal(1) diff --git a/server/tests/api/users/users.ts b/server/tests/api/users/users.ts index b6ab4f660..1192ef9e4 100644 --- a/server/tests/api/users/users.ts +++ b/server/tests/api/users/users.ts @@ -168,7 +168,7 @@ describe('Test users', function () { expect(user.username).to.equal('user_1') expect(user.email).to.equal('user_1@example.com') - expect(user.displayNSFW).to.be.false + expect(user.nsfwPolicy).to.equal('display') expect(user.videoQuota).to.equal(2 * 1024 * 1024) expect(user.roleLabel).to.equal('User') expect(user.id).to.be.a('number') @@ -215,12 +215,12 @@ describe('Test users', function () { const user = users[ 0 ] expect(user.username).to.equal('user_1') expect(user.email).to.equal('user_1@example.com') - expect(user.displayNSFW).to.be.false + expect(user.nsfwPolicy).to.equal('display') const rootUser = users[ 1 ] expect(rootUser.username).to.equal('root') expect(rootUser.email).to.equal('admin1@example.com') - expect(rootUser.displayNSFW).to.be.false + expect(user.nsfwPolicy).to.equal('display') userId = user.id }) @@ -239,7 +239,7 @@ describe('Test users', function () { expect(user.username).to.equal('root') expect(user.email).to.equal('admin1@example.com') expect(user.roleLabel).to.equal('Administrator') - expect(user.displayNSFW).to.be.false + expect(user.nsfwPolicy).to.equal('display') }) it('Should list only the first user by username desc', async function () { @@ -254,7 +254,7 @@ describe('Test users', function () { const user = users[ 0 ] expect(user.username).to.equal('user_1') expect(user.email).to.equal('user_1@example.com') - expect(user.displayNSFW).to.be.false + expect(user.nsfwPolicy).to.equal('display') }) it('Should list only the second user by createdAt desc', async function () { @@ -269,7 +269,7 @@ describe('Test users', function () { const user = users[ 0 ] expect(user.username).to.equal('user_1') expect(user.email).to.equal('user_1@example.com') - expect(user.displayNSFW).to.be.false + expect(user.nsfwPolicy).to.equal('display') }) it('Should list all the users by createdAt asc', async function () { @@ -283,11 +283,11 @@ describe('Test users', function () { expect(users[ 0 ].username).to.equal('root') expect(users[ 0 ].email).to.equal('admin1@example.com') - expect(users[ 0 ].displayNSFW).to.be.false + expect(users[ 0 ].nsfwPolicy).to.equal('display') expect(users[ 1 ].username).to.equal('user_1') expect(users[ 1 ].email).to.equal('user_1@example.com') - expect(users[ 1 ].displayNSFW).to.be.false + expect(users[ 1 ].nsfwPolicy).to.equal('display') }) it('Should update my password', async function () { @@ -305,7 +305,7 @@ describe('Test users', function () { await updateMyUser({ url: server.url, accessToken: accessTokenUser, - displayNSFW: true + nsfwPolicy: 'do_not_list' }) const res = await getMyUserInformation(server.url, accessTokenUser) @@ -313,7 +313,7 @@ describe('Test users', function () { expect(user.username).to.equal('user_1') expect(user.email).to.equal('user_1@example.com') - expect(user.displayNSFW).to.be.ok + expect(user.nsfwPolicy).to.equal('do_not_list') expect(user.videoQuota).to.equal(2 * 1024 * 1024) expect(user.id).to.be.a('number') expect(user.account.description).to.be.null @@ -344,7 +344,7 @@ describe('Test users', function () { expect(user.username).to.equal('user_1') expect(user.email).to.equal('updated@example.com') - expect(user.displayNSFW).to.be.ok + expect(user.nsfwPolicy).to.equal('do_not_list') expect(user.videoQuota).to.equal(2 * 1024 * 1024) expect(user.id).to.be.a('number') expect(user.account.description).to.be.null @@ -377,7 +377,7 @@ describe('Test users', function () { expect(user.username).to.equal('user_1') expect(user.email).to.equal('updated@example.com') - expect(user.displayNSFW).to.be.ok + expect(user.nsfwPolicy).to.equal('do_not_list') expect(user.videoQuota).to.equal(2 * 1024 * 1024) expect(user.id).to.be.a('number') expect(user.account.description).to.equal('my super description updated') @@ -398,7 +398,7 @@ describe('Test users', function () { expect(user.username).to.equal('user_1') expect(user.email).to.equal('updated2@example.com') - expect(user.displayNSFW).to.be.ok + expect(user.nsfwPolicy).to.equal('do_not_list') expect(user.videoQuota).to.equal(42) expect(user.roleLabel).to.equal('Moderator') expect(user.id).to.be.a('number') diff --git a/server/tests/api/videos/video-nsfw.ts b/server/tests/api/videos/video-nsfw.ts new file mode 100644 index 000000000..4e5ab11ce --- /dev/null +++ b/server/tests/api/videos/video-nsfw.ts @@ -0,0 +1,197 @@ +/* tslint:disable:no-unused-expression */ + +import * as chai from 'chai' +import 'mocha' +import { flushTests, getVideosList, killallServers, ServerInfo, setAccessTokensToServers, uploadVideo } from '../../utils/index' +import { userLogin } from '../../utils/users/login' +import { createUser } from '../../utils/users/users' +import { getMyVideos } from '../../utils/videos/videos' +import { + getConfig, getCustomConfig, + getMyUserInformation, + getVideosListWithToken, + runServer, + searchVideo, + searchVideoWithToken, updateCustomConfig, + updateMyUser +} from '../../utils' +import { ServerConfig } from '../../../../shared/models' +import { CustomConfig } from '../../../../shared/models/server/custom-config.model' + +const expect = chai.expect + +describe('Test video NSFW policy', function () { + let server: ServerInfo + let userAccessToken: string + let customConfig: CustomConfig + + before(async function () { + this.timeout(50000) + + await flushTests() + server = await runServer(1) + + // Get the access tokens + await setAccessTokensToServers([ server ]) + + { + const attributes = { name: 'nsfw', nsfw: true } + await uploadVideo(server.url, server.accessToken, attributes) + } + + { + const attributes = { name: 'normal', nsfw: false } + await uploadVideo(server.url, server.accessToken, attributes) + } + + { + const res = await getCustomConfig(server.url, server.accessToken) + customConfig = res.body + } + }) + + describe('Instance default NSFW policy', function () { + it('Should display NSFW videos with display default NSFW policy', async function () { + const resConfig = await getConfig(server.url) + const serverConfig: ServerConfig = resConfig.body + expect(serverConfig.instance.defaultNSFWPolicy).to.equal('display') + + for (const res of [ await getVideosList(server.url), await searchVideo(server.url, 'n') ]) { + expect(res.body.total).to.equal(2) + + const videos = res.body.data + expect(videos).to.have.lengthOf(2) + 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' + await updateCustomConfig(server.url, server.accessToken, customConfig) + + const resConfig = await getConfig(server.url) + const serverConfig: ServerConfig = resConfig.body + expect(serverConfig.instance.defaultNSFWPolicy).to.equal('do_not_list') + + for (const res of [ await getVideosList(server.url), await searchVideo(server.url, 'n') ]) { + expect(res.body.total).to.equal(1) + + const videos = res.body.data + expect(videos).to.have.lengthOf(1) + expect(videos[ 0 ].name).to.equal('normal') + } + }) + + it('Should display NSFW videos with blur default NSFW policy', async function () { + customConfig.instance.defaultNSFWPolicy = 'blur' + await updateCustomConfig(server.url, server.accessToken, customConfig) + + const resConfig = await getConfig(server.url) + const serverConfig: ServerConfig = resConfig.body + expect(serverConfig.instance.defaultNSFWPolicy).to.equal('blur') + + for (const res of [ await getVideosList(server.url), await searchVideo(server.url, 'n') ]) { + expect(res.body.total).to.equal(2) + + const videos = res.body.data + expect(videos).to.have.lengthOf(2) + 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' + await createUser(server.url, server.accessToken, username, password) + + userAccessToken = await userLogin(server, { username, password }) + + const res = await getMyUserInformation(server.url, userAccessToken) + const user = res.body + + expect(user.nsfwPolicy).to.equal('blur') + }) + + it('Should display NSFW videos with blur user NSFW policy', async function () { + const results = [ + await getVideosListWithToken(server.url, userAccessToken), + await searchVideoWithToken(server.url, 'n', userAccessToken) + ] + + for (const res of results) { + expect(res.body.total).to.equal(2) + + const videos = res.body.data + expect(videos).to.have.lengthOf(2) + 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 () { + await updateMyUser({ + url: server.url, + accessToken: server.accessToken, + nsfwPolicy: 'display' + }) + + const results = [ + await getVideosListWithToken(server.url, server.accessToken), + await searchVideoWithToken(server.url, 'n', server.accessToken) + ] + + for (const res of results) { + expect(res.body.total).to.equal(2) + + const videos = res.body.data + expect(videos).to.have.lengthOf(2) + 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 () { + await updateMyUser({ + url: server.url, + accessToken: server.accessToken, + nsfwPolicy: 'do_not_list' + }) + + const results = [ + await getVideosListWithToken(server.url, server.accessToken), + await searchVideoWithToken(server.url, 'n', server.accessToken) + ] + for (const res of results) { + expect(res.body.total).to.equal(1) + + const videos = res.body.data + expect(videos).to.have.lengthOf(1) + 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 () { + const res = await getMyVideos(server.url, server.accessToken, 0, 5) + expect(res.body.total).to.equal(2) + + const videos = res.body.data + expect(videos).to.have.lengthOf(2) + expect(videos[ 0 ].name).to.equal('normal') + expect(videos[ 1 ].name).to.equal('nsfw') + }) + }) + + after(async function () { + killallServers([ server ]) + + // Keep the logs if the test failed + if (this['ok']) { + await flushTests() + } + }) +}) diff --git a/server/tests/utils/users/users.ts b/server/tests/utils/users/users.ts index daf731a14..fc6b26c50 100644 --- a/server/tests/utils/users/users.ts +++ b/server/tests/utils/users/users.ts @@ -3,6 +3,7 @@ import * as request from 'supertest' import { makePostBodyRequest, makeUploadRequest, makePutBodyRequest } from '../' import { UserRole } from '../../../../shared/index' +import { NSFWPolicyType } from '../../../../shared/models/videos/nsfw-policy.type' function createUser ( url: string, @@ -128,7 +129,7 @@ function updateMyUser (options: { url: string accessToken: string, newPassword?: string, - displayNSFW?: boolean, + nsfwPolicy?: NSFWPolicyType, email?: string, autoPlayVideo?: boolean description?: string @@ -137,7 +138,7 @@ function updateMyUser (options: { const toSend = {} if (options.newPassword !== undefined && options.newPassword !== null) toSend['password'] = options.newPassword - if (options.displayNSFW !== undefined && options.displayNSFW !== null) toSend['displayNSFW'] = options.displayNSFW + if (options.nsfwPolicy !== undefined && options.nsfwPolicy !== null) toSend['nsfwPolicy'] = options.nsfwPolicy if (options.autoPlayVideo !== undefined && options.autoPlayVideo !== null) toSend['autoPlayVideo'] = options.autoPlayVideo if (options.email !== undefined && options.email !== null) toSend['email'] = options.email if (options.description !== undefined && options.description !== null) toSend['description'] = options.description diff --git a/server/tests/utils/videos/videos.ts b/server/tests/utils/videos/videos.ts index 01e7fa5a1..df9071c29 100644 --- a/server/tests/utils/videos/videos.ts +++ b/server/tests/utils/videos/videos.ts @@ -128,6 +128,18 @@ function getVideosList (url: string) { .expect('Content-Type', /json/) } +function getVideosListWithToken (url: string, token: string) { + const path = '/api/v1/videos' + + return request(url) + .get(path) + .set('Authorization', 'Bearer ' + token) + .query({ sort: 'name' }) + .set('Accept', 'application/json') + .expect(200) + .expect('Content-Type', /json/) +} + function getLocalVideos (url: string) { const path = '/api/v1/videos' @@ -202,6 +214,18 @@ function searchVideo (url: string, search: string) { .expect('Content-Type', /json/) } +function searchVideoWithToken (url: string, search: string, token: string) { + const path = '/api/v1/videos' + const req = request(url) + .get(path + '/search') + .set('Authorization', 'Bearer ' + token) + .query({ search }) + .set('Accept', 'application/json') + + return req.expect(200) + .expect('Content-Type', /json/) +} + function searchVideoWithPagination (url: string, search: string, start: number, count: number, sort?: string) { const path = '/api/v1/videos' @@ -490,6 +514,7 @@ export { getVideoPrivacies, getVideoLanguages, getMyVideos, + searchVideoWithToken, getVideo, getVideoWithToken, getVideosList, @@ -499,6 +524,7 @@ export { searchVideo, searchVideoWithPagination, searchVideoWithSort, + getVideosListWithToken, uploadVideo, updateVideo, rateVideo, -- cgit v1.2.3