1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
3 import { expect } from 'chai'
4 import { wait } from '@shared/core-utils'
5 import { VideoChannel } from '@shared/models'
11 setAccessTokensToServers,
12 setDefaultAccountAvatar,
13 setDefaultVideoChannel,
15 } from '@shared/server-commands'
17 describe('Test ActivityPub video channels search', function () {
18 let servers: PeerTubeServer[]
19 let userServer2Token: string
20 let videoServer2UUID: string
21 let channelIdServer2: number
22 let command: SearchCommand
24 before(async function () {
27 servers = await createMultipleServers(2)
29 await setAccessTokensToServers(servers)
30 await setDefaultVideoChannel(servers)
31 await setDefaultAccountAvatar(servers)
34 await servers[0].users.create({ username: 'user1_server1', password: 'password' })
36 name: 'channel1_server1',
37 displayName: 'Channel 1 server 1'
39 await servers[0].channels.create({ attributes: channel })
43 const user = { username: 'user1_server2', password: 'password' }
44 await servers[1].users.create({ username: user.username, password: user.password })
45 userServer2Token = await servers[1].login.getAccessToken(user)
48 name: 'channel1_server2',
49 displayName: 'Channel 1 server 2'
51 const created = await servers[1].channels.create({ token: userServer2Token, attributes: channel })
52 channelIdServer2 = created.id
54 const attributes = { name: 'video 1 server 2', channelId: channelIdServer2 }
55 const { uuid } = await servers[1].videos.upload({ token: userServer2Token, attributes })
56 videoServer2UUID = uuid
59 await waitJobs(servers)
61 command = servers[0].search
64 it('Should not find a remote video channel', async function () {
68 const search = servers[1].url + '/video-channels/channel1_server3'
69 const body = await command.searchChannels({ search, token: servers[0].accessToken })
71 expect(body.total).to.equal(0)
72 expect(body.data).to.be.an('array')
73 expect(body.data).to.have.lengthOf(0)
78 const search = servers[1].url + '/video-channels/channel1_server2'
79 const body = await command.searchChannels({ search })
81 expect(body.total).to.equal(0)
82 expect(body.data).to.be.an('array')
83 expect(body.data).to.have.lengthOf(0)
87 it('Should search a local video channel', async function () {
89 servers[0].url + '/video-channels/channel1_server1',
90 'channel1_server1@localhost:' + servers[0].port
93 for (const search of searches) {
94 const body = await command.searchChannels({ search })
96 expect(body.total).to.equal(1)
97 expect(body.data).to.be.an('array')
98 expect(body.data).to.have.lengthOf(1)
99 expect(body.data[0].name).to.equal('channel1_server1')
100 expect(body.data[0].displayName).to.equal('Channel 1 server 1')
104 it('Should search a local video channel with an alternative URL', async function () {
105 const search = servers[0].url + '/c/channel1_server1'
107 for (const token of [ undefined, servers[0].accessToken ]) {
108 const body = await command.searchChannels({ search, token })
110 expect(body.total).to.equal(1)
111 expect(body.data).to.be.an('array')
112 expect(body.data).to.have.lengthOf(1)
113 expect(body.data[0].name).to.equal('channel1_server1')
114 expect(body.data[0].displayName).to.equal('Channel 1 server 1')
118 it('Should search a local video channel with a query in URL', async function () {
120 servers[0].url + '/video-channels/channel1_server1',
121 servers[0].url + '/c/channel1_server1'
124 for (const search of searches) {
125 for (const token of [ undefined, servers[0].accessToken ]) {
126 const body = await command.searchChannels({ search: search + '?param=2', token })
128 expect(body.total).to.equal(1)
129 expect(body.data).to.be.an('array')
130 expect(body.data).to.have.lengthOf(1)
131 expect(body.data[0].name).to.equal('channel1_server1')
132 expect(body.data[0].displayName).to.equal('Channel 1 server 1')
137 it('Should search a remote video channel with URL or handle', async function () {
139 servers[1].url + '/video-channels/channel1_server2',
140 servers[1].url + '/c/channel1_server2',
141 servers[1].url + '/c/channel1_server2/videos',
142 'channel1_server2@localhost:' + servers[1].port
145 for (const search of searches) {
146 const body = await command.searchChannels({ search, token: servers[0].accessToken })
148 expect(body.total).to.equal(1)
149 expect(body.data).to.be.an('array')
150 expect(body.data).to.have.lengthOf(1)
151 expect(body.data[0].name).to.equal('channel1_server2')
152 expect(body.data[0].displayName).to.equal('Channel 1 server 2')
156 it('Should not list this remote video channel', async function () {
157 const body = await servers[0].channels.list()
158 expect(body.total).to.equal(3)
159 expect(body.data).to.have.lengthOf(3)
160 expect(body.data[0].name).to.equal('channel1_server1')
161 expect(body.data[1].name).to.equal('user1_server1_channel')
162 expect(body.data[2].name).to.equal('root_channel')
165 it('Should list video channel videos of server 2 without token', async function () {
168 await waitJobs(servers)
170 const { total, data } = await servers[0].videos.listByChannel({
172 handle: 'channel1_server2@localhost:' + servers[1].port
174 expect(total).to.equal(0)
175 expect(data).to.have.lengthOf(0)
178 it('Should list video channel videos of server 2 with token', async function () {
179 const { total, data } = await servers[0].videos.listByChannel({
180 handle: 'channel1_server2@localhost:' + servers[1].port
183 expect(total).to.equal(1)
184 expect(data[0].name).to.equal('video 1 server 2')
187 it('Should update video channel of server 2, and refresh it on server 1', async function () {
190 await servers[1].channels.update({
191 token: userServer2Token,
192 channelName: 'channel1_server2',
193 attributes: { displayName: 'channel updated' }
195 await servers[1].users.updateMe({ token: userServer2Token, displayName: 'user updated' })
197 await waitJobs(servers)
198 // Expire video channel
201 const search = servers[1].url + '/video-channels/channel1_server2'
202 const body = await command.searchChannels({ search, token: servers[0].accessToken })
203 expect(body.total).to.equal(1)
204 expect(body.data).to.have.lengthOf(1)
206 const videoChannel: VideoChannel = body.data[0]
207 expect(videoChannel.displayName).to.equal('channel updated')
209 // We don't return the owner account for now
210 // expect(videoChannel.ownerAccount.displayName).to.equal('user updated')
213 it('Should update and add a video on server 2, and update it on server 1 after a search', async function () {
216 await servers[1].videos.update({ token: userServer2Token, id: videoServer2UUID, attributes: { name: 'video 1 updated' } })
217 await servers[1].videos.upload({ token: userServer2Token, attributes: { name: 'video 2 server 2', channelId: channelIdServer2 } })
219 await waitJobs(servers)
221 // Expire video channel
224 const search = servers[1].url + '/video-channels/channel1_server2'
225 await command.searchChannels({ search, token: servers[0].accessToken })
227 await waitJobs(servers)
229 const handle = 'channel1_server2@localhost:' + servers[1].port
230 const { total, data } = await servers[0].videos.listByChannel({ handle, sort: '-createdAt' })
232 expect(total).to.equal(2)
233 expect(data[0].name).to.equal('video 2 server 2')
234 expect(data[1].name).to.equal('video 1 updated')
237 it('Should delete video channel of server 2, and delete it on server 1', async function () {
240 await servers[1].channels.delete({ token: userServer2Token, channelName: 'channel1_server2' })
242 await waitJobs(servers)
246 const search = servers[1].url + '/video-channels/channel1_server2'
247 const body = await command.searchChannels({ search, token: servers[0].accessToken })
248 expect(body.total).to.equal(0)
249 expect(body.data).to.have.lengthOf(0)
252 after(async function () {
253 await cleanupTests(servers)