diff options
author | Chocobozzz <me@florianbigard.com> | 2023-08-17 08:59:21 +0200 |
---|---|---|
committer | Chocobozzz <me@florianbigard.com> | 2023-08-17 08:59:21 +0200 |
commit | c380e3928517eb5311b38cf257816642617d7a33 (patch) | |
tree | 2ea9b70ebca16b5d109bcce98fe7f944dad89319 /packages/tests/src/api/videos/video-nsfw.ts | |
parent | a8ca6190fb462bf6eb5685cfc1d8ae444164a487 (diff) | |
parent | 3a4992633ee62d5edfbb484d9c6bcb3cf158489d (diff) | |
download | PeerTube-c380e3928517eb5311b38cf257816642617d7a33.tar.gz PeerTube-c380e3928517eb5311b38cf257816642617d7a33.tar.zst PeerTube-c380e3928517eb5311b38cf257816642617d7a33.zip |
Merge branch 'feature/esm-and-nx' into develop
Diffstat (limited to 'packages/tests/src/api/videos/video-nsfw.ts')
-rw-r--r-- | packages/tests/src/api/videos/video-nsfw.ts | 227 |
1 files changed, 227 insertions, 0 deletions
diff --git a/packages/tests/src/api/videos/video-nsfw.ts b/packages/tests/src/api/videos/video-nsfw.ts new file mode 100644 index 000000000..fc5225dd2 --- /dev/null +++ b/packages/tests/src/api/videos/video-nsfw.ts | |||
@@ -0,0 +1,227 @@ | |||
1 | /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */ | ||
2 | |||
3 | import { expect } from 'chai' | ||
4 | import { cleanupTests, createSingleServer, PeerTubeServer, setAccessTokensToServers } from '@peertube/peertube-server-commands' | ||
5 | import { BooleanBothQuery, CustomConfig, ResultList, Video, VideosOverview } from '@peertube/peertube-models' | ||
6 | |||
7 | function createOverviewRes (overview: VideosOverview) { | ||
8 | const videos = overview.categories[0].videos | ||
9 | return { data: videos, total: videos.length } | ||
10 | } | ||
11 | |||
12 | describe('Test video NSFW policy', function () { | ||
13 | let server: PeerTubeServer | ||
14 | let userAccessToken: string | ||
15 | let customConfig: CustomConfig | ||
16 | |||
17 | async function getVideosFunctions (token?: string, query: { nsfw?: BooleanBothQuery } = {}) { | ||
18 | const user = await server.users.getMyInfo() | ||
19 | |||
20 | const channelName = user.videoChannels[0].name | ||
21 | const accountName = user.account.name + '@' + user.account.host | ||
22 | |||
23 | const hasQuery = Object.keys(query).length !== 0 | ||
24 | let promises: Promise<ResultList<Video>>[] | ||
25 | |||
26 | if (token) { | ||
27 | promises = [ | ||
28 | server.search.advancedVideoSearch({ token, search: { search: 'n', sort: '-publishedAt', ...query } }), | ||
29 | server.videos.listWithToken({ token, ...query }), | ||
30 | server.videos.listByAccount({ token, handle: accountName, ...query }), | ||
31 | server.videos.listByChannel({ token, handle: channelName, ...query }) | ||
32 | ] | ||
33 | |||
34 | // Overviews do not support video filters | ||
35 | if (!hasQuery) { | ||
36 | const p = server.overviews.getVideos({ page: 1, token }) | ||
37 | .then(res => createOverviewRes(res)) | ||
38 | promises.push(p) | ||
39 | } | ||
40 | |||
41 | return Promise.all(promises) | ||
42 | } | ||
43 | |||
44 | promises = [ | ||
45 | server.search.searchVideos({ search: 'n', sort: '-publishedAt' }), | ||
46 | server.videos.list(), | ||
47 | server.videos.listByAccount({ token: null, handle: accountName }), | ||
48 | server.videos.listByChannel({ token: null, handle: channelName }) | ||
49 | ] | ||
50 | |||
51 | // Overviews do not support video filters | ||
52 | if (!hasQuery) { | ||
53 | const p = server.overviews.getVideos({ page: 1 }) | ||
54 | .then(res => createOverviewRes(res)) | ||
55 | promises.push(p) | ||
56 | } | ||
57 | |||
58 | return Promise.all(promises) | ||
59 | } | ||
60 | |||
61 | before(async function () { | ||
62 | this.timeout(50000) | ||
63 | server = await createSingleServer(1) | ||
64 | |||
65 | // Get the access tokens | ||
66 | await setAccessTokensToServers([ server ]) | ||
67 | |||
68 | { | ||
69 | const attributes = { name: 'nsfw', nsfw: true, category: 1 } | ||
70 | await server.videos.upload({ attributes }) | ||
71 | } | ||
72 | |||
73 | { | ||
74 | const attributes = { name: 'normal', nsfw: false, category: 1 } | ||
75 | await server.videos.upload({ attributes }) | ||
76 | } | ||
77 | |||
78 | customConfig = await server.config.getCustomConfig() | ||
79 | }) | ||
80 | |||
81 | describe('Instance default NSFW policy', function () { | ||
82 | |||
83 | it('Should display NSFW videos with display default NSFW policy', async function () { | ||
84 | const serverConfig = await server.config.getConfig() | ||
85 | expect(serverConfig.instance.defaultNSFWPolicy).to.equal('display') | ||
86 | |||
87 | for (const body of await getVideosFunctions()) { | ||
88 | expect(body.total).to.equal(2) | ||
89 | |||
90 | const videos = body.data | ||
91 | expect(videos).to.have.lengthOf(2) | ||
92 | expect(videos[0].name).to.equal('normal') | ||
93 | expect(videos[1].name).to.equal('nsfw') | ||
94 | } | ||
95 | }) | ||
96 | |||
97 | it('Should not display NSFW videos with do_not_list default NSFW policy', async function () { | ||
98 | customConfig.instance.defaultNSFWPolicy = 'do_not_list' | ||
99 | await server.config.updateCustomConfig({ newCustomConfig: customConfig }) | ||
100 | |||
101 | const serverConfig = await server.config.getConfig() | ||
102 | expect(serverConfig.instance.defaultNSFWPolicy).to.equal('do_not_list') | ||
103 | |||
104 | for (const body of await getVideosFunctions()) { | ||
105 | expect(body.total).to.equal(1) | ||
106 | |||
107 | const videos = 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 server.config.updateCustomConfig({ newCustomConfig: customConfig }) | ||
116 | |||
117 | const serverConfig = await server.config.getConfig() | ||
118 | expect(serverConfig.instance.defaultNSFWPolicy).to.equal('blur') | ||
119 | |||
120 | for (const body of await getVideosFunctions()) { | ||
121 | expect(body.total).to.equal(2) | ||
122 | |||
123 | const videos = body.data | ||
124 | expect(videos).to.have.lengthOf(2) | ||
125 | expect(videos[0].name).to.equal('normal') | ||
126 | expect(videos[1].name).to.equal('nsfw') | ||
127 | } | ||
128 | }) | ||
129 | }) | ||
130 | |||
131 | describe('User NSFW policy', function () { | ||
132 | |||
133 | it('Should create a user having the default nsfw policy', async function () { | ||
134 | const username = 'user1' | ||
135 | const password = 'my super password' | ||
136 | await server.users.create({ username, password }) | ||
137 | |||
138 | userAccessToken = await server.login.getAccessToken({ username, password }) | ||
139 | |||
140 | const user = await server.users.getMyInfo({ token: userAccessToken }) | ||
141 | expect(user.nsfwPolicy).to.equal('blur') | ||
142 | }) | ||
143 | |||
144 | it('Should display NSFW videos with blur user NSFW policy', async function () { | ||
145 | customConfig.instance.defaultNSFWPolicy = 'do_not_list' | ||
146 | await server.config.updateCustomConfig({ newCustomConfig: customConfig }) | ||
147 | |||
148 | for (const body of await getVideosFunctions(userAccessToken)) { | ||
149 | expect(body.total).to.equal(2) | ||
150 | |||
151 | const videos = 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 server.users.updateMe({ nsfwPolicy: 'display' }) | ||
160 | |||
161 | for (const body of await getVideosFunctions(server.accessToken)) { | ||
162 | expect(body.total).to.equal(2) | ||
163 | |||
164 | const videos = body.data | ||
165 | expect(videos).to.have.lengthOf(2) | ||
166 | expect(videos[0].name).to.equal('normal') | ||
167 | expect(videos[1].name).to.equal('nsfw') | ||
168 | } | ||
169 | }) | ||
170 | |||
171 | it('Should not display NSFW videos with do_not_list user NSFW policy', async function () { | ||
172 | await server.users.updateMe({ nsfwPolicy: 'do_not_list' }) | ||
173 | |||
174 | for (const body of await getVideosFunctions(server.accessToken)) { | ||
175 | expect(body.total).to.equal(1) | ||
176 | |||
177 | const videos = body.data | ||
178 | expect(videos).to.have.lengthOf(1) | ||
179 | expect(videos[0].name).to.equal('normal') | ||
180 | } | ||
181 | }) | ||
182 | |||
183 | it('Should be able to see my NSFW videos even with do_not_list user NSFW policy', async function () { | ||
184 | const { total, data } = await server.videos.listMyVideos() | ||
185 | expect(total).to.equal(2) | ||
186 | |||
187 | expect(data).to.have.lengthOf(2) | ||
188 | expect(data[0].name).to.equal('normal') | ||
189 | expect(data[1].name).to.equal('nsfw') | ||
190 | }) | ||
191 | |||
192 | it('Should display NSFW videos when the nsfw param === true', async function () { | ||
193 | for (const body of await getVideosFunctions(server.accessToken, { nsfw: 'true' })) { | ||
194 | expect(body.total).to.equal(1) | ||
195 | |||
196 | const videos = body.data | ||
197 | expect(videos).to.have.lengthOf(1) | ||
198 | expect(videos[0].name).to.equal('nsfw') | ||
199 | } | ||
200 | }) | ||
201 | |||
202 | it('Should hide NSFW videos when the nsfw param === true', async function () { | ||
203 | for (const body of await getVideosFunctions(server.accessToken, { nsfw: 'false' })) { | ||
204 | expect(body.total).to.equal(1) | ||
205 | |||
206 | const videos = body.data | ||
207 | expect(videos).to.have.lengthOf(1) | ||
208 | expect(videos[0].name).to.equal('normal') | ||
209 | } | ||
210 | }) | ||
211 | |||
212 | it('Should display both videos when the nsfw param === both', async function () { | ||
213 | for (const body of await getVideosFunctions(server.accessToken, { nsfw: 'both' })) { | ||
214 | expect(body.total).to.equal(2) | ||
215 | |||
216 | const videos = body.data | ||
217 | expect(videos).to.have.lengthOf(2) | ||
218 | expect(videos[0].name).to.equal('normal') | ||
219 | expect(videos[1].name).to.equal('nsfw') | ||
220 | } | ||
221 | }) | ||
222 | }) | ||
223 | |||
224 | after(async function () { | ||
225 | await cleanupTests([ server ]) | ||
226 | }) | ||
227 | }) | ||