1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
4 import * as chai from 'chai'
10 setAccessTokensToServers,
13 } from '@shared/server-commands'
14 import { VideoChannel } from '@shared/models'
16 const expect = chai.expect
18 describe('Test ActivityPub video channels search', function () {
19 let servers: PeerTubeServer[]
20 let userServer2Token: string
21 let videoServer2UUID: string
22 let channelIdServer2: number
23 let command: SearchCommand
25 before(async function () {
28 servers = await createMultipleServers(2)
30 await setAccessTokensToServers(servers)
33 await servers[0].users.create({ username: 'user1_server1', password: 'password' })
35 name: 'channel1_server1',
36 displayName: 'Channel 1 server 1'
38 await servers[0].channels.create({ attributes: channel })
42 const user = { username: 'user1_server2', password: 'password' }
43 await servers[1].users.create({ username: user.username, password: user.password })
44 userServer2Token = await servers[1].login.getAccessToken(user)
47 name: 'channel1_server2',
48 displayName: 'Channel 1 server 2'
50 const created = await servers[1].channels.create({ token: userServer2Token, attributes: channel })
51 channelIdServer2 = created.id
53 const attributes = { name: 'video 1 server 2', channelId: channelIdServer2 }
54 const { uuid } = await servers[1].videos.upload({ token: userServer2Token, attributes })
55 videoServer2UUID = uuid
58 await waitJobs(servers)
60 command = servers[0].search
63 it('Should not find a remote video channel', async function () {
67 const search = servers[1].url + '/video-channels/channel1_server3'
68 const body = await command.searchChannels({ search, token: servers[0].accessToken })
70 expect(body.total).to.equal(0)
71 expect(body.data).to.be.an('array')
72 expect(body.data).to.have.lengthOf(0)
77 const search = servers[1].url + '/video-channels/channel1_server2'
78 const body = await command.searchChannels({ search })
80 expect(body.total).to.equal(0)
81 expect(body.data).to.be.an('array')
82 expect(body.data).to.have.lengthOf(0)
86 it('Should search a local video channel', async function () {
88 servers[0].url + '/video-channels/channel1_server1',
89 'channel1_server1@localhost:' + servers[0].port
92 for (const search of searches) {
93 const body = await command.searchChannels({ search })
95 expect(body.total).to.equal(1)
96 expect(body.data).to.be.an('array')
97 expect(body.data).to.have.lengthOf(1)
98 expect(body.data[0].name).to.equal('channel1_server1')
99 expect(body.data[0].displayName).to.equal('Channel 1 server 1')
103 it('Should search a local video channel with an alternative URL', async function () {
104 const search = servers[0].url + '/c/channel1_server1'
106 for (const token of [ undefined, servers[0].accessToken ]) {
107 const body = await command.searchChannels({ search, token })
109 expect(body.total).to.equal(1)
110 expect(body.data).to.be.an('array')
111 expect(body.data).to.have.lengthOf(1)
112 expect(body.data[0].name).to.equal('channel1_server1')
113 expect(body.data[0].displayName).to.equal('Channel 1 server 1')
117 it('Should search a local video channel with a query in URL', async function () {
119 servers[0].url + '/video-channels/channel1_server1',
120 servers[0].url + '/c/channel1_server1'
123 for (const search of searches) {
124 for (const token of [ undefined, servers[0].accessToken ]) {
125 const body = await command.searchChannels({ search: search + '?param=2', token })
127 expect(body.total).to.equal(1)
128 expect(body.data).to.be.an('array')
129 expect(body.data).to.have.lengthOf(1)
130 expect(body.data[0].name).to.equal('channel1_server1')
131 expect(body.data[0].displayName).to.equal('Channel 1 server 1')
136 it('Should search a remote video channel with URL or handle', async function () {
138 servers[1].url + '/video-channels/channel1_server2',
139 servers[1].url + '/c/channel1_server2',
140 servers[1].url + '/c/channel1_server2/videos',
141 'channel1_server2@localhost:' + servers[1].port
144 for (const search of searches) {
145 const body = await command.searchChannels({ search, token: servers[0].accessToken })
147 expect(body.total).to.equal(1)
148 expect(body.data).to.be.an('array')
149 expect(body.data).to.have.lengthOf(1)
150 expect(body.data[0].name).to.equal('channel1_server2')
151 expect(body.data[0].displayName).to.equal('Channel 1 server 2')
155 it('Should not list this remote video channel', async function () {
156 const body = await servers[0].channels.list()
157 expect(body.total).to.equal(3)
158 expect(body.data).to.have.lengthOf(3)
159 expect(body.data[0].name).to.equal('channel1_server1')
160 expect(body.data[1].name).to.equal('user1_server1_channel')
161 expect(body.data[2].name).to.equal('root_channel')
164 it('Should list video channel videos of server 2 without token', async function () {
167 await waitJobs(servers)
169 const { total, data } = await servers[0].videos.listByChannel({
171 handle: 'channel1_server2@localhost:' + servers[1].port
173 expect(total).to.equal(0)
174 expect(data).to.have.lengthOf(0)
177 it('Should list video channel videos of server 2 with token', async function () {
178 const { total, data } = await servers[0].videos.listByChannel({
179 handle: 'channel1_server2@localhost:' + servers[1].port
182 expect(total).to.equal(1)
183 expect(data[0].name).to.equal('video 1 server 2')
186 it('Should update video channel of server 2, and refresh it on server 1', async function () {
189 await servers[1].channels.update({
190 token: userServer2Token,
191 channelName: 'channel1_server2',
192 attributes: { displayName: 'channel updated' }
194 await servers[1].users.updateMe({ token: userServer2Token, displayName: 'user updated' })
196 await waitJobs(servers)
197 // Expire video channel
200 const search = servers[1].url + '/video-channels/channel1_server2'
201 const body = await command.searchChannels({ search, token: servers[0].accessToken })
202 expect(body.total).to.equal(1)
203 expect(body.data).to.have.lengthOf(1)
205 const videoChannel: VideoChannel = body.data[0]
206 expect(videoChannel.displayName).to.equal('channel updated')
208 // We don't return the owner account for now
209 // expect(videoChannel.ownerAccount.displayName).to.equal('user updated')
212 it('Should update and add a video on server 2, and update it on server 1 after a search', async function () {
215 await servers[1].videos.update({ token: userServer2Token, id: videoServer2UUID, attributes: { name: 'video 1 updated' } })
216 await servers[1].videos.upload({ token: userServer2Token, attributes: { name: 'video 2 server 2', channelId: channelIdServer2 } })
218 await waitJobs(servers)
220 // Expire video channel
223 const search = servers[1].url + '/video-channels/channel1_server2'
224 await command.searchChannels({ search, token: servers[0].accessToken })
226 await waitJobs(servers)
228 const handle = 'channel1_server2@localhost:' + servers[1].port
229 const { total, data } = await servers[0].videos.listByChannel({ handle, sort: '-createdAt' })
231 expect(total).to.equal(2)
232 expect(data[0].name).to.equal('video 2 server 2')
233 expect(data[1].name).to.equal('video 1 updated')
236 it('Should delete video channel of server 2, and delete it on server 1', async function () {
239 await servers[1].channels.delete({ token: userServer2Token, channelName: 'channel1_server2' })
241 await waitJobs(servers)
245 const search = servers[1].url + '/video-channels/channel1_server2'
246 const body = await command.searchChannels({ search, token: servers[0].accessToken })
247 expect(body.total).to.equal(0)
248 expect(body.data).to.have.lengthOf(0)
251 after(async function () {
252 await cleanupTests(servers)