diff options
Diffstat (limited to 'packages/tests/src/plugins/action-hooks.ts')
-rw-r--r-- | packages/tests/src/plugins/action-hooks.ts | 298 |
1 files changed, 298 insertions, 0 deletions
diff --git a/packages/tests/src/plugins/action-hooks.ts b/packages/tests/src/plugins/action-hooks.ts new file mode 100644 index 000000000..136c7671b --- /dev/null +++ b/packages/tests/src/plugins/action-hooks.ts | |||
@@ -0,0 +1,298 @@ | |||
1 | /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */ | ||
2 | |||
3 | import { ServerHookName, VideoPlaylistPrivacy, VideoPrivacy } from '@peertube/peertube-models' | ||
4 | import { | ||
5 | cleanupTests, | ||
6 | createMultipleServers, | ||
7 | doubleFollow, | ||
8 | killallServers, | ||
9 | PeerTubeServer, | ||
10 | PluginsCommand, | ||
11 | setAccessTokensToServers, | ||
12 | setDefaultVideoChannel, | ||
13 | stopFfmpeg, | ||
14 | waitJobs | ||
15 | } from '@peertube/peertube-server-commands' | ||
16 | |||
17 | describe('Test plugin action hooks', function () { | ||
18 | let servers: PeerTubeServer[] | ||
19 | let videoUUID: string | ||
20 | let threadId: number | ||
21 | |||
22 | function checkHook (hook: ServerHookName, strictCount = true, count = 1) { | ||
23 | return servers[0].servers.waitUntilLog('Run hook ' + hook, count, strictCount) | ||
24 | } | ||
25 | |||
26 | before(async function () { | ||
27 | this.timeout(120000) | ||
28 | |||
29 | servers = await createMultipleServers(2) | ||
30 | await setAccessTokensToServers(servers) | ||
31 | await setDefaultVideoChannel(servers) | ||
32 | |||
33 | await servers[0].plugins.install({ path: PluginsCommand.getPluginTestPath() }) | ||
34 | |||
35 | await killallServers([ servers[0] ]) | ||
36 | |||
37 | await servers[0].run({ | ||
38 | live: { | ||
39 | enabled: true | ||
40 | } | ||
41 | }) | ||
42 | |||
43 | await servers[0].config.enableFileUpdate() | ||
44 | |||
45 | await doubleFollow(servers[0], servers[1]) | ||
46 | }) | ||
47 | |||
48 | describe('Application hooks', function () { | ||
49 | it('Should run action:application.listening', async function () { | ||
50 | await checkHook('action:application.listening') | ||
51 | }) | ||
52 | }) | ||
53 | |||
54 | describe('Videos hooks', function () { | ||
55 | |||
56 | it('Should run action:api.video.uploaded', async function () { | ||
57 | const { uuid } = await servers[0].videos.upload({ attributes: { name: 'video' } }) | ||
58 | videoUUID = uuid | ||
59 | |||
60 | await checkHook('action:api.video.uploaded') | ||
61 | }) | ||
62 | |||
63 | it('Should run action:api.video.updated', async function () { | ||
64 | await servers[0].videos.update({ id: videoUUID, attributes: { name: 'video updated' } }) | ||
65 | |||
66 | await checkHook('action:api.video.updated') | ||
67 | }) | ||
68 | |||
69 | it('Should run action:api.video.viewed', async function () { | ||
70 | await servers[0].views.simulateView({ id: videoUUID }) | ||
71 | |||
72 | await checkHook('action:api.video.viewed') | ||
73 | }) | ||
74 | |||
75 | it('Should run action:api.video.file-updated', async function () { | ||
76 | await servers[0].videos.replaceSourceFile({ videoId: videoUUID, fixture: 'video_short.mp4' }) | ||
77 | |||
78 | await checkHook('action:api.video.file-updated') | ||
79 | }) | ||
80 | |||
81 | it('Should run action:api.video.deleted', async function () { | ||
82 | await servers[0].videos.remove({ id: videoUUID }) | ||
83 | |||
84 | await checkHook('action:api.video.deleted') | ||
85 | }) | ||
86 | |||
87 | after(async function () { | ||
88 | const { uuid } = await servers[0].videos.quickUpload({ name: 'video' }) | ||
89 | videoUUID = uuid | ||
90 | }) | ||
91 | }) | ||
92 | |||
93 | describe('Video channel hooks', function () { | ||
94 | const channelName = 'my_super_channel' | ||
95 | |||
96 | it('Should run action:api.video-channel.created', async function () { | ||
97 | await servers[0].channels.create({ attributes: { name: channelName } }) | ||
98 | |||
99 | await checkHook('action:api.video-channel.created') | ||
100 | }) | ||
101 | |||
102 | it('Should run action:api.video-channel.updated', async function () { | ||
103 | await servers[0].channels.update({ channelName, attributes: { displayName: 'my display name' } }) | ||
104 | |||
105 | await checkHook('action:api.video-channel.updated') | ||
106 | }) | ||
107 | |||
108 | it('Should run action:api.video-channel.deleted', async function () { | ||
109 | await servers[0].channels.delete({ channelName }) | ||
110 | |||
111 | await checkHook('action:api.video-channel.deleted') | ||
112 | }) | ||
113 | }) | ||
114 | |||
115 | describe('Live hooks', function () { | ||
116 | |||
117 | it('Should run action:api.live-video.created', async function () { | ||
118 | const attributes = { | ||
119 | name: 'live', | ||
120 | privacy: VideoPrivacy.PUBLIC, | ||
121 | channelId: servers[0].store.channel.id | ||
122 | } | ||
123 | |||
124 | await servers[0].live.create({ fields: attributes }) | ||
125 | |||
126 | await checkHook('action:api.live-video.created') | ||
127 | }) | ||
128 | |||
129 | it('Should run action:live.video.state.updated', async function () { | ||
130 | this.timeout(60000) | ||
131 | |||
132 | const attributes = { | ||
133 | name: 'live', | ||
134 | privacy: VideoPrivacy.PUBLIC, | ||
135 | channelId: servers[0].store.channel.id | ||
136 | } | ||
137 | |||
138 | const { uuid: liveVideoId } = await servers[0].live.create({ fields: attributes }) | ||
139 | const ffmpegCommand = await servers[0].live.sendRTMPStreamInVideo({ videoId: liveVideoId }) | ||
140 | await servers[0].live.waitUntilPublished({ videoId: liveVideoId }) | ||
141 | await waitJobs(servers) | ||
142 | |||
143 | await checkHook('action:live.video.state.updated', true, 1) | ||
144 | |||
145 | await stopFfmpeg(ffmpegCommand) | ||
146 | await servers[0].live.waitUntilEnded({ videoId: liveVideoId }) | ||
147 | await waitJobs(servers) | ||
148 | |||
149 | await checkHook('action:live.video.state.updated', true, 2) | ||
150 | }) | ||
151 | }) | ||
152 | |||
153 | describe('Comments hooks', function () { | ||
154 | it('Should run action:api.video-thread.created', async function () { | ||
155 | const created = await servers[0].comments.createThread({ videoId: videoUUID, text: 'thread' }) | ||
156 | threadId = created.id | ||
157 | |||
158 | await checkHook('action:api.video-thread.created') | ||
159 | }) | ||
160 | |||
161 | it('Should run action:api.video-comment-reply.created', async function () { | ||
162 | await servers[0].comments.addReply({ videoId: videoUUID, toCommentId: threadId, text: 'reply' }) | ||
163 | |||
164 | await checkHook('action:api.video-comment-reply.created') | ||
165 | }) | ||
166 | |||
167 | it('Should run action:api.video-comment.deleted', async function () { | ||
168 | await servers[0].comments.delete({ videoId: videoUUID, commentId: threadId }) | ||
169 | |||
170 | await checkHook('action:api.video-comment.deleted') | ||
171 | }) | ||
172 | }) | ||
173 | |||
174 | describe('Captions hooks', function () { | ||
175 | it('Should run action:api.video-caption.created', async function () { | ||
176 | await servers[0].captions.add({ videoId: videoUUID, language: 'en', fixture: 'subtitle-good.srt' }) | ||
177 | |||
178 | await checkHook('action:api.video-caption.created') | ||
179 | }) | ||
180 | |||
181 | it('Should run action:api.video-caption.deleted', async function () { | ||
182 | await servers[0].captions.delete({ videoId: videoUUID, language: 'en' }) | ||
183 | |||
184 | await checkHook('action:api.video-caption.deleted') | ||
185 | }) | ||
186 | }) | ||
187 | |||
188 | describe('Users hooks', function () { | ||
189 | let userId: number | ||
190 | |||
191 | it('Should run action:api.user.registered', async function () { | ||
192 | await servers[0].registrations.register({ username: 'registered_user' }) | ||
193 | |||
194 | await checkHook('action:api.user.registered') | ||
195 | }) | ||
196 | |||
197 | it('Should run action:api.user.created', async function () { | ||
198 | const user = await servers[0].users.create({ username: 'created_user' }) | ||
199 | userId = user.id | ||
200 | |||
201 | await checkHook('action:api.user.created') | ||
202 | }) | ||
203 | |||
204 | it('Should run action:api.user.oauth2-got-token', async function () { | ||
205 | await servers[0].login.login({ user: { username: 'created_user' } }) | ||
206 | |||
207 | await checkHook('action:api.user.oauth2-got-token') | ||
208 | }) | ||
209 | |||
210 | it('Should run action:api.user.blocked', async function () { | ||
211 | await servers[0].users.banUser({ userId }) | ||
212 | |||
213 | await checkHook('action:api.user.blocked') | ||
214 | }) | ||
215 | |||
216 | it('Should run action:api.user.unblocked', async function () { | ||
217 | await servers[0].users.unbanUser({ userId }) | ||
218 | |||
219 | await checkHook('action:api.user.unblocked') | ||
220 | }) | ||
221 | |||
222 | it('Should run action:api.user.updated', async function () { | ||
223 | await servers[0].users.update({ userId, videoQuota: 50 }) | ||
224 | |||
225 | await checkHook('action:api.user.updated') | ||
226 | }) | ||
227 | |||
228 | it('Should run action:api.user.deleted', async function () { | ||
229 | await servers[0].users.remove({ userId }) | ||
230 | |||
231 | await checkHook('action:api.user.deleted') | ||
232 | }) | ||
233 | }) | ||
234 | |||
235 | describe('Playlist hooks', function () { | ||
236 | let playlistId: number | ||
237 | let videoId: number | ||
238 | |||
239 | before(async function () { | ||
240 | { | ||
241 | const { id } = await servers[0].playlists.create({ | ||
242 | attributes: { | ||
243 | displayName: 'My playlist', | ||
244 | privacy: VideoPlaylistPrivacy.PRIVATE | ||
245 | } | ||
246 | }) | ||
247 | playlistId = id | ||
248 | } | ||
249 | |||
250 | { | ||
251 | const { id } = await servers[0].videos.upload({ attributes: { name: 'my super name' } }) | ||
252 | videoId = id | ||
253 | } | ||
254 | }) | ||
255 | |||
256 | it('Should run action:api.video-playlist-element.created', async function () { | ||
257 | await servers[0].playlists.addElement({ playlistId, attributes: { videoId } }) | ||
258 | |||
259 | await checkHook('action:api.video-playlist-element.created') | ||
260 | }) | ||
261 | }) | ||
262 | |||
263 | describe('Notification hook', function () { | ||
264 | |||
265 | it('Should run action:notifier.notification.created', async function () { | ||
266 | await checkHook('action:notifier.notification.created', false) | ||
267 | }) | ||
268 | }) | ||
269 | |||
270 | describe('Activity Pub hooks', function () { | ||
271 | let videoUUID: string | ||
272 | |||
273 | it('Should run action:activity-pub.remote-video.created', async function () { | ||
274 | this.timeout(30000) | ||
275 | |||
276 | const { uuid } = await servers[1].videos.quickUpload({ name: 'remote video' }) | ||
277 | videoUUID = uuid | ||
278 | |||
279 | await servers[0].servers.waitUntilLog('action:activity-pub.remote-video.created - AP remote video - video remote video') | ||
280 | }) | ||
281 | |||
282 | it('Should run action:activity-pub.remote-video.updated', async function () { | ||
283 | this.timeout(30000) | ||
284 | |||
285 | await servers[1].videos.update({ id: videoUUID, attributes: { name: 'remote video updated' } }) | ||
286 | |||
287 | await servers[0].servers.waitUntilLog( | ||
288 | 'action:activity-pub.remote-video.updated - AP remote video updated - video remote video updated', | ||
289 | 1, | ||
290 | false | ||
291 | ) | ||
292 | }) | ||
293 | }) | ||
294 | |||
295 | after(async function () { | ||
296 | await cleanupTests(servers) | ||
297 | }) | ||
298 | }) | ||