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