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