1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
4 import * as chai from 'chai'
5 import { wait } from '@shared/core-utils'
6 import { VideoChannel } from '@shared/models'
12 setAccessTokensToServers,
13 setDefaultAccountAvatar,
14 setDefaultVideoChannel,
16 } from '@shared/server-commands'
18 const expect = chai.expect
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
27 before(async function () {
30 servers = await createMultipleServers(2)
32 await setAccessTokensToServers(servers)
33 await setDefaultVideoChannel(servers)
34 await setDefaultAccountAvatar(servers)
37 await servers[0].users.create({ username: 'user1_server1', password: 'password' })
39 name: 'channel1_server1',
40 displayName: 'Channel 1 server 1'
42 await servers[0].channels.create({ attributes: channel })
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)
51 name: 'channel1_server2',
52 displayName: 'Channel 1 server 2'
54 const created = await servers[1].channels.create({ token: userServer2Token, attributes: channel })
55 channelIdServer2 = created.id
57 const attributes = { name: 'video 1 server 2', channelId: channelIdServer2 }
58 const { uuid } = await servers[1].videos.upload({ token: userServer2Token, attributes })
59 videoServer2UUID = uuid
62 await waitJobs(servers)
64 command = servers[0].search
67 it('Should not find a remote video channel', async function () {
71 const search = servers[1].url + '/video-channels/channel1_server3'
72 const body = await command.searchChannels({ search, token: servers[0].accessToken })
74 expect(body.total).to.equal(0)
75 expect(body.data).to.be.an('array')
76 expect(body.data).to.have.lengthOf(0)
81 const search = servers[1].url + '/video-channels/channel1_server2'
82 const body = await command.searchChannels({ search })
84 expect(body.total).to.equal(0)
85 expect(body.data).to.be.an('array')
86 expect(body.data).to.have.lengthOf(0)
90 it('Should search a local video channel', async function () {
92 servers[0].url + '/video-channels/channel1_server1',
93 'channel1_server1@localhost:' + servers[0].port
96 for (const search of searches) {
97 const body = await command.searchChannels({ search })
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')
107 it('Should search a local video channel with an alternative URL', async function () {
108 const search = servers[0].url + '/c/channel1_server1'
110 for (const token of [ undefined, servers[0].accessToken ]) {
111 const body = await command.searchChannels({ search, token })
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')
121 it('Should search a local video channel with a query in URL', async function () {
123 servers[0].url + '/video-channels/channel1_server1',
124 servers[0].url + '/c/channel1_server1'
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 })
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')
140 it('Should search a remote video channel with URL or handle', async function () {
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
148 for (const search of searches) {
149 const body = await command.searchChannels({ search, token: servers[0].accessToken })
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')
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')
168 it('Should list video channel videos of server 2 without token', async function () {
171 await waitJobs(servers)
173 const { total, data } = await servers[0].videos.listByChannel({
175 handle: 'channel1_server2@localhost:' + servers[1].port
177 expect(total).to.equal(0)
178 expect(data).to.have.lengthOf(0)
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
186 expect(total).to.equal(1)
187 expect(data[0].name).to.equal('video 1 server 2')
190 it('Should update video channel of server 2, and refresh it on server 1', async function () {
193 await servers[1].channels.update({
194 token: userServer2Token,
195 channelName: 'channel1_server2',
196 attributes: { displayName: 'channel updated' }
198 await servers[1].users.updateMe({ token: userServer2Token, displayName: 'user updated' })
200 await waitJobs(servers)
201 // Expire video channel
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)
209 const videoChannel: VideoChannel = body.data[0]
210 expect(videoChannel.displayName).to.equal('channel updated')
212 // We don't return the owner account for now
213 // expect(videoChannel.ownerAccount.displayName).to.equal('user updated')
216 it('Should update and add a video on server 2, and update it on server 1 after a search', async function () {
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 } })
222 await waitJobs(servers)
224 // Expire video channel
227 const search = servers[1].url + '/video-channels/channel1_server2'
228 await command.searchChannels({ search, token: servers[0].accessToken })
230 await waitJobs(servers)
232 const handle = 'channel1_server2@localhost:' + servers[1].port
233 const { total, data } = await servers[0].videos.listByChannel({ handle, sort: '-createdAt' })
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')
240 it('Should delete video channel of server 2, and delete it on server 1', async function () {
243 await servers[1].channels.delete({ token: userServer2Token, channelName: 'channel1_server2' })
245 await waitJobs(servers)
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)
255 after(async function () {
256 await cleanupTests(servers)