Merge branch 'release/v1.3.0' into develop
[oweals/peertube.git] / server / tests / api / search / search-activitypub-videos.ts
1 /* tslint:disable:no-unused-expression */
2
3 import * as chai from 'chai'
4 import 'mocha'
5 import {
6   addVideoChannel,
7   cleanupTests,
8   flushAndRunMultipleServers,
9   getVideosList,
10   removeVideo,
11   searchVideo,
12   searchVideoWithToken,
13   ServerInfo,
14   setAccessTokensToServers,
15   updateVideo,
16   uploadVideo,
17   wait
18 } from '../../../../shared/extra-utils'
19 import { waitJobs } from '../../../../shared/extra-utils/server/jobs'
20 import { Video, VideoPrivacy } from '../../../../shared/models/videos'
21
22 const expect = chai.expect
23
24 describe('Test ActivityPub videos search', function () {
25   let servers: ServerInfo[]
26   let videoServer1UUID: string
27   let videoServer2UUID: string
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
49   it('Should not find a remote video', async function () {
50     {
51       const search = 'http://localhost:' + servers[1].port + '/videos/watch/43'
52       const res = await searchVideoWithToken(servers[ 0 ].url, search, servers[ 0 ].accessToken)
53
54       expect(res.body.total).to.equal(0)
55       expect(res.body.data).to.be.an('array')
56       expect(res.body.data).to.have.lengthOf(0)
57     }
58
59     {
60       // Without token
61       const search = 'http://localhost:' + servers[1].port + '/videos/watch/' + videoServer2UUID
62       const res = await searchVideo(servers[0].url, search)
63
64       expect(res.body.total).to.equal(0)
65       expect(res.body.data).to.be.an('array')
66       expect(res.body.data).to.have.lengthOf(0)
67     }
68   })
69
70   it('Should search a local video', async function () {
71     const search = 'http://localhost:' + servers[0].port + '/videos/watch/' + videoServer1UUID
72     const res = await searchVideo(servers[0].url, search)
73
74     expect(res.body.total).to.equal(1)
75     expect(res.body.data).to.be.an('array')
76     expect(res.body.data).to.have.lengthOf(1)
77     expect(res.body.data[0].name).to.equal('video 1 on server 1')
78   })
79
80   it('Should search a remote video', async function () {
81     const search = 'http://localhost:' + servers[1].port + '/videos/watch/' + videoServer2UUID
82     const res = await searchVideoWithToken(servers[0].url, search, servers[0].accessToken)
83
84     expect(res.body.total).to.equal(1)
85     expect(res.body.data).to.be.an('array')
86     expect(res.body.data).to.have.lengthOf(1)
87     expect(res.body.data[0].name).to.equal('video 1 on server 2')
88   })
89
90   it('Should not list this remote video', async function () {
91     const res = await getVideosList(servers[0].url)
92     expect(res.body.total).to.equal(1)
93     expect(res.body.data).to.have.lengthOf(1)
94     expect(res.body.data[0].name).to.equal('video 1 on server 1')
95   })
96
97   it('Should update video of server 2, and refresh it on server 1', async function () {
98     this.timeout(60000)
99
100     const channelAttributes = {
101       name: 'super_channel',
102       displayName: 'super channel'
103     }
104     const resChannel = await addVideoChannel(servers[1].url, servers[1].accessToken, channelAttributes)
105     const videoChannelId = resChannel.body.videoChannel.id
106
107     const attributes = {
108       name: 'updated',
109       tag: [ 'tag1', 'tag2' ],
110       privacy: VideoPrivacy.UNLISTED,
111       channelId: videoChannelId
112     }
113     await updateVideo(servers[1].url, servers[1].accessToken, videoServer2UUID, attributes)
114
115     await waitJobs(servers)
116     // Expire video
117     await wait(10000)
118
119     // Will run refresh async
120     const search = 'http://localhost:' + servers[1].port + '/videos/watch/' + videoServer2UUID
121     await searchVideoWithToken(servers[0].url, search, servers[0].accessToken)
122
123     // Wait refresh
124     await wait(5000)
125
126     const res = await searchVideoWithToken(servers[0].url, search, servers[0].accessToken)
127     expect(res.body.total).to.equal(1)
128     expect(res.body.data).to.have.lengthOf(1)
129
130     const video: Video = res.body.data[0]
131     expect(video.name).to.equal('updated')
132     expect(video.channel.name).to.equal('super_channel')
133     expect(video.privacy.id).to.equal(VideoPrivacy.UNLISTED)
134   })
135
136   it('Should delete video of server 2, and delete it on server 1', async function () {
137     this.timeout(60000)
138
139     await removeVideo(servers[1].url, servers[1].accessToken, videoServer2UUID)
140
141     await waitJobs(servers)
142     // Expire video
143     await wait(10000)
144
145     // Will run refresh async
146     const search = 'http://localhost:' + servers[1].port + '/videos/watch/' + videoServer2UUID
147     await searchVideoWithToken(servers[0].url, search, servers[0].accessToken)
148
149     // Wait refresh
150     await wait(5000)
151
152     const res = await searchVideoWithToken(servers[0].url, search, servers[0].accessToken)
153     expect(res.body.total).to.equal(0)
154     expect(res.body.data).to.have.lengthOf(0)
155   })
156
157   after(async function () {
158     await cleanupTests(servers)
159   })
160 })