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