]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/tests/api/notifications/comments-notifications.ts
Use an object to represent a server
[github/Chocobozzz/PeerTube.git] / server / tests / api / notifications / comments-notifications.ts
index fd009e9d9b41dd17efaa5836c15e9368a5cff862..d54819aaabc0b3d9807c04a289fe8f97a0be745a 100644 (file)
@@ -2,35 +2,37 @@
 
 import 'mocha'
 import * as chai from 'chai'
-import { cleanupTests, getVideoCommentThreads, getVideoThreadComments, updateMyUser } from '../../../../shared/extra-utils'
-import { ServerInfo, uploadVideo } from '../../../../shared/extra-utils/index'
-import { MockSmtpServer } from '../../../../shared/extra-utils/miscs/email'
-import { waitJobs } from '../../../../shared/extra-utils/server/jobs'
-import { addAccountToAccountBlocklist, removeAccountFromAccountBlocklist } from '../../../../shared/extra-utils/users/blocklist'
 import {
   checkCommentMention,
   CheckerBaseParams,
   checkNewCommentOnMyVideo,
-  prepareNotificationsTest
-} from '../../../../shared/extra-utils/users/user-notifications'
-import { addVideoCommentReply, addVideoCommentThread } from '../../../../shared/extra-utils/videos/video-comments'
-import { UserNotification } from '../../../../shared/models/users'
-import { VideoCommentThreadTree } from '../../../../shared/models/videos/video-comment.model'
+  cleanupTests,
+  MockSmtpServer,
+  prepareNotificationsTest,
+  PeerTubeServer,
+  waitJobs
+} from '@shared/extra-utils'
+import { UserNotification } from '@shared/models'
 
 const expect = chai.expect
 
 describe('Test comments notifications', function () {
-  let servers: ServerInfo[] = []
-  let userAccessToken: string
+  let servers: PeerTubeServer[] = []
+  let userToken: string
   let userNotifications: UserNotification[] = []
   let emails: object[] = []
 
+  const commentText = '**hello** <a href="https://joinpeertube.org">world</a>, <h1>what do you think about peertube?</h1>'
+  const expectedHtml = '<strong style="-ms-text-size-adjust: 100%; -webkit-text-size-adjust: 100%;">hello</strong> ' +
+  '<a href="https://joinpeertube.org" target="_blank" rel="noopener noreferrer" style="-ms-text-size-adjust: 100%; ' +
+  '-webkit-text-size-adjust: 100%; text-decoration: none; color: #f2690d;">world</a>, </p>what do you think about peertube?'
+
   before(async function () {
     this.timeout(120000)
 
     const res = await prepareNotificationsTest(2)
     emails = res.emails
-    userAccessToken = res.userAccessToken
+    userToken = res.userAccessToken
     servers = res.servers
     userNotifications = res.userNotifications
   })
@@ -43,122 +45,112 @@ describe('Test comments notifications', function () {
         server: servers[0],
         emails,
         socketNotifications: userNotifications,
-        token: userAccessToken
+        token: userToken
       }
     })
 
     it('Should not send a new comment notification after a comment on another video', async function () {
-      this.timeout(10000)
+      this.timeout(20000)
 
-      const resVideo = await uploadVideo(servers[0].url, servers[0].accessToken, { name: 'super video' })
-      const uuid = resVideo.body.video.uuid
+      const { uuid } = await servers[0].videos.upload({ attributes: { name: 'super video' } })
 
-      const resComment = await addVideoCommentThread(servers[0].url, servers[0].accessToken, uuid, 'comment')
-      const commentId = resComment.body.comment.id
+      const created = await servers[0].comments.createThread({ videoId: uuid, text: 'comment' })
+      const commentId = created.id
 
       await waitJobs(servers)
       await checkNewCommentOnMyVideo(baseParams, uuid, commentId, commentId, 'absence')
     })
 
     it('Should not send a new comment notification if I comment my own video', async function () {
-      this.timeout(10000)
+      this.timeout(20000)
 
-      const resVideo = await uploadVideo(servers[0].url, userAccessToken, { name: 'super video' })
-      const uuid = resVideo.body.video.uuid
+      const { uuid } = await servers[0].videos.upload({ token: userToken, attributes: { name: 'super video' } })
 
-      const resComment = await addVideoCommentThread(servers[0].url, userAccessToken, uuid, 'comment')
-      const commentId = resComment.body.comment.id
+      const created = await servers[0].comments.createThread({ token: userToken, videoId: uuid, text: 'comment' })
+      const commentId = created.id
 
       await waitJobs(servers)
       await checkNewCommentOnMyVideo(baseParams, uuid, commentId, commentId, 'absence')
     })
 
     it('Should not send a new comment notification if the account is muted', async function () {
-      this.timeout(10000)
+      this.timeout(20000)
 
-      await addAccountToAccountBlocklist(servers[0].url, userAccessToken, 'root')
+      await servers[0].blocklist.addToMyBlocklist({ token: userToken, account: 'root' })
 
-      const resVideo = await uploadVideo(servers[0].url, userAccessToken, { name: 'super video' })
-      const uuid = resVideo.body.video.uuid
+      const { uuid } = await servers[0].videos.upload({ token: userToken, attributes: { name: 'super video' } })
 
-      const resComment = await addVideoCommentThread(servers[0].url, servers[0].accessToken, uuid, 'comment')
-      const commentId = resComment.body.comment.id
+      const created = await servers[0].comments.createThread({ videoId: uuid, text: 'comment' })
+      const commentId = created.id
 
       await waitJobs(servers)
       await checkNewCommentOnMyVideo(baseParams, uuid, commentId, commentId, 'absence')
 
-      await removeAccountFromAccountBlocklist(servers[0].url, userAccessToken, 'root')
+      await servers[0].blocklist.removeFromMyBlocklist({ token: userToken, account: 'root' })
     })
 
     it('Should send a new comment notification after a local comment on my video', async function () {
-      this.timeout(10000)
+      this.timeout(20000)
 
-      const resVideo = await uploadVideo(servers[0].url, userAccessToken, { name: 'super video' })
-      const uuid = resVideo.body.video.uuid
+      const { uuid } = await servers[0].videos.upload({ token: userToken, attributes: { name: 'super video' } })
 
-      const resComment = await addVideoCommentThread(servers[0].url, servers[0].accessToken, uuid, 'comment')
-      const commentId = resComment.body.comment.id
+      const created = await servers[0].comments.createThread({ videoId: uuid, text: 'comment' })
+      const commentId = created.id
 
       await waitJobs(servers)
       await checkNewCommentOnMyVideo(baseParams, uuid, commentId, commentId, 'presence')
     })
 
     it('Should send a new comment notification after a remote comment on my video', async function () {
-      this.timeout(10000)
+      this.timeout(20000)
 
-      const resVideo = await uploadVideo(servers[0].url, userAccessToken, { name: 'super video' })
-      const uuid = resVideo.body.video.uuid
+      const { uuid } = await servers[0].videos.upload({ token: userToken, attributes: { name: 'super video' } })
 
       await waitJobs(servers)
 
-      await addVideoCommentThread(servers[1].url, servers[1].accessToken, uuid, 'comment')
+      await servers[1].comments.createThread({ videoId: uuid, text: 'comment' })
 
       await waitJobs(servers)
 
-      const resComment = await getVideoCommentThreads(servers[0].url, uuid, 0, 5)
-      expect(resComment.body.data).to.have.lengthOf(1)
-      const commentId = resComment.body.data[0].id
+      const { data } = await servers[0].comments.listThreads({ videoId: uuid })
+      expect(data).to.have.lengthOf(1)
 
+      const commentId = data[0].id
       await checkNewCommentOnMyVideo(baseParams, uuid, commentId, commentId, 'presence')
     })
 
     it('Should send a new comment notification after a local reply on my video', async function () {
-      this.timeout(10000)
+      this.timeout(20000)
 
-      const resVideo = await uploadVideo(servers[0].url, userAccessToken, { name: 'super video' })
-      const uuid = resVideo.body.video.uuid
+      const { uuid } = await servers[0].videos.upload({ token: userToken, attributes: { name: 'super video' } })
 
-      const resThread = await addVideoCommentThread(servers[0].url, servers[0].accessToken, uuid, 'comment')
-      const threadId = resThread.body.comment.id
+      const { id: threadId } = await servers[0].comments.createThread({ videoId: uuid, text: 'comment' })
 
-      const resComment = await addVideoCommentReply(servers[0].url, servers[0].accessToken, uuid, threadId, 'reply')
-      const commentId = resComment.body.comment.id
+      const { id: commentId } = await servers[0].comments.addReply({ videoId: uuid, toCommentId: threadId, text: 'reply' })
 
       await waitJobs(servers)
       await checkNewCommentOnMyVideo(baseParams, uuid, commentId, threadId, 'presence')
     })
 
     it('Should send a new comment notification after a remote reply on my video', async function () {
-      this.timeout(10000)
+      this.timeout(20000)
 
-      const resVideo = await uploadVideo(servers[0].url, userAccessToken, { name: 'super video' })
-      const uuid = resVideo.body.video.uuid
+      const { uuid } = await servers[0].videos.upload({ token: userToken, attributes: { name: 'super video' } })
       await waitJobs(servers)
 
       {
-        const resThread = await addVideoCommentThread(servers[1].url, servers[1].accessToken, uuid, 'comment')
-        const threadId = resThread.body.comment.id
-        await addVideoCommentReply(servers[1].url, servers[1].accessToken, uuid, threadId, 'reply')
+        const created = await servers[1].comments.createThread({ videoId: uuid, text: 'comment' })
+        const threadId = created.id
+        await servers[1].comments.addReply({ videoId: uuid, toCommentId: threadId, text: 'reply' })
       }
 
       await waitJobs(servers)
 
-      const resThread = await getVideoCommentThreads(servers[0].url, uuid, 0, 5)
-      expect(resThread.body.data).to.have.lengthOf(1)
-      const threadId = resThread.body.data[0].id
+      const { data } = await servers[0].comments.listThreads({ videoId: uuid })
+      expect(data).to.have.lengthOf(1)
 
-      const resComments = await getVideoThreadComments(servers[0].url, uuid, threadId)
-      const tree = resComments.body as VideoCommentThreadTree
+      const threadId = data[0].id
+      const tree = await servers[0].comments.getThread({ videoId: uuid, threadId })
 
       expect(tree.children).to.have.lengthOf(1)
       const commentId = tree.children[0].comment.id
@@ -167,25 +159,16 @@ describe('Test comments notifications', function () {
     })
 
     it('Should convert markdown in comment to html', async function () {
-      this.timeout(10000)
+      this.timeout(20000)
 
-      const resVideo = await uploadVideo(servers[0].url, userAccessToken, { name: 'cool video' })
-      const uuid = resVideo.body.video.uuid
+      const { uuid } = await servers[0].videos.upload({ token: userToken, attributes: { name: 'cool video' } })
 
-      const commentText = '**hello** <a href="https://joinpeertube.org">world</a>, <h1>what do you think about peertube?</h1>'
-      const resComment = await addVideoCommentThread(servers[0].url, servers[0].accessToken, uuid, commentText)
-      const commentId = resComment.body.comment.id
+      await servers[0].comments.createThread({ videoId: uuid, text: commentText })
 
       await waitJobs(servers)
-      await checkNewCommentOnMyVideo(baseParams, uuid, commentId, commentId, 'presence')
 
       const latestEmail = emails[emails.length - 1]
-
-      const expected = '<strong style="-ms-text-size-adjust: 100%; -webkit-text-size-adjust: 100%;">hello</strong> ' +
-      '<a href="https://joinpeertube.org" target="_blank" rel="noopener noreferrer" style="-ms-text-size-adjust: 100%; ' +
-      '-webkit-text-size-adjust: 100%; text-decoration: none; color: #f2690d;">world</a>, </p>what do you think about peertube?'
-
-      expect(latestEmail['html']).to.contain(expected)
+      expect(latestEmail['html']).to.contain(expectedHtml)
     })
   })
 
@@ -197,30 +180,19 @@ describe('Test comments notifications', function () {
         server: servers[0],
         emails,
         socketNotifications: userNotifications,
-        token: userAccessToken
+        token: userToken
       }
 
-      await updateMyUser({
-        url: servers[0].url,
-        accessToken: servers[0].accessToken,
-        displayName: 'super root name'
-      })
-
-      await updateMyUser({
-        url: servers[1].url,
-        accessToken: servers[1].accessToken,
-        displayName: 'super root 2 name'
-      })
+      await servers[0].users.updateMe({ displayName: 'super root name' })
+      await servers[1].users.updateMe({ displayName: 'super root 2 name' })
     })
 
     it('Should not send a new mention comment notification if I mention the video owner', async function () {
       this.timeout(10000)
 
-      const resVideo = await uploadVideo(servers[0].url, userAccessToken, { name: 'super video' })
-      const uuid = resVideo.body.video.uuid
+      const { uuid } = await servers[0].videos.upload({ token: userToken, attributes: { name: 'super video' } })
 
-      const resComment = await addVideoCommentThread(servers[0].url, servers[0].accessToken, uuid, '@user_1 hello')
-      const commentId = resComment.body.comment.id
+      const { id: commentId } = await servers[0].comments.createThread({ videoId: uuid, text: '@user_1 hello' })
 
       await waitJobs(servers)
       await checkCommentMention(baseParams, uuid, commentId, commentId, 'super root name', 'absence')
@@ -229,11 +201,9 @@ describe('Test comments notifications', function () {
     it('Should not send a new mention comment notification if I mention myself', async function () {
       this.timeout(10000)
 
-      const resVideo = await uploadVideo(servers[0].url, servers[0].accessToken, { name: 'super video' })
-      const uuid = resVideo.body.video.uuid
+      const { uuid } = await servers[0].videos.upload({ attributes: { name: 'super video' } })
 
-      const resComment = await addVideoCommentThread(servers[0].url, userAccessToken, uuid, '@user_1 hello')
-      const commentId = resComment.body.comment.id
+      const { id: commentId } = await servers[0].comments.createThread({ token: userToken, videoId: uuid, text: '@user_1 hello' })
 
       await waitJobs(servers)
       await checkCommentMention(baseParams, uuid, commentId, commentId, 'super root name', 'absence')
@@ -242,29 +212,25 @@ describe('Test comments notifications', function () {
     it('Should not send a new mention notification if the account is muted', async function () {
       this.timeout(10000)
 
-      await addAccountToAccountBlocklist(servers[0].url, userAccessToken, 'root')
+      await servers[0].blocklist.addToMyBlocklist({ token: userToken, account: 'root' })
 
-      const resVideo = await uploadVideo(servers[0].url, servers[0].accessToken, { name: 'super video' })
-      const uuid = resVideo.body.video.uuid
+      const { uuid } = await servers[0].videos.upload({ attributes: { name: 'super video' } })
 
-      const resComment = await addVideoCommentThread(servers[0].url, servers[0].accessToken, uuid, '@user_1 hello')
-      const commentId = resComment.body.comment.id
+      const { id: commentId } = await servers[0].comments.createThread({ videoId: uuid, text: '@user_1 hello' })
 
       await waitJobs(servers)
       await checkCommentMention(baseParams, uuid, commentId, commentId, 'super root name', 'absence')
 
-      await removeAccountFromAccountBlocklist(servers[0].url, userAccessToken, 'root')
+      await servers[0].blocklist.removeFromMyBlocklist({ token: userToken, account: 'root' })
     })
 
     it('Should not send a new mention notification if the remote account mention a local account', async function () {
       this.timeout(20000)
 
-      const resVideo = await uploadVideo(servers[0].url, servers[0].accessToken, { name: 'super video' })
-      const uuid = resVideo.body.video.uuid
+      const { uuid } = await servers[0].videos.upload({ attributes: { name: 'super video' } })
 
       await waitJobs(servers)
-      const resThread = await addVideoCommentThread(servers[1].url, servers[1].accessToken, uuid, '@user_1 hello')
-      const threadId = resThread.body.comment.id
+      const { id: threadId } = await servers[1].comments.createThread({ videoId: uuid, text: '@user_1 hello' })
 
       await waitJobs(servers)
       await checkCommentMention(baseParams, uuid, threadId, threadId, 'super root 2 name', 'absence')
@@ -273,17 +239,14 @@ describe('Test comments notifications', function () {
     it('Should send a new mention notification after local comments', async function () {
       this.timeout(10000)
 
-      const resVideo = await uploadVideo(servers[0].url, servers[0].accessToken, { name: 'super video' })
-      const uuid = resVideo.body.video.uuid
+      const { uuid } = await servers[0].videos.upload({ attributes: { name: 'super video' } })
 
-      const resThread = await addVideoCommentThread(servers[0].url, servers[0].accessToken, uuid, '@user_1 hello 1')
-      const threadId = resThread.body.comment.id
+      const { id: threadId } = await servers[0].comments.createThread({ videoId: uuid, text: '@user_1 hellotext:  1' })
 
       await waitJobs(servers)
       await checkCommentMention(baseParams, uuid, threadId, threadId, 'super root name', 'presence')
 
-      const resComment = await addVideoCommentReply(servers[0].url, servers[0].accessToken, uuid, threadId, 'hello 2 @user_1')
-      const commentId = resComment.body.comment.id
+      const { id: commentId } = await servers[0].comments.addReply({ videoId: uuid, toCommentId: threadId, text: 'hello 2 @user_1' })
 
       await waitJobs(servers)
       await checkCommentMention(baseParams, uuid, commentId, threadId, 'super root name', 'presence')
@@ -292,35 +255,48 @@ describe('Test comments notifications', function () {
     it('Should send a new mention notification after remote comments', async function () {
       this.timeout(20000)
 
-      const resVideo = await uploadVideo(servers[0].url, servers[0].accessToken, { name: 'super video' })
-      const uuid = resVideo.body.video.uuid
+      const { uuid } = await servers[0].videos.upload({ attributes: { name: 'super video' } })
 
       await waitJobs(servers)
 
       const text1 = `hello @user_1@localhost:${servers[0].port} 1`
-      const resThread = await addVideoCommentThread(servers[1].url, servers[1].accessToken, uuid, text1)
-      const server2ThreadId = resThread.body.comment.id
+      const { id: server2ThreadId } = await servers[1].comments.createThread({ videoId: uuid, text: text1 })
 
       await waitJobs(servers)
 
-      const resThread2 = await getVideoCommentThreads(servers[0].url, uuid, 0, 5)
-      expect(resThread2.body.data).to.have.lengthOf(1)
-      const server1ThreadId = resThread2.body.data[0].id
+      const { data } = await servers[0].comments.listThreads({ videoId: uuid })
+      expect(data).to.have.lengthOf(1)
+
+      const server1ThreadId = data[0].id
       await checkCommentMention(baseParams, uuid, server1ThreadId, server1ThreadId, 'super root 2 name', 'presence')
 
       const text2 = `@user_1@localhost:${servers[0].port} hello 2 @root@localhost:${servers[0].port}`
-      await addVideoCommentReply(servers[1].url, servers[1].accessToken, uuid, server2ThreadId, text2)
+      await servers[1].comments.addReply({ videoId: uuid, toCommentId: server2ThreadId, text: text2 })
 
       await waitJobs(servers)
 
-      const resComments = await getVideoThreadComments(servers[0].url, uuid, server1ThreadId)
-      const tree = resComments.body as VideoCommentThreadTree
+      const tree = await servers[0].comments.getThread({ videoId: uuid, threadId: server1ThreadId })
 
       expect(tree.children).to.have.lengthOf(1)
       const commentId = tree.children[0].comment.id
 
       await checkCommentMention(baseParams, uuid, commentId, server1ThreadId, 'super root 2 name', 'presence')
     })
+
+    it('Should convert markdown in comment to html', async function () {
+      this.timeout(10000)
+
+      const { uuid } = await servers[0].videos.upload({ attributes: { name: 'super video' } })
+
+      const { id: threadId } = await servers[0].comments.createThread({ videoId: uuid, text: '@user_1 hello 1' })
+
+      await servers[0].comments.addReply({ videoId: uuid, toCommentId: threadId, text: '@user_1 ' + commentText })
+
+      await waitJobs(servers)
+
+      const latestEmail = emails[emails.length - 1]
+      expect(latestEmail['html']).to.contain(expectedHtml)
+    })
   })
 
   after(async function () {