1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
3 import { HttpStatusCode, UserRole, VideoInclude, VideoPrivacy } from '@shared/models'
9 setAccessTokensToServers,
10 setDefaultVideoChannel
11 } from '@shared/server-commands'
13 describe('Test video filters validators', function () {
14 let server: PeerTubeServer
15 let userAccessToken: string
16 let moderatorAccessToken: string
18 // ---------------------------------------------------------------
20 before(async function () {
23 server = await createSingleServer(1)
25 await setAccessTokensToServers([ server ])
26 await setDefaultVideoChannel([ server ])
28 const user = { username: 'user1', password: 'my super password' }
29 await server.users.create({ username: user.username, password: user.password })
30 userAccessToken = await server.login.getAccessToken(user)
32 const moderator = { username: 'moderator', password: 'my super password' }
33 await server.users.create({ username: moderator.username, password: moderator.password, role: UserRole.MODERATOR })
35 moderatorAccessToken = await server.login.getAccessToken(moderator)
38 describe('When setting a deprecated video filter', function () {
40 async function testEndpoints (token: string, filter: string, expectedStatus: HttpStatusCode) {
42 '/api/v1/video-channels/root_channel/videos',
43 '/api/v1/accounts/root/videos',
45 '/api/v1/search/videos'
48 for (const path of paths) {
49 await makeGetRequest({
61 it('Should fail with a bad filter', async function () {
62 await testEndpoints(server.accessToken, 'bad-filter', HttpStatusCode.BAD_REQUEST_400)
65 it('Should succeed with a good filter', async function () {
66 await testEndpoints(server.accessToken, 'local', HttpStatusCode.OK_200)
69 it('Should fail to list all-local/all with a simple user', async function () {
70 await testEndpoints(userAccessToken, 'all-local', HttpStatusCode.UNAUTHORIZED_401)
71 await testEndpoints(userAccessToken, 'all', HttpStatusCode.UNAUTHORIZED_401)
74 it('Should succeed to list all-local/all with a moderator', async function () {
75 await testEndpoints(moderatorAccessToken, 'all-local', HttpStatusCode.OK_200)
76 await testEndpoints(moderatorAccessToken, 'all', HttpStatusCode.OK_200)
79 it('Should succeed to list all-local/all with an admin', async function () {
80 await testEndpoints(server.accessToken, 'all-local', HttpStatusCode.OK_200)
81 await testEndpoints(server.accessToken, 'all', HttpStatusCode.OK_200)
84 // Because we cannot authenticate the user on the RSS endpoint
85 it('Should fail on the feeds endpoint with the all-local/all filter', async function () {
86 for (const filter of [ 'all', 'all-local' ]) {
87 await makeGetRequest({
89 path: '/feeds/videos.json',
90 expectedStatus: HttpStatusCode.UNAUTHORIZED_401,
98 it('Should succeed on the feeds endpoint with the local filter', async function () {
99 await makeGetRequest({
101 path: '/feeds/videos.json',
102 expectedStatus: HttpStatusCode.OK_200,
110 describe('When setting video filters', function () {
112 const validIncludes = [
114 VideoInclude.BLOCKED_OWNER,
115 VideoInclude.NOT_PUBLISHED_STATE | VideoInclude.BLACKLISTED
118 async function testEndpoints (options: {
121 include?: VideoInclude
122 privacyOneOf?: VideoPrivacy[]
123 expectedStatus: HttpStatusCode
126 '/api/v1/video-channels/root_channel/videos',
127 '/api/v1/accounts/root/videos',
129 '/api/v1/search/videos'
132 for (const path of paths) {
133 await makeGetRequest({
136 token: options.token || server.accessToken,
138 isLocal: options.isLocal,
139 privacyOneOf: options.privacyOneOf,
140 include: options.include
142 expectedStatus: options.expectedStatus
147 it('Should fail with a bad privacyOneOf', async function () {
148 await testEndpoints({ privacyOneOf: [ 'toto' ] as any, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
151 it('Should succeed with a good privacyOneOf', async function () {
152 await testEndpoints({ privacyOneOf: [ VideoPrivacy.INTERNAL ], expectedStatus: HttpStatusCode.OK_200 })
155 it('Should fail to use privacyOneOf with a simple user', async function () {
156 await testEndpoints({
157 privacyOneOf: [ VideoPrivacy.INTERNAL ],
158 token: userAccessToken,
159 expectedStatus: HttpStatusCode.UNAUTHORIZED_401
163 it('Should fail with a bad include', async function () {
164 await testEndpoints({ include: 'toto' as any, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
167 it('Should succeed with a good include', async function () {
168 for (const include of validIncludes) {
169 await testEndpoints({ include, expectedStatus: HttpStatusCode.OK_200 })
173 it('Should fail to include more videos with a simple user', async function () {
174 for (const include of validIncludes) {
175 await testEndpoints({ token: userAccessToken, include, expectedStatus: HttpStatusCode.UNAUTHORIZED_401 })
179 it('Should succeed to list all local/all with a moderator', async function () {
180 for (const include of validIncludes) {
181 await testEndpoints({ token: moderatorAccessToken, include, expectedStatus: HttpStatusCode.OK_200 })
185 it('Should succeed to list all local/all with an admin', async function () {
186 for (const include of validIncludes) {
187 await testEndpoints({ token: server.accessToken, include, expectedStatus: HttpStatusCode.OK_200 })
191 // Because we cannot authenticate the user on the RSS endpoint
192 it('Should fail on the feeds endpoint with the all filter', async function () {
193 for (const include of [ VideoInclude.NOT_PUBLISHED_STATE ]) {
194 await makeGetRequest({
196 path: '/feeds/videos.json',
197 expectedStatus: HttpStatusCode.UNAUTHORIZED_401,
205 it('Should succeed on the feeds endpoint with the local filter', async function () {
206 await makeGetRequest({
208 path: '/feeds/videos.json',
209 expectedStatus: HttpStatusCode.OK_200,
217 after(async function () {
218 await cleanupTests([ server ])