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