]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/tests/api/videos/videos-overview.ts
emit more specific status codes on video upload (#3423)
[github/Chocobozzz/PeerTube.git] / server / tests / api / videos / videos-overview.ts
index 233ca8a5dd6f41f43782b76c7c707610f2446e01..7889bcf80a104a46edaef4c59b5ae78318b08105 100644 (file)
@@ -1,39 +1,69 @@
-/* tslint:disable:no-unused-expression */
+/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
 
-import * as chai from 'chai'
 import 'mocha'
-import { flushTests, killallServers, runServer, ServerInfo, setAccessTokensToServers, uploadVideo } from '../../../../shared/extra-utils'
-import { getVideosOverview } from '../../../../shared/extra-utils/overviews/overviews'
+import * as chai from 'chai'
+
+import {
+  cleanupTests,
+  flushAndRunServer,
+  generateUserAccessToken,
+  ServerInfo,
+  setAccessTokensToServers,
+  uploadVideo,
+  wait
+} from '../../../../shared/extra-utils'
+import { getVideosOverview, getVideosOverviewWithToken } from '../../../../shared/extra-utils/overviews/overviews'
 import { VideosOverview } from '../../../../shared/models/overviews'
+import { addAccountToAccountBlocklist } from '@shared/extra-utils/users/blocklist'
+import { Response } from 'superagent'
 
 const expect = chai.expect
 
 describe('Test a videos overview', function () {
   let server: ServerInfo = null
 
+  function testOverviewCount (res: Response, expected: number) {
+    const overview: VideosOverview = res.body
+
+    expect(overview.tags).to.have.lengthOf(expected)
+    expect(overview.categories).to.have.lengthOf(expected)
+    expect(overview.channels).to.have.lengthOf(expected)
+  }
+
   before(async function () {
     this.timeout(30000)
 
-    await flushTests()
-
-    server = await runServer(1)
+    server = await flushAndRunServer(1)
 
     await setAccessTokensToServers([ server ])
   })
 
   it('Should send empty overview', async function () {
-    const res = await getVideosOverview(server.url)
+    const res = await getVideosOverview(server.url, 1)
 
-    const overview: VideosOverview = res.body
-    expect(overview.tags).to.have.lengthOf(0)
-    expect(overview.categories).to.have.lengthOf(0)
-    expect(overview.channels).to.have.lengthOf(0)
+    testOverviewCount(res, 0)
   })
 
   it('Should upload 5 videos in a specific category, tag and channel but not include them in overview', async function () {
     this.timeout(15000)
 
-    for (let i = 0; i < 5; i++) {
+    await wait(3000)
+
+    await uploadVideo(server.url, server.accessToken, {
+      name: 'video 0',
+      category: 3,
+      tags: [ 'coucou1', 'coucou2' ]
+    })
+
+    const res = await getVideosOverview(server.url, 1)
+
+    testOverviewCount(res, 0)
+  })
+
+  it('Should upload another video and include all videos in the overview', async function () {
+    this.timeout(15000)
+
+    for (let i = 1; i < 6; i++) {
       await uploadVideo(server.url, server.accessToken, {
         name: 'video ' + i,
         category: 3,
@@ -41,36 +71,42 @@ describe('Test a videos overview', function () {
       })
     }
 
-    const res = await getVideosOverview(server.url)
+    await wait(3000)
 
-    const overview: VideosOverview = res.body
-    expect(overview.tags).to.have.lengthOf(0)
-    expect(overview.categories).to.have.lengthOf(0)
-    expect(overview.channels).to.have.lengthOf(0)
-  })
+    {
+      const res = await getVideosOverview(server.url, 1)
 
-  it('Should upload another video and include all videos in the overview', async function () {
-    await uploadVideo(server.url, server.accessToken, {
-      name: 'video 5',
-      category: 3,
-      tags: [ 'coucou1', 'coucou2' ]
-    })
+      testOverviewCount(res, 1)
+    }
 
-    const res = await getVideosOverview(server.url)
+    {
+      const res = await getVideosOverview(server.url, 2)
 
-    const overview: VideosOverview = res.body
-    expect(overview.tags).to.have.lengthOf(2)
-    expect(overview.categories).to.have.lengthOf(1)
-    expect(overview.channels).to.have.lengthOf(1)
+      const overview: VideosOverview = res.body
+      expect(overview.tags).to.have.lengthOf(1)
+      expect(overview.categories).to.have.lengthOf(0)
+      expect(overview.channels).to.have.lengthOf(0)
+    }
   })
 
   it('Should have the correct overview', async function () {
-    const res = await getVideosOverview(server.url)
+    const res1 = await getVideosOverview(server.url, 1)
+    const res2 = await getVideosOverview(server.url, 2)
 
-    const overview: VideosOverview = res.body
+    const overview1: VideosOverview = res1.body
+    const overview2: VideosOverview = res2.body
+
+    const tmp = [
+      overview1.tags,
+      overview1.categories,
+      overview1.channels,
+      overview2.tags
+    ]
 
-    for (const attr of [ 'tags', 'categories', 'channels' ]) {
-      const obj = overview[attr][0]
+    for (const arr of tmp) {
+      expect(arr).to.have.lengthOf(1)
+
+      const obj = arr[0]
 
       expect(obj.videos).to.have.lengthOf(6)
       expect(obj.videos[0].name).to.equal('video 5')
@@ -81,20 +117,34 @@ describe('Test a videos overview', function () {
       expect(obj.videos[5].name).to.equal('video 0')
     }
 
-    expect(overview.tags.find(t => t.tag === 'coucou1')).to.not.be.undefined
-    expect(overview.tags.find(t => t.tag === 'coucou2')).to.not.be.undefined
+    const tags = [ overview1.tags[0].tag, overview2.tags[0].tag ]
+    expect(tags.find(t => t === 'coucou1')).to.not.be.undefined
+    expect(tags.find(t => t === 'coucou2')).to.not.be.undefined
 
-    expect(overview.categories[0].category.id).to.equal(3)
+    expect(overview1.categories[0].category.id).to.equal(3)
 
-    expect(overview.channels[0].channel.name).to.equal('root_channel')
+    expect(overview1.channels[0].channel.name).to.equal('root_channel')
   })
 
-  after(async function () {
-    killallServers([ server ])
+  it('Should hide muted accounts', async function () {
+    const token = await generateUserAccessToken(server, 'choco')
+
+    await addAccountToAccountBlocklist(server.url, token, 'root@' + server.host)
+
+    {
+      const res = await getVideosOverview(server.url, 1)
+
+      testOverviewCount(res, 1)
+    }
+
+    {
+      const res = await getVideosOverviewWithToken(server.url, 1, token)
 
-    // Keep the logs if the test failed
-    if (this['ok']) {
-      await flushTests()
+      testOverviewCount(res, 0)
     }
   })
+
+  after(async function () {
+    await cleanupTests([ server ])
+  })
 })