aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/tests/api/check-params
diff options
context:
space:
mode:
Diffstat (limited to 'server/tests/api/check-params')
-rw-r--r--server/tests/api/check-params/index.ts2
-rw-r--r--server/tests/api/check-params/videos-common-filters.ts203
-rw-r--r--server/tests/api/check-params/videos-filter.ts114
3 files changed, 204 insertions, 115 deletions
diff --git a/server/tests/api/check-params/index.ts b/server/tests/api/check-params/index.ts
index a14e4d3e0..0882f8176 100644
--- a/server/tests/api/check-params/index.ts
+++ b/server/tests/api/check-params/index.ts
@@ -27,6 +27,6 @@ import './video-comments'
27import './video-imports' 27import './video-imports'
28import './video-playlists' 28import './video-playlists'
29import './videos' 29import './videos'
30import './videos-filter' 30import './videos-common-filters'
31import './videos-history' 31import './videos-history'
32import './videos-overviews' 32import './videos-overviews'
diff --git a/server/tests/api/check-params/videos-common-filters.ts b/server/tests/api/check-params/videos-common-filters.ts
new file mode 100644
index 000000000..afe42b0d5
--- /dev/null
+++ b/server/tests/api/check-params/videos-common-filters.ts
@@ -0,0 +1,203 @@
1/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
2
3import 'mocha'
4import {
5 cleanupTests,
6 createSingleServer,
7 makeGetRequest,
8 PeerTubeServer,
9 setAccessTokensToServers,
10 setDefaultVideoChannel
11} from '@shared/extra-utils'
12import { HttpStatusCode, UserRole, VideoInclude } from '@shared/models'
13
14describe('Test video filters validators', function () {
15 let server: PeerTubeServer
16 let userAccessToken: string
17 let moderatorAccessToken: string
18
19 // ---------------------------------------------------------------
20
21 before(async function () {
22 this.timeout(30000)
23
24 server = await createSingleServer(1)
25
26 await setAccessTokensToServers([ server ])
27 await setDefaultVideoChannel([ server ])
28
29 const user = { username: 'user1', password: 'my super password' }
30 await server.users.create({ username: user.username, password: user.password })
31 userAccessToken = await server.login.getAccessToken(user)
32
33 const moderator = { username: 'moderator', password: 'my super password' }
34 await server.users.create({ username: moderator.username, password: moderator.password, role: UserRole.MODERATOR })
35
36 moderatorAccessToken = await server.login.getAccessToken(moderator)
37 })
38
39 describe('When setting a deprecated video filter', function () {
40
41 async function testEndpoints (token: string, filter: string, expectedStatus: HttpStatusCode) {
42 const paths = [
43 '/api/v1/video-channels/root_channel/videos',
44 '/api/v1/accounts/root/videos',
45 '/api/v1/videos',
46 '/api/v1/search/videos'
47 ]
48
49 for (const path of paths) {
50 await makeGetRequest({
51 url: server.url,
52 path,
53 token,
54 query: {
55 filter
56 },
57 expectedStatus
58 })
59 }
60 }
61
62 it('Should fail with a bad filter', async function () {
63 await testEndpoints(server.accessToken, 'bad-filter', HttpStatusCode.BAD_REQUEST_400)
64 })
65
66 it('Should succeed with a good filter', async function () {
67 await testEndpoints(server.accessToken, 'local', HttpStatusCode.OK_200)
68 })
69
70 it('Should fail to list all-local/all with a simple user', async function () {
71 await testEndpoints(userAccessToken, 'all-local', HttpStatusCode.UNAUTHORIZED_401)
72 await testEndpoints(userAccessToken, 'all', HttpStatusCode.UNAUTHORIZED_401)
73 })
74
75 it('Should succeed to list all-local/all with a moderator', async function () {
76 await testEndpoints(moderatorAccessToken, 'all-local', HttpStatusCode.OK_200)
77 await testEndpoints(moderatorAccessToken, 'all', HttpStatusCode.OK_200)
78 })
79
80 it('Should succeed to list all-local/all with an admin', async function () {
81 await testEndpoints(server.accessToken, 'all-local', HttpStatusCode.OK_200)
82 await testEndpoints(server.accessToken, 'all', HttpStatusCode.OK_200)
83 })
84
85 // Because we cannot authenticate the user on the RSS endpoint
86 it('Should fail on the feeds endpoint with the all-local/all filter', async function () {
87 for (const filter of [ 'all', 'all-local' ]) {
88 await makeGetRequest({
89 url: server.url,
90 path: '/feeds/videos.json',
91 expectedStatus: HttpStatusCode.UNAUTHORIZED_401,
92 query: {
93 filter
94 }
95 })
96 }
97 })
98
99 it('Should succeed on the feeds endpoint with the local filter', async function () {
100 await makeGetRequest({
101 url: server.url,
102 path: '/feeds/videos.json',
103 expectedStatus: HttpStatusCode.OK_200,
104 query: {
105 filter: 'local'
106 }
107 })
108 })
109 })
110
111 describe('When setting video filters', function () {
112
113 const validIncludes = [
114 VideoInclude.NONE,
115 VideoInclude.HIDDEN_PRIVACY,
116 VideoInclude.NOT_PUBLISHED_STATE | VideoInclude.BLACKLISTED
117 ]
118
119 async function testEndpoints (options: {
120 token?: string
121 isLocal?: boolean
122 include?: VideoInclude
123 expectedStatus: HttpStatusCode
124 }) {
125 const paths = [
126 '/api/v1/video-channels/root_channel/videos',
127 '/api/v1/accounts/root/videos',
128 '/api/v1/videos',
129 '/api/v1/search/videos'
130 ]
131
132 for (const path of paths) {
133 await makeGetRequest({
134 url: server.url,
135 path,
136 token: options.token || server.accessToken,
137 query: {
138 isLocal: options.isLocal,
139 include: options.include
140 },
141 expectedStatus: options.expectedStatus
142 })
143 }
144 }
145
146 it('Should fail with a bad include', async function () {
147 await testEndpoints({ include: 'toto' as any, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
148 })
149
150 it('Should succeed with a good include', async function () {
151 for (const include of validIncludes) {
152 await testEndpoints({ include, expectedStatus: HttpStatusCode.OK_200 })
153 }
154 })
155
156 it('Should fail to include more videos with a simple user', async function () {
157 for (const include of validIncludes) {
158 await testEndpoints({ token: userAccessToken, include, expectedStatus: HttpStatusCode.UNAUTHORIZED_401 })
159 }
160 })
161
162 it('Should succeed to list all local/all with a moderator', async function () {
163 for (const include of validIncludes) {
164 await testEndpoints({ token: moderatorAccessToken, include, expectedStatus: HttpStatusCode.OK_200 })
165 }
166 })
167
168 it('Should succeed to list all local/all with an admin', async function () {
169 for (const include of validIncludes) {
170 await testEndpoints({ token: server.accessToken, include, expectedStatus: HttpStatusCode.OK_200 })
171 }
172 })
173
174 // Because we cannot authenticate the user on the RSS endpoint
175 it('Should fail on the feeds endpoint with the all filter', async function () {
176 for (const include of [ VideoInclude.NOT_PUBLISHED_STATE ]) {
177 await makeGetRequest({
178 url: server.url,
179 path: '/feeds/videos.json',
180 expectedStatus: HttpStatusCode.UNAUTHORIZED_401,
181 query: {
182 include
183 }
184 })
185 }
186 })
187
188 it('Should succeed on the feeds endpoint with the local filter', async function () {
189 await makeGetRequest({
190 url: server.url,
191 path: '/feeds/videos.json',
192 expectedStatus: HttpStatusCode.OK_200,
193 query: {
194 isLocal: true
195 }
196 })
197 })
198 })
199
200 after(async function () {
201 await cleanupTests([ server ])
202 })
203})
diff --git a/server/tests/api/check-params/videos-filter.ts b/server/tests/api/check-params/videos-filter.ts
deleted file mode 100644
index d08570bbe..000000000
--- a/server/tests/api/check-params/videos-filter.ts
+++ /dev/null
@@ -1,114 +0,0 @@
1/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
2
3import 'mocha'
4import {
5 cleanupTests,
6 createSingleServer,
7 makeGetRequest,
8 PeerTubeServer,
9 setAccessTokensToServers,
10 setDefaultVideoChannel
11} from '@shared/extra-utils'
12import { HttpStatusCode, UserRole } from '@shared/models'
13
14async function testEndpoints (server: PeerTubeServer, token: string, filter: string, expectedStatus: HttpStatusCode) {
15 const paths = [
16 '/api/v1/video-channels/root_channel/videos',
17 '/api/v1/accounts/root/videos',
18 '/api/v1/videos',
19 '/api/v1/search/videos'
20 ]
21
22 for (const path of paths) {
23 await makeGetRequest({
24 url: server.url,
25 path,
26 token,
27 query: {
28 filter
29 },
30 expectedStatus
31 })
32 }
33}
34
35describe('Test video filters validators', function () {
36 let server: PeerTubeServer
37 let userAccessToken: string
38 let moderatorAccessToken: string
39
40 // ---------------------------------------------------------------
41
42 before(async function () {
43 this.timeout(30000)
44
45 server = await createSingleServer(1)
46
47 await setAccessTokensToServers([ server ])
48 await setDefaultVideoChannel([ server ])
49
50 const user = { username: 'user1', password: 'my super password' }
51 await server.users.create({ username: user.username, password: user.password })
52 userAccessToken = await server.login.getAccessToken(user)
53
54 const moderator = { username: 'moderator', password: 'my super password' }
55 await server.users.create({ username: moderator.username, password: moderator.password, role: UserRole.MODERATOR })
56
57 moderatorAccessToken = await server.login.getAccessToken(moderator)
58 })
59
60 describe('When setting a video filter', function () {
61
62 it('Should fail with a bad filter', async function () {
63 await testEndpoints(server, server.accessToken, 'bad-filter', HttpStatusCode.BAD_REQUEST_400)
64 })
65
66 it('Should succeed with a good filter', async function () {
67 await testEndpoints(server, server.accessToken, 'local', HttpStatusCode.OK_200)
68 })
69
70 it('Should fail to list all-local/all with a simple user', async function () {
71 await testEndpoints(server, userAccessToken, 'all-local', HttpStatusCode.UNAUTHORIZED_401)
72 await testEndpoints(server, userAccessToken, 'all', HttpStatusCode.UNAUTHORIZED_401)
73 })
74
75 it('Should succeed to list all-local/all with a moderator', async function () {
76 await testEndpoints(server, moderatorAccessToken, 'all-local', HttpStatusCode.OK_200)
77 await testEndpoints(server, moderatorAccessToken, 'all', HttpStatusCode.OK_200)
78 })
79
80 it('Should succeed to list all-local/all with an admin', async function () {
81 await testEndpoints(server, server.accessToken, 'all-local', HttpStatusCode.OK_200)
82 await testEndpoints(server, server.accessToken, 'all', HttpStatusCode.OK_200)
83 })
84
85 // Because we cannot authenticate the user on the RSS endpoint
86 it('Should fail on the feeds endpoint with the all-local/all filter', async function () {
87 for (const filter of [ 'all', 'all-local' ]) {
88 await makeGetRequest({
89 url: server.url,
90 path: '/feeds/videos.json',
91 expectedStatus: HttpStatusCode.UNAUTHORIZED_401,
92 query: {
93 filter
94 }
95 })
96 }
97 })
98
99 it('Should succeed on the feeds endpoint with the local filter', async function () {
100 await makeGetRequest({
101 url: server.url,
102 path: '/feeds/videos.json',
103 expectedStatus: HttpStatusCode.OK_200,
104 query: {
105 filter: 'local'
106 }
107 })
108 })
109 })
110
111 after(async function () {
112 await cleanupTests([ server ])
113 })
114})