aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/tests/api
diff options
context:
space:
mode:
Diffstat (limited to 'server/tests/api')
-rw-r--r--server/tests/api/check-params/blocklist.ts222
-rw-r--r--server/tests/api/check-params/index.ts1
-rw-r--r--server/tests/api/users/account-blocklist.ts294
3 files changed, 517 insertions, 0 deletions
diff --git a/server/tests/api/check-params/blocklist.ts b/server/tests/api/check-params/blocklist.ts
new file mode 100644
index 000000000..8117c46a6
--- /dev/null
+++ b/server/tests/api/check-params/blocklist.ts
@@ -0,0 +1,222 @@
1/* tslint:disable:no-unused-expression */
2
3import 'mocha'
4
5import {
6 createUser,
7 doubleFollow,
8 flushAndRunMultipleServers,
9 flushTests,
10 killallServers,
11 makeDeleteRequest,
12 makeGetRequest,
13 makePostBodyRequest,
14 ServerInfo,
15 setAccessTokensToServers
16} from '../../utils'
17import { checkBadCountPagination, checkBadSortPagination, checkBadStartPagination } from '../../utils/requests/check-api-params'
18
19describe('Test blocklist API validators', function () {
20 let servers: ServerInfo[]
21 let server: ServerInfo
22
23 before(async function () {
24 this.timeout(60000)
25
26 await flushTests()
27
28 servers = await flushAndRunMultipleServers(2)
29 await setAccessTokensToServers(servers)
30
31 server = servers[0]
32
33 const user = { username: 'user1', password: 'password' }
34 await createUser(server.url, server.accessToken, user.username, user.password)
35
36 await doubleFollow(servers[0], servers[1])
37 })
38
39 // ---------------------------------------------------------------
40
41 describe('When managing user blocklist', function () {
42 const path = '/api/v1/users/me/blocklist/accounts'
43
44 describe('When managing user accounts blocklist', function () {
45
46 describe('When listing blocked accounts', function () {
47 it('Should fail with an unauthenticated user', async function () {
48 await makeGetRequest({
49 url: server.url,
50 path,
51 statusCodeExpected: 401
52 })
53 })
54
55 it('Should fail with a bad start pagination', async function () {
56 await checkBadStartPagination(server.url, path, server.accessToken)
57 })
58
59 it('Should fail with a bad count pagination', async function () {
60 await checkBadCountPagination(server.url, path, server.accessToken)
61 })
62
63 it('Should fail with an incorrect sort', async function () {
64 await checkBadSortPagination(server.url, path, server.accessToken)
65 })
66 })
67
68 describe('When blocking an account', function () {
69 it('Should fail with an unauthenticated user', async function () {
70 await makePostBodyRequest({
71 url: server.url,
72 path,
73 fields: { accountName: 'user1' },
74 statusCodeExpected: 401
75 })
76 })
77
78 it('Should fail with an unknown account', async function () {
79 await makePostBodyRequest({
80 url: server.url,
81 token: server.accessToken,
82 path,
83 fields: { accountName: 'user2' },
84 statusCodeExpected: 404
85 })
86 })
87
88 it('Should succeed with the correct params', async function () {
89 await makePostBodyRequest({
90 url: server.url,
91 token: server.accessToken,
92 path,
93 fields: { accountName: 'user1' },
94 statusCodeExpected: 204
95 })
96 })
97 })
98
99 describe('When unblocking an account', function () {
100 it('Should fail with an unauthenticated user', async function () {
101 await makeDeleteRequest({
102 url: server.url,
103 path: path + '/user1',
104 statusCodeExpected: 401
105 })
106 })
107
108 it('Should fail with an unknown account block', async function () {
109 await makeDeleteRequest({
110 url: server.url,
111 path: path + '/user2',
112 token: server.accessToken,
113 statusCodeExpected: 404
114 })
115 })
116
117 it('Should succeed with the correct params', async function () {
118 await makeDeleteRequest({
119 url: server.url,
120 path: path + '/user1',
121 token: server.accessToken,
122 statusCodeExpected: 204
123 })
124 })
125 })
126 })
127
128 describe('When managing user servers blocklist', function () {
129 const path = '/api/v1/users/me/blocklist/servers'
130
131 describe('When listing blocked servers', function () {
132 it('Should fail with an unauthenticated user', async function () {
133 await makeGetRequest({
134 url: server.url,
135 path,
136 statusCodeExpected: 401
137 })
138 })
139
140 it('Should fail with a bad start pagination', async function () {
141 await checkBadStartPagination(server.url, path, server.accessToken)
142 })
143
144 it('Should fail with a bad count pagination', async function () {
145 await checkBadCountPagination(server.url, path, server.accessToken)
146 })
147
148 it('Should fail with an incorrect sort', async function () {
149 await checkBadSortPagination(server.url, path, server.accessToken)
150 })
151 })
152
153 describe('When blocking a server', function () {
154 it('Should fail with an unauthenticated user', async function () {
155 await makePostBodyRequest({
156 url: server.url,
157 path,
158 fields: { host: 'localhost:9002' },
159 statusCodeExpected: 401
160 })
161 })
162
163 it('Should fail with an unknown server', async function () {
164 await makePostBodyRequest({
165 url: server.url,
166 token: server.accessToken,
167 path,
168 fields: { host: 'localhost:9003' },
169 statusCodeExpected: 404
170 })
171 })
172
173 it('Should succeed with the correct params', async function () {
174 await makePostBodyRequest({
175 url: server.url,
176 token: server.accessToken,
177 path,
178 fields: { host: 'localhost:9002' },
179 statusCodeExpected: 204
180 })
181 })
182 })
183
184 describe('When unblocking a server', function () {
185 it('Should fail with an unauthenticated user', async function () {
186 await makeDeleteRequest({
187 url: server.url,
188 path: path + '/localhost:9002',
189 statusCodeExpected: 401
190 })
191 })
192
193 it('Should fail with an unknown server block', async function () {
194 await makeDeleteRequest({
195 url: server.url,
196 path: path + '/localhost:9003',
197 token: server.accessToken,
198 statusCodeExpected: 404
199 })
200 })
201
202 it('Should succeed with the correct params', async function () {
203 await makeDeleteRequest({
204 url: server.url,
205 path: path + '/localhost:9002',
206 token: server.accessToken,
207 statusCodeExpected: 204
208 })
209 })
210 })
211 })
212 })
213
214 after(async function () {
215 killallServers(servers)
216
217 // Keep the logs if the test failed
218 if (this['ok']) {
219 await flushTests()
220 }
221 })
222})
diff --git a/server/tests/api/check-params/index.ts b/server/tests/api/check-params/index.ts
index bfc550ae5..877ceb0a7 100644
--- a/server/tests/api/check-params/index.ts
+++ b/server/tests/api/check-params/index.ts
@@ -1,5 +1,6 @@
1// Order of the tests we want to execute 1// Order of the tests we want to execute
2import './accounts' 2import './accounts'
3import './blocklist'
3import './config' 4import './config'
4import './follows' 5import './follows'
5import './jobs' 6import './jobs'
diff --git a/server/tests/api/users/account-blocklist.ts b/server/tests/api/users/account-blocklist.ts
new file mode 100644
index 000000000..00ad51461
--- /dev/null
+++ b/server/tests/api/users/account-blocklist.ts
@@ -0,0 +1,294 @@
1/* tslint:disable:no-unused-expression */
2
3import * as chai from 'chai'
4import 'mocha'
5import { AccountBlock, ServerBlock, Video } from '../../../../shared/index'
6import {
7 createUser,
8 doubleFollow,
9 flushAndRunMultipleServers,
10 flushTests,
11 killallServers,
12 ServerInfo,
13 uploadVideo,
14 userLogin
15} from '../../utils/index'
16import { setAccessTokensToServers } from '../../utils/users/login'
17import { getVideosListWithToken } from '../../utils/videos/videos'
18import {
19 addVideoCommentReply,
20 addVideoCommentThread,
21 getVideoCommentThreads,
22 getVideoThreadComments
23} from '../../utils/videos/video-comments'
24import { waitJobs } from '../../utils/server/jobs'
25import { VideoComment, VideoCommentThreadTree } from '../../../../shared/models/videos/video-comment.model'
26import {
27 addAccountToAccountBlocklist,
28 addServerToAccountBlocklist,
29 getAccountBlocklistByAccount, getServerBlocklistByAccount,
30 removeAccountFromAccountBlocklist,
31 removeServerFromAccountBlocklist
32} from '../../utils/users/blocklist'
33
34const expect = chai.expect
35
36async function checkAllVideos (url: string, token: string) {
37 const res = await getVideosListWithToken(url, token)
38
39 expect(res.body.data).to.have.lengthOf(4)
40}
41
42async function checkAllComments (url: string, token: string, videoUUID: string) {
43 const resThreads = await getVideoCommentThreads(url, videoUUID, 0, 5, '-createdAt', token)
44
45 const threads: VideoComment[] = resThreads.body.data
46 expect(threads).to.have.lengthOf(2)
47
48 for (const thread of threads) {
49 const res = await getVideoThreadComments(url, videoUUID, thread.id, token)
50
51 const tree: VideoCommentThreadTree = res.body
52 expect(tree.children).to.have.lengthOf(1)
53 }
54}
55
56describe('Test accounts blocklist', function () {
57 let servers: ServerInfo[]
58 let videoUUID1: string
59 let videoUUID2: string
60 let userToken1: string
61 let userToken2: string
62
63 before(async function () {
64 this.timeout(60000)
65
66 await flushTests()
67
68 servers = await flushAndRunMultipleServers(2)
69 await setAccessTokensToServers(servers)
70
71 {
72 const user = { username: 'user1', password: 'password' }
73 await createUser(servers[0].url, servers[0].accessToken, user.username, user.password)
74
75 userToken1 = await userLogin(servers[0], user)
76 await uploadVideo(servers[0].url, userToken1, { name: 'video user 1' })
77 }
78
79 {
80 const user = { username: 'user2', password: 'password' }
81 await createUser(servers[1].url, servers[1].accessToken, user.username, user.password)
82
83 userToken2 = await userLogin(servers[1], user)
84 await uploadVideo(servers[1].url, userToken2, { name: 'video user 2' })
85 }
86
87 {
88 const res = await uploadVideo(servers[0].url, servers[0].accessToken, { name: 'video server 1' })
89 videoUUID1 = res.body.video.uuid
90 }
91
92 {
93 const res = await uploadVideo(servers[1].url, servers[1].accessToken, { name: 'video server 2' })
94 videoUUID2 = res.body.video.uuid
95 }
96
97 await doubleFollow(servers[0], servers[1])
98
99 {
100 const resComment = await addVideoCommentThread(servers[ 0 ].url, servers[ 0 ].accessToken, videoUUID1, 'comment root 1')
101 const resReply = await addVideoCommentReply(servers[ 0 ].url, userToken1, videoUUID1, resComment.body.comment.id, 'comment user 1')
102 await addVideoCommentReply(servers[ 0 ].url, servers[ 0 ].accessToken, videoUUID1, resReply.body.comment.id, 'comment root 1')
103 }
104
105 {
106 const resComment = await addVideoCommentThread(servers[ 0 ].url, userToken1, videoUUID1, 'comment user 1')
107 await addVideoCommentReply(servers[ 0 ].url, servers[ 0 ].accessToken, videoUUID1, resComment.body.comment.id, 'comment root 1')
108 }
109
110 await waitJobs(servers)
111 })
112
113 describe('When managing account blocklist', function () {
114 it('Should list all videos', function () {
115 return checkAllVideos(servers[0].url, servers[0].accessToken)
116 })
117
118 it('Should list the comments', function () {
119 return checkAllComments(servers[0].url, servers[0].accessToken, videoUUID1)
120 })
121
122 it('Should block a remote account', async function () {
123 await addAccountToAccountBlocklist(servers[0].url, servers[0].accessToken, 'user2@localhost:9002')
124 })
125
126 it('Should hide its videos', async function () {
127 const res = await getVideosListWithToken(servers[0].url, servers[0].accessToken)
128
129 const videos: Video[] = res.body.data
130 expect(videos).to.have.lengthOf(3)
131
132 const v = videos.find(v => v.name === 'video user 2')
133 expect(v).to.be.undefined
134 })
135
136 it('Should block a local account', async function () {
137 await addAccountToAccountBlocklist(servers[0].url, servers[0].accessToken, 'user1')
138 })
139
140 it('Should hide its videos', async function () {
141 const res = await getVideosListWithToken(servers[0].url, servers[0].accessToken)
142
143 const videos: Video[] = res.body.data
144 expect(videos).to.have.lengthOf(2)
145
146 const v = videos.find(v => v.name === 'video user 1')
147 expect(v).to.be.undefined
148 })
149
150 it('Should hide its comments', async function () {
151 const resThreads = await getVideoCommentThreads(servers[0].url, videoUUID1, 0, 5, '-createdAt', servers[0].accessToken)
152
153 const threads: VideoComment[] = resThreads.body.data
154 expect(threads).to.have.lengthOf(1)
155 expect(threads[0].totalReplies).to.equal(0)
156
157 const t = threads.find(t => t.text === 'comment user 1')
158 expect(t).to.be.undefined
159
160 for (const thread of threads) {
161 const res = await getVideoThreadComments(servers[0].url, videoUUID1, thread.id, servers[0].accessToken)
162
163 const tree: VideoCommentThreadTree = res.body
164 expect(tree.children).to.have.lengthOf(0)
165 }
166 })
167
168 it('Should list all the videos with another user', async function () {
169 return checkAllVideos(servers[0].url, userToken1)
170 })
171
172 it('Should list all the comments with another user', async function () {
173 return checkAllComments(servers[0].url, userToken1, videoUUID1)
174 })
175
176 it('Should list blocked accounts', async function () {
177 {
178 const res = await getAccountBlocklistByAccount(servers[ 0 ].url, servers[ 0 ].accessToken, 0, 1, 'createdAt')
179 const blocks: AccountBlock[] = res.body.data
180
181 expect(res.body.total).to.equal(2)
182
183 const block = blocks[0]
184 expect(block.byAccount.displayName).to.equal('root')
185 expect(block.byAccount.name).to.equal('root')
186 expect(block.accountBlocked.displayName).to.equal('user2')
187 expect(block.accountBlocked.name).to.equal('user2')
188 expect(block.accountBlocked.host).to.equal('localhost:9002')
189 }
190
191 {
192 const res = await getAccountBlocklistByAccount(servers[ 0 ].url, servers[ 0 ].accessToken, 1, 2, 'createdAt')
193 const blocks: AccountBlock[] = res.body.data
194
195 expect(res.body.total).to.equal(2)
196
197 const block = blocks[0]
198 expect(block.byAccount.displayName).to.equal('root')
199 expect(block.byAccount.name).to.equal('root')
200 expect(block.accountBlocked.displayName).to.equal('user1')
201 expect(block.accountBlocked.name).to.equal('user1')
202 expect(block.accountBlocked.host).to.equal('localhost:9001')
203 }
204 })
205
206 it('Should unblock the remote account', async function () {
207 await removeAccountFromAccountBlocklist(servers[0].url, servers[0].accessToken, 'user2@localhost:9002')
208 })
209
210 it('Should display its videos', async function () {
211 const res = await getVideosListWithToken(servers[0].url, servers[0].accessToken)
212
213 const videos: Video[] = res.body.data
214 expect(videos).to.have.lengthOf(3)
215
216 const v = videos.find(v => v.name === 'video user 2')
217 expect(v).not.to.be.undefined
218 })
219
220 it('Should unblock the local account', async function () {
221 await removeAccountFromAccountBlocklist(servers[0].url, servers[0].accessToken, 'user1')
222 })
223
224 it('Should display its comments', function () {
225 return checkAllComments(servers[0].url, servers[0].accessToken, videoUUID1)
226 })
227 })
228
229 describe('When managing server blocklist', function () {
230 it('Should list all videos', function () {
231 return checkAllVideos(servers[0].url, servers[0].accessToken)
232 })
233
234 it('Should list the comments', function () {
235 return checkAllComments(servers[0].url, servers[0].accessToken, videoUUID1)
236 })
237
238 it('Should block a remote server', async function () {
239 await addServerToAccountBlocklist(servers[0].url, servers[0].accessToken, 'localhost:9002')
240 })
241
242 it('Should hide its videos', async function () {
243 const res = await getVideosListWithToken(servers[0].url, servers[0].accessToken)
244
245 const videos: Video[] = res.body.data
246 expect(videos).to.have.lengthOf(2)
247
248 const v1 = videos.find(v => v.name === 'video user 2')
249 const v2 = videos.find(v => v.name === 'video server 2')
250
251 expect(v1).to.be.undefined
252 expect(v2).to.be.undefined
253 })
254
255 it('Should list all the videos with another user', async function () {
256 return checkAllVideos(servers[0].url, userToken1)
257 })
258
259 it('Should hide its comments')
260
261 it('Should list blocked servers', async function () {
262 const res = await getServerBlocklistByAccount(servers[ 0 ].url, servers[ 0 ].accessToken, 0, 1, 'createdAt')
263 const blocks: ServerBlock[] = res.body.data
264
265 expect(res.body.total).to.equal(1)
266
267 const block = blocks[0]
268 expect(block.byAccount.displayName).to.equal('root')
269 expect(block.byAccount.name).to.equal('root')
270 expect(block.serverBlocked.host).to.equal('localhost:9002')
271 })
272
273 it('Should unblock the remote server', async function () {
274 await removeServerFromAccountBlocklist(servers[0].url, servers[0].accessToken, 'localhost:9002')
275 })
276
277 it('Should display its videos', function () {
278 return checkAllVideos(servers[0].url, servers[0].accessToken)
279 })
280
281 it('Should display its comments', function () {
282 return checkAllComments(servers[0].url, servers[0].accessToken, videoUUID1)
283 })
284 })
285
286 after(async function () {
287 killallServers(servers)
288
289 // Keep the logs if the test failed
290 if (this[ 'ok' ]) {
291 await flushTests()
292 }
293 })
294})