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