Fix tests
[oweals/peertube.git] / server / tests / api / activitypub / refresher.ts
1 /* tslint:disable:no-unused-expression */
2
3 import 'mocha'
4 import {
5   cleanupTests, closeAllSequelize,
6   createVideoPlaylist,
7   doubleFollow,
8   flushAndRunMultipleServers,
9   generateUserAccessToken,
10   getVideo,
11   getVideoPlaylist,
12   killallServers,
13   reRunServer,
14   ServerInfo,
15   setAccessTokensToServers,
16   setActorField,
17   setDefaultVideoChannel,
18   setPlaylistField,
19   setVideoField,
20   uploadVideo,
21   uploadVideoAndGetId,
22   wait,
23   waitJobs
24 } from '../../../../shared/extra-utils'
25 import { getAccount } from '../../../../shared/extra-utils/users/accounts'
26 import { VideoPlaylistPrivacy } from '../../../../shared/models/videos'
27
28 describe('Test AP refresher', function () {
29   let servers: ServerInfo[] = []
30   let videoUUID1: string
31   let videoUUID2: string
32   let videoUUID3: string
33   let playlistUUID1: string
34   let playlistUUID2: string
35
36   before(async function () {
37     this.timeout(60000)
38
39     servers = await flushAndRunMultipleServers(2, { transcoding: { enabled: false } })
40
41     // Get the access tokens
42     await setAccessTokensToServers(servers)
43     await setDefaultVideoChannel(servers)
44
45     {
46       videoUUID1 = (await uploadVideoAndGetId({ server: servers[ 1 ], videoName: 'video1' })).uuid
47       videoUUID2 = (await uploadVideoAndGetId({ server: servers[ 1 ], videoName: 'video2' })).uuid
48       videoUUID3 = (await uploadVideoAndGetId({ server: servers[ 1 ], videoName: 'video3' })).uuid
49     }
50
51     {
52       const a1 = await generateUserAccessToken(servers[ 1 ], 'user1')
53       await uploadVideo(servers[ 1 ].url, a1, { name: 'video4' })
54
55       const a2 = await generateUserAccessToken(servers[ 1 ], 'user2')
56       await uploadVideo(servers[ 1 ].url, a2, { name: 'video5' })
57     }
58
59     {
60       const playlistAttrs = { displayName: 'playlist1', privacy: VideoPlaylistPrivacy.PUBLIC, videoChannelId: servers[ 1 ].videoChannel.id }
61       const res = await createVideoPlaylist({ url: servers[ 1 ].url, token: servers[ 1 ].accessToken, playlistAttrs })
62       playlistUUID1 = res.body.videoPlaylist.uuid
63     }
64
65     {
66       const playlistAttrs = { displayName: 'playlist2', privacy: VideoPlaylistPrivacy.PUBLIC, videoChannelId: servers[ 1 ].videoChannel.id }
67       const res = await createVideoPlaylist({ url: servers[ 1 ].url, token: servers[ 1 ].accessToken, playlistAttrs })
68       playlistUUID2 = res.body.videoPlaylist.uuid
69     }
70
71     await doubleFollow(servers[ 0 ], servers[ 1 ])
72   })
73
74   describe('Videos refresher', function () {
75
76     it('Should remove a deleted remote video', async function () {
77       this.timeout(60000)
78
79       await wait(10000)
80
81       // Change UUID so the remote server returns a 404
82       await setVideoField(servers[ 1 ].internalServerNumber, videoUUID1, 'uuid', '304afe4f-39f9-4d49-8ed7-ac57b86b174f')
83
84       await getVideo(servers[ 0 ].url, videoUUID1)
85       await getVideo(servers[ 0 ].url, videoUUID2)
86
87       await waitJobs(servers)
88
89       await getVideo(servers[ 0 ].url, videoUUID1, 404)
90       await getVideo(servers[ 0 ].url, videoUUID2, 200)
91     })
92
93     it('Should not update a remote video if the remote instance is down', async function () {
94       this.timeout(60000)
95
96       killallServers([ servers[ 1 ] ])
97
98       await setVideoField(servers[ 1 ].internalServerNumber, videoUUID3, 'uuid', '304afe4f-39f9-4d49-8ed7-ac57b86b174e')
99
100       // Video will need a refresh
101       await wait(10000)
102
103       await getVideo(servers[ 0 ].url, videoUUID3)
104       // The refresh should fail
105       await waitJobs([ servers[ 0 ] ])
106
107       await reRunServer(servers[ 1 ])
108
109       // Should not refresh the video, even if the last refresh failed (to avoid a loop on dead instances)
110       await getVideo(servers[ 0 ].url, videoUUID3)
111       await waitJobs(servers)
112
113       await getVideo(servers[ 0 ].url, videoUUID3, 200)
114     })
115   })
116
117   describe('Actors refresher', function () {
118
119     it('Should remove a deleted actor', async function () {
120       this.timeout(60000)
121
122       await wait(10000)
123
124       // Change actor name so the remote server returns a 404
125       const to = 'http://localhost:' + servers[ 1 ].port + '/accounts/user2'
126       await setActorField(servers[ 1 ].internalServerNumber, to, 'preferredUsername', 'toto')
127
128       await getAccount(servers[ 0 ].url, 'user1@localhost:' + servers[ 1 ].port)
129       await getAccount(servers[ 0 ].url, 'user2@localhost:' + servers[ 1 ].port)
130
131       await waitJobs(servers)
132
133       await getAccount(servers[ 0 ].url, 'user1@localhost:' + servers[ 1 ].port, 200)
134       await getAccount(servers[ 0 ].url, 'user2@localhost:' + servers[ 1 ].port, 404)
135     })
136   })
137
138   describe('Playlist refresher', function () {
139
140     it('Should remove a deleted playlist', async function () {
141       this.timeout(60000)
142
143       await wait(10000)
144
145       // Change UUID so the remote server returns a 404
146       await setPlaylistField(servers[ 1 ].internalServerNumber, playlistUUID2, 'uuid', '304afe4f-39f9-4d49-8ed7-ac57b86b178e')
147
148       await getVideoPlaylist(servers[ 0 ].url, playlistUUID1)
149       await getVideoPlaylist(servers[ 0 ].url, playlistUUID2)
150
151       await waitJobs(servers)
152
153       await getVideoPlaylist(servers[ 0 ].url, playlistUUID1, 200)
154       await getVideoPlaylist(servers[ 0 ].url, playlistUUID2, 404)
155     })
156   })
157
158   after(async function () {
159     this.timeout(10000)
160
161     await cleanupTests(servers)
162
163     await closeAllSequelize(servers)
164   })
165 })