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