Move utils to /shared
[oweals/peertube.git] / server / tests / api / check-params / videos-filter.ts
1 /* tslint:disable:no-unused-expression */
2
3 import * as chai from 'chai'
4 import 'mocha'
5 import {
6   createUser,
7   flushTests,
8   killallServers,
9   makeGetRequest,
10   runServer,
11   ServerInfo,
12   setAccessTokensToServers,
13   userLogin
14 } from '../../../../shared/utils'
15 import { UserRole } from '../../../../shared/models/users'
16
17 const expect = chai.expect
18
19 async function testEndpoints (server: ServerInfo, token: string, filter: string, statusCodeExpected: number) {
20   const paths = [
21     '/api/v1/video-channels/root_channel/videos',
22     '/api/v1/accounts/root/videos',
23     '/api/v1/videos',
24     '/api/v1/search/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
45   // ---------------------------------------------------------------
46
47   before(async function () {
48     this.timeout(30000)
49
50     await flushTests()
51
52     server = await runServer(1)
53
54     await setAccessTokensToServers([ server ])
55
56     const user = { username: 'user1', password: 'my super password' }
57     await createUser(server.url, server.accessToken, user.username, user.password)
58     userAccessToken = await userLogin(server, user)
59
60     const moderator = { username: 'moderator', password: 'my super password' }
61     await createUser(
62       server.url,
63       server.accessToken,
64       moderator.username,
65       moderator.password,
66       undefined,
67       undefined,
68       UserRole.MODERATOR
69     )
70     moderatorAccessToken = await userLogin(server, moderator)
71   })
72
73   describe('When setting a video filter', function () {
74
75     it('Should fail with a bad filter', async function () {
76       await testEndpoints(server, server.accessToken, 'bad-filter', 400)
77     })
78
79     it('Should succeed with a good filter', async function () {
80       await testEndpoints(server, server.accessToken,'local', 200)
81     })
82
83     it('Should fail to list all-local with a simple user', async function () {
84       await testEndpoints(server, userAccessToken, 'all-local', 401)
85     })
86
87     it('Should succeed to list all-local with a moderator', async function () {
88       await testEndpoints(server, moderatorAccessToken, 'all-local', 200)
89     })
90
91     it('Should succeed to list all-local with an admin', async function () {
92       await testEndpoints(server, server.accessToken, 'all-local', 200)
93     })
94
95     // Because we cannot authenticate the user on the RSS endpoint
96     it('Should fail on the feeds endpoint with the all-local filter', async function () {
97       await makeGetRequest({
98         url: server.url,
99         path: '/feeds/videos.json',
100         statusCodeExpected: 401,
101         query: {
102           filter: 'all-local'
103         }
104       })
105     })
106
107     it('Should succed on the feeds endpoint with the local filter', async function () {
108       await makeGetRequest({
109         url: server.url,
110         path: '/feeds/videos.json',
111         statusCodeExpected: 200,
112         query: {
113           filter: 'local'
114         }
115       })
116     })
117   })
118
119   after(async function () {
120     killallServers([ server ])
121
122     // Keep the logs if the test failed
123     if (this['ok']) {
124       await flushTests()
125     }
126   })
127 })