]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/tests/api/search/search-activitypub-video-playlists.ts
Introduce playlist command
[github/Chocobozzz/PeerTube.git] / server / tests / api / search / search-activitypub-video-playlists.ts
1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
2
3 import 'mocha'
4 import * as chai from 'chai'
5 import {
6 cleanupTests,
7 flushAndRunMultipleServers,
8 SearchCommand,
9 ServerInfo,
10 setAccessTokensToServers,
11 setDefaultVideoChannel,
12 uploadVideoAndGetId,
13 wait,
14 waitJobs
15 } from '@shared/extra-utils'
16 import { VideoPlaylistPrivacy } from '@shared/models'
17
18 const expect = chai.expect
19
20 describe('Test ActivityPub playlists search', function () {
21 let servers: ServerInfo[]
22 let playlistServer1UUID: string
23 let playlistServer2UUID: string
24 let video2Server2: string
25
26 let command: SearchCommand
27
28 before(async function () {
29 this.timeout(120000)
30
31 servers = await flushAndRunMultipleServers(2)
32
33 await setAccessTokensToServers(servers)
34 await setDefaultVideoChannel(servers)
35
36 {
37 const video1 = (await uploadVideoAndGetId({ server: servers[0], videoName: 'video 1' })).uuid
38 const video2 = (await uploadVideoAndGetId({ server: servers[0], videoName: 'video 2' })).uuid
39
40 const attributes = {
41 displayName: 'playlist 1 on server 1',
42 privacy: VideoPlaylistPrivacy.PUBLIC,
43 videoChannelId: servers[0].videoChannel.id
44 }
45 const created = await servers[0].playlistsCommand.create({ attributes })
46 playlistServer1UUID = created.uuid
47
48 for (const videoId of [ video1, video2 ]) {
49 await servers[0].playlistsCommand.addElement({ playlistId: playlistServer1UUID, attributes: { videoId } })
50 }
51 }
52
53 {
54 const videoId = (await uploadVideoAndGetId({ server: servers[1], videoName: 'video 1' })).uuid
55 video2Server2 = (await uploadVideoAndGetId({ server: servers[1], videoName: 'video 2' })).uuid
56
57 const attributes = {
58 displayName: 'playlist 1 on server 2',
59 privacy: VideoPlaylistPrivacy.PUBLIC,
60 videoChannelId: servers[1].videoChannel.id
61 }
62 const created = await servers[1].playlistsCommand.create({ attributes })
63 playlistServer2UUID = created.uuid
64
65 await servers[1].playlistsCommand.addElement({ playlistId: playlistServer2UUID, attributes: { videoId } })
66 }
67
68 await waitJobs(servers)
69
70 command = servers[0].searchCommand
71 })
72
73 it('Should not find a remote playlist', async function () {
74 {
75 const search = 'http://localhost:' + servers[1].port + '/video-playlists/43'
76 const body = await command.searchPlaylists({ search, token: servers[0].accessToken })
77
78 expect(body.total).to.equal(0)
79 expect(body.data).to.be.an('array')
80 expect(body.data).to.have.lengthOf(0)
81 }
82
83 {
84 // Without token
85 const search = 'http://localhost:' + servers[1].port + '/video-playlists/' + playlistServer2UUID
86 const body = await command.searchPlaylists({ search })
87
88 expect(body.total).to.equal(0)
89 expect(body.data).to.be.an('array')
90 expect(body.data).to.have.lengthOf(0)
91 }
92 })
93
94 it('Should search a local playlist', async function () {
95 const search = 'http://localhost:' + servers[0].port + '/video-playlists/' + playlistServer1UUID
96 const body = await command.searchPlaylists({ search })
97
98 expect(body.total).to.equal(1)
99 expect(body.data).to.be.an('array')
100 expect(body.data).to.have.lengthOf(1)
101 expect(body.data[0].displayName).to.equal('playlist 1 on server 1')
102 expect(body.data[0].videosLength).to.equal(2)
103 })
104
105 it('Should search a local playlist with an alternative URL', async function () {
106 const searches = [
107 'http://localhost:' + servers[0].port + '/videos/watch/playlist/' + playlistServer1UUID,
108 'http://localhost:' + servers[0].port + '/w/p/' + playlistServer1UUID
109 ]
110
111 for (const search of searches) {
112 for (const token of [ undefined, servers[0].accessToken ]) {
113 const body = await command.searchPlaylists({ search, token })
114
115 expect(body.total).to.equal(1)
116 expect(body.data).to.be.an('array')
117 expect(body.data).to.have.lengthOf(1)
118 expect(body.data[0].displayName).to.equal('playlist 1 on server 1')
119 expect(body.data[0].videosLength).to.equal(2)
120 }
121 }
122 })
123
124 it('Should search a remote playlist', async function () {
125 const searches = [
126 'http://localhost:' + servers[1].port + '/video-playlists/' + playlistServer2UUID,
127 'http://localhost:' + servers[1].port + '/videos/watch/playlist/' + playlistServer2UUID,
128 'http://localhost:' + servers[1].port + '/w/p/' + playlistServer2UUID
129 ]
130
131 for (const search of searches) {
132 const body = await command.searchPlaylists({ search, token: servers[0].accessToken })
133
134 expect(body.total).to.equal(1)
135 expect(body.data).to.be.an('array')
136 expect(body.data).to.have.lengthOf(1)
137 expect(body.data[0].displayName).to.equal('playlist 1 on server 2')
138 expect(body.data[0].videosLength).to.equal(1)
139 }
140 })
141
142 it('Should not list this remote playlist', async function () {
143 const body = await servers[0].playlistsCommand.list({ start: 0, count: 10 })
144 expect(body.total).to.equal(1)
145 expect(body.data).to.have.lengthOf(1)
146 expect(body.data[0].displayName).to.equal('playlist 1 on server 1')
147 })
148
149 it('Should update the playlist of server 2, and refresh it on server 1', async function () {
150 this.timeout(60000)
151
152 await servers[1].playlistsCommand.addElement({ playlistId: playlistServer2UUID, attributes: { videoId: video2Server2 } })
153
154 await waitJobs(servers)
155 // Expire playlist
156 await wait(10000)
157
158 // Will run refresh async
159 const search = 'http://localhost:' + servers[1].port + '/video-playlists/' + playlistServer2UUID
160 await command.searchPlaylists({ search, token: servers[0].accessToken })
161
162 // Wait refresh
163 await wait(5000)
164
165 const body = await command.searchPlaylists({ search, token: servers[0].accessToken })
166 expect(body.total).to.equal(1)
167 expect(body.data).to.have.lengthOf(1)
168
169 const playlist = body.data[0]
170 expect(playlist.videosLength).to.equal(2)
171 })
172
173 it('Should delete playlist of server 2, and delete it on server 1', async function () {
174 this.timeout(60000)
175
176 await servers[1].playlistsCommand.delete({ playlistId: playlistServer2UUID })
177
178 await waitJobs(servers)
179 // Expiration
180 await wait(10000)
181
182 // Will run refresh async
183 const search = 'http://localhost:' + servers[1].port + '/video-playlists/' + playlistServer2UUID
184 await command.searchPlaylists({ search, token: servers[0].accessToken })
185
186 // Wait refresh
187 await wait(5000)
188
189 const body = await command.searchPlaylists({ search, token: servers[0].accessToken })
190 expect(body.total).to.equal(0)
191 expect(body.data).to.have.lengthOf(0)
192 })
193
194 after(async function () {
195 await cleanupTests(servers)
196 })
197 })