]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/tests/api/videos/video-nsfw.ts
Fix optimize old videos script
[github/Chocobozzz/PeerTube.git] / server / tests / api / videos / video-nsfw.ts
1 /* tslint:disable:no-unused-expression */
2
3 import * as chai from 'chai'
4 import 'mocha'
5 import { flushTests, getVideosList, killallServers, ServerInfo, setAccessTokensToServers, uploadVideo } from '../../utils/index'
6 import { userLogin } from '../../utils/users/login'
7 import { createUser } from '../../utils/users/users'
8 import { getMyVideos } from '../../utils/videos/videos'
9 import {
10 getAccountVideos,
11 getConfig,
12 getCustomConfig,
13 getMyUserInformation,
14 getVideoChannelVideos,
15 getVideosListWithToken,
16 runServer,
17 searchVideo,
18 searchVideoWithToken,
19 updateCustomConfig,
20 updateMyUser
21 } from '../../utils'
22 import { ServerConfig } from '../../../../shared/models'
23 import { CustomConfig } from '../../../../shared/models/server/custom-config.model'
24 import { User } from '../../../../shared/models/users'
25
26 const expect = chai.expect
27
28 describe('Test video NSFW policy', function () {
29 let server: ServerInfo
30 let userAccessToken: string
31 let customConfig: CustomConfig
32
33 function getVideosFunctions (token?: string, query = {}) {
34 return getMyUserInformation(server.url, server.accessToken)
35 .then(res => {
36 const user: User = res.body
37 const videoChannelName = user.videoChannels[0].name
38 const accountName = user.account.name + '@' + user.account.host
39
40 if (token) {
41 return Promise.all([
42 getVideosListWithToken(server.url, token, query),
43 searchVideoWithToken(server.url, 'n', token, query),
44 getAccountVideos(server.url, token, accountName, 0, 5, undefined, query),
45 getVideoChannelVideos(server.url, token, videoChannelName, 0, 5, undefined, query)
46 ])
47 }
48
49 return Promise.all([
50 getVideosList(server.url),
51 searchVideo(server.url, 'n'),
52 getAccountVideos(server.url, undefined, accountName, 0, 5),
53 getVideoChannelVideos(server.url, undefined, videoChannelName, 0, 5)
54 ])
55 })
56 }
57
58 before(async function () {
59 this.timeout(50000)
60
61 await flushTests()
62 server = await runServer(1)
63
64 // Get the access tokens
65 await setAccessTokensToServers([ server ])
66
67 {
68 const attributes = { name: 'nsfw', nsfw: true }
69 await uploadVideo(server.url, server.accessToken, attributes)
70 }
71
72 {
73 const attributes = { name: 'normal', nsfw: false }
74 await uploadVideo(server.url, server.accessToken, attributes)
75 }
76
77 {
78 const res = await getCustomConfig(server.url, server.accessToken)
79 customConfig = res.body
80 }
81 })
82
83 describe('Instance default NSFW policy', function () {
84 it('Should display NSFW videos with display default NSFW policy', async function () {
85 const resConfig = await getConfig(server.url)
86 const serverConfig: ServerConfig = resConfig.body
87 expect(serverConfig.instance.defaultNSFWPolicy).to.equal('display')
88
89 for (const res of await getVideosFunctions()) {
90 expect(res.body.total).to.equal(2)
91
92 const videos = res.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')
96 }
97 })
98
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 updateCustomConfig(server.url, server.accessToken, customConfig)
102
103 const resConfig = await getConfig(server.url)
104 const serverConfig: ServerConfig = resConfig.body
105 expect(serverConfig.instance.defaultNSFWPolicy).to.equal('do_not_list')
106
107 for (const res of await getVideosFunctions()) {
108 expect(res.body.total).to.equal(1)
109
110 const videos = res.body.data
111 expect(videos).to.have.lengthOf(1)
112 expect(videos[ 0 ].name).to.equal('normal')
113 }
114 })
115
116 it('Should display NSFW videos with blur default NSFW policy', async function () {
117 customConfig.instance.defaultNSFWPolicy = 'blur'
118 await updateCustomConfig(server.url, server.accessToken, customConfig)
119
120 const resConfig = await getConfig(server.url)
121 const serverConfig: ServerConfig = resConfig.body
122 expect(serverConfig.instance.defaultNSFWPolicy).to.equal('blur')
123
124 for (const res of await getVideosFunctions()) {
125 expect(res.body.total).to.equal(2)
126
127 const videos = res.body.data
128 expect(videos).to.have.lengthOf(2)
129 expect(videos[ 0 ].name).to.equal('normal')
130 expect(videos[ 1 ].name).to.equal('nsfw')
131 }
132 })
133 })
134
135 describe('User NSFW policy', function () {
136
137 it('Should create a user having the default nsfw policy', async function () {
138 const username = 'user1'
139 const password = 'my super password'
140 await createUser(server.url, server.accessToken, username, password)
141
142 userAccessToken = await userLogin(server, { username, password })
143
144 const res = await getMyUserInformation(server.url, userAccessToken)
145 const user = res.body
146
147 expect(user.nsfwPolicy).to.equal('blur')
148 })
149
150 it('Should display NSFW videos with blur user NSFW policy', async function () {
151 customConfig.instance.defaultNSFWPolicy = 'do_not_list'
152 await updateCustomConfig(server.url, server.accessToken, customConfig)
153
154 for (const res of await getVideosFunctions(userAccessToken)) {
155 expect(res.body.total).to.equal(2)
156
157 const videos = res.body.data
158 expect(videos).to.have.lengthOf(2)
159 expect(videos[ 0 ].name).to.equal('normal')
160 expect(videos[ 1 ].name).to.equal('nsfw')
161 }
162 })
163
164 it('Should display NSFW videos with display user NSFW policy', async function () {
165 await updateMyUser({
166 url: server.url,
167 accessToken: server.accessToken,
168 nsfwPolicy: 'display'
169 })
170
171 for (const res of await getVideosFunctions(server.accessToken)) {
172 expect(res.body.total).to.equal(2)
173
174 const videos = res.body.data
175 expect(videos).to.have.lengthOf(2)
176 expect(videos[ 0 ].name).to.equal('normal')
177 expect(videos[ 1 ].name).to.equal('nsfw')
178 }
179 })
180
181 it('Should not display NSFW videos with do_not_list user NSFW policy', async function () {
182 await updateMyUser({
183 url: server.url,
184 accessToken: server.accessToken,
185 nsfwPolicy: 'do_not_list'
186 })
187
188 for (const res of await getVideosFunctions(server.accessToken)) {
189 expect(res.body.total).to.equal(1)
190
191 const videos = res.body.data
192 expect(videos).to.have.lengthOf(1)
193 expect(videos[ 0 ].name).to.equal('normal')
194 }
195 })
196
197 it('Should be able to see my NSFW videos even with do_not_list user NSFW policy', async function () {
198 const res = await getMyVideos(server.url, server.accessToken, 0, 5)
199 expect(res.body.total).to.equal(2)
200
201 const videos = res.body.data
202 expect(videos).to.have.lengthOf(2)
203 expect(videos[ 0 ].name).to.equal('normal')
204 expect(videos[ 1 ].name).to.equal('nsfw')
205 })
206
207 it('Should display NSFW videos when the nsfw param === true', async function () {
208 for (const res of await getVideosFunctions(server.accessToken, { nsfw: true })) {
209 expect(res.body.total).to.equal(1)
210
211 const videos = res.body.data
212 expect(videos).to.have.lengthOf(1)
213 expect(videos[ 0 ].name).to.equal('nsfw')
214 }
215 })
216
217 it('Should hide NSFW videos when the nsfw param === true', async function () {
218 for (const res of await getVideosFunctions(server.accessToken, { nsfw: false })) {
219 expect(res.body.total).to.equal(1)
220
221 const videos = res.body.data
222 expect(videos).to.have.lengthOf(1)
223 expect(videos[ 0 ].name).to.equal('normal')
224 }
225 })
226
227 it('Should display both videos when the nsfw param === both', async function () {
228 for (const res of await getVideosFunctions(server.accessToken, { nsfw: 'both' })) {
229 expect(res.body.total).to.equal(2)
230
231 const videos = res.body.data
232 expect(videos).to.have.lengthOf(2)
233 expect(videos[ 0 ].name).to.equal('normal')
234 expect(videos[ 1 ].name).to.equal('nsfw')
235 }
236 })
237 })
238
239 after(async function () {
240 killallServers([ server ])
241 })
242 })