1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
9 setAccessTokensToServers,
10 setDefaultVideoChannel
11 } from '@shared/extra-utils'
12 import { HttpStatusCode, UserRole, VideoInclude } from '@shared/models'
14 describe('Test video filters validators', function () {
15 let server: PeerTubeServer
16 let userAccessToken: string
17 let moderatorAccessToken: string
19 // ---------------------------------------------------------------
21 before(async function () {
24 server = await createSingleServer(1)
26 await setAccessTokensToServers([ server ])
27 await setDefaultVideoChannel([ server ])
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)
33 const moderator = { username: 'moderator', password: 'my super password' }
34 await server.users.create({ username: moderator.username, password: moderator.password, role: UserRole.MODERATOR })
36 moderatorAccessToken = await server.login.getAccessToken(moderator)
39 describe('When setting a deprecated video filter', function () {
41 async function testEndpoints (token: string, filter: string, expectedStatus: HttpStatusCode) {
43 '/api/v1/video-channels/root_channel/videos',
44 '/api/v1/accounts/root/videos',
46 '/api/v1/search/videos'
49 for (const path of paths) {
50 await makeGetRequest({
62 it('Should fail with a bad filter', async function () {
63 await testEndpoints(server.accessToken, 'bad-filter', HttpStatusCode.BAD_REQUEST_400)
66 it('Should succeed with a good filter', async function () {
67 await testEndpoints(server.accessToken, 'local', HttpStatusCode.OK_200)
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)
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)
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)
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({
90 path: '/feeds/videos.json',
91 expectedStatus: HttpStatusCode.UNAUTHORIZED_401,
99 it('Should succeed on the feeds endpoint with the local filter', async function () {
100 await makeGetRequest({
102 path: '/feeds/videos.json',
103 expectedStatus: HttpStatusCode.OK_200,
111 describe('When setting video filters', function () {
113 const validIncludes = [
115 VideoInclude.HIDDEN_PRIVACY,
116 VideoInclude.NOT_PUBLISHED_STATE | VideoInclude.BLACKLISTED
119 async function testEndpoints (options: {
122 include?: VideoInclude
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 include: options.include
141 expectedStatus: options.expectedStatus
146 it('Should fail with a bad include', async function () {
147 await testEndpoints({ include: 'toto' as any, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
150 it('Should succeed with a good include', async function () {
151 for (const include of validIncludes) {
152 await testEndpoints({ include, expectedStatus: HttpStatusCode.OK_200 })
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 })
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 })
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 })
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({
179 path: '/feeds/videos.json',
180 expectedStatus: HttpStatusCode.UNAUTHORIZED_401,
188 it('Should succeed on the feeds endpoint with the local filter', async function () {
189 await makeGetRequest({
191 path: '/feeds/videos.json',
192 expectedStatus: HttpStatusCode.OK_200,
200 after(async function () {
201 await cleanupTests([ server ])