aboutsummaryrefslogtreecommitdiffhomepage
path: root/packages/tests/src/api/server/logs.ts
blob: 11c86d694281dca99eba78f9dcfbe4d385172229 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */

import { expect } from 'chai'
import { HttpStatusCode } from '@peertube/peertube-models'
import {
  cleanupTests,
  createSingleServer,
  killallServers,
  LogsCommand,
  PeerTubeServer,
  setAccessTokensToServers,
  waitJobs
} from '@peertube/peertube-server-commands'

describe('Test logs', function () {
  let server: PeerTubeServer
  let logsCommand: LogsCommand

  before(async function () {
    this.timeout(30000)

    server = await createSingleServer(1)
    await setAccessTokensToServers([ server ])

    logsCommand = server.logs
  })

  describe('With the standard log file', function () {

    it('Should get logs with a start date', async function () {
      this.timeout(60000)

      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 with name video 1')).to.be.false
      expect(logsString.includes('Video with name video 2')).to.be.true
    })

    it('Should get logs with an end date', async function () {
      this.timeout(60000)

      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 with name video 3')).to.be.false
      expect(logsString.includes('Video with name video 4')).to.be.true
      expect(logsString.includes('Video with name video 5')).to.be.false
    })

    it('Should filter by level', async function () {
      this.timeout(60000)

      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 with name video 6')).to.be.true
      }

      {
        const body = await logsCommand.getLogs({ startDate: now, level: 'warn' })
        const logsString = JSON.stringify(body)

        expect(logsString.includes('Video with name video 6')).to.be.false
      }
    })

    it('Should filter by tag', async function () {
      const now = new Date()

      const { uuid } = await server.videos.upload({ attributes: { name: 'video 6' } })
      await waitJobs([ server ])

      {
        const body = await logsCommand.getLogs({ startDate: now, level: 'debug', tagsOneOf: [ 'toto' ] })
        expect(body).to.have.lengthOf(0)
      }

      {
        const body = await logsCommand.getLogs({ startDate: now, level: 'debug', tagsOneOf: [ uuid ] })
        expect(body).to.not.have.lengthOf(0)

        for (const line of body) {
          expect(line.tags).to.contain(uuid)
        }
      }
    })

    it('Should log ping requests', async function () {
      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(60000)

      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
    })
  })

  describe('With the audit log', function () {

    it('Should get logs with a start date', async function () {
      this.timeout(60000)

      await server.videos.upload({ attributes: { name: 'video 7' } })
      await waitJobs([ server ])

      const now = new Date()

      await server.videos.upload({ attributes: { name: 'video 8' } })
      await waitJobs([ server ])

      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(body).to.have.lengthOf(1)

      const item = body[0]

      const message = JSON.parse(item.message)
      expect(message.domain).to.equal('videos')
      expect(message.action).to.equal('create')
    })

    it('Should get logs with an end date', async function () {
      this.timeout(60000)

      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
    })
  })

  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()

      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'
        }
      })

      const body = await logsCommand.getLogs({ startDate: now })
      const logsString = JSON.stringify(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
    })

    it('Should refuse to create client logs', async function () {
      await server.kill()

      await server.run({
        log: {
          accept_client_log: false
        }
      })

      await server.logs.createLogClient({
        payload: {
          level: 'warn',
          url: 'http://example.com',
          message: 'my super client message'
        },
        expectedStatus: HttpStatusCode.FORBIDDEN_403
      })
    })
  })

  after(async function () {
    await cleanupTests([ server ])
  })
})