]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/tests/api/videos/videos-filter.ts
Refactor requests
[github/Chocobozzz/PeerTube.git] / server / tests / api / videos / videos-filter.ts
CommitLineData
a1587156 1/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
1cd3facc 2
1cd3facc 3import 'mocha'
d23dd9fb 4import { expect } from 'chai'
c0e8b12e 5import { HttpStatusCode } from '@shared/models'
1cd3facc 6import {
7c3b7976 7 cleanupTests,
1cd3facc 8 doubleFollow,
254d3579 9 createMultipleServers,
1cd3facc 10 makeGetRequest,
254d3579 11 PeerTubeServer,
d23dd9fb
C
12 setAccessTokensToServers
13} from '@shared/extra-utils'
14import { UserRole, Video, VideoPrivacy } from '@shared/models'
1cd3facc 15
c0e8b12e 16async function getVideosNames (server: PeerTubeServer, token: string, filter: string, expectedStatus = HttpStatusCode.OK_200) {
1cd3facc
C
17 const paths = [
18 '/api/v1/video-channels/root_channel/videos',
19 '/api/v1/accounts/root/videos',
20 '/api/v1/videos',
21 '/api/v1/search/videos'
22 ]
23
24 const videosResults: Video[][] = []
25
26 for (const path of paths) {
27 const res = await makeGetRequest({
28 url: server.url,
29 path,
30 token,
31 query: {
32 sort: 'createdAt',
33 filter
34 },
c0e8b12e 35 expectedStatus
1cd3facc
C
36 })
37
38 videosResults.push(res.body.data.map(v => v.name))
39 }
40
41 return videosResults
42}
43
3124b469 44describe('Test videos filter', function () {
254d3579 45 let servers: PeerTubeServer[]
1cd3facc
C
46
47 // ---------------------------------------------------------------
48
49 before(async function () {
3124b469 50 this.timeout(160000)
1cd3facc 51
254d3579 52 servers = await createMultipleServers(2)
1cd3facc
C
53
54 await setAccessTokensToServers(servers)
55
56 for (const server of servers) {
57 const moderator = { username: 'moderator', password: 'my super password' }
89d241a7
C
58 await server.users.create({ username: moderator.username, password: moderator.password, role: UserRole.MODERATOR })
59 server['moderatorAccessToken'] = await server.login.getAccessToken(moderator)
1cd3facc 60
89d241a7 61 await server.videos.upload({ attributes: { name: 'public ' + server.serverNumber } })
1cd3facc
C
62
63 {
64 const attributes = { name: 'unlisted ' + server.serverNumber, privacy: VideoPrivacy.UNLISTED }
89d241a7 65 await server.videos.upload({ attributes })
1cd3facc
C
66 }
67
68 {
69 const attributes = { name: 'private ' + server.serverNumber, privacy: VideoPrivacy.PRIVATE }
89d241a7 70 await server.videos.upload({ attributes })
1cd3facc
C
71 }
72 }
73
74 await doubleFollow(servers[0], servers[1])
75 })
76
77 describe('Check videos filter', function () {
78
79 it('Should display local videos', async function () {
80 for (const server of servers) {
81 const namesResults = await getVideosNames(server, server.accessToken, 'local')
82 for (const names of namesResults) {
83 expect(names).to.have.lengthOf(1)
a1587156 84 expect(names[0]).to.equal('public ' + server.serverNumber)
1cd3facc
C
85 }
86 }
87 })
88
89 it('Should display all local videos by the admin or the moderator', async function () {
90 for (const server of servers) {
91 for (const token of [ server.accessToken, server['moderatorAccessToken'] ]) {
92
93 const namesResults = await getVideosNames(server, token, 'all-local')
94 for (const names of namesResults) {
95 expect(names).to.have.lengthOf(3)
96
a1587156
C
97 expect(names[0]).to.equal('public ' + server.serverNumber)
98 expect(names[1]).to.equal('unlisted ' + server.serverNumber)
99 expect(names[2]).to.equal('private ' + server.serverNumber)
1cd3facc
C
100 }
101 }
102 }
103 })
0aa52e17
C
104
105 it('Should display all videos by the admin or the moderator', async function () {
106 for (const server of servers) {
107 for (const token of [ server.accessToken, server['moderatorAccessToken'] ]) {
108
109 const [ channelVideos, accountVideos, videos, searchVideos ] = await getVideosNames(server, token, 'all')
110 expect(channelVideos).to.have.lengthOf(3)
111 expect(accountVideos).to.have.lengthOf(3)
112
113 expect(videos).to.have.lengthOf(5)
114 expect(searchVideos).to.have.lengthOf(5)
115 }
116 }
117 })
1cd3facc
C
118 })
119
7c3b7976
C
120 after(async function () {
121 await cleanupTests(servers)
1cd3facc
C
122 })
123})