]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/tests/api/check-params/videos-filter.ts
971867b27d30a58d104560dfcb873b31cca7e4d0
[github/Chocobozzz/PeerTube.git] / server / tests / api / check-params / videos-filter.ts
1 /* tslint:disable:no-unused-expression */
2
3 import 'mocha'
4 import {
5 createUser,
6 createVideoPlaylist,
7 flushTests,
8 killallServers,
9 makeGetRequest,
10 runServer,
11 ServerInfo,
12 setAccessTokensToServers, setDefaultVideoChannel,
13 userLogin
14 } from '../../../../shared/extra-utils'
15 import { UserRole } from '../../../../shared/models/users'
16 import { VideoPlaylistPrivacy } from '../../../../shared/models/videos/playlist/video-playlist-privacy.model'
17
18 async function testEndpoints (server: ServerInfo, token: string, filter: string, playlistUUID: string, statusCodeExpected: number) {
19 const paths = [
20 '/api/v1/video-channels/root_channel/videos',
21 '/api/v1/accounts/root/videos',
22 '/api/v1/videos',
23 '/api/v1/search/videos',
24 '/api/v1/video-playlists/' + playlistUUID + '/videos'
25 ]
26
27 for (const path of paths) {
28 await makeGetRequest({
29 url: server.url,
30 path,
31 token,
32 query: {
33 filter
34 },
35 statusCodeExpected
36 })
37 }
38 }
39
40 describe('Test videos filters', function () {
41 let server: ServerInfo
42 let userAccessToken: string
43 let moderatorAccessToken: string
44 let playlistUUID: string
45
46 // ---------------------------------------------------------------
47
48 before(async function () {
49 this.timeout(30000)
50
51 await flushTests()
52
53 server = await runServer(1)
54
55 await setAccessTokensToServers([ server ])
56 await setDefaultVideoChannel([ server ])
57
58 const user = { username: 'user1', password: 'my super password' }
59 await createUser({ url: server.url, accessToken: server.accessToken, username: user.username, password: user.password })
60 userAccessToken = await userLogin(server, user)
61
62 const moderator = { username: 'moderator', password: 'my super password' }
63 await createUser(
64 {
65 url: server.url,
66 accessToken: server.accessToken,
67 username: moderator.username,
68 password: moderator.password,
69 videoQuota: undefined,
70 videoQuotaDaily: undefined,
71 role: UserRole.MODERATOR
72 }
73 )
74 moderatorAccessToken = await userLogin(server, moderator)
75
76 const res = await createVideoPlaylist({
77 url: server.url,
78 token: server.accessToken,
79 playlistAttrs: {
80 displayName: 'super playlist',
81 privacy: VideoPlaylistPrivacy.PUBLIC,
82 videoChannelId: server.videoChannel.id
83 }
84 })
85 playlistUUID = res.body.videoPlaylist.uuid
86 })
87
88 describe('When setting a video filter', function () {
89
90 it('Should fail with a bad filter', async function () {
91 await testEndpoints(server, server.accessToken, 'bad-filter', playlistUUID, 400)
92 })
93
94 it('Should succeed with a good filter', async function () {
95 await testEndpoints(server, server.accessToken,'local', playlistUUID, 200)
96 })
97
98 it('Should fail to list all-local with a simple user', async function () {
99 await testEndpoints(server, userAccessToken, 'all-local', playlistUUID, 401)
100 })
101
102 it('Should succeed to list all-local with a moderator', async function () {
103 await testEndpoints(server, moderatorAccessToken, 'all-local', playlistUUID, 200)
104 })
105
106 it('Should succeed to list all-local with an admin', async function () {
107 await testEndpoints(server, server.accessToken, 'all-local', playlistUUID, 200)
108 })
109
110 // Because we cannot authenticate the user on the RSS endpoint
111 it('Should fail on the feeds endpoint with the all-local filter', async function () {
112 await makeGetRequest({
113 url: server.url,
114 path: '/feeds/videos.json',
115 statusCodeExpected: 401,
116 query: {
117 filter: 'all-local'
118 }
119 })
120 })
121
122 it('Should succeed on the feeds endpoint with the local filter', async function () {
123 await makeGetRequest({
124 url: server.url,
125 path: '/feeds/videos.json',
126 statusCodeExpected: 200,
127 query: {
128 filter: 'local'
129 }
130 })
131 })
132 })
133
134 after(async function () {
135 killallServers([ server ])
136
137 // Keep the logs if the test failed
138 if (this['ok']) {
139 await flushTests()
140 }
141 })
142 })