Improve check users parameters tests
[oweals/peertube.git] / server / tests / api / videos / video-comments.ts
1 /* tslint:disable:no-unused-expression */
2
3 import * as chai from 'chai'
4 import 'mocha'
5 import { VideoComment, VideoCommentThreadTree } from '../../../../shared/models/videos/video-comment.model'
6 import { dateIsValid, flushTests, killallServers, runServer, ServerInfo, setAccessTokensToServers, uploadVideo } from '../../utils/index'
7 import {
8   addVideoCommentReply, addVideoCommentThread, getVideoCommentThreads,
9   getVideoThreadComments
10 } from '../../utils/videos/video-comments'
11
12 const expect = chai.expect
13
14 describe('Test video comments', function () {
15   let server: ServerInfo
16   let videoId
17   let videoUUID
18   let threadId
19
20   before(async function () {
21     this.timeout(10000)
22
23     await flushTests()
24
25     server = await runServer(1)
26
27     await setAccessTokensToServers([ server ])
28
29     const res = await uploadVideo(server.url, server.accessToken, {})
30     videoUUID = res.body.video.uuid
31     videoId = res.body.video.id
32   })
33
34   it('Should not have threads on this video', async function () {
35     const res = await getVideoCommentThreads(server.url, videoUUID, 0, 5)
36
37     expect(res.body.total).to.equal(0)
38     expect(res.body.data).to.be.an('array')
39     expect(res.body.data).to.have.lengthOf(0)
40   })
41
42   it('Should create a thread in this video', async function () {
43     const text = 'my super first comment'
44
45     const res = await addVideoCommentThread(server.url, server.accessToken, videoUUID, text)
46     const comment = res.body.comment
47
48     expect(comment.inReplyToCommentId).to.be.null
49     expect(comment.text).equal('my super first comment')
50     expect(comment.videoId).to.equal(videoId)
51     expect(comment.id).to.equal(comment.threadId)
52     expect(comment.account.name).to.equal('root')
53     expect(comment.account.host).to.equal('localhost:9001')
54     expect(comment.totalReplies).to.equal(0)
55     expect(dateIsValid(comment.createdAt as string)).to.be.true
56     expect(dateIsValid(comment.updatedAt as string)).to.be.true
57   })
58
59   it('Should list threads of this video', async function () {
60     const res = await getVideoCommentThreads(server.url, videoUUID, 0, 5)
61
62     expect(res.body.total).to.equal(1)
63     expect(res.body.data).to.be.an('array')
64     expect(res.body.data).to.have.lengthOf(1)
65
66     const comment: VideoComment = res.body.data[0]
67     expect(comment.inReplyToCommentId).to.be.null
68     expect(comment.text).equal('my super first comment')
69     expect(comment.videoId).to.equal(videoId)
70     expect(comment.id).to.equal(comment.threadId)
71     expect(comment.account.name).to.equal('root')
72     expect(comment.account.host).to.equal('localhost:9001')
73     expect(comment.totalReplies).to.equal(0)
74     expect(dateIsValid(comment.createdAt as string)).to.be.true
75     expect(dateIsValid(comment.updatedAt as string)).to.be.true
76
77     threadId = comment.threadId
78   })
79
80   it('Should get all the thread created', async function () {
81     const res = await getVideoThreadComments(server.url, videoUUID, threadId)
82
83     const rootComment = res.body.comment
84     expect(rootComment.inReplyToCommentId).to.be.null
85     expect(rootComment.text).equal('my super first comment')
86     expect(rootComment.videoId).to.equal(videoId)
87     expect(dateIsValid(rootComment.createdAt as string)).to.be.true
88     expect(dateIsValid(rootComment.updatedAt as string)).to.be.true
89   })
90
91   it('Should create multiple replies in this thread', async function () {
92     const text1 = 'my super answer to thread 1'
93     const childCommentRes = await addVideoCommentReply(server.url, server.accessToken, videoId, threadId, text1)
94     const childCommentId = childCommentRes.body.comment.id
95
96     const text2 = 'my super answer to answer of thread 1'
97     await addVideoCommentReply(server.url, server.accessToken, videoId, childCommentId, text2)
98
99     const text3 = 'my second answer to thread 1'
100     await addVideoCommentReply(server.url, server.accessToken, videoId, threadId, text3)
101   })
102
103   it('Should get correctly the replies', async function () {
104     const res = await getVideoThreadComments(server.url, videoUUID, threadId)
105
106     const tree: VideoCommentThreadTree = res.body
107     expect(tree.comment.text).equal('my super first comment')
108     expect(tree.children).to.have.lengthOf(2)
109
110     const firstChild = tree.children[0]
111     expect(firstChild.comment.text).to.equal('my super answer to thread 1')
112     expect(firstChild.children).to.have.lengthOf(1)
113
114     const childOfFirstChild = firstChild.children[0]
115     expect(childOfFirstChild.comment.text).to.equal('my super answer to answer of thread 1')
116     expect(childOfFirstChild.children).to.have.lengthOf(0)
117
118     const secondChild = tree.children[1]
119     expect(secondChild.comment.text).to.equal('my second answer to thread 1')
120     expect(secondChild.children).to.have.lengthOf(0)
121   })
122
123   it('Should create other threads', async function () {
124     const text1 = 'super thread 2'
125     await addVideoCommentThread(server.url, server.accessToken, videoUUID, text1)
126
127     const text2 = 'super thread 3'
128     await addVideoCommentThread(server.url, server.accessToken, videoUUID, text2)
129   })
130
131   it('Should list the threads', async function () {
132     const res = await getVideoCommentThreads(server.url, videoUUID, 0, 5, 'createdAt')
133
134     expect(res.body.total).to.equal(3)
135     expect(res.body.data).to.be.an('array')
136     expect(res.body.data).to.have.lengthOf(3)
137
138     expect(res.body.data[0].text).to.equal('my super first comment')
139     expect(res.body.data[0].totalReplies).to.equal(3)
140     expect(res.body.data[1].text).to.equal('super thread 2')
141     expect(res.body.data[1].totalReplies).to.equal(0)
142     expect(res.body.data[2].text).to.equal('super thread 3')
143     expect(res.body.data[2].totalReplies).to.equal(0)
144   })
145
146   after(async function () {
147     killallServers([ server ])
148
149     // Keep the logs if the test failed
150     if (this['ok']) {
151       await flushTests()
152     }
153   })
154 })