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