]>
Commit | Line | Data |
---|---|---|
a1587156 | 1 | /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */ |
0883b324 | 2 | |
0883b324 | 3 | import 'mocha' |
23a3a882 | 4 | import * as chai from 'chai' |
254d3579 | 5 | import { cleanupTests, createSingleServer, PeerTubeServer, setAccessTokensToServers } from '@shared/extra-utils' |
7926c5f9 | 6 | import { BooleanBothQuery, CustomConfig, ResultList, Video, VideosOverview } from '@shared/models' |
0883b324 C |
7 | |
8 | const expect = chai.expect | |
9 | ||
23a3a882 | 10 | function createOverviewRes (overview: VideosOverview) { |
764a9657 | 11 | const videos = overview.categories[0].videos |
dd0ebb71 | 12 | return { data: videos, total: videos.length } |
764a9657 C |
13 | } |
14 | ||
0883b324 | 15 | describe('Test video NSFW policy', function () { |
254d3579 | 16 | let server: PeerTubeServer |
0883b324 C |
17 | let userAccessToken: string |
18 | let customConfig: CustomConfig | |
19 | ||
dd0ebb71 | 20 | async function getVideosFunctions (token?: string, query: { nsfw?: BooleanBothQuery } = {}) { |
89d241a7 | 21 | const user = await server.users.getMyInfo() |
c0e8b12e C |
22 | |
23 | const channelName = user.videoChannels[0].name | |
dd0ebb71 | 24 | const accountName = user.account.name + '@' + user.account.host |
c0e8b12e | 25 | |
dd0ebb71 C |
26 | const hasQuery = Object.keys(query).length !== 0 |
27 | let promises: Promise<ResultList<Video>>[] | |
28 | ||
29 | if (token) { | |
30 | promises = [ | |
89d241a7 C |
31 | server.search.advancedVideoSearch({ token, search: { search: 'n', sort: '-publishedAt', ...query } }), |
32 | server.videos.listWithToken({ token, ...query }), | |
4c7e60bc C |
33 | server.videos.listByAccount({ token, handle: accountName, ...query }), |
34 | server.videos.listByChannel({ token, handle: channelName, ...query }) | |
dd0ebb71 C |
35 | ] |
36 | ||
37 | // Overviews do not support video filters | |
38 | if (!hasQuery) { | |
89d241a7 | 39 | const p = server.overviews.getVideos({ page: 1, token }) |
dd0ebb71 C |
40 | .then(res => createOverviewRes(res)) |
41 | promises.push(p) | |
42 | } | |
43 | ||
44 | return Promise.all(promises) | |
45 | } | |
46 | ||
47 | promises = [ | |
89d241a7 C |
48 | server.search.searchVideos({ search: 'n', sort: '-publishedAt' }), |
49 | server.videos.list(), | |
4c7e60bc C |
50 | server.videos.listByAccount({ token: null, handle: accountName }), |
51 | server.videos.listByChannel({ token: null, handle: channelName }) | |
dd0ebb71 C |
52 | ] |
53 | ||
54 | // Overviews do not support video filters | |
55 | if (!hasQuery) { | |
89d241a7 | 56 | const p = server.overviews.getVideos({ page: 1 }) |
dd0ebb71 C |
57 | .then(res => createOverviewRes(res)) |
58 | promises.push(p) | |
59 | } | |
60 | ||
61 | return Promise.all(promises) | |
6b738c7a C |
62 | } |
63 | ||
0883b324 C |
64 | before(async function () { |
65 | this.timeout(50000) | |
254d3579 | 66 | server = await createSingleServer(1) |
0883b324 C |
67 | |
68 | // Get the access tokens | |
69 | await setAccessTokensToServers([ server ]) | |
70 | ||
71 | { | |
764a9657 | 72 | const attributes = { name: 'nsfw', nsfw: true, category: 1 } |
89d241a7 | 73 | await server.videos.upload({ attributes }) |
0883b324 C |
74 | } |
75 | ||
76 | { | |
764a9657 | 77 | const attributes = { name: 'normal', nsfw: false, category: 1 } |
89d241a7 | 78 | await server.videos.upload({ attributes }) |
0883b324 C |
79 | } |
80 | ||
89d241a7 | 81 | customConfig = await server.config.getCustomConfig() |
0883b324 C |
82 | }) |
83 | ||
84 | describe('Instance default NSFW policy', function () { | |
4c7e60bc | 85 | |
0883b324 | 86 | it('Should display NSFW videos with display default NSFW policy', async function () { |
89d241a7 | 87 | const serverConfig = await server.config.getConfig() |
0883b324 C |
88 | expect(serverConfig.instance.defaultNSFWPolicy).to.equal('display') |
89 | ||
dd0ebb71 C |
90 | for (const body of await getVideosFunctions()) { |
91 | expect(body.total).to.equal(2) | |
0883b324 | 92 | |
dd0ebb71 | 93 | const videos = body.data |
0883b324 | 94 | expect(videos).to.have.lengthOf(2) |
a1587156 C |
95 | expect(videos[0].name).to.equal('normal') |
96 | expect(videos[1].name).to.equal('nsfw') | |
0883b324 C |
97 | } |
98 | }) | |
99 | ||
100 | it('Should not display NSFW videos with do_not_list default NSFW policy', async function () { | |
101 | customConfig.instance.defaultNSFWPolicy = 'do_not_list' | |
89d241a7 | 102 | await server.config.updateCustomConfig({ newCustomConfig: customConfig }) |
0883b324 | 103 | |
89d241a7 | 104 | const serverConfig = await server.config.getConfig() |
0883b324 C |
105 | expect(serverConfig.instance.defaultNSFWPolicy).to.equal('do_not_list') |
106 | ||
dd0ebb71 C |
107 | for (const body of await getVideosFunctions()) { |
108 | expect(body.total).to.equal(1) | |
0883b324 | 109 | |
dd0ebb71 | 110 | const videos = body.data |
0883b324 | 111 | expect(videos).to.have.lengthOf(1) |
a1587156 | 112 | expect(videos[0].name).to.equal('normal') |
0883b324 C |
113 | } |
114 | }) | |
115 | ||
116 | it('Should display NSFW videos with blur default NSFW policy', async function () { | |
117 | customConfig.instance.defaultNSFWPolicy = 'blur' | |
89d241a7 | 118 | await server.config.updateCustomConfig({ newCustomConfig: customConfig }) |
0883b324 | 119 | |
89d241a7 | 120 | const serverConfig = await server.config.getConfig() |
0883b324 C |
121 | expect(serverConfig.instance.defaultNSFWPolicy).to.equal('blur') |
122 | ||
dd0ebb71 C |
123 | for (const body of await getVideosFunctions()) { |
124 | expect(body.total).to.equal(2) | |
0883b324 | 125 | |
dd0ebb71 | 126 | const videos = body.data |
0883b324 | 127 | expect(videos).to.have.lengthOf(2) |
a1587156 C |
128 | expect(videos[0].name).to.equal('normal') |
129 | expect(videos[1].name).to.equal('nsfw') | |
0883b324 C |
130 | } |
131 | }) | |
132 | }) | |
133 | ||
134 | describe('User NSFW policy', function () { | |
135 | ||
136 | it('Should create a user having the default nsfw policy', async function () { | |
137 | const username = 'user1' | |
138 | const password = 'my super password' | |
89d241a7 | 139 | await server.users.create({ username: username, password: password }) |
0883b324 | 140 | |
89d241a7 | 141 | userAccessToken = await server.login.getAccessToken({ username, password }) |
0883b324 | 142 | |
89d241a7 | 143 | const user = await server.users.getMyInfo({ token: userAccessToken }) |
0883b324 C |
144 | expect(user.nsfwPolicy).to.equal('blur') |
145 | }) | |
146 | ||
147 | it('Should display NSFW videos with blur user NSFW policy', async function () { | |
eb87f9a4 | 148 | customConfig.instance.defaultNSFWPolicy = 'do_not_list' |
89d241a7 | 149 | await server.config.updateCustomConfig({ newCustomConfig: customConfig }) |
eb87f9a4 | 150 | |
dd0ebb71 C |
151 | for (const body of await getVideosFunctions(userAccessToken)) { |
152 | expect(body.total).to.equal(2) | |
0883b324 | 153 | |
dd0ebb71 | 154 | const videos = body.data |
0883b324 | 155 | expect(videos).to.have.lengthOf(2) |
a1587156 C |
156 | expect(videos[0].name).to.equal('normal') |
157 | expect(videos[1].name).to.equal('nsfw') | |
0883b324 C |
158 | } |
159 | }) | |
160 | ||
161 | it('Should display NSFW videos with display user NSFW policy', async function () { | |
89d241a7 | 162 | await server.users.updateMe({ nsfwPolicy: 'display' }) |
0883b324 | 163 | |
dd0ebb71 C |
164 | for (const body of await getVideosFunctions(server.accessToken)) { |
165 | expect(body.total).to.equal(2) | |
0883b324 | 166 | |
dd0ebb71 | 167 | const videos = body.data |
0883b324 | 168 | expect(videos).to.have.lengthOf(2) |
a1587156 C |
169 | expect(videos[0].name).to.equal('normal') |
170 | expect(videos[1].name).to.equal('nsfw') | |
0883b324 C |
171 | } |
172 | }) | |
173 | ||
174 | it('Should not display NSFW videos with do_not_list user NSFW policy', async function () { | |
89d241a7 | 175 | await server.users.updateMe({ nsfwPolicy: 'do_not_list' }) |
0883b324 | 176 | |
dd0ebb71 C |
177 | for (const body of await getVideosFunctions(server.accessToken)) { |
178 | expect(body.total).to.equal(1) | |
0883b324 | 179 | |
dd0ebb71 | 180 | const videos = body.data |
0883b324 | 181 | expect(videos).to.have.lengthOf(1) |
a1587156 | 182 | expect(videos[0].name).to.equal('normal') |
0883b324 C |
183 | } |
184 | }) | |
185 | ||
186 | it('Should be able to see my NSFW videos even with do_not_list user NSFW policy', async function () { | |
89d241a7 | 187 | const { total, data } = await server.videos.listMyVideos() |
d23dd9fb | 188 | expect(total).to.equal(2) |
0883b324 | 189 | |
d23dd9fb C |
190 | expect(data).to.have.lengthOf(2) |
191 | expect(data[0].name).to.equal('normal') | |
192 | expect(data[1].name).to.equal('nsfw') | |
0883b324 | 193 | }) |
d525fc39 C |
194 | |
195 | it('Should display NSFW videos when the nsfw param === true', async function () { | |
dd0ebb71 C |
196 | for (const body of await getVideosFunctions(server.accessToken, { nsfw: 'true' })) { |
197 | expect(body.total).to.equal(1) | |
d525fc39 | 198 | |
dd0ebb71 | 199 | const videos = body.data |
d525fc39 | 200 | expect(videos).to.have.lengthOf(1) |
a1587156 | 201 | expect(videos[0].name).to.equal('nsfw') |
d525fc39 C |
202 | } |
203 | }) | |
204 | ||
205 | it('Should hide NSFW videos when the nsfw param === true', async function () { | |
dd0ebb71 C |
206 | for (const body of await getVideosFunctions(server.accessToken, { nsfw: 'false' })) { |
207 | expect(body.total).to.equal(1) | |
d525fc39 | 208 | |
dd0ebb71 | 209 | const videos = body.data |
d525fc39 | 210 | expect(videos).to.have.lengthOf(1) |
a1587156 | 211 | expect(videos[0].name).to.equal('normal') |
d525fc39 C |
212 | } |
213 | }) | |
0b18f4aa C |
214 | |
215 | it('Should display both videos when the nsfw param === both', async function () { | |
dd0ebb71 C |
216 | for (const body of await getVideosFunctions(server.accessToken, { nsfw: 'both' })) { |
217 | expect(body.total).to.equal(2) | |
0b18f4aa | 218 | |
dd0ebb71 | 219 | const videos = body.data |
0b18f4aa | 220 | expect(videos).to.have.lengthOf(2) |
a1587156 C |
221 | expect(videos[0].name).to.equal('normal') |
222 | expect(videos[1].name).to.equal('nsfw') | |
0b18f4aa C |
223 | } |
224 | }) | |
0883b324 C |
225 | }) |
226 | ||
7c3b7976 C |
227 | after(async function () { |
228 | await cleanupTests([ server ]) | |
0883b324 C |
229 | }) |
230 | }) |