aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/tests
diff options
context:
space:
mode:
Diffstat (limited to 'server/tests')
-rw-r--r--server/tests/api/index-slow.ts1
-rw-r--r--server/tests/api/video-comments.ts135
-rw-r--r--server/tests/utils/video-comments.ts64
3 files changed, 200 insertions, 0 deletions
diff --git a/server/tests/api/index-slow.ts b/server/tests/api/index-slow.ts
index 4cd5b09a2..b525d6f01 100644
--- a/server/tests/api/index-slow.ts
+++ b/server/tests/api/index-slow.ts
@@ -4,3 +4,4 @@ import './video-transcoder'
4import './multiple-servers' 4import './multiple-servers'
5import './follows' 5import './follows'
6import './jobs' 6import './jobs'
7import './video-comments'
diff --git a/server/tests/api/video-comments.ts b/server/tests/api/video-comments.ts
new file mode 100644
index 000000000..fbc1a0a20
--- /dev/null
+++ b/server/tests/api/video-comments.ts
@@ -0,0 +1,135 @@
1/* tslint:disable:no-unused-expression */
2
3import * as chai from 'chai'
4import 'mocha'
5import { VideoComment, VideoCommentThreadTree } from '../../../shared/models/videos/video-comment.model'
6import { dateIsValid, flushTests, killallServers, runServer, ServerInfo, setAccessTokensToServers, uploadVideo } from '../utils'
7import { addVideoCommentReply, addVideoCommentThread, getVideoCommentThreads, getVideoThreadComments } from '../utils/video-comments'
8
9const expect = chai.expect
10
11describe('Test a video comments', function () {
12 let server: ServerInfo
13 let videoId
14 let videoUUID
15 let threadId
16
17 before(async function () {
18 this.timeout(10000)
19
20 await flushTests()
21
22 server = await runServer(1)
23
24 await setAccessTokensToServers([ server ])
25
26 const res = await uploadVideo(server.url, server.accessToken, {})
27 videoUUID = res.body.video.uuid
28 videoId = res.body.video.id
29 })
30
31 it('Should not have threads on this video', async function () {
32 const res = await getVideoCommentThreads(server.url, videoUUID, 0, 5)
33
34 expect(res.body.total).to.equal(0)
35 expect(res.body.data).to.be.an('array')
36 expect(res.body.data).to.have.lengthOf(0)
37 })
38
39 it('Should create a thread in this video', async function () {
40 const text = 'my super first comment'
41
42 await addVideoCommentThread(server.url, server.accessToken, videoUUID, text)
43 })
44
45 it('Should list threads of this video', async function () {
46 const res = await getVideoCommentThreads(server.url, videoUUID, 0, 5)
47
48 expect(res.body.total).to.equal(1)
49 expect(res.body.data).to.be.an('array')
50 expect(res.body.data).to.have.lengthOf(1)
51
52 const comment: VideoComment = res.body.data[0]
53 expect(comment.inReplyToCommentId).to.be.null
54 expect(comment.text).equal('my super first comment')
55 expect(comment.videoId).to.equal(videoId)
56 expect(comment.id).to.equal(comment.threadId)
57 expect(comment.account.name).to.equal('root')
58 expect(dateIsValid(comment.createdAt as string)).to.be.true
59 expect(dateIsValid(comment.updatedAt as string)).to.be.true
60
61 threadId = comment.threadId
62 })
63
64 it('Should get all the thread created', async function () {
65 const res = await getVideoThreadComments(server.url, videoUUID, threadId)
66
67 const rootComment = res.body.comment
68 expect(rootComment.inReplyToCommentId).to.be.null
69 expect(rootComment.text).equal('my super first comment')
70 expect(rootComment.videoId).to.equal(videoId)
71 expect(dateIsValid(rootComment.createdAt as string)).to.be.true
72 expect(dateIsValid(rootComment.updatedAt as string)).to.be.true
73 })
74
75 it('Should create multiple replies in this thread', async function () {
76 const text1 = 'my super answer to thread 1'
77 const childCommentRes = await addVideoCommentReply(server.url, server.accessToken, videoId, threadId, text1)
78 const childCommentId = childCommentRes.body.comment.id
79
80 const text2 = 'my super answer to answer of thread 1'
81 await addVideoCommentReply(server.url, server.accessToken, videoId, childCommentId, text2)
82
83 const text3 = 'my second answer to thread 1'
84 await addVideoCommentReply(server.url, server.accessToken, videoId, threadId, text3)
85 })
86
87 it('Should get correctly the replies', async function () {
88 const res = await getVideoThreadComments(server.url, videoUUID, threadId)
89
90 const tree: VideoCommentThreadTree = res.body
91 expect(tree.comment.text).equal('my super first comment')
92 expect(tree.children).to.have.lengthOf(2)
93
94 const firstChild = tree.children[0]
95 expect(firstChild.comment.text).to.equal('my super answer to thread 1')
96 expect(firstChild.children).to.have.lengthOf(1)
97
98 const childOfFirstChild = firstChild.children[0]
99 expect(childOfFirstChild.comment.text).to.equal('my super answer to answer of thread 1')
100 expect(childOfFirstChild.children).to.have.lengthOf(0)
101
102 const secondChild = tree.children[1]
103 expect(secondChild.comment.text).to.equal('my second answer to thread 1')
104 expect(secondChild.children).to.have.lengthOf(0)
105 })
106
107 it('Should create other threads', async function () {
108 const text1 = 'super thread 2'
109 await addVideoCommentThread(server.url, server.accessToken, videoUUID, text1)
110
111 const text2 = 'super thread 3'
112 await addVideoCommentThread(server.url, server.accessToken, videoUUID, text2)
113 })
114
115 it('Should list the threads', async function () {
116 const res = await getVideoCommentThreads(server.url, videoUUID, 0, 5, 'createdAt')
117
118 expect(res.body.total).to.equal(3)
119 expect(res.body.data).to.be.an('array')
120 expect(res.body.data).to.have.lengthOf(3)
121
122 expect(res.body.data[0].text).to.equal('my super first comment')
123 expect(res.body.data[1].text).to.equal('super thread 2')
124 expect(res.body.data[2].text).to.equal('super thread 3')
125 })
126
127 after(async function () {
128 killallServers([ server ])
129
130 // Keep the logs if the test failed
131 if (this['ok']) {
132 await flushTests()
133 }
134 })
135})
diff --git a/server/tests/utils/video-comments.ts b/server/tests/utils/video-comments.ts
new file mode 100644
index 000000000..be062f815
--- /dev/null
+++ b/server/tests/utils/video-comments.ts
@@ -0,0 +1,64 @@
1import * as request from 'supertest'
2
3function getVideoCommentThreads (url: string, videoId: number, start: number, count: number, sort?: string) {
4 const path = '/api/v1/videos/' + videoId + '/comment-threads'
5
6 const req = request(url)
7 .get(path)
8 .query({ start: start })
9 .query({ count: count })
10
11 if (sort) req.query({ sort })
12
13 return req.set('Accept', 'application/json')
14 .expect(200)
15 .expect('Content-Type', /json/)
16}
17
18function getVideoThreadComments (url: string, videoId: number, threadId: number) {
19 const path = '/api/v1/videos/' + videoId + '/comment-threads/' + threadId
20
21 return request(url)
22 .get(path)
23 .set('Accept', 'application/json')
24 .expect(200)
25 .expect('Content-Type', /json/)
26}
27
28function addVideoCommentThread (url: string, token: string, videoId: number, text: string, expectedStatus = 200) {
29 const path = '/api/v1/videos/' + videoId + '/comment-threads'
30
31 return request(url)
32 .post(path)
33 .send({ text })
34 .set('Accept', 'application/json')
35 .set('Authorization', 'Bearer ' + token)
36 .expect(expectedStatus)
37}
38
39function addVideoCommentReply (
40 url: string,
41 token: string,
42 videoId: number,
43 inReplyToCommentId: number,
44 text: string,
45 expectedStatus = 200
46) {
47 const path = '/api/v1/videos/' + videoId + '/comments/' + inReplyToCommentId
48
49 return request(url)
50 .post(path)
51 .send({ text })
52 .set('Accept', 'application/json')
53 .set('Authorization', 'Bearer ' + token)
54 .expect(expectedStatus)
55}
56
57// ---------------------------------------------------------------------------
58
59export {
60 getVideoCommentThreads,
61 getVideoThreadComments,
62 addVideoCommentThread,
63 addVideoCommentReply
64}