]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/tests/api/search/search-activitypub-videos.ts
Adapt CLI to new commands
[github/Chocobozzz/PeerTube.git] / server / tests / api / search / search-activitypub-videos.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 getVideosList,
9 removeVideo,
10 SearchCommand,
11 ServerInfo,
12 setAccessTokensToServers,
13 updateVideo,
14 uploadVideo,
15 wait
16 } from '../../../../shared/extra-utils'
17 import { waitJobs } from '../../../../shared/extra-utils/server/jobs'
18 import { VideoPrivacy } from '../../../../shared/models/videos'
19
20 const expect = chai.expect
21
22 describe('Test ActivityPub videos search', function () {
23 let servers: ServerInfo[]
24 let videoServer1UUID: string
25 let videoServer2UUID: string
26
27 let command: SearchCommand
28
29 before(async function () {
30 this.timeout(120000)
31
32 servers = await flushAndRunMultipleServers(2)
33
34 await setAccessTokensToServers(servers)
35
36 {
37 const res = await uploadVideo(servers[0].url, servers[0].accessToken, { name: 'video 1 on server 1' })
38 videoServer1UUID = res.body.video.uuid
39 }
40
41 {
42 const res = await uploadVideo(servers[1].url, servers[1].accessToken, { name: 'video 1 on server 2' })
43 videoServer2UUID = res.body.video.uuid
44 }
45
46 await waitJobs(servers)
47
48 command = servers[0].searchCommand
49 })
50
51 it('Should not find a remote video', async function () {
52 {
53 const search = 'http://localhost:' + servers[1].port + '/videos/watch/43'
54 const body = await command.searchVideos({ search, token: servers[0].accessToken })
55
56 expect(body.total).to.equal(0)
57 expect(body.data).to.be.an('array')
58 expect(body.data).to.have.lengthOf(0)
59 }
60
61 {
62 // Without token
63 const search = 'http://localhost:' + servers[1].port + '/videos/watch/' + videoServer2UUID
64 const body = await command.searchVideos({ search })
65
66 expect(body.total).to.equal(0)
67 expect(body.data).to.be.an('array')
68 expect(body.data).to.have.lengthOf(0)
69 }
70 })
71
72 it('Should search a local video', async function () {
73 const search = 'http://localhost:' + servers[0].port + '/videos/watch/' + videoServer1UUID
74 const body = await command.searchVideos({ search })
75
76 expect(body.total).to.equal(1)
77 expect(body.data).to.be.an('array')
78 expect(body.data).to.have.lengthOf(1)
79 expect(body.data[0].name).to.equal('video 1 on server 1')
80 })
81
82 it('Should search a local video with an alternative URL', async function () {
83 const search = 'http://localhost:' + servers[0].port + '/w/' + videoServer1UUID
84 const body1 = await command.searchVideos({ search })
85 const body2 = await command.searchVideos({ search, token: servers[0].accessToken })
86
87 for (const body of [ body1, body2 ]) {
88 expect(body.total).to.equal(1)
89 expect(body.data).to.be.an('array')
90 expect(body.data).to.have.lengthOf(1)
91 expect(body.data[0].name).to.equal('video 1 on server 1')
92 }
93 })
94
95 it('Should search a remote video', async function () {
96 const searches = [
97 'http://localhost:' + servers[1].port + '/w/' + videoServer2UUID,
98 'http://localhost:' + servers[1].port + '/videos/watch/' + videoServer2UUID
99 ]
100
101 for (const search of searches) {
102 const body = await command.searchVideos({ search, token: servers[0].accessToken })
103
104 expect(body.total).to.equal(1)
105 expect(body.data).to.be.an('array')
106 expect(body.data).to.have.lengthOf(1)
107 expect(body.data[0].name).to.equal('video 1 on server 2')
108 }
109 })
110
111 it('Should not list this remote video', async function () {
112 const res = await getVideosList(servers[0].url)
113 expect(res.body.total).to.equal(1)
114 expect(res.body.data).to.have.lengthOf(1)
115 expect(res.body.data[0].name).to.equal('video 1 on server 1')
116 })
117
118 it('Should update video of server 2, and refresh it on server 1', async function () {
119 this.timeout(120000)
120
121 const channelAttributes = {
122 name: 'super_channel',
123 displayName: 'super channel'
124 }
125 const created = await servers[1].channelsCommand.create({ attributes: channelAttributes })
126 const videoChannelId = created.id
127
128 const attributes = {
129 name: 'updated',
130 tag: [ 'tag1', 'tag2' ],
131 privacy: VideoPrivacy.UNLISTED,
132 channelId: videoChannelId
133 }
134 await updateVideo(servers[1].url, servers[1].accessToken, videoServer2UUID, attributes)
135
136 await waitJobs(servers)
137 // Expire video
138 await wait(10000)
139
140 // Will run refresh async
141 const search = 'http://localhost:' + servers[1].port + '/videos/watch/' + videoServer2UUID
142 await command.searchVideos({ search, token: servers[0].accessToken })
143
144 // Wait refresh
145 await wait(5000)
146
147 const body = await command.searchVideos({ search, token: servers[0].accessToken })
148 expect(body.total).to.equal(1)
149 expect(body.data).to.have.lengthOf(1)
150
151 const video = body.data[0]
152 expect(video.name).to.equal('updated')
153 expect(video.channel.name).to.equal('super_channel')
154 expect(video.privacy.id).to.equal(VideoPrivacy.UNLISTED)
155 })
156
157 it('Should delete video of server 2, and delete it on server 1', async function () {
158 this.timeout(120000)
159
160 await removeVideo(servers[1].url, servers[1].accessToken, videoServer2UUID)
161
162 await waitJobs(servers)
163 // Expire video
164 await wait(10000)
165
166 // Will run refresh async
167 const search = 'http://localhost:' + servers[1].port + '/videos/watch/' + videoServer2UUID
168 await command.searchVideos({ search, token: servers[0].accessToken })
169
170 // Wait refresh
171 await wait(5000)
172
173 const body = await command.searchVideos({ search, token: servers[0].accessToken })
174 expect(body.total).to.equal(0)
175 expect(body.data).to.have.lengthOf(0)
176 })
177
178 after(async function () {
179 await cleanupTests(servers)
180 })
181 })