aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/tests/api/videos/video-nsfw.ts
diff options
context:
space:
mode:
Diffstat (limited to 'server/tests/api/videos/video-nsfw.ts')
-rw-r--r--server/tests/api/videos/video-nsfw.ts226
1 files changed, 96 insertions, 130 deletions
diff --git a/server/tests/api/videos/video-nsfw.ts b/server/tests/api/videos/video-nsfw.ts
index b16b484b9..b5d183d62 100644
--- a/server/tests/api/videos/video-nsfw.ts
+++ b/server/tests/api/videos/video-nsfw.ts
@@ -1,117 +1,96 @@
1/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */ 1/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
2 2
3import * as chai from 'chai'
4import 'mocha' 3import 'mocha'
5import { cleanupTests, getVideosList, ServerInfo, setAccessTokensToServers, uploadVideo } from '../../../../shared/extra-utils/index' 4import * as chai from 'chai'
6import { userLogin } from '../../../../shared/extra-utils/users/login' 5import { cleanupTests, createSingleServer, PeerTubeServer, setAccessTokensToServers } from '@shared/extra-utils'
7import { createUser } from '../../../../shared/extra-utils/users/users' 6import { BooleanBothQuery, CustomConfig, ResultList, Video, VideosOverview } from '@shared/models'
8import { getMyVideos } from '../../../../shared/extra-utils/videos/videos'
9import {
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'
22import { ServerConfig, VideosOverview } from '../../../../shared/models'
23import { CustomConfig } from '../../../../shared/models/server/custom-config.model'
24import { User } from '../../../../shared/models/users'
25import { getVideosOverview, getVideosOverviewWithToken } from '@shared/extra-utils/overviews/overviews'
26 7
27const expect = chai.expect 8const expect = chai.expect
28 9
29function createOverviewRes (res: any) { 10function createOverviewRes (overview: VideosOverview) {
30 const overview = res.body as VideosOverview
31
32 const videos = overview.categories[0].videos 11 const videos = overview.categories[0].videos
33 return { body: { data: videos, total: videos.length } } 12 return { data: videos, total: videos.length }
34} 13}
35 14
36describe('Test video NSFW policy', function () { 15describe('Test video NSFW policy', function () {
37 let server: ServerInfo 16 let server: PeerTubeServer
38 let userAccessToken: string 17 let userAccessToken: string
39 let customConfig: CustomConfig 18 let customConfig: CustomConfig
40 19
41 function getVideosFunctions (token?: string, query = {}) { 20 async function getVideosFunctions (token?: string, query: { nsfw?: BooleanBothQuery } = {}) {
42 return getMyUserInformation(server.url, server.accessToken) 21 const user = await server.users.getMyInfo()
43 .then(res => { 22
44 const user: User = res.body 23 const channelName = user.videoChannels[0].name
45 const videoChannelName = user.videoChannels[0].name 24 const accountName = user.account.name + '@' + user.account.host
46 const accountName = user.account.name + '@' + user.account.host 25
47 const hasQuery = Object.keys(query).length !== 0 26 const hasQuery = Object.keys(query).length !== 0
48 let promises: Promise<any>[] 27 let promises: Promise<ResultList<Video>>[]
49 28
50 if (token) { 29 if (token) {
51 promises = [ 30 promises = [
52 getVideosListWithToken(server.url, token, query), 31 server.search.advancedVideoSearch({ token, search: { search: 'n', sort: '-publishedAt', ...query } }),
53 searchVideoWithToken(server.url, 'n', token, query), 32 server.videos.listWithToken({ token, ...query }),
54 getAccountVideos(server.url, token, accountName, 0, 5, undefined, query), 33 server.videos.listByAccount({ token, handle: accountName, ...query }),
55 getVideoChannelVideos(server.url, token, videoChannelName, 0, 5, undefined, query) 34 server.videos.listByChannel({ token, handle: channelName, ...query })
56 ] 35 ]
57 36
58 // Overviews do not support video filters 37 // Overviews do not support video filters
59 if (!hasQuery) { 38 if (!hasQuery) {
60 promises.push(getVideosOverviewWithToken(server.url, 1, token).then(res => createOverviewRes(res))) 39 const p = server.overviews.getVideos({ page: 1, token })
61 } 40 .then(res => createOverviewRes(res))
62 41 promises.push(p)
63 return Promise.all(promises) 42 }
64 } 43
65 44 return Promise.all(promises)
66 promises = [ 45 }
67 getVideosList(server.url), 46
68 searchVideo(server.url, 'n'), 47 promises = [
69 getAccountVideos(server.url, undefined, accountName, 0, 5), 48 server.search.searchVideos({ search: 'n', sort: '-publishedAt' }),
70 getVideoChannelVideos(server.url, undefined, videoChannelName, 0, 5) 49 server.videos.list(),
71 ] 50 server.videos.listByAccount({ token: null, handle: accountName }),
72 51 server.videos.listByChannel({ token: null, handle: channelName })
73 // Overviews do not support video filters 52 ]
74 if (!hasQuery) { 53
75 promises.push(getVideosOverview(server.url, 1).then(res => createOverviewRes(res))) 54 // Overviews do not support video filters
76 } 55 if (!hasQuery) {
77 56 const p = server.overviews.getVideos({ page: 1 })
78 return Promise.all(promises) 57 .then(res => createOverviewRes(res))
79 }) 58 promises.push(p)
59 }
60
61 return Promise.all(promises)
80 } 62 }
81 63
82 before(async function () { 64 before(async function () {
83 this.timeout(50000) 65 this.timeout(50000)
84 server = await flushAndRunServer(1) 66 server = await createSingleServer(1)
85 67
86 // Get the access tokens 68 // Get the access tokens
87 await setAccessTokensToServers([ server ]) 69 await setAccessTokensToServers([ server ])
88 70
89 { 71 {
90 const attributes = { name: 'nsfw', nsfw: true, category: 1 } 72 const attributes = { name: 'nsfw', nsfw: true, category: 1 }
91 await uploadVideo(server.url, server.accessToken, attributes) 73 await server.videos.upload({ attributes })
92 } 74 }
93 75
94 { 76 {
95 const attributes = { name: 'normal', nsfw: false, category: 1 } 77 const attributes = { name: 'normal', nsfw: false, category: 1 }
96 await uploadVideo(server.url, server.accessToken, attributes) 78 await server.videos.upload({ attributes })
97 } 79 }
98 80
99 { 81 customConfig = await server.config.getCustomConfig()
100 const res = await getCustomConfig(server.url, server.accessToken)
101 customConfig = res.body
102 }
103 }) 82 })
104 83
105 describe('Instance default NSFW policy', function () { 84 describe('Instance default NSFW policy', function () {
85
106 it('Should display NSFW videos with display default NSFW policy', async function () { 86 it('Should display NSFW videos with display default NSFW policy', async function () {
107 const resConfig = await getConfig(server.url) 87 const serverConfig = await server.config.getConfig()
108 const serverConfig: ServerConfig = resConfig.body
109 expect(serverConfig.instance.defaultNSFWPolicy).to.equal('display') 88 expect(serverConfig.instance.defaultNSFWPolicy).to.equal('display')
110 89
111 for (const res of await getVideosFunctions()) { 90 for (const body of await getVideosFunctions()) {
112 expect(res.body.total).to.equal(2) 91 expect(body.total).to.equal(2)
113 92
114 const videos = res.body.data 93 const videos = body.data
115 expect(videos).to.have.lengthOf(2) 94 expect(videos).to.have.lengthOf(2)
116 expect(videos[0].name).to.equal('normal') 95 expect(videos[0].name).to.equal('normal')
117 expect(videos[1].name).to.equal('nsfw') 96 expect(videos[1].name).to.equal('nsfw')
@@ -120,16 +99,15 @@ describe('Test video NSFW policy', function () {
120 99
121 it('Should not display NSFW videos with do_not_list default NSFW policy', async function () { 100 it('Should not display NSFW videos with do_not_list default NSFW policy', async function () {
122 customConfig.instance.defaultNSFWPolicy = 'do_not_list' 101 customConfig.instance.defaultNSFWPolicy = 'do_not_list'
123 await updateCustomConfig(server.url, server.accessToken, customConfig) 102 await server.config.updateCustomConfig({ newCustomConfig: customConfig })
124 103
125 const resConfig = await getConfig(server.url) 104 const serverConfig = await server.config.getConfig()
126 const serverConfig: ServerConfig = resConfig.body
127 expect(serverConfig.instance.defaultNSFWPolicy).to.equal('do_not_list') 105 expect(serverConfig.instance.defaultNSFWPolicy).to.equal('do_not_list')
128 106
129 for (const res of await getVideosFunctions()) { 107 for (const body of await getVideosFunctions()) {
130 expect(res.body.total).to.equal(1) 108 expect(body.total).to.equal(1)
131 109
132 const videos = res.body.data 110 const videos = body.data
133 expect(videos).to.have.lengthOf(1) 111 expect(videos).to.have.lengthOf(1)
134 expect(videos[0].name).to.equal('normal') 112 expect(videos[0].name).to.equal('normal')
135 } 113 }
@@ -137,16 +115,15 @@ describe('Test video NSFW policy', function () {
137 115
138 it('Should display NSFW videos with blur default NSFW policy', async function () { 116 it('Should display NSFW videos with blur default NSFW policy', async function () {
139 customConfig.instance.defaultNSFWPolicy = 'blur' 117 customConfig.instance.defaultNSFWPolicy = 'blur'
140 await updateCustomConfig(server.url, server.accessToken, customConfig) 118 await server.config.updateCustomConfig({ newCustomConfig: customConfig })
141 119
142 const resConfig = await getConfig(server.url) 120 const serverConfig = await server.config.getConfig()
143 const serverConfig: ServerConfig = resConfig.body
144 expect(serverConfig.instance.defaultNSFWPolicy).to.equal('blur') 121 expect(serverConfig.instance.defaultNSFWPolicy).to.equal('blur')
145 122
146 for (const res of await getVideosFunctions()) { 123 for (const body of await getVideosFunctions()) {
147 expect(res.body.total).to.equal(2) 124 expect(body.total).to.equal(2)
148 125
149 const videos = res.body.data 126 const videos = body.data
150 expect(videos).to.have.lengthOf(2) 127 expect(videos).to.have.lengthOf(2)
151 expect(videos[0].name).to.equal('normal') 128 expect(videos[0].name).to.equal('normal')
152 expect(videos[1].name).to.equal('nsfw') 129 expect(videos[1].name).to.equal('nsfw')
@@ -159,24 +136,22 @@ describe('Test video NSFW policy', function () {
159 it('Should create a user having the default nsfw policy', async function () { 136 it('Should create a user having the default nsfw policy', async function () {
160 const username = 'user1' 137 const username = 'user1'
161 const password = 'my super password' 138 const password = 'my super password'
162 await createUser({ url: server.url, accessToken: server.accessToken, username: username, password: password }) 139 await server.users.create({ username: username, password: password })
163
164 userAccessToken = await userLogin(server, { username, password })
165 140
166 const res = await getMyUserInformation(server.url, userAccessToken) 141 userAccessToken = await server.login.getAccessToken({ username, password })
167 const user = res.body
168 142
143 const user = await server.users.getMyInfo({ token: userAccessToken })
169 expect(user.nsfwPolicy).to.equal('blur') 144 expect(user.nsfwPolicy).to.equal('blur')
170 }) 145 })
171 146
172 it('Should display NSFW videos with blur user NSFW policy', async function () { 147 it('Should display NSFW videos with blur user NSFW policy', async function () {
173 customConfig.instance.defaultNSFWPolicy = 'do_not_list' 148 customConfig.instance.defaultNSFWPolicy = 'do_not_list'
174 await updateCustomConfig(server.url, server.accessToken, customConfig) 149 await server.config.updateCustomConfig({ newCustomConfig: customConfig })
175 150
176 for (const res of await getVideosFunctions(userAccessToken)) { 151 for (const body of await getVideosFunctions(userAccessToken)) {
177 expect(res.body.total).to.equal(2) 152 expect(body.total).to.equal(2)
178 153
179 const videos = res.body.data 154 const videos = body.data
180 expect(videos).to.have.lengthOf(2) 155 expect(videos).to.have.lengthOf(2)
181 expect(videos[0].name).to.equal('normal') 156 expect(videos[0].name).to.equal('normal')
182 expect(videos[1].name).to.equal('nsfw') 157 expect(videos[1].name).to.equal('nsfw')
@@ -184,16 +159,12 @@ describe('Test video NSFW policy', function () {
184 }) 159 })
185 160
186 it('Should display NSFW videos with display user NSFW policy', async function () { 161 it('Should display NSFW videos with display user NSFW policy', async function () {
187 await updateMyUser({ 162 await server.users.updateMe({ nsfwPolicy: 'display' })
188 url: server.url,
189 accessToken: server.accessToken,
190 nsfwPolicy: 'display'
191 })
192 163
193 for (const res of await getVideosFunctions(server.accessToken)) { 164 for (const body of await getVideosFunctions(server.accessToken)) {
194 expect(res.body.total).to.equal(2) 165 expect(body.total).to.equal(2)
195 166
196 const videos = res.body.data 167 const videos = body.data
197 expect(videos).to.have.lengthOf(2) 168 expect(videos).to.have.lengthOf(2)
198 expect(videos[0].name).to.equal('normal') 169 expect(videos[0].name).to.equal('normal')
199 expect(videos[1].name).to.equal('nsfw') 170 expect(videos[1].name).to.equal('nsfw')
@@ -201,56 +172,51 @@ describe('Test video NSFW policy', function () {
201 }) 172 })
202 173
203 it('Should not display NSFW videos with do_not_list user NSFW policy', async function () { 174 it('Should not display NSFW videos with do_not_list user NSFW policy', async function () {
204 await updateMyUser({ 175 await server.users.updateMe({ nsfwPolicy: 'do_not_list' })
205 url: server.url,
206 accessToken: server.accessToken,
207 nsfwPolicy: 'do_not_list'
208 })
209 176
210 for (const res of await getVideosFunctions(server.accessToken)) { 177 for (const body of await getVideosFunctions(server.accessToken)) {
211 expect(res.body.total).to.equal(1) 178 expect(body.total).to.equal(1)
212 179
213 const videos = res.body.data 180 const videos = body.data
214 expect(videos).to.have.lengthOf(1) 181 expect(videos).to.have.lengthOf(1)
215 expect(videos[0].name).to.equal('normal') 182 expect(videos[0].name).to.equal('normal')
216 } 183 }
217 }) 184 })
218 185
219 it('Should be able to see my NSFW videos even with do_not_list user NSFW policy', async function () { 186 it('Should be able to see my NSFW videos even with do_not_list user NSFW policy', async function () {
220 const res = await getMyVideos(server.url, server.accessToken, 0, 5) 187 const { total, data } = await server.videos.listMyVideos()
221 expect(res.body.total).to.equal(2) 188 expect(total).to.equal(2)
222 189
223 const videos = res.body.data 190 expect(data).to.have.lengthOf(2)
224 expect(videos).to.have.lengthOf(2) 191 expect(data[0].name).to.equal('normal')
225 expect(videos[0].name).to.equal('normal') 192 expect(data[1].name).to.equal('nsfw')
226 expect(videos[1].name).to.equal('nsfw')
227 }) 193 })
228 194
229 it('Should display NSFW videos when the nsfw param === true', async function () { 195 it('Should display NSFW videos when the nsfw param === true', async function () {
230 for (const res of await getVideosFunctions(server.accessToken, { nsfw: true })) { 196 for (const body of await getVideosFunctions(server.accessToken, { nsfw: 'true' })) {
231 expect(res.body.total).to.equal(1) 197 expect(body.total).to.equal(1)
232 198
233 const videos = res.body.data 199 const videos = body.data
234 expect(videos).to.have.lengthOf(1) 200 expect(videos).to.have.lengthOf(1)
235 expect(videos[0].name).to.equal('nsfw') 201 expect(videos[0].name).to.equal('nsfw')
236 } 202 }
237 }) 203 })
238 204
239 it('Should hide NSFW videos when the nsfw param === true', async function () { 205 it('Should hide NSFW videos when the nsfw param === true', async function () {
240 for (const res of await getVideosFunctions(server.accessToken, { nsfw: false })) { 206 for (const body of await getVideosFunctions(server.accessToken, { nsfw: 'false' })) {
241 expect(res.body.total).to.equal(1) 207 expect(body.total).to.equal(1)
242 208
243 const videos = res.body.data 209 const videos = body.data
244 expect(videos).to.have.lengthOf(1) 210 expect(videos).to.have.lengthOf(1)
245 expect(videos[0].name).to.equal('normal') 211 expect(videos[0].name).to.equal('normal')
246 } 212 }
247 }) 213 })
248 214
249 it('Should display both videos when the nsfw param === both', async function () { 215 it('Should display both videos when the nsfw param === both', async function () {
250 for (const res of await getVideosFunctions(server.accessToken, { nsfw: 'both' })) { 216 for (const body of await getVideosFunctions(server.accessToken, { nsfw: 'both' })) {
251 expect(res.body.total).to.equal(2) 217 expect(body.total).to.equal(2)
252 218
253 const videos = res.body.data 219 const videos = body.data
254 expect(videos).to.have.lengthOf(2) 220 expect(videos).to.have.lengthOf(2)
255 expect(videos[0].name).to.equal('normal') 221 expect(videos[0].name).to.equal('normal')
256 expect(videos[1].name).to.equal('nsfw') 222 expect(videos[1].name).to.equal('nsfw')