]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/tests/api/search/search-activitypub-video-channels.ts
Merge branch 'release/4.1.0' into develop
[github/Chocobozzz/PeerTube.git] / server / tests / api / search / search-activitypub-video-channels.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 { wait } from '@shared/core-utils'
6 import { VideoChannel } from '@shared/models'
7 import {
8 cleanupTests,
9 createMultipleServers,
10 PeerTubeServer,
11 SearchCommand,
12 setAccessTokensToServers,
13 setDefaultAccountAvatar,
14 setDefaultVideoChannel,
15 waitJobs
16 } from '@shared/server-commands'
17
18 const expect = chai.expect
19
20 describe('Test ActivityPub video channels search', function () {
21 let servers: PeerTubeServer[]
22 let userServer2Token: string
23 let videoServer2UUID: string
24 let channelIdServer2: number
25 let command: SearchCommand
26
27 before(async function () {
28 this.timeout(120000)
29
30 servers = await createMultipleServers(2)
31
32 await setAccessTokensToServers(servers)
33 await setDefaultVideoChannel(servers)
34 await setDefaultAccountAvatar(servers)
35
36 {
37 await servers[0].users.create({ username: 'user1_server1', password: 'password' })
38 const channel = {
39 name: 'channel1_server1',
40 displayName: 'Channel 1 server 1'
41 }
42 await servers[0].channels.create({ attributes: channel })
43 }
44
45 {
46 const user = { username: 'user1_server2', password: 'password' }
47 await servers[1].users.create({ username: user.username, password: user.password })
48 userServer2Token = await servers[1].login.getAccessToken(user)
49
50 const channel = {
51 name: 'channel1_server2',
52 displayName: 'Channel 1 server 2'
53 }
54 const created = await servers[1].channels.create({ token: userServer2Token, attributes: channel })
55 channelIdServer2 = created.id
56
57 const attributes = { name: 'video 1 server 2', channelId: channelIdServer2 }
58 const { uuid } = await servers[1].videos.upload({ token: userServer2Token, attributes })
59 videoServer2UUID = uuid
60 }
61
62 await waitJobs(servers)
63
64 command = servers[0].search
65 })
66
67 it('Should not find a remote video channel', async function () {
68 this.timeout(15000)
69
70 {
71 const search = servers[1].url + '/video-channels/channel1_server3'
72 const body = await command.searchChannels({ search, token: servers[0].accessToken })
73
74 expect(body.total).to.equal(0)
75 expect(body.data).to.be.an('array')
76 expect(body.data).to.have.lengthOf(0)
77 }
78
79 {
80 // Without token
81 const search = servers[1].url + '/video-channels/channel1_server2'
82 const body = await command.searchChannels({ search })
83
84 expect(body.total).to.equal(0)
85 expect(body.data).to.be.an('array')
86 expect(body.data).to.have.lengthOf(0)
87 }
88 })
89
90 it('Should search a local video channel', async function () {
91 const searches = [
92 servers[0].url + '/video-channels/channel1_server1',
93 'channel1_server1@localhost:' + servers[0].port
94 ]
95
96 for (const search of searches) {
97 const body = await command.searchChannels({ search })
98
99 expect(body.total).to.equal(1)
100 expect(body.data).to.be.an('array')
101 expect(body.data).to.have.lengthOf(1)
102 expect(body.data[0].name).to.equal('channel1_server1')
103 expect(body.data[0].displayName).to.equal('Channel 1 server 1')
104 }
105 })
106
107 it('Should search a local video channel with an alternative URL', async function () {
108 const search = servers[0].url + '/c/channel1_server1'
109
110 for (const token of [ undefined, servers[0].accessToken ]) {
111 const body = await command.searchChannels({ search, token })
112
113 expect(body.total).to.equal(1)
114 expect(body.data).to.be.an('array')
115 expect(body.data).to.have.lengthOf(1)
116 expect(body.data[0].name).to.equal('channel1_server1')
117 expect(body.data[0].displayName).to.equal('Channel 1 server 1')
118 }
119 })
120
121 it('Should search a local video channel with a query in URL', async function () {
122 const searches = [
123 servers[0].url + '/video-channels/channel1_server1',
124 servers[0].url + '/c/channel1_server1'
125 ]
126
127 for (const search of searches) {
128 for (const token of [ undefined, servers[0].accessToken ]) {
129 const body = await command.searchChannels({ search: search + '?param=2', token })
130
131 expect(body.total).to.equal(1)
132 expect(body.data).to.be.an('array')
133 expect(body.data).to.have.lengthOf(1)
134 expect(body.data[0].name).to.equal('channel1_server1')
135 expect(body.data[0].displayName).to.equal('Channel 1 server 1')
136 }
137 }
138 })
139
140 it('Should search a remote video channel with URL or handle', async function () {
141 const searches = [
142 servers[1].url + '/video-channels/channel1_server2',
143 servers[1].url + '/c/channel1_server2',
144 servers[1].url + '/c/channel1_server2/videos',
145 'channel1_server2@localhost:' + servers[1].port
146 ]
147
148 for (const search of searches) {
149 const body = await command.searchChannels({ search, token: servers[0].accessToken })
150
151 expect(body.total).to.equal(1)
152 expect(body.data).to.be.an('array')
153 expect(body.data).to.have.lengthOf(1)
154 expect(body.data[0].name).to.equal('channel1_server2')
155 expect(body.data[0].displayName).to.equal('Channel 1 server 2')
156 }
157 })
158
159 it('Should not list this remote video channel', async function () {
160 const body = await servers[0].channels.list()
161 expect(body.total).to.equal(3)
162 expect(body.data).to.have.lengthOf(3)
163 expect(body.data[0].name).to.equal('channel1_server1')
164 expect(body.data[1].name).to.equal('user1_server1_channel')
165 expect(body.data[2].name).to.equal('root_channel')
166 })
167
168 it('Should list video channel videos of server 2 without token', async function () {
169 this.timeout(30000)
170
171 await waitJobs(servers)
172
173 const { total, data } = await servers[0].videos.listByChannel({
174 token: null,
175 handle: 'channel1_server2@localhost:' + servers[1].port
176 })
177 expect(total).to.equal(0)
178 expect(data).to.have.lengthOf(0)
179 })
180
181 it('Should list video channel videos of server 2 with token', async function () {
182 const { total, data } = await servers[0].videos.listByChannel({
183 handle: 'channel1_server2@localhost:' + servers[1].port
184 })
185
186 expect(total).to.equal(1)
187 expect(data[0].name).to.equal('video 1 server 2')
188 })
189
190 it('Should update video channel of server 2, and refresh it on server 1', async function () {
191 this.timeout(60000)
192
193 await servers[1].channels.update({
194 token: userServer2Token,
195 channelName: 'channel1_server2',
196 attributes: { displayName: 'channel updated' }
197 })
198 await servers[1].users.updateMe({ token: userServer2Token, displayName: 'user updated' })
199
200 await waitJobs(servers)
201 // Expire video channel
202 await wait(10000)
203
204 const search = servers[1].url + '/video-channels/channel1_server2'
205 const body = await command.searchChannels({ search, token: servers[0].accessToken })
206 expect(body.total).to.equal(1)
207 expect(body.data).to.have.lengthOf(1)
208
209 const videoChannel: VideoChannel = body.data[0]
210 expect(videoChannel.displayName).to.equal('channel updated')
211
212 // We don't return the owner account for now
213 // expect(videoChannel.ownerAccount.displayName).to.equal('user updated')
214 })
215
216 it('Should update and add a video on server 2, and update it on server 1 after a search', async function () {
217 this.timeout(60000)
218
219 await servers[1].videos.update({ token: userServer2Token, id: videoServer2UUID, attributes: { name: 'video 1 updated' } })
220 await servers[1].videos.upload({ token: userServer2Token, attributes: { name: 'video 2 server 2', channelId: channelIdServer2 } })
221
222 await waitJobs(servers)
223
224 // Expire video channel
225 await wait(10000)
226
227 const search = servers[1].url + '/video-channels/channel1_server2'
228 await command.searchChannels({ search, token: servers[0].accessToken })
229
230 await waitJobs(servers)
231
232 const handle = 'channel1_server2@localhost:' + servers[1].port
233 const { total, data } = await servers[0].videos.listByChannel({ handle, sort: '-createdAt' })
234
235 expect(total).to.equal(2)
236 expect(data[0].name).to.equal('video 2 server 2')
237 expect(data[1].name).to.equal('video 1 updated')
238 })
239
240 it('Should delete video channel of server 2, and delete it on server 1', async function () {
241 this.timeout(60000)
242
243 await servers[1].channels.delete({ token: userServer2Token, channelName: 'channel1_server2' })
244
245 await waitJobs(servers)
246 // Expire video
247 await wait(10000)
248
249 const search = servers[1].url + '/video-channels/channel1_server2'
250 const body = await command.searchChannels({ search, token: servers[0].accessToken })
251 expect(body.total).to.equal(0)
252 expect(body.data).to.have.lengthOf(0)
253 })
254
255 after(async function () {
256 await cleanupTests(servers)
257 })
258 })