Fix 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 { 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       for (const res of await getVideosFunctions(userAccessToken)) {
152         expect(res.body.total).to.equal(2)
153
154         const videos = res.body.data
155         expect(videos).to.have.lengthOf(2)
156         expect(videos[ 0 ].name).to.equal('normal')
157         expect(videos[ 1 ].name).to.equal('nsfw')
158       }
159     })
160
161     it('Should display NSFW videos with display user NSFW policy', async function () {
162       await updateMyUser({
163         url: server.url,
164         accessToken: server.accessToken,
165         nsfwPolicy: 'display'
166       })
167
168       for (const res of await getVideosFunctions(server.accessToken)) {
169         expect(res.body.total).to.equal(2)
170
171         const videos = res.body.data
172         expect(videos).to.have.lengthOf(2)
173         expect(videos[ 0 ].name).to.equal('normal')
174         expect(videos[ 1 ].name).to.equal('nsfw')
175       }
176     })
177
178     it('Should not display NSFW videos with do_not_list user NSFW policy', async function () {
179       await updateMyUser({
180         url: server.url,
181         accessToken: server.accessToken,
182         nsfwPolicy: 'do_not_list'
183       })
184
185       for (const res of await getVideosFunctions(server.accessToken)) {
186         expect(res.body.total).to.equal(1)
187
188         const videos = res.body.data
189         expect(videos).to.have.lengthOf(1)
190         expect(videos[ 0 ].name).to.equal('normal')
191       }
192     })
193
194     it('Should be able to see my NSFW videos even with do_not_list user NSFW policy', async function () {
195       const res = await getMyVideos(server.url, server.accessToken, 0, 5)
196       expect(res.body.total).to.equal(2)
197
198       const videos = res.body.data
199       expect(videos).to.have.lengthOf(2)
200       expect(videos[ 0 ].name).to.equal('normal')
201       expect(videos[ 1 ].name).to.equal('nsfw')
202     })
203
204     it('Should display NSFW videos when the nsfw param === true', async function () {
205       for (const res of await getVideosFunctions(server.accessToken, { nsfw: true })) {
206         expect(res.body.total).to.equal(1)
207
208         const videos = res.body.data
209         expect(videos).to.have.lengthOf(1)
210         expect(videos[ 0 ].name).to.equal('nsfw')
211       }
212     })
213
214     it('Should hide NSFW videos when the nsfw param === true', async function () {
215       for (const res of await getVideosFunctions(server.accessToken, { nsfw: false })) {
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('normal')
221       }
222     })
223
224     it('Should display both videos when the nsfw param === both', async function () {
225       for (const res of await getVideosFunctions(server.accessToken, { nsfw: 'both' })) {
226         expect(res.body.total).to.equal(2)
227
228         const videos = res.body.data
229         expect(videos).to.have.lengthOf(2)
230         expect(videos[ 0 ].name).to.equal('normal')
231         expect(videos[ 1 ].name).to.equal('nsfw')
232       }
233     })
234   })
235
236   after(async function () {
237     killallServers([ server ])
238   })
239 })