Fix tests
[oweals/peertube.git] / server / tests / plugins / filter-hooks.ts
1 /* tslint:disable:no-unused-expression */
2
3 import * as chai from 'chai'
4 import 'mocha'
5 import {
6   cleanupTests,
7   flushAndRunMultipleServers,
8   flushAndRunServer, killallServers, reRunServer,
9   ServerInfo,
10   waitUntilLog
11 } from '../../../shared/extra-utils/server/servers'
12 import {
13   addVideoCommentReply,
14   addVideoCommentThread,
15   deleteVideoComment,
16   getPluginTestPath,
17   getVideosList,
18   installPlugin,
19   removeVideo,
20   setAccessTokensToServers,
21   updateVideo,
22   uploadVideo,
23   viewVideo,
24   getVideosListPagination,
25   getVideo,
26   getVideoCommentThreads,
27   getVideoThreadComments,
28   getVideoWithToken,
29   setDefaultVideoChannel,
30   waitJobs,
31   doubleFollow, getConfig, registerUser
32 } from '../../../shared/extra-utils'
33 import { VideoCommentThreadTree } from '../../../shared/models/videos/video-comment.model'
34 import { VideoDetails } from '../../../shared/models/videos'
35 import { getYoutubeVideoUrl, importVideo } from '../../../shared/extra-utils/videos/video-imports'
36 import { ServerConfig } from '@shared/models'
37
38 const expect = chai.expect
39
40 describe('Test plugin filter hooks', function () {
41   let servers: ServerInfo[]
42   let videoUUID: string
43   let threadId: number
44
45   before(async function () {
46     this.timeout(30000)
47
48     servers = await flushAndRunMultipleServers(2)
49     await setAccessTokensToServers(servers)
50     await setDefaultVideoChannel(servers)
51     await doubleFollow(servers[0], servers[1])
52
53     await installPlugin({
54       url: servers[0].url,
55       accessToken: servers[0].accessToken,
56       path: getPluginTestPath()
57     })
58
59     await installPlugin({
60       url: servers[0].url,
61       accessToken: servers[0].accessToken,
62       path: getPluginTestPath('-two')
63     })
64
65     for (let i = 0; i < 10; i++) {
66       await uploadVideo(servers[0].url, servers[0].accessToken, { name: 'default video ' + i })
67     }
68
69     const res = await getVideosList(servers[0].url)
70     videoUUID = res.body.data[0].uuid
71   })
72
73   it('Should run filter:api.videos.list.params', async function () {
74     const res = await getVideosListPagination(servers[0].url, 0, 2)
75
76     // 2 plugins do +1 to the count parameter
77     expect(res.body.data).to.have.lengthOf(4)
78   })
79
80   it('Should run filter:api.videos.list.result', async function () {
81     const res = await getVideosListPagination(servers[0].url, 0, 0)
82
83     // Plugin do +1 to the total result
84     expect(res.body.total).to.equal(11)
85   })
86
87   it('Should run filter:api.video.get.result', async function () {
88     const res = await getVideo(servers[0].url, videoUUID)
89
90     expect(res.body.name).to.contain('<3')
91   })
92
93   it('Should run filter:api.video.upload.accept.result', async function () {
94     await uploadVideo(servers[0].url, servers[0].accessToken, { name: 'video with bad word' }, 403)
95   })
96
97   it('Should run filter:api.video-thread.create.accept.result', async function () {
98     await addVideoCommentThread(servers[0].url, servers[0].accessToken, videoUUID, 'comment with bad word', 403)
99   })
100
101   it('Should run filter:api.video-comment-reply.create.accept.result', async function () {
102     const res = await addVideoCommentThread(servers[0].url, servers[0].accessToken, videoUUID, 'thread')
103     threadId = res.body.comment.id
104
105     await addVideoCommentReply(servers[0].url, servers[0].accessToken, videoUUID, threadId, 'comment with bad word', 403)
106     await addVideoCommentReply(servers[0].url, servers[0].accessToken, videoUUID, threadId, 'comment with good word', 200)
107   })
108
109   it('Should run filter:api.video-threads.list.params', async function () {
110     const res = await getVideoCommentThreads(servers[0].url, videoUUID, 0, 0)
111
112     // our plugin do +1 to the count parameter
113     expect(res.body.data).to.have.lengthOf(1)
114   })
115
116   it('Should run filter:api.video-threads.list.result', async function () {
117     const res = await getVideoCommentThreads(servers[0].url, videoUUID, 0, 0)
118
119     // Plugin do +1 to the total result
120     expect(res.body.total).to.equal(2)
121   })
122
123   it('Should run filter:api.video-thread-comments.list.params')
124
125   it('Should run filter:api.video-thread-comments.list.result', async function () {
126     const res = await getVideoThreadComments(servers[0].url, videoUUID, threadId)
127
128     const thread = res.body as VideoCommentThreadTree
129     expect(thread.comment.text.endsWith(' <3')).to.be.true
130   })
131
132   describe('Should run filter:video.auto-blacklist.result', function () {
133
134     async function checkIsBlacklisted (oldRes: any, value: boolean) {
135       const videoId = oldRes.body.video.uuid
136
137       const res = await getVideoWithToken(servers[0].url, servers[0].accessToken, videoId)
138       const video: VideoDetails = res.body
139       expect(video.blacklisted).to.equal(value)
140     }
141
142     it('Should blacklist on upload', async function () {
143       const res = await uploadVideo(servers[ 0 ].url, servers[ 0 ].accessToken, { name: 'video please blacklist me' })
144       await checkIsBlacklisted(res, true)
145     })
146
147     it('Should blacklist on import', async function () {
148       const attributes = {
149         name: 'video please blacklist me',
150         targetUrl: getYoutubeVideoUrl(),
151         channelId: servers[0].videoChannel.id
152       }
153       const res = await importVideo(servers[0].url, servers[0].accessToken, attributes)
154       await checkIsBlacklisted(res, true)
155     })
156
157     it('Should blacklist on update', async function () {
158       const res = await uploadVideo(servers[ 0 ].url, servers[ 0 ].accessToken, { name: 'video' })
159       const videoId = res.body.video.uuid
160       await checkIsBlacklisted(res, false)
161
162       await updateVideo(servers[ 0 ].url, servers[ 0 ].accessToken, videoId, { name: 'please blacklist me' })
163       await checkIsBlacklisted(res, true)
164     })
165
166     it('Should blacklist on remote upload', async function () {
167       this.timeout(45000)
168
169       const res = await uploadVideo(servers[ 1 ].url, servers[ 1 ].accessToken, { name: 'remote please blacklist me' })
170       await waitJobs(servers)
171
172       await checkIsBlacklisted(res, true)
173     })
174
175     it('Should blacklist on remote update', async function () {
176       this.timeout(45000)
177
178       const res = await uploadVideo(servers[ 1 ].url, servers[ 1 ].accessToken, { name: 'video' })
179       await waitJobs(servers)
180
181       const videoId = res.body.video.uuid
182       await checkIsBlacklisted(res, false)
183
184       await updateVideo(servers[1].url, servers[1].accessToken, videoId, { name: 'please blacklist me' })
185       await waitJobs(servers)
186
187       await checkIsBlacklisted(res, true)
188     })
189   })
190
191   describe('Should run filter:api.user.signup.allowed.result', function () {
192
193     it('Should run on config endpoint', async function () {
194       const res = await getConfig(servers[0].url)
195       expect((res.body as ServerConfig).signup.allowed).to.be.true
196     })
197
198     it('Should allow a signup', async function () {
199       await registerUser(servers[0].url, 'john', 'password')
200     })
201
202     it('Should not allow a signup', async function () {
203       const res = await registerUser(servers[0].url, 'jma', 'password', 403)
204
205       expect(res.body.error).to.equal('No jma')
206     })
207   })
208
209   after(async function () {
210     await cleanupTests(servers)
211   })
212 })