]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/tests/api/search/search-activitypub-videos.ts
Merge branch 'release/4.2.0' into develop
[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 { wait } from '@shared/core-utils'
6 import { VideoPrivacy } from '@shared/models'
7 import {
8 cleanupTests,
9 createMultipleServers,
10 PeerTubeServer,
11 SearchCommand,
12 setAccessTokensToServers,
13 setDefaultAccountAvatar,
14 setDefaultVideoChannel,
15 waitJobs
16 } from '@shared/server-commands'
17
18 const expect = chai.expect
19
20 describe('Test ActivityPub videos search', function () {
21 let servers: PeerTubeServer[]
22 let videoServer1UUID: string
23 let videoServer2UUID: string
24
25 let command: SearchCommand
26
27 before(async function () {
28 this.timeout(120000)
29
30 servers = await createMultipleServers(2)
31
32 await setAccessTokensToServers(servers)
33 await setDefaultVideoChannel(servers)
34 await setDefaultAccountAvatar(servers)
35
36 {
37 const { uuid } = await servers[0].videos.upload({ attributes: { name: 'video 1 on server 1' } })
38 videoServer1UUID = uuid
39 }
40
41 {
42 const { uuid } = await servers[1].videos.upload({ attributes: { name: 'video 1 on server 2' } })
43 videoServer2UUID = uuid
44 }
45
46 await waitJobs(servers)
47
48 command = servers[0].search
49 })
50
51 it('Should not find a remote video', async function () {
52 {
53 const search = servers[1].url + '/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 = servers[1].url + '/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 = servers[0].url + '/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 = servers[0].url + '/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 local video with a query in URL', async function () {
96 const searches = [
97 servers[0].url + '/w/' + videoServer1UUID,
98 servers[0].url + '/videos/watch/' + videoServer1UUID
99 ]
100
101 for (const search of searches) {
102 for (const token of [ undefined, servers[0].accessToken ]) {
103 const body = await command.searchVideos({ search: search + '?startTime=4', token })
104
105 expect(body.total).to.equal(1)
106 expect(body.data).to.be.an('array')
107 expect(body.data).to.have.lengthOf(1)
108 expect(body.data[0].name).to.equal('video 1 on server 1')
109 }
110 }
111 })
112
113 it('Should search a remote video', async function () {
114 const searches = [
115 servers[1].url + '/w/' + videoServer2UUID,
116 servers[1].url + '/videos/watch/' + videoServer2UUID
117 ]
118
119 for (const search of searches) {
120 const body = await command.searchVideos({ search, token: servers[0].accessToken })
121
122 expect(body.total).to.equal(1)
123 expect(body.data).to.be.an('array')
124 expect(body.data).to.have.lengthOf(1)
125 expect(body.data[0].name).to.equal('video 1 on server 2')
126 }
127 })
128
129 it('Should not list this remote video', async function () {
130 const { total, data } = await servers[0].videos.list()
131 expect(total).to.equal(1)
132 expect(data).to.have.lengthOf(1)
133 expect(data[0].name).to.equal('video 1 on server 1')
134 })
135
136 it('Should update video of server 2, and refresh it on server 1', async function () {
137 this.timeout(120000)
138
139 const channelAttributes = {
140 name: 'super_channel',
141 displayName: 'super channel'
142 }
143 const created = await servers[1].channels.create({ attributes: channelAttributes })
144 const videoChannelId = created.id
145
146 const attributes = {
147 name: 'updated',
148 tag: [ 'tag1', 'tag2' ],
149 privacy: VideoPrivacy.UNLISTED,
150 channelId: videoChannelId
151 }
152 await servers[1].videos.update({ id: videoServer2UUID, attributes })
153
154 await waitJobs(servers)
155 // Expire video
156 await wait(10000)
157
158 // Will run refresh async
159 const search = servers[1].url + '/videos/watch/' + videoServer2UUID
160 await command.searchVideos({ search, token: servers[0].accessToken })
161
162 // Wait refresh
163 await wait(5000)
164
165 const body = await command.searchVideos({ search, token: servers[0].accessToken })
166 expect(body.total).to.equal(1)
167 expect(body.data).to.have.lengthOf(1)
168
169 const video = body.data[0]
170 expect(video.name).to.equal('updated')
171 expect(video.channel.name).to.equal('super_channel')
172 expect(video.privacy.id).to.equal(VideoPrivacy.UNLISTED)
173 })
174
175 it('Should delete video of server 2, and delete it on server 1', async function () {
176 this.timeout(120000)
177
178 await servers[1].videos.remove({ id: videoServer2UUID })
179
180 await waitJobs(servers)
181 // Expire video
182 await wait(10000)
183
184 // Will run refresh async
185 const search = servers[1].url + '/videos/watch/' + videoServer2UUID
186 await command.searchVideos({ search, token: servers[0].accessToken })
187
188 // Wait refresh
189 await wait(5000)
190
191 const body = await command.searchVideos({ search, token: servers[0].accessToken })
192 expect(body.total).to.equal(0)
193 expect(body.data).to.have.lengthOf(0)
194 })
195
196 after(async function () {
197 await cleanupTests(servers)
198 })
199 })