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