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