Cleanup tests
[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 {
6   flushTests,
7   getVideosList,
8   killallServers,
9   ServerInfo,
10   setAccessTokensToServers,
11   uploadVideo
12 } from '../../../../shared/extra-utils/index'
13 import { userLogin } from '../../../../shared/extra-utils/users/login'
14 import { createUser } from '../../../../shared/extra-utils/users/users'
15 import { getMyVideos } from '../../../../shared/extra-utils/videos/videos'
16 import {
17   getAccountVideos,
18   getConfig,
19   getCustomConfig,
20   getMyUserInformation,
21   getVideoChannelVideos,
22   getVideosListWithToken,
23   flushAndRunServer,
24   searchVideo,
25   searchVideoWithToken,
26   updateCustomConfig,
27   updateMyUser
28 } from '../../../../shared/extra-utils'
29 import { ServerConfig } from '../../../../shared/models'
30 import { CustomConfig } from '../../../../shared/models/server/custom-config.model'
31 import { User } from '../../../../shared/models/users'
32
33 const expect = chai.expect
34
35 describe('Test video NSFW policy', function () {
36   let server: ServerInfo
37   let userAccessToken: string
38   let customConfig: CustomConfig
39
40   function getVideosFunctions (token?: string, query = {}) {
41     return getMyUserInformation(server.url, server.accessToken)
42       .then(res => {
43         const user: User = res.body
44         const videoChannelName = user.videoChannels[0].name
45         const accountName = user.account.name + '@' + user.account.host
46
47         if (token) {
48           return Promise.all([
49             getVideosListWithToken(server.url, token, query),
50             searchVideoWithToken(server.url, 'n', token, query),
51             getAccountVideos(server.url, token, accountName, 0, 5, undefined, query),
52             getVideoChannelVideos(server.url, token, videoChannelName, 0, 5, undefined, query)
53           ])
54         }
55
56         return Promise.all([
57           getVideosList(server.url),
58           searchVideo(server.url, 'n'),
59           getAccountVideos(server.url, undefined, accountName, 0, 5),
60           getVideoChannelVideos(server.url, undefined, videoChannelName, 0, 5)
61         ])
62       })
63   }
64
65   before(async function () {
66     this.timeout(50000)
67     server = await flushAndRunServer(1)
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
94       for (const res of await getVideosFunctions()) {
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
112       for (const res of await getVideosFunctions()) {
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
129       for (const res of await getVideosFunctions()) {
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'
145       await createUser({ url: server.url, accessToken: server.accessToken, username: username, password: password })
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 () {
156       customConfig.instance.defaultNSFWPolicy = 'do_not_list'
157       await updateCustomConfig(server.url, server.accessToken, customConfig)
158
159       for (const res of await getVideosFunctions(userAccessToken)) {
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
176       for (const res of await getVideosFunctions(server.accessToken)) {
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
193       for (const res of await getVideosFunctions(server.accessToken)) {
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     })
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     })
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     })
242   })
243
244   after(function () {
245     killallServers([ server ])
246   })
247 })