]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/commitdiff
Introduce logs command
authorChocobozzz <me@florianbigard.com>
Tue, 6 Jul 2021 08:34:29 +0000 (10:34 +0200)
committerChocobozzz <me@florianbigard.com>
Tue, 20 Jul 2021 13:27:16 +0000 (15:27 +0200)
server/tests/api/server/logs.ts
shared/extra-utils/index.ts
shared/extra-utils/logs/index.ts [new file with mode: 0644]
shared/extra-utils/logs/logs-command.ts [new file with mode: 0644]
shared/extra-utils/logs/logs.ts [deleted file]
shared/extra-utils/server/servers.ts
shared/extra-utils/shared/abstract-command.ts

index bc398ea731ac04bd8479fa1114703edb62c11a91..ab83a329f4966132f1d34ca221bc51137eac564c 100644 (file)
@@ -6,25 +6,28 @@ import {
   cleanupTests,
   flushAndRunServer,
   killallServers,
+  LogsCommand,
   makePingRequest,
   reRunServer,
   ServerInfo,
-  setAccessTokensToServers
-} from '../../../../shared/extra-utils/index'
-import { getAuditLogs, getLogs } from '../../../../shared/extra-utils/logs/logs'
-import { waitJobs } from '../../../../shared/extra-utils/server/jobs'
-import { uploadVideo } from '../../../../shared/extra-utils/videos/videos'
+  setAccessTokensToServers,
+  uploadVideo,
+  waitJobs
+} from '@shared/extra-utils'
 
 const expect = chai.expect
 
 describe('Test logs', function () {
   let server: ServerInfo
+  let logsCommand: LogsCommand
 
   before(async function () {
     this.timeout(30000)
 
     server = await flushAndRunServer(1)
     await setAccessTokensToServers([ server ])
+
+    logsCommand = server.logsCommand
   })
 
   describe('With the standard log file', function () {
@@ -40,8 +43,8 @@ describe('Test logs', function () {
       await uploadVideo(server.url, server.accessToken, { name: 'video 2' })
       await waitJobs([ server ])
 
-      const res = await getLogs(server.url, server.accessToken, now)
-      const logsString = JSON.stringify(res.body)
+      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
@@ -63,8 +66,8 @@ describe('Test logs', function () {
       await uploadVideo(server.url, server.accessToken, { name: 'video 5' })
       await waitJobs([ server ])
 
-      const res = await getLogs(server.url, server.accessToken, now1, now2)
-      const logsString = JSON.stringify(res.body)
+      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
@@ -80,15 +83,15 @@ describe('Test logs', function () {
       await waitJobs([ server ])
 
       {
-        const res = await getLogs(server.url, server.accessToken, now, undefined, 'info')
-        const logsString = JSON.stringify(res.body)
+        const body = await logsCommand.getLogs({ startDate: now, level: 'info' })
+        const logsString = JSON.stringify(body)
 
         expect(logsString.includes('video 6')).to.be.true
       }
 
       {
-        const res = await getLogs(server.url, server.accessToken, now, undefined, 'warn')
-        const logsString = JSON.stringify(res.body)
+        const body = await logsCommand.getLogs({ startDate: now, level: 'warn' })
+        const logsString = JSON.stringify(body)
 
         expect(logsString.includes('video 6')).to.be.false
       }
@@ -101,8 +104,8 @@ describe('Test logs', function () {
 
       await makePingRequest(server)
 
-      const res = await getLogs(server.url, server.accessToken, now, undefined, 'info')
-      const logsString = JSON.stringify(res.body)
+      const body = await logsCommand.getLogs({ startDate: now, level: 'info' })
+      const logsString = JSON.stringify(body)
 
       expect(logsString.includes('/api/v1/ping')).to.be.true
     })
@@ -118,8 +121,8 @@ describe('Test logs', function () {
 
       await makePingRequest(server)
 
-      const res = await getLogs(server.url, server.accessToken, now, undefined, 'info')
-      const logsString = JSON.stringify(res.body)
+      const body = await logsCommand.getLogs({ startDate: now, level: 'info' })
+      const logsString = JSON.stringify(body)
 
       expect(logsString.includes('/api/v1/ping')).to.be.false
     })
@@ -137,15 +140,15 @@ describe('Test logs', function () {
       await uploadVideo(server.url, server.accessToken, { name: 'video 8' })
       await waitJobs([ server ])
 
-      const res = await getAuditLogs(server.url, server.accessToken, now)
-      const logsString = JSON.stringify(res.body)
+      const body = await logsCommand.getAuditLogs({ startDate: now })
+      const logsString = JSON.stringify(body)
 
       expect(logsString.includes('video 7')).to.be.false
       expect(logsString.includes('video 8')).to.be.true
 
-      expect(res.body).to.have.lengthOf(1)
+      expect(body).to.have.lengthOf(1)
 
-      const item = res.body[0]
+      const item = body[0]
 
       const message = JSON.parse(item.message)
       expect(message.domain).to.equal('videos')
@@ -168,8 +171,8 @@ describe('Test logs', function () {
       await uploadVideo(server.url, server.accessToken, { name: 'video 11' })
       await waitJobs([ server ])
 
-      const res = await getAuditLogs(server.url, server.accessToken, now1, now2)
-      const logsString = JSON.stringify(res.body)
+      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
index c4f0afdc48ad213ece43069dfead6eb0d492798e..a3d3970afbbbfa588950395194cda809a15d1414 100644 (file)
@@ -6,6 +6,8 @@ export * from './custom-pages'
 
 export * from './feeds'
 
+export * from './logs'
+
 export * from './mock-servers/mock-instances-index'
 
 export * from './miscs/email'
diff --git a/shared/extra-utils/logs/index.ts b/shared/extra-utils/logs/index.ts
new file mode 100644 (file)
index 0000000..69452d7
--- /dev/null
@@ -0,0 +1 @@
+export * from './logs-command'
diff --git a/shared/extra-utils/logs/logs-command.ts b/shared/extra-utils/logs/logs-command.ts
new file mode 100644 (file)
index 0000000..f759473
--- /dev/null
@@ -0,0 +1,41 @@
+import { HttpStatusCode } from '../../core-utils/miscs/http-error-codes'
+import { LogLevel } from '../../models/server/log-level.type'
+import { AbstractCommand, OverrideCommandOptions } from '../shared'
+
+export class LogsCommand extends AbstractCommand {
+
+  getLogs (options: OverrideCommandOptions & {
+    startDate: Date
+    endDate?: Date
+    level?: LogLevel
+  }) {
+    const { startDate, endDate, level } = options
+    const path = '/api/v1/server/logs'
+
+    return this.getRequestBody({
+      ...options,
+
+      path,
+      query: { startDate, endDate, level },
+      defaultExpectedStatus: HttpStatusCode.OK_200
+    })
+  }
+
+  getAuditLogs (options: OverrideCommandOptions & {
+    startDate: Date
+    endDate?: Date
+  }) {
+    const { startDate, endDate } = options
+
+    const path = '/api/v1/server/audit-logs'
+
+    return this.getRequestBody({
+      ...options,
+
+      path,
+      query: { startDate, endDate },
+      defaultExpectedStatus: HttpStatusCode.OK_200
+    })
+  }
+
+}
diff --git a/shared/extra-utils/logs/logs.ts b/shared/extra-utils/logs/logs.ts
deleted file mode 100644 (file)
index 8d74127..0000000
+++ /dev/null
@@ -1,32 +0,0 @@
-import { makeGetRequest } from '../requests/requests'
-import { LogLevel } from '../../models/server/log-level.type'
-import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes'
-
-function getLogs (url: string, accessToken: string, startDate: Date, endDate?: Date, level?: LogLevel) {
-  const path = '/api/v1/server/logs'
-
-  return makeGetRequest({
-    url,
-    path,
-    token: accessToken,
-    query: { startDate, endDate, level },
-    statusCodeExpected: HttpStatusCode.OK_200
-  })
-}
-
-function getAuditLogs (url: string, accessToken: string, startDate: Date, endDate?: Date) {
-  const path = '/api/v1/server/audit-logs'
-
-  return makeGetRequest({
-    url,
-    path,
-    token: accessToken,
-    query: { startDate, endDate },
-    statusCodeExpected: HttpStatusCode.OK_200
-  })
-}
-
-export {
-  getLogs,
-  getAuditLogs
-}
index b64c9eec66f9c43457845004dfa2c310ca3be7fe..4343eab934b2840377f9f1eedfcc4367e834cd33 100644 (file)
@@ -10,6 +10,7 @@ import { BulkCommand } from '../bulk'
 import { CLICommand } from '../cli'
 import { CustomPagesCommand } from '../custom-pages'
 import { FeedCommand } from '../feeds'
+import { LogsCommand } from '../logs'
 import { buildServerDirectory, getFileSize, isGithubCI, root, wait } from '../miscs/miscs'
 import { makeGetRequest } from '../requests/requests'
 
@@ -69,6 +70,7 @@ interface ServerInfo {
   cliCommand?: CLICommand
   customPageCommand?: CustomPagesCommand
   feedCommand?: FeedCommand
+  logsCommand?: LogsCommand
 }
 
 function parallelTests () {
@@ -278,6 +280,7 @@ async function runServer (server: ServerInfo, configOverrideArg?: any, args = []
       server.cliCommand = new CLICommand(server)
       server.customPageCommand = new CustomPagesCommand(server)
       server.feedCommand = new FeedCommand(server)
+      server.logsCommand = new LogsCommand(server)
 
       res(server)
     })
index 7f812daa898240bdfee7e2645e81774ef6213dbf..a572222161ac0c4f05f2e77fa8eda49549be7985 100644 (file)
@@ -13,7 +13,7 @@ interface CommonCommandOptions extends OverrideCommandOptions {
 }
 
 interface GetCommandOptions extends CommonCommandOptions {
-  query?: { [ id: string ]: string }
+  query?: { [ id: string ]: any }
   contentType?: string
   accept?: string
 }