]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/tests/api/server/logs.ts
Shorter server command names
[github/Chocobozzz/PeerTube.git] / server / tests / api / server / logs.ts
CommitLineData
a1587156 1/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
fd8710b8 2
fd8710b8 3import 'mocha'
c9c43612
C
4import * as chai from 'chai'
5import {
6 cleanupTests,
7 flushAndRunServer,
8 killallServers,
a92ddacb 9 LogsCommand,
c9c43612
C
10 reRunServer,
11 ServerInfo,
a92ddacb 12 setAccessTokensToServers,
a92ddacb
C
13 waitJobs
14} from '@shared/extra-utils'
fd8710b8
C
15
16const expect = chai.expect
17
18describe('Test logs', function () {
19 let server: ServerInfo
a92ddacb 20 let logsCommand: LogsCommand
fd8710b8
C
21
22 before(async function () {
23 this.timeout(30000)
24
210feb6c 25 server = await flushAndRunServer(1)
fd8710b8 26 await setAccessTokensToServers([ server ])
a92ddacb 27
89d241a7 28 logsCommand = server.logs
fd8710b8
C
29 })
30
566c125d 31 describe('With the standard log file', function () {
78d62f4d 32
566c125d 33 it('Should get logs with a start date', async function () {
ff9112ad 34 this.timeout(20000)
fd8710b8 35
89d241a7 36 await server.videos.upload({ attributes: { name: 'video 1' } })
566c125d 37 await waitJobs([ server ])
fd8710b8 38
566c125d 39 const now = new Date()
fd8710b8 40
89d241a7 41 await server.videos.upload({ attributes: { name: 'video 2' } })
566c125d 42 await waitJobs([ server ])
fd8710b8 43
a92ddacb
C
44 const body = await logsCommand.getLogs({ startDate: now })
45 const logsString = JSON.stringify(body)
fd8710b8 46
566c125d
C
47 expect(logsString.includes('video 1')).to.be.false
48 expect(logsString.includes('video 2')).to.be.true
49 })
50
51 it('Should get logs with an end date', async function () {
ff9112ad 52 this.timeout(30000)
566c125d 53
89d241a7 54 await server.videos.upload({ attributes: { name: 'video 3' } })
566c125d
C
55 await waitJobs([ server ])
56
57 const now1 = new Date()
58
89d241a7 59 await server.videos.upload({ attributes: { name: 'video 4' } })
566c125d
C
60 await waitJobs([ server ])
61
62 const now2 = new Date()
63
89d241a7 64 await server.videos.upload({ attributes: { name: 'video 5' } })
566c125d
C
65 await waitJobs([ server ])
66
a92ddacb
C
67 const body = await logsCommand.getLogs({ startDate: now1, endDate: now2 })
68 const logsString = JSON.stringify(body)
fd8710b8 69
566c125d
C
70 expect(logsString.includes('video 3')).to.be.false
71 expect(logsString.includes('video 4')).to.be.true
72 expect(logsString.includes('video 5')).to.be.false
73 })
fd8710b8 74
566c125d 75 it('Should get filter by level', async function () {
ff9112ad 76 this.timeout(20000)
fd8710b8 77
566c125d 78 const now = new Date()
fd8710b8 79
89d241a7 80 await server.videos.upload({ attributes: { name: 'video 6' } })
566c125d 81 await waitJobs([ server ])
fd8710b8 82
566c125d 83 {
a92ddacb
C
84 const body = await logsCommand.getLogs({ startDate: now, level: 'info' })
85 const logsString = JSON.stringify(body)
fd8710b8 86
566c125d
C
87 expect(logsString.includes('video 6')).to.be.true
88 }
fd8710b8 89
566c125d 90 {
a92ddacb
C
91 const body = await logsCommand.getLogs({ startDate: now, level: 'warn' })
92 const logsString = JSON.stringify(body)
fd8710b8 93
566c125d
C
94 expect(logsString.includes('video 6')).to.be.false
95 }
96 })
78d62f4d
C
97
98 it('Should log ping requests', async function () {
ff9112ad
C
99 this.timeout(10000)
100
78d62f4d
C
101 const now = new Date()
102
89d241a7 103 await server.servers.ping()
78d62f4d 104
a92ddacb
C
105 const body = await logsCommand.getLogs({ startDate: now, level: 'info' })
106 const logsString = JSON.stringify(body)
78d62f4d
C
107
108 expect(logsString.includes('/api/v1/ping')).to.be.true
109 })
110
111 it('Should not log ping requests', async function () {
112 this.timeout(30000)
113
9293139f 114 await killallServers([ server ])
78d62f4d
C
115
116 await reRunServer(server, { log: { log_ping_requests: false } })
117
118 const now = new Date()
119
89d241a7 120 await server.servers.ping()
78d62f4d 121
a92ddacb
C
122 const body = await logsCommand.getLogs({ startDate: now, level: 'info' })
123 const logsString = JSON.stringify(body)
78d62f4d
C
124
125 expect(logsString.includes('/api/v1/ping')).to.be.false
126 })
fd8710b8
C
127 })
128
566c125d
C
129 describe('With the audit log', function () {
130 it('Should get logs with a start date', async function () {
ff9112ad 131 this.timeout(20000)
fd8710b8 132
89d241a7 133 await server.videos.upload({ attributes: { name: 'video 7' } })
566c125d 134 await waitJobs([ server ])
fd8710b8 135
566c125d 136 const now = new Date()
fd8710b8 137
89d241a7 138 await server.videos.upload({ attributes: { name: 'video 8' } })
566c125d
C
139 await waitJobs([ server ])
140
a92ddacb
C
141 const body = await logsCommand.getAuditLogs({ startDate: now })
142 const logsString = JSON.stringify(body)
fd8710b8 143
566c125d
C
144 expect(logsString.includes('video 7')).to.be.false
145 expect(logsString.includes('video 8')).to.be.true
146
a92ddacb 147 expect(body).to.have.lengthOf(1)
566c125d 148
a92ddacb 149 const item = body[0]
566c125d
C
150
151 const message = JSON.parse(item.message)
152 expect(message.domain).to.equal('videos')
153 expect(message.action).to.equal('create')
154 })
155
156 it('Should get logs with an end date', async function () {
ff9112ad 157 this.timeout(30000)
566c125d 158
89d241a7 159 await server.videos.upload({ attributes: { name: 'video 9' } })
566c125d
C
160 await waitJobs([ server ])
161
162 const now1 = new Date()
163
89d241a7 164 await server.videos.upload({ attributes: { name: 'video 10' } })
566c125d
C
165 await waitJobs([ server ])
166
167 const now2 = new Date()
168
89d241a7 169 await server.videos.upload({ attributes: { name: 'video 11' } })
566c125d 170 await waitJobs([ server ])
fd8710b8 171
a92ddacb
C
172 const body = await logsCommand.getAuditLogs({ startDate: now1, endDate: now2 })
173 const logsString = JSON.stringify(body)
fd8710b8 174
566c125d
C
175 expect(logsString.includes('video 9')).to.be.false
176 expect(logsString.includes('video 10')).to.be.true
177 expect(logsString.includes('video 11')).to.be.false
178 })
fd8710b8
C
179 })
180
7c3b7976
C
181 after(async function () {
182 await cleanupTests([ server ])
fd8710b8
C
183 })
184})