Add user adminFlags
[oweals/peertube.git] / server / tests / api / videos / videos-filter.ts
1 /* tslint:disable:no-unused-expression */
2
3 import * as chai from 'chai'
4 import 'mocha'
5 import {
6   createUser,
7   doubleFollow,
8   flushAndRunMultipleServers,
9   flushTests,
10   killallServers,
11   makeGetRequest,
12   ServerInfo,
13   setAccessTokensToServers,
14   uploadVideo,
15   userLogin
16 } from '../../../../shared/utils'
17 import { Video, VideoPrivacy } from '../../../../shared/models/videos'
18 import { UserRole } from '../../../../shared/models/users'
19
20 const expect = chai.expect
21
22 async function getVideosNames (server: ServerInfo, token: string, filter: string, statusCodeExpected = 200) {
23   const paths = [
24     '/api/v1/video-channels/root_channel/videos',
25     '/api/v1/accounts/root/videos',
26     '/api/v1/videos',
27     '/api/v1/search/videos'
28   ]
29
30   const videosResults: Video[][] = []
31
32   for (const path of paths) {
33     const res = await makeGetRequest({
34       url: server.url,
35       path,
36       token,
37       query: {
38         sort: 'createdAt',
39         filter
40       },
41       statusCodeExpected
42     })
43
44     videosResults.push(res.body.data.map(v => v.name))
45   }
46
47   return videosResults
48 }
49
50 describe('Test videos filter validator', function () {
51   let servers: ServerInfo[]
52
53   // ---------------------------------------------------------------
54
55   before(async function () {
56     this.timeout(120000)
57
58     await flushTests()
59
60     servers = await flushAndRunMultipleServers(2)
61
62     await setAccessTokensToServers(servers)
63
64     for (const server of servers) {
65       const moderator = { username: 'moderator', password: 'my super password' }
66       await createUser(
67         {
68           url: server.url,
69           accessToken: server.accessToken,
70           username: moderator.username,
71           password: moderator.password,
72           videoQuota: undefined,
73           videoQuotaDaily: undefined,
74           role: UserRole.MODERATOR
75         }
76       )
77       server['moderatorAccessToken'] = await userLogin(server, moderator)
78
79       await uploadVideo(server.url, server.accessToken, { name: 'public ' + server.serverNumber })
80
81       {
82         const attributes = { name: 'unlisted ' + server.serverNumber, privacy: VideoPrivacy.UNLISTED }
83         await uploadVideo(server.url, server.accessToken, attributes)
84       }
85
86       {
87         const attributes = { name: 'private ' + server.serverNumber, privacy: VideoPrivacy.PRIVATE }
88         await uploadVideo(server.url, server.accessToken, attributes)
89       }
90     }
91
92     await doubleFollow(servers[0], servers[1])
93   })
94
95   describe('Check videos filter', function () {
96
97     it('Should display local videos', async function () {
98       for (const server of servers) {
99         const namesResults = await getVideosNames(server, server.accessToken, 'local')
100         for (const names of namesResults) {
101           expect(names).to.have.lengthOf(1)
102           expect(names[ 0 ]).to.equal('public ' + server.serverNumber)
103         }
104       }
105     })
106
107     it('Should display all local videos by the admin or the moderator', async function () {
108       for (const server of servers) {
109         for (const token of [ server.accessToken, server['moderatorAccessToken'] ]) {
110
111           const namesResults = await getVideosNames(server, token, 'all-local')
112           for (const names of namesResults) {
113             expect(names).to.have.lengthOf(3)
114
115             expect(names[ 0 ]).to.equal('public ' + server.serverNumber)
116             expect(names[ 1 ]).to.equal('unlisted ' + server.serverNumber)
117             expect(names[ 2 ]).to.equal('private ' + server.serverNumber)
118           }
119         }
120       }
121     })
122   })
123
124   after(async function () {
125     killallServers(servers)
126
127     // Keep the logs if the test failed
128     if (this['ok']) {
129       await flushTests()
130     }
131   })
132 })