]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/tests/api/server/logs.ts
Handle rejected follows in client
[github/Chocobozzz/PeerTube.git] / server / tests / api / server / logs.ts
index 05b0308deba1c82cc110b37eba8362863f74716f..ed7555fd76691023f3825c9df40acc4a7ed42674 100644 (file)
-/* 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 } from '../../../../shared/utils/index'
-import { waitJobs } from '../../../../shared/utils/server/jobs'
-import { uploadVideo } from '../../../../shared/utils/videos/videos'
-import { getLogs } from '../../../../shared/utils/logs/logs'
+import * as chai from 'chai'
+import { HttpStatusCode } from '@shared/models'
+import {
+  cleanupTests,
+  createSingleServer,
+  killallServers,
+  LogsCommand,
+  PeerTubeServer,
+  setAccessTokensToServers,
+  waitJobs
+} from '@shared/server-commands'
 
 const expect = chai.expect
 
 describe('Test logs', function () {
-  let server: ServerInfo
+  let server: PeerTubeServer
+  let logsCommand: LogsCommand
 
   before(async function () {
     this.timeout(30000)
 
-    await flushTests()
-
-    server = await runServer(1)
+    server = await createSingleServer(1)
     await setAccessTokensToServers([ server ])
+
+    logsCommand = server.logs
   })
 
-  it('Should get logs with a start date', async function () {
-    this.timeout(10000)
+  describe('With the standard log file', function () {
+
+    it('Should get logs with a start date', async function () {
+      this.timeout(20000)
+
+      await server.videos.upload({ attributes: { name: 'video 1' } })
+      await waitJobs([ server ])
+
+      const now = new Date()
+
+      await server.videos.upload({ attributes: { name: 'video 2' } })
+      await waitJobs([ server ])
+
+      const body = await logsCommand.getLogs({ startDate: now })
+      const logsString = JSON.stringify(body)
+
+      expect(logsString.includes('video 1')).to.be.false
+      expect(logsString.includes('video 2')).to.be.true
+    })
+
+    it('Should get logs with an end date', async function () {
+      this.timeout(30000)
+
+      await server.videos.upload({ attributes: { name: 'video 3' } })
+      await waitJobs([ server ])
+
+      const now1 = new Date()
+
+      await server.videos.upload({ attributes: { name: 'video 4' } })
+      await waitJobs([ server ])
+
+      const now2 = new Date()
+
+      await server.videos.upload({ attributes: { name: 'video 5' } })
+      await waitJobs([ server ])
+
+      const body = await logsCommand.getLogs({ startDate: now1, endDate: now2 })
+      const logsString = JSON.stringify(body)
+
+      expect(logsString.includes('video 3')).to.be.false
+      expect(logsString.includes('video 4')).to.be.true
+      expect(logsString.includes('video 5')).to.be.false
+    })
+
+    it('Should filter by level', async function () {
+      this.timeout(20000)
+
+      const now = new Date()
+
+      await server.videos.upload({ attributes: { name: 'video 6' } })
+      await waitJobs([ server ])
+
+      {
+        const body = await logsCommand.getLogs({ startDate: now, level: 'info' })
+        const logsString = JSON.stringify(body)
+
+        expect(logsString.includes('video 6')).to.be.true
+      }
+
+      {
+        const body = await logsCommand.getLogs({ startDate: now, level: 'warn' })
+        const logsString = JSON.stringify(body)
+
+        expect(logsString.includes('video 6')).to.be.false
+      }
+    })
+
+    it('Should filter by tag', async function () {
+      const now = new Date()
 
-    await uploadVideo(server.url, server.accessToken, { name: 'video 1' })
-    await waitJobs([ server ])
+      const { uuid } = await server.videos.upload({ attributes: { name: 'video 6' } })
+      await waitJobs([ server ])
 
-    const now = new Date()
+      {
+        const body = await logsCommand.getLogs({ startDate: now, level: 'debug', tagsOneOf: [ 'toto' ] })
+        expect(body).to.have.lengthOf(0)
+      }
 
-    await uploadVideo(server.url, server.accessToken, { name: 'video 2' })
-    await waitJobs([ server ])
+      {
+        const body = await logsCommand.getLogs({ startDate: now, level: 'debug', tagsOneOf: [ uuid ] })
+        expect(body).to.not.have.lengthOf(0)
 
-    const res = await getLogs(server.url, server.accessToken, now)
-    const logsString = JSON.stringify(res.body)
+        for (const line of body) {
+          expect(line.tags).to.contain(uuid)
+        }
+      }
+    })
 
-    expect(logsString.includes('video 1')).to.be.false
-    expect(logsString.includes('video 2')).to.be.true
+    it('Should log ping requests', async function () {
+      this.timeout(10000)
+
+      const now = new Date()
+
+      await server.servers.ping()
+
+      const body = await logsCommand.getLogs({ startDate: now, level: 'info' })
+      const logsString = JSON.stringify(body)
+
+      expect(logsString.includes('/api/v1/ping')).to.be.true
+    })
+
+    it('Should not log ping requests', async function () {
+      this.timeout(30000)
+
+      await killallServers([ server ])
+
+      await server.run({ log: { log_ping_requests: false } })
+
+      const now = new Date()
+
+      await server.servers.ping()
+
+      const body = await logsCommand.getLogs({ startDate: now, level: 'info' })
+      const logsString = JSON.stringify(body)
+
+      expect(logsString.includes('/api/v1/ping')).to.be.false
+    })
   })
 
-  it('Should get logs with an end date', async function () {
-    this.timeout(10000)
+  describe('With the audit log', function () {
+    it('Should get logs with a start date', async function () {
+      this.timeout(20000)
+
+      await server.videos.upload({ attributes: { name: 'video 7' } })
+      await waitJobs([ server ])
+
+      const now = new Date()
 
-    await uploadVideo(server.url, server.accessToken, { name: 'video 3' })
-    await waitJobs([ server ])
+      await server.videos.upload({ attributes: { name: 'video 8' } })
+      await waitJobs([ server ])
 
-    const now1 = new Date()
+      const body = await logsCommand.getAuditLogs({ startDate: now })
+      const logsString = JSON.stringify(body)
 
-    await uploadVideo(server.url, server.accessToken, { name: 'video 4' })
-    await waitJobs([ server ])
+      expect(logsString.includes('video 7')).to.be.false
+      expect(logsString.includes('video 8')).to.be.true
 
-    const now2 = new Date()
+      expect(body).to.have.lengthOf(1)
 
-    await uploadVideo(server.url, server.accessToken, { name: 'video 5' })
-    await waitJobs([ server ])
+      const item = body[0]
 
-    const res = await getLogs(server.url, server.accessToken, now1, now2)
-    const logsString = JSON.stringify(res.body)
+      const message = JSON.parse(item.message)
+      expect(message.domain).to.equal('videos')
+      expect(message.action).to.equal('create')
+    })
 
-    expect(logsString.includes('video 3')).to.be.false
-    expect(logsString.includes('video 4')).to.be.true
-    expect(logsString.includes('video 5')).to.be.false
+    it('Should get logs with an end date', async function () {
+      this.timeout(30000)
+
+      await server.videos.upload({ attributes: { name: 'video 9' } })
+      await waitJobs([ server ])
+
+      const now1 = new Date()
+
+      await server.videos.upload({ attributes: { name: 'video 10' } })
+      await waitJobs([ server ])
+
+      const now2 = new Date()
+
+      await server.videos.upload({ attributes: { name: 'video 11' } })
+      await waitJobs([ server ])
+
+      const body = await logsCommand.getAuditLogs({ startDate: now1, endDate: now2 })
+      const logsString = JSON.stringify(body)
+
+      expect(logsString.includes('video 9')).to.be.false
+      expect(logsString.includes('video 10')).to.be.true
+      expect(logsString.includes('video 11')).to.be.false
+    })
   })
 
-  it('Should get filter by level', async function () {
-    this.timeout(10000)
+  describe('When creating log from the client', function () {
+
+    it('Should create a warn client log', async function () {
+      const now = new Date()
+
+      await server.logs.createLogClient({
+        payload: {
+          level: 'warn',
+          url: 'http://example.com',
+          message: 'my super client message'
+        },
+        token: null
+      })
+
+      const body = await logsCommand.getLogs({ startDate: now })
+      const logsString = JSON.stringify(body)
+
+      expect(logsString.includes('my super client message')).to.be.true
+    })
+
+    it('Should create an error authenticated client log', async function () {
+      const now = new Date()
 
-    const now = new Date()
+      await server.logs.createLogClient({
+        payload: {
+          url: 'https://example.com/page1',
+          level: 'error',
+          message: 'my super client message 2',
+          userAgent: 'super user agent',
+          meta: '{hello}',
+          stackTrace: 'super stack trace'
+        }
+      })
 
-    await uploadVideo(server.url, server.accessToken, { name: 'video 6' })
-    await waitJobs([ server ])
+      const body = await logsCommand.getLogs({ startDate: now })
+      const logsString = JSON.stringify(body)
 
-    {
-      const res = await getLogs(server.url, server.accessToken, now, undefined, 'info')
-      const logsString = JSON.stringify(res.body)
+      expect(logsString.includes('my super client message 2')).to.be.true
+      expect(logsString.includes('super user agent')).to.be.true
+      expect(logsString.includes('super stack trace')).to.be.true
+      expect(logsString.includes('{hello}')).to.be.true
+      expect(logsString.includes('https://example.com/page1')).to.be.true
+    })
 
-      expect(logsString.includes('video 6')).to.be.true
-    }
+    it('Should refuse to create client logs', async function () {
+      await server.kill()
 
-    {
-      const res = await getLogs(server.url, server.accessToken, now, undefined, 'warn')
-      const logsString = JSON.stringify(res.body)
+      await server.run({
+        log: {
+          accept_client_log: false
+        }
+      })
 
-      expect(logsString.includes('video 6')).to.be.false
-    }
+      await server.logs.createLogClient({
+        payload: {
+          level: 'warn',
+          url: 'http://example.com',
+          message: 'my super client message'
+        },
+        expectedStatus: HttpStatusCode.FORBIDDEN_403
+      })
+    })
   })
 
   after(async function () {
-    killallServers([ server ])
+    await cleanupTests([ server ])
   })
 })