]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/tests/api/search/search-activitypub-video-channels.ts
3cba2b0191c69929b020bef36596367737ccc469
[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 {
6 cleanupTests,
7 createUser,
8 flushAndRunMultipleServers,
9 getVideoChannelVideos,
10 SearchCommand,
11 ServerInfo,
12 setAccessTokensToServers,
13 updateMyUser,
14 updateVideo,
15 uploadVideo,
16 wait,
17 waitJobs
18 } from '@shared/extra-utils'
19 import { VideoChannel } from '@shared/models'
20
21 const expect = chai.expect
22
23 describe('Test ActivityPub video channels search', function () {
24 let servers: ServerInfo[]
25 let userServer2Token: string
26 let videoServer2UUID: string
27 let channelIdServer2: number
28 let command: SearchCommand
29
30 before(async function () {
31 this.timeout(120000)
32
33 servers = await flushAndRunMultipleServers(2)
34
35 await setAccessTokensToServers(servers)
36
37 {
38 await createUser({ url: servers[0].url, accessToken: servers[0].accessToken, username: 'user1_server1', password: 'password' })
39 const channel = {
40 name: 'channel1_server1',
41 displayName: 'Channel 1 server 1'
42 }
43 await servers[0].channelsCommand.create({ attributes: channel })
44 }
45
46 {
47 const user = { username: 'user1_server2', password: 'password' }
48 await createUser({ url: servers[1].url, accessToken: servers[1].accessToken, username: user.username, password: user.password })
49 userServer2Token = await servers[1].loginCommand.getAccessToken(user)
50
51 const channel = {
52 name: 'channel1_server2',
53 displayName: 'Channel 1 server 2'
54 }
55 const created = await servers[1].channelsCommand.create({ token: userServer2Token, attributes: channel })
56 channelIdServer2 = created.id
57
58 const res = await uploadVideo(servers[1].url, userServer2Token, { name: 'video 1 server 2', channelId: channelIdServer2 })
59 videoServer2UUID = res.body.video.uuid
60 }
61
62 await waitJobs(servers)
63
64 command = servers[0].searchCommand
65 })
66
67 it('Should not find a remote video channel', async function () {
68 this.timeout(15000)
69
70 {
71 const search = 'http://localhost:' + servers[1].port + '/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 = 'http://localhost:' + servers[1].port + '/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 'http://localhost:' + servers[0].port + '/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 = 'http://localhost:' + servers[0].port + '/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 remote video channel with URL or handle', async function () {
122 const searches = [
123 'http://localhost:' + servers[1].port + '/video-channels/channel1_server2',
124 'http://localhost:' + servers[1].port + '/c/channel1_server2',
125 'http://localhost:' + servers[1].port + '/c/channel1_server2/videos',
126 'channel1_server2@localhost:' + servers[1].port
127 ]
128
129 for (const search of searches) {
130 const body = await command.searchChannels({ search, token: servers[0].accessToken })
131
132 expect(body.total).to.equal(1)
133 expect(body.data).to.be.an('array')
134 expect(body.data).to.have.lengthOf(1)
135 expect(body.data[0].name).to.equal('channel1_server2')
136 expect(body.data[0].displayName).to.equal('Channel 1 server 2')
137 }
138 })
139
140 it('Should not list this remote video channel', async function () {
141 const body = await servers[0].channelsCommand.list()
142 expect(body.total).to.equal(3)
143 expect(body.data).to.have.lengthOf(3)
144 expect(body.data[0].name).to.equal('channel1_server1')
145 expect(body.data[1].name).to.equal('user1_server1_channel')
146 expect(body.data[2].name).to.equal('root_channel')
147 })
148
149 it('Should list video channel videos of server 2 without token', async function () {
150 this.timeout(30000)
151
152 await waitJobs(servers)
153
154 const res = await getVideoChannelVideos(servers[0].url, null, 'channel1_server2@localhost:' + servers[1].port, 0, 5)
155 expect(res.body.total).to.equal(0)
156 expect(res.body.data).to.have.lengthOf(0)
157 })
158
159 it('Should list video channel videos of server 2 with token', async function () {
160 const res = await getVideoChannelVideos(servers[0].url, servers[0].accessToken, 'channel1_server2@localhost:' + servers[1].port, 0, 5)
161
162 expect(res.body.total).to.equal(1)
163 expect(res.body.data[0].name).to.equal('video 1 server 2')
164 })
165
166 it('Should update video channel of server 2, and refresh it on server 1', async function () {
167 this.timeout(60000)
168
169 await servers[1].channelsCommand.update({
170 token: userServer2Token,
171 channelName: 'channel1_server2',
172 attributes: { displayName: 'channel updated' }
173 })
174 await updateMyUser({ url: servers[1].url, accessToken: userServer2Token, displayName: 'user updated' })
175
176 await waitJobs(servers)
177 // Expire video channel
178 await wait(10000)
179
180 const search = 'http://localhost:' + servers[1].port + '/video-channels/channel1_server2'
181 const body = await command.searchChannels({ search, token: servers[0].accessToken })
182 expect(body.total).to.equal(1)
183 expect(body.data).to.have.lengthOf(1)
184
185 const videoChannel: VideoChannel = body.data[0]
186 expect(videoChannel.displayName).to.equal('channel updated')
187
188 // We don't return the owner account for now
189 // expect(videoChannel.ownerAccount.displayName).to.equal('user updated')
190 })
191
192 it('Should update and add a video on server 2, and update it on server 1 after a search', async function () {
193 this.timeout(60000)
194
195 await updateVideo(servers[1].url, userServer2Token, videoServer2UUID, { name: 'video 1 updated' })
196 await uploadVideo(servers[1].url, userServer2Token, { name: 'video 2 server 2', channelId: channelIdServer2 })
197
198 await waitJobs(servers)
199
200 // Expire video channel
201 await wait(10000)
202
203 const search = 'http://localhost:' + servers[1].port + '/video-channels/channel1_server2'
204 await command.searchChannels({ search, token: servers[0].accessToken })
205
206 await waitJobs(servers)
207
208 const videoChannelName = 'channel1_server2@localhost:' + servers[1].port
209 const res = await getVideoChannelVideos(servers[0].url, servers[0].accessToken, videoChannelName, 0, 5, '-createdAt')
210
211 expect(res.body.total).to.equal(2)
212 expect(res.body.data[0].name).to.equal('video 2 server 2')
213 expect(res.body.data[1].name).to.equal('video 1 updated')
214 })
215
216 it('Should delete video channel of server 2, and delete it on server 1', async function () {
217 this.timeout(60000)
218
219 await servers[1].channelsCommand.delete({ token: userServer2Token, channelName: 'channel1_server2' })
220
221 await waitJobs(servers)
222 // Expire video
223 await wait(10000)
224
225 const search = 'http://localhost:' + servers[1].port + '/video-channels/channel1_server2'
226 const body = await command.searchChannels({ search, token: servers[0].accessToken })
227 expect(body.total).to.equal(0)
228 expect(body.data).to.have.lengthOf(0)
229 })
230
231 after(async function () {
232 await cleanupTests(servers)
233 })
234 })