]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/tests/api/videos/video-nsfw.ts
Fix rerunserver function
[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
94565d52
C
12} from '../../../../shared/extra-utils/index'
13import { userLogin } from '../../../../shared/extra-utils/users/login'
14import { createUser } from '../../../../shared/extra-utils/users/users'
15import { getMyVideos } from '../../../../shared/extra-utils/videos/videos'
0883b324 16import {
6b738c7a 17 getAccountVideos,
3cd0734f
C
18 getConfig,
19 getCustomConfig,
20 getMyUserInformation,
21 getVideoChannelVideos,
0883b324 22 getVideosListWithToken,
210feb6c 23 flushAndRunServer,
0883b324 24 searchVideo,
3cd0734f
C
25 searchVideoWithToken,
26 updateCustomConfig,
0883b324 27 updateMyUser
94565d52 28} from '../../../../shared/extra-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)
210feb6c 67 server = await flushAndRunServer(1)
0883b324
C
68
69 // Get the access tokens
70 await setAccessTokensToServers([ server ])
71
72 {
73 const attributes = { name: 'nsfw', nsfw: true }
74 await uploadVideo(server.url, server.accessToken, attributes)
75 }
76
77 {
78 const attributes = { name: 'normal', nsfw: false }
79 await uploadVideo(server.url, server.accessToken, attributes)
80 }
81
82 {
83 const res = await getCustomConfig(server.url, server.accessToken)
84 customConfig = res.body
85 }
86 })
87
88 describe('Instance default NSFW policy', function () {
89 it('Should display NSFW videos with display default NSFW policy', async function () {
90 const resConfig = await getConfig(server.url)
91 const serverConfig: ServerConfig = resConfig.body
92 expect(serverConfig.instance.defaultNSFWPolicy).to.equal('display')
93
6b738c7a 94 for (const res of await getVideosFunctions()) {
0883b324
C
95 expect(res.body.total).to.equal(2)
96
97 const videos = res.body.data
98 expect(videos).to.have.lengthOf(2)
99 expect(videos[ 0 ].name).to.equal('normal')
100 expect(videos[ 1 ].name).to.equal('nsfw')
101 }
102 })
103
104 it('Should not display NSFW videos with do_not_list default NSFW policy', async function () {
105 customConfig.instance.defaultNSFWPolicy = 'do_not_list'
106 await updateCustomConfig(server.url, server.accessToken, customConfig)
107
108 const resConfig = await getConfig(server.url)
109 const serverConfig: ServerConfig = resConfig.body
110 expect(serverConfig.instance.defaultNSFWPolicy).to.equal('do_not_list')
111
6b738c7a 112 for (const res of await getVideosFunctions()) {
0883b324
C
113 expect(res.body.total).to.equal(1)
114
115 const videos = res.body.data
116 expect(videos).to.have.lengthOf(1)
117 expect(videos[ 0 ].name).to.equal('normal')
118 }
119 })
120
121 it('Should display NSFW videos with blur default NSFW policy', async function () {
122 customConfig.instance.defaultNSFWPolicy = 'blur'
123 await updateCustomConfig(server.url, server.accessToken, customConfig)
124
125 const resConfig = await getConfig(server.url)
126 const serverConfig: ServerConfig = resConfig.body
127 expect(serverConfig.instance.defaultNSFWPolicy).to.equal('blur')
128
6b738c7a 129 for (const res of await getVideosFunctions()) {
0883b324
C
130 expect(res.body.total).to.equal(2)
131
132 const videos = res.body.data
133 expect(videos).to.have.lengthOf(2)
134 expect(videos[ 0 ].name).to.equal('normal')
135 expect(videos[ 1 ].name).to.equal('nsfw')
136 }
137 })
138 })
139
140 describe('User NSFW policy', function () {
141
142 it('Should create a user having the default nsfw policy', async function () {
143 const username = 'user1'
144 const password = 'my super password'
1eddc9a7 145 await createUser({ url: server.url, accessToken: server.accessToken, username: username, password: password })
0883b324
C
146
147 userAccessToken = await userLogin(server, { username, password })
148
149 const res = await getMyUserInformation(server.url, userAccessToken)
150 const user = res.body
151
152 expect(user.nsfwPolicy).to.equal('blur')
153 })
154
155 it('Should display NSFW videos with blur user NSFW policy', async function () {
eb87f9a4
C
156 customConfig.instance.defaultNSFWPolicy = 'do_not_list'
157 await updateCustomConfig(server.url, server.accessToken, customConfig)
158
6b738c7a 159 for (const res of await getVideosFunctions(userAccessToken)) {
0883b324
C
160 expect(res.body.total).to.equal(2)
161
162 const videos = res.body.data
163 expect(videos).to.have.lengthOf(2)
164 expect(videos[ 0 ].name).to.equal('normal')
165 expect(videos[ 1 ].name).to.equal('nsfw')
166 }
167 })
168
169 it('Should display NSFW videos with display user NSFW policy', async function () {
170 await updateMyUser({
171 url: server.url,
172 accessToken: server.accessToken,
173 nsfwPolicy: 'display'
174 })
175
6b738c7a 176 for (const res of await getVideosFunctions(server.accessToken)) {
0883b324
C
177 expect(res.body.total).to.equal(2)
178
179 const videos = res.body.data
180 expect(videos).to.have.lengthOf(2)
181 expect(videos[ 0 ].name).to.equal('normal')
182 expect(videos[ 1 ].name).to.equal('nsfw')
183 }
184 })
185
186 it('Should not display NSFW videos with do_not_list user NSFW policy', async function () {
187 await updateMyUser({
188 url: server.url,
189 accessToken: server.accessToken,
190 nsfwPolicy: 'do_not_list'
191 })
192
6b738c7a 193 for (const res of await getVideosFunctions(server.accessToken)) {
0883b324
C
194 expect(res.body.total).to.equal(1)
195
196 const videos = res.body.data
197 expect(videos).to.have.lengthOf(1)
198 expect(videos[ 0 ].name).to.equal('normal')
199 }
200 })
201
202 it('Should be able to see my NSFW videos even with do_not_list user NSFW policy', async function () {
203 const res = await getMyVideos(server.url, server.accessToken, 0, 5)
204 expect(res.body.total).to.equal(2)
205
206 const videos = res.body.data
207 expect(videos).to.have.lengthOf(2)
208 expect(videos[ 0 ].name).to.equal('normal')
209 expect(videos[ 1 ].name).to.equal('nsfw')
210 })
d525fc39
C
211
212 it('Should display NSFW videos when the nsfw param === true', async function () {
213 for (const res of await getVideosFunctions(server.accessToken, { nsfw: true })) {
214 expect(res.body.total).to.equal(1)
215
216 const videos = res.body.data
217 expect(videos).to.have.lengthOf(1)
218 expect(videos[ 0 ].name).to.equal('nsfw')
219 }
220 })
221
222 it('Should hide NSFW videos when the nsfw param === true', async function () {
223 for (const res of await getVideosFunctions(server.accessToken, { nsfw: false })) {
224 expect(res.body.total).to.equal(1)
225
226 const videos = res.body.data
227 expect(videos).to.have.lengthOf(1)
228 expect(videos[ 0 ].name).to.equal('normal')
229 }
230 })
0b18f4aa
C
231
232 it('Should display both videos when the nsfw param === both', async function () {
233 for (const res of await getVideosFunctions(server.accessToken, { nsfw: 'both' })) {
234 expect(res.body.total).to.equal(2)
235
236 const videos = res.body.data
237 expect(videos).to.have.lengthOf(2)
238 expect(videos[ 0 ].name).to.equal('normal')
239 expect(videos[ 1 ].name).to.equal('nsfw')
240 }
241 })
0883b324
C
242 })
243
210feb6c 244 after(function () {
0883b324 245 killallServers([ server ])
0883b324
C
246 })
247})