Merge branch 'develop' of framagit.org:chocobozzz/PeerTube into develop
[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   getConfig, getCustomConfig,
11   getMyUserInformation,
12   getVideosListWithToken,
13   runServer,
14   searchVideo,
15   searchVideoWithToken, updateCustomConfig,
16   updateMyUser
17 } from '../../utils'
18 import { ServerConfig } from '../../../../shared/models'
19 import { CustomConfig } from '../../../../shared/models/server/custom-config.model'
20
21 const expect = chai.expect
22
23 describe('Test video NSFW policy', function () {
24   let server: ServerInfo
25   let userAccessToken: string
26   let customConfig: CustomConfig
27
28   before(async function () {
29     this.timeout(50000)
30
31     await flushTests()
32     server = await runServer(1)
33
34     // Get the access tokens
35     await setAccessTokensToServers([ server ])
36
37     {
38       const attributes = { name: 'nsfw', nsfw: true }
39       await uploadVideo(server.url, server.accessToken, attributes)
40     }
41
42     {
43       const attributes = { name: 'normal', nsfw: false }
44       await uploadVideo(server.url, server.accessToken, attributes)
45     }
46
47     {
48       const res = await getCustomConfig(server.url, server.accessToken)
49       customConfig = res.body
50     }
51   })
52
53   describe('Instance default NSFW policy', function () {
54     it('Should display NSFW videos with display default NSFW policy', async function () {
55       const resConfig = await getConfig(server.url)
56       const serverConfig: ServerConfig = resConfig.body
57       expect(serverConfig.instance.defaultNSFWPolicy).to.equal('display')
58
59       for (const res of [ await getVideosList(server.url), await searchVideo(server.url, 'n') ]) {
60         expect(res.body.total).to.equal(2)
61
62         const videos = res.body.data
63         expect(videos).to.have.lengthOf(2)
64         expect(videos[ 0 ].name).to.equal('normal')
65         expect(videos[ 1 ].name).to.equal('nsfw')
66       }
67     })
68
69     it('Should not display NSFW videos with do_not_list default NSFW policy', async function () {
70       customConfig.instance.defaultNSFWPolicy = 'do_not_list'
71       await updateCustomConfig(server.url, server.accessToken, customConfig)
72
73       const resConfig = await getConfig(server.url)
74       const serverConfig: ServerConfig = resConfig.body
75       expect(serverConfig.instance.defaultNSFWPolicy).to.equal('do_not_list')
76
77       for (const res of [ await getVideosList(server.url), await searchVideo(server.url, 'n') ]) {
78         expect(res.body.total).to.equal(1)
79
80         const videos = res.body.data
81         expect(videos).to.have.lengthOf(1)
82         expect(videos[ 0 ].name).to.equal('normal')
83       }
84     })
85
86     it('Should display NSFW videos with blur default NSFW policy', async function () {
87       customConfig.instance.defaultNSFWPolicy = 'blur'
88       await updateCustomConfig(server.url, server.accessToken, customConfig)
89
90       const resConfig = await getConfig(server.url)
91       const serverConfig: ServerConfig = resConfig.body
92       expect(serverConfig.instance.defaultNSFWPolicy).to.equal('blur')
93
94       for (const res of [ await getVideosList(server.url), await searchVideo(server.url, 'n') ]) {
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
105   describe('User NSFW policy', function () {
106
107     it('Should create a user having the default nsfw policy', async function () {
108       const username = 'user1'
109       const password = 'my super password'
110       await createUser(server.url, server.accessToken, username, password)
111
112       userAccessToken = await userLogin(server, { username, password })
113
114       const res = await getMyUserInformation(server.url, userAccessToken)
115       const user = res.body
116
117       expect(user.nsfwPolicy).to.equal('blur')
118     })
119
120     it('Should display NSFW videos with blur user NSFW policy', async function () {
121       const results = [
122         await getVideosListWithToken(server.url, userAccessToken),
123         await searchVideoWithToken(server.url, 'n', userAccessToken)
124       ]
125
126       for (const res of results) {
127         expect(res.body.total).to.equal(2)
128
129         const videos = res.body.data
130         expect(videos).to.have.lengthOf(2)
131         expect(videos[ 0 ].name).to.equal('normal')
132         expect(videos[ 1 ].name).to.equal('nsfw')
133       }
134     })
135
136     it('Should display NSFW videos with display user NSFW policy', async function () {
137       await updateMyUser({
138         url: server.url,
139         accessToken: server.accessToken,
140         nsfwPolicy: 'display'
141       })
142
143       const results = [
144         await getVideosListWithToken(server.url, server.accessToken),
145         await searchVideoWithToken(server.url, 'n', server.accessToken)
146       ]
147
148       for (const res of results) {
149         expect(res.body.total).to.equal(2)
150
151         const videos = res.body.data
152         expect(videos).to.have.lengthOf(2)
153         expect(videos[ 0 ].name).to.equal('normal')
154         expect(videos[ 1 ].name).to.equal('nsfw')
155       }
156     })
157
158     it('Should not display NSFW videos with do_not_list user NSFW policy', async function () {
159       await updateMyUser({
160         url: server.url,
161         accessToken: server.accessToken,
162         nsfwPolicy: 'do_not_list'
163       })
164
165       const results = [
166         await getVideosListWithToken(server.url, server.accessToken),
167         await searchVideoWithToken(server.url, 'n', server.accessToken)
168       ]
169       for (const res of results) {
170         expect(res.body.total).to.equal(1)
171
172         const videos = res.body.data
173         expect(videos).to.have.lengthOf(1)
174         expect(videos[ 0 ].name).to.equal('normal')
175       }
176     })
177
178     it('Should be able to see my NSFW videos even with do_not_list user NSFW policy', async function () {
179       const res = await getMyVideos(server.url, server.accessToken, 0, 5)
180       expect(res.body.total).to.equal(2)
181
182       const videos = res.body.data
183       expect(videos).to.have.lengthOf(2)
184       expect(videos[ 0 ].name).to.equal('normal')
185       expect(videos[ 1 ].name).to.equal('nsfw')
186     })
187   })
188
189   after(async function () {
190     killallServers([ server ])
191
192     // Keep the logs if the test failed
193     if (this['ok']) {
194       await flushTests()
195     }
196   })
197 })