Merge branch 'master' 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   flushAndRunMultipleServers,
8   flushTests,
9   getVideosList,
10   killallServers,
11   removeVideo,
12   searchVideoWithToken,
13   ServerInfo,
14   setAccessTokensToServers,
15   updateVideo,
16   uploadVideo,
17   wait,
18   searchVideo, cleanupTests
19 } from '../../../../shared/extra-utils'
20 import { waitJobs } from '../../../../shared/extra-utils/server/jobs'
21 import { Video, VideoPrivacy } from '../../../../shared/models/videos'
22
23 const expect = chai.expect
24
25 describe('Test a ActivityPub videos search', function () {
26   let servers: ServerInfo[]
27   let videoServer1UUID: string
28   let videoServer2UUID: string
29
30   before(async function () {
31     this.timeout(120000)
32
33     servers = await flushAndRunMultipleServers(2)
34
35     await setAccessTokensToServers(servers)
36
37     {
38       const res = await uploadVideo(servers[ 0 ].url, servers[ 0 ].accessToken, { name: 'video 1 on server 1' })
39       videoServer1UUID = res.body.video.uuid
40     }
41
42     {
43       const res = await uploadVideo(servers[ 1 ].url, servers[ 1 ].accessToken, { name: 'video 1 on server 2' })
44       videoServer2UUID = res.body.video.uuid
45     }
46
47     await waitJobs(servers)
48   })
49
50   it('Should not find a remote video', async function () {
51     {
52       const res = await searchVideoWithToken(servers[ 0 ].url, 'http://localhost:9002/videos/watch/43', 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 res = await searchVideo(servers[0].url, 'http://localhost:9002/videos/watch/' + videoServer2UUID)
62
63       expect(res.body.total).to.equal(0)
64       expect(res.body.data).to.be.an('array')
65       expect(res.body.data).to.have.lengthOf(0)
66     }
67   })
68
69   it('Should search a local video', async function () {
70     const res = await searchVideo(servers[0].url, 'http://localhost:9001/videos/watch/' + videoServer1UUID)
71
72     expect(res.body.total).to.equal(1)
73     expect(res.body.data).to.be.an('array')
74     expect(res.body.data).to.have.lengthOf(1)
75     expect(res.body.data[0].name).to.equal('video 1 on server 1')
76   })
77
78   it('Should search a remote video', async function () {
79     const res = await searchVideoWithToken(servers[0].url, 'http://localhost:9002/videos/watch/' + videoServer2UUID, servers[0].accessToken)
80
81     expect(res.body.total).to.equal(1)
82     expect(res.body.data).to.be.an('array')
83     expect(res.body.data).to.have.lengthOf(1)
84     expect(res.body.data[0].name).to.equal('video 1 on server 2')
85   })
86
87   it('Should not list this remote video', async function () {
88     const res = await getVideosList(servers[0].url)
89     expect(res.body.total).to.equal(1)
90     expect(res.body.data).to.have.lengthOf(1)
91     expect(res.body.data[0].name).to.equal('video 1 on server 1')
92   })
93
94   it('Should update video of server 2, and refresh it on server 1', async function () {
95     this.timeout(60000)
96
97     const channelAttributes = {
98       name: 'super_channel',
99       displayName: 'super channel'
100     }
101     const resChannel = await addVideoChannel(servers[1].url, servers[1].accessToken, channelAttributes)
102     const videoChannelId = resChannel.body.videoChannel.id
103
104     const attributes = {
105       name: 'updated',
106       tag: [ 'tag1', 'tag2' ],
107       privacy: VideoPrivacy.UNLISTED,
108       channelId: videoChannelId
109     }
110     await updateVideo(servers[1].url, servers[1].accessToken, videoServer2UUID, attributes)
111
112     await waitJobs(servers)
113     // Expire video
114     await wait(10000)
115
116     // Will run refresh async
117     await searchVideoWithToken(servers[0].url, 'http://localhost:9002/videos/watch/' + videoServer2UUID, servers[0].accessToken)
118
119     // Wait refresh
120     await wait(5000)
121
122     const res = await searchVideoWithToken(servers[0].url, 'http://localhost:9002/videos/watch/' + videoServer2UUID, servers[0].accessToken)
123     expect(res.body.total).to.equal(1)
124     expect(res.body.data).to.have.lengthOf(1)
125
126     const video: Video = res.body.data[0]
127     expect(video.name).to.equal('updated')
128     expect(video.channel.name).to.equal('super_channel')
129     expect(video.privacy.id).to.equal(VideoPrivacy.UNLISTED)
130   })
131
132   it('Should delete video of server 2, and delete it on server 1', async function () {
133     this.timeout(60000)
134
135     await removeVideo(servers[1].url, servers[1].accessToken, videoServer2UUID)
136
137     await waitJobs(servers)
138     // Expire video
139     await wait(10000)
140
141     // Will run refresh async
142     await searchVideoWithToken(servers[0].url, 'http://localhost:9002/videos/watch/' + videoServer2UUID, servers[0].accessToken)
143
144     // Wait refresh
145     await wait(5000)
146
147     const res = await searchVideoWithToken(servers[0].url, 'http://localhost:9002/videos/watch/' + videoServer2UUID, servers[0].accessToken)
148     expect(res.body.total).to.equal(0)
149     expect(res.body.data).to.have.lengthOf(0)
150   })
151
152   after(async function () {
153     await cleanupTests(servers)
154   })
155 })