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