]>
Commit | Line | Data |
---|---|---|
3c4754a3 C |
1 | /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */ |
2 | ||
2a491182 F |
3 | import { expect } from 'chai' |
4 | import { FIXTURE_URLS } from '@server/tests/shared' | |
5 | import { areHttpImportTestsDisabled } from '@shared/core-utils' | |
6 | import { HttpStatusCode, VideoChannelSyncState, VideoInclude, VideoPrivacy } from '@shared/models' | |
7 | import { | |
3204f4d1 | 8 | createMultipleServers, |
2a491182 | 9 | getServerImportConfig, |
3204f4d1 | 10 | killallServers, |
2a491182 F |
11 | PeerTubeServer, |
12 | setAccessTokensToServers, | |
13 | setDefaultAccountAvatar, | |
14 | setDefaultChannelAvatar, | |
15 | setDefaultVideoChannel, | |
16 | waitJobs | |
17 | } from '@shared/server-commands' | |
18 | ||
19 | describe('Test channel synchronizations', function () { | |
20 | if (areHttpImportTestsDisabled()) return | |
21 | ||
22 | function runSuite (mode: 'youtube-dl' | 'yt-dlp') { | |
23 | ||
24 | describe('Sync using ' + mode, function () { | |
3204f4d1 | 25 | let servers: PeerTubeServer[] |
a3b472a1 | 26 | |
2a491182 | 27 | let startTestDate: Date |
a3b472a1 C |
28 | |
29 | let rootChannelSyncId: number | |
2a491182 F |
30 | const userInfo = { |
31 | accessToken: '', | |
32 | username: 'user1', | |
33 | channelName: 'user1_channel', | |
34 | channelId: -1, | |
35 | syncId: -1 | |
36 | } | |
37 | ||
38 | async function changeDateForSync (channelSyncId: number, newDate: string) { | |
3204f4d1 | 39 | await servers[0].sql.updateQuery( |
2a491182 F |
40 | `UPDATE "videoChannelSync" ` + |
41 | `SET "createdAt"='${newDate}', "lastSyncAt"='${newDate}' ` + | |
42 | `WHERE id=${channelSyncId}` | |
43 | ) | |
44 | } | |
45 | ||
46 | before(async function () { | |
3204f4d1 | 47 | this.timeout(240_000) |
2a491182 F |
48 | |
49 | startTestDate = new Date() | |
50 | ||
3204f4d1 | 51 | servers = await createMultipleServers(2, getServerImportConfig(mode)) |
2a491182 | 52 | |
3204f4d1 C |
53 | await setAccessTokensToServers(servers) |
54 | await setDefaultVideoChannel(servers) | |
55 | await setDefaultChannelAvatar(servers) | |
56 | await setDefaultAccountAvatar(servers) | |
2a491182 | 57 | |
3204f4d1 | 58 | await servers[0].config.enableChannelSync() |
2a491182 F |
59 | |
60 | { | |
3204f4d1 | 61 | userInfo.accessToken = await servers[0].users.generateUserAndToken(userInfo.username) |
2a491182 | 62 | |
3204f4d1 | 63 | const { videoChannels } = await servers[0].users.getMyInfo({ token: userInfo.accessToken }) |
2a491182 F |
64 | userInfo.channelId = videoChannels[0].id |
65 | } | |
66 | }) | |
67 | ||
68 | it('Should fetch the latest channel videos of a remote channel', async function () { | |
69 | this.timeout(120_000) | |
70 | ||
71 | { | |
3204f4d1 | 72 | const { video } = await servers[0].imports.importVideo({ |
2a491182 | 73 | attributes: { |
3204f4d1 | 74 | channelId: servers[0].store.channel.id, |
2a491182 F |
75 | privacy: VideoPrivacy.PUBLIC, |
76 | targetUrl: FIXTURE_URLS.youtube | |
77 | } | |
78 | }) | |
79 | ||
80 | expect(video.name).to.equal('small video - youtube') | |
3c4754a3 | 81 | expect(video.waitTranscoding).to.be.true |
2a491182 | 82 | |
3204f4d1 | 83 | const { total } = await servers[0].videos.listByChannel({ handle: 'root_channel', include: VideoInclude.NOT_PUBLISHED_STATE }) |
2a491182 F |
84 | expect(total).to.equal(1) |
85 | } | |
86 | ||
3204f4d1 | 87 | const { videoChannelSync } = await servers[0].channelSyncs.create({ |
2a491182 F |
88 | attributes: { |
89 | externalChannelUrl: FIXTURE_URLS.youtubeChannel, | |
3204f4d1 | 90 | videoChannelId: servers[0].store.channel.id |
2a491182 | 91 | }, |
3204f4d1 | 92 | token: servers[0].accessToken, |
2a491182 F |
93 | expectedStatus: HttpStatusCode.OK_200 |
94 | }) | |
a3b472a1 | 95 | rootChannelSyncId = videoChannelSync.id |
2a491182 F |
96 | |
97 | // Ensure any missing video not already fetched will be considered as new | |
98 | await changeDateForSync(videoChannelSync.id, '1970-01-01') | |
99 | ||
3204f4d1 | 100 | await servers[0].debug.sendCommand({ |
2a491182 F |
101 | body: { |
102 | command: 'process-video-channel-sync-latest' | |
103 | } | |
104 | }) | |
105 | ||
106 | { | |
3204f4d1 | 107 | await waitJobs(servers) |
2a491182 | 108 | |
3204f4d1 C |
109 | const { total, data } = await servers[0].videos.listByChannel({ |
110 | handle: 'root_channel', | |
111 | include: VideoInclude.NOT_PUBLISHED_STATE | |
112 | }) | |
2a491182 F |
113 | expect(total).to.equal(2) |
114 | expect(data[0].name).to.equal('test') | |
3c4754a3 | 115 | expect(data[0].waitTranscoding).to.be.true |
2a491182 F |
116 | } |
117 | }) | |
118 | ||
119 | it('Should add another synchronization', async function () { | |
120 | const externalChannelUrl = FIXTURE_URLS.youtubeChannel + '?foo=bar' | |
121 | ||
3204f4d1 | 122 | const { videoChannelSync } = await servers[0].channelSyncs.create({ |
2a491182 F |
123 | attributes: { |
124 | externalChannelUrl, | |
3204f4d1 | 125 | videoChannelId: servers[0].store.channel.id |
2a491182 | 126 | }, |
3204f4d1 | 127 | token: servers[0].accessToken, |
2a491182 F |
128 | expectedStatus: HttpStatusCode.OK_200 |
129 | }) | |
130 | ||
131 | expect(videoChannelSync.externalChannelUrl).to.equal(externalChannelUrl) | |
132 | expect(videoChannelSync.channel).to.include({ | |
3204f4d1 | 133 | id: servers[0].store.channel.id, |
2a491182 F |
134 | name: 'root_channel' |
135 | }) | |
136 | expect(videoChannelSync.state.id).to.equal(VideoChannelSyncState.WAITING_FIRST_RUN) | |
137 | expect(new Date(videoChannelSync.createdAt)).to.be.above(startTestDate).and.to.be.at.most(new Date()) | |
138 | }) | |
139 | ||
140 | it('Should add a synchronization for another user', async function () { | |
3204f4d1 | 141 | const { videoChannelSync } = await servers[0].channelSyncs.create({ |
2a491182 F |
142 | attributes: { |
143 | externalChannelUrl: FIXTURE_URLS.youtubeChannel + '?baz=qux', | |
144 | videoChannelId: userInfo.channelId | |
145 | }, | |
146 | token: userInfo.accessToken | |
147 | }) | |
148 | userInfo.syncId = videoChannelSync.id | |
149 | }) | |
150 | ||
151 | it('Should not import a channel if not asked', async function () { | |
3204f4d1 | 152 | await waitJobs(servers) |
2a491182 | 153 | |
3204f4d1 | 154 | const { data } = await servers[0].channelSyncs.listByAccount({ accountName: userInfo.username }) |
2a491182 F |
155 | |
156 | expect(data[0].state).to.contain({ | |
157 | id: VideoChannelSyncState.WAITING_FIRST_RUN, | |
158 | label: 'Waiting first run' | |
159 | }) | |
160 | }) | |
161 | ||
162 | it('Should only fetch the videos newer than the creation date', async function () { | |
163 | this.timeout(120_000) | |
164 | ||
165 | await changeDateForSync(userInfo.syncId, '2019-03-01') | |
166 | ||
3204f4d1 | 167 | await servers[0].debug.sendCommand({ |
2a491182 F |
168 | body: { |
169 | command: 'process-video-channel-sync-latest' | |
170 | } | |
171 | }) | |
172 | ||
3204f4d1 | 173 | await waitJobs(servers) |
2a491182 | 174 | |
3204f4d1 | 175 | const { data, total } = await servers[0].videos.listByChannel({ |
2a491182 F |
176 | handle: userInfo.channelName, |
177 | include: VideoInclude.NOT_PUBLISHED_STATE | |
178 | }) | |
179 | ||
180 | expect(total).to.equal(1) | |
181 | expect(data[0].name).to.equal('test') | |
182 | }) | |
183 | ||
184 | it('Should list channel synchronizations', async function () { | |
185 | // Root | |
186 | { | |
3204f4d1 | 187 | const { total, data } = await servers[0].channelSyncs.listByAccount({ accountName: 'root' }) |
2a491182 F |
188 | expect(total).to.equal(2) |
189 | ||
190 | expect(data[0]).to.deep.contain({ | |
191 | externalChannelUrl: FIXTURE_URLS.youtubeChannel, | |
192 | state: { | |
193 | id: VideoChannelSyncState.SYNCED, | |
194 | label: 'Synchronized' | |
195 | } | |
196 | }) | |
197 | ||
198 | expect(new Date(data[0].lastSyncAt)).to.be.greaterThan(startTestDate) | |
199 | ||
3204f4d1 | 200 | expect(data[0].channel).to.contain({ id: servers[0].store.channel.id }) |
2a491182 F |
201 | expect(data[1]).to.contain({ externalChannelUrl: FIXTURE_URLS.youtubeChannel + '?foo=bar' }) |
202 | } | |
203 | ||
204 | // User | |
205 | { | |
3204f4d1 | 206 | const { total, data } = await servers[0].channelSyncs.listByAccount({ accountName: userInfo.username }) |
2a491182 F |
207 | expect(total).to.equal(1) |
208 | expect(data[0]).to.deep.contain({ | |
209 | externalChannelUrl: FIXTURE_URLS.youtubeChannel + '?baz=qux', | |
210 | state: { | |
211 | id: VideoChannelSyncState.SYNCED, | |
212 | label: 'Synchronized' | |
213 | } | |
214 | }) | |
215 | } | |
216 | }) | |
217 | ||
a3b472a1 | 218 | it('Should list imports of a channel synchronization', async function () { |
3204f4d1 | 219 | const { total, data } = await servers[0].imports.getMyVideoImports({ videoChannelSyncId: rootChannelSyncId }) |
a3b472a1 C |
220 | |
221 | expect(total).to.equal(1) | |
222 | expect(data).to.have.lengthOf(1) | |
223 | expect(data[0].video.name).to.equal('test') | |
224 | }) | |
225 | ||
2a491182 | 226 | it('Should remove user\'s channel synchronizations', async function () { |
3204f4d1 | 227 | await servers[0].channelSyncs.delete({ channelSyncId: userInfo.syncId }) |
2a491182 | 228 | |
3204f4d1 | 229 | const { total } = await servers[0].channelSyncs.listByAccount({ accountName: userInfo.username }) |
2a491182 F |
230 | expect(total).to.equal(0) |
231 | }) | |
232 | ||
3204f4d1 C |
233 | // FIXME: youtube-dl doesn't work when speicifying a port after the hostname |
234 | // it('Should import a remote PeerTube channel', async function () { | |
235 | // this.timeout(240_000) | |
236 | ||
237 | // await servers[1].videos.quickUpload({ name: 'remote 1' }) | |
238 | // await waitJobs(servers) | |
239 | ||
240 | // const { videoChannelSync } = await servers[0].channelSyncs.create({ | |
241 | // attributes: { | |
242 | // externalChannelUrl: servers[1].url + '/c/root_channel', | |
243 | // videoChannelId: userInfo.channelId | |
244 | // }, | |
245 | // token: userInfo.accessToken | |
246 | // }) | |
247 | // await servers[0].channels.importVideos({ | |
248 | // channelName: userInfo.channelName, | |
249 | // externalChannelUrl: servers[1].url + '/c/root_channel', | |
250 | // videoChannelSyncId: videoChannelSync.id, | |
251 | // token: userInfo.accessToken | |
252 | // }) | |
253 | ||
254 | // await waitJobs(servers) | |
255 | ||
256 | // const { data, total } = await servers[0].videos.listByChannel({ | |
257 | // handle: userInfo.channelName, | |
258 | // include: VideoInclude.NOT_PUBLISHED_STATE | |
259 | // }) | |
260 | ||
261 | // expect(total).to.equal(2) | |
262 | // expect(data[0].name).to.equal('remote 1') | |
263 | // }) | |
264 | ||
265 | // it('Should keep synced a remote PeerTube channel', async function () { | |
266 | // this.timeout(240_000) | |
267 | ||
268 | // await servers[1].videos.quickUpload({ name: 'remote 2' }) | |
269 | // await waitJobs(servers) | |
270 | ||
271 | // await servers[0].debug.sendCommand({ | |
272 | // body: { | |
273 | // command: 'process-video-channel-sync-latest' | |
274 | // } | |
275 | // }) | |
276 | ||
277 | // await waitJobs(servers) | |
278 | ||
279 | // const { data, total } = await servers[0].videos.listByChannel({ | |
280 | // handle: userInfo.channelName, | |
281 | // include: VideoInclude.NOT_PUBLISHED_STATE | |
282 | // }) | |
283 | // expect(total).to.equal(2) | |
284 | // expect(data[0].name).to.equal('remote 2') | |
285 | // }) | |
286 | ||
2a491182 | 287 | after(async function () { |
3204f4d1 | 288 | await killallServers(servers) |
2a491182 F |
289 | }) |
290 | }) | |
291 | } | |
292 | ||
293 | runSuite('youtube-dl') | |
294 | runSuite('yt-dlp') | |
295 | }) |