aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/tests/api/check-params/logs.ts
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2023-07-31 14:34:36 +0200
committerChocobozzz <me@florianbigard.com>2023-08-11 15:02:33 +0200
commit3a4992633ee62d5edfbb484d9c6bcb3cf158489d (patch)
treee4510b39bdac9c318fdb4b47018d08f15368b8f0 /server/tests/api/check-params/logs.ts
parent04d1da5621d25d59bd5fa1543b725c497bf5d9a8 (diff)
downloadPeerTube-3a4992633ee62d5edfbb484d9c6bcb3cf158489d.tar.gz
PeerTube-3a4992633ee62d5edfbb484d9c6bcb3cf158489d.tar.zst
PeerTube-3a4992633ee62d5edfbb484d9c6bcb3cf158489d.zip
Migrate server to ESM
Sorry for the very big commit that may lead to git log issues and merge conflicts, but it's a major step forward: * Server can be faster at startup because imports() are async and we can easily lazy import big modules * Angular doesn't seem to support ES import (with .js extension), so we had to correctly organize peertube into a monorepo: * Use yarn workspace feature * Use typescript reference projects for dependencies * Shared projects have been moved into "packages", each one is now a node module (with a dedicated package.json/tsconfig.json) * server/tools have been moved into apps/ and is now a dedicated app bundled and published on NPM so users don't have to build peertube cli tools manually * server/tests have been moved into packages/ so we don't compile them every time we want to run the server * Use isolatedModule option: * Had to move from const enum to const (https://www.typescriptlang.org/docs/handbook/enums.html#objects-vs-enums) * Had to explictely specify "type" imports when used in decorators * Prefer tsx (that uses esbuild under the hood) instead of ts-node to load typescript files (tests with mocha or scripts): * To reduce test complexity as esbuild doesn't support decorator metadata, we only test server files that do not import server models * We still build tests files into js files for a faster CI * Remove unmaintained peertube CLI import script * Removed some barrels to speed up execution (less imports)
Diffstat (limited to 'server/tests/api/check-params/logs.ts')
-rw-r--r--server/tests/api/check-params/logs.ts157
1 files changed, 0 insertions, 157 deletions
diff --git a/server/tests/api/check-params/logs.ts b/server/tests/api/check-params/logs.ts
deleted file mode 100644
index 2496cee31..000000000
--- a/server/tests/api/check-params/logs.ts
+++ /dev/null
@@ -1,157 +0,0 @@
1/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
2
3import { expect } from 'chai'
4import { HttpStatusCode } from '@shared/models'
5import { cleanupTests, createSingleServer, makeGetRequest, PeerTubeServer, setAccessTokensToServers } from '@shared/server-commands'
6
7describe('Test logs API validators', function () {
8 const path = '/api/v1/server/logs'
9 let server: PeerTubeServer
10 let userAccessToken = ''
11
12 // ---------------------------------------------------------------
13
14 before(async function () {
15 this.timeout(120000)
16
17 server = await createSingleServer(1)
18
19 await setAccessTokensToServers([ server ])
20
21 const user = {
22 username: 'user1',
23 password: 'my super password'
24 }
25 await server.users.create({ username: user.username, password: user.password })
26 userAccessToken = await server.login.getAccessToken(user)
27 })
28
29 describe('When getting logs', function () {
30
31 it('Should fail with a non authenticated user', async function () {
32 await makeGetRequest({
33 url: server.url,
34 path,
35 expectedStatus: HttpStatusCode.UNAUTHORIZED_401
36 })
37 })
38
39 it('Should fail with a non admin user', async function () {
40 await makeGetRequest({
41 url: server.url,
42 path,
43 token: userAccessToken,
44 expectedStatus: HttpStatusCode.FORBIDDEN_403
45 })
46 })
47
48 it('Should fail with a missing startDate query', async function () {
49 await makeGetRequest({
50 url: server.url,
51 path,
52 token: server.accessToken,
53 expectedStatus: HttpStatusCode.BAD_REQUEST_400
54 })
55 })
56
57 it('Should fail with a bad startDate query', async function () {
58 await makeGetRequest({
59 url: server.url,
60 path,
61 token: server.accessToken,
62 query: { startDate: 'toto' },
63 expectedStatus: HttpStatusCode.BAD_REQUEST_400
64 })
65 })
66
67 it('Should fail with a bad endDate query', async function () {
68 await makeGetRequest({
69 url: server.url,
70 path,
71 token: server.accessToken,
72 query: { startDate: new Date().toISOString(), endDate: 'toto' },
73 expectedStatus: HttpStatusCode.BAD_REQUEST_400
74 })
75 })
76
77 it('Should fail with a bad level parameter', async function () {
78 await makeGetRequest({
79 url: server.url,
80 path,
81 token: server.accessToken,
82 query: { startDate: new Date().toISOString(), level: 'toto' },
83 expectedStatus: HttpStatusCode.BAD_REQUEST_400
84 })
85 })
86
87 it('Should succeed with the correct params', async function () {
88 await makeGetRequest({
89 url: server.url,
90 path,
91 token: server.accessToken,
92 query: { startDate: new Date().toISOString() },
93 expectedStatus: HttpStatusCode.OK_200
94 })
95 })
96 })
97
98 describe('When creating client logs', function () {
99 const base = {
100 level: 'warn' as 'warn',
101 message: 'my super message',
102 url: 'https://example.com/toto'
103 }
104 const expectedStatus = HttpStatusCode.BAD_REQUEST_400
105
106 it('Should fail with an invalid level', async function () {
107 await server.logs.createLogClient({ payload: { ...base, level: '' as any }, expectedStatus })
108 await server.logs.createLogClient({ payload: { ...base, level: undefined }, expectedStatus })
109 await server.logs.createLogClient({ payload: { ...base, level: 'toto' as any }, expectedStatus })
110 })
111
112 it('Should fail with an invalid message', async function () {
113 await server.logs.createLogClient({ payload: { ...base, message: undefined }, expectedStatus })
114 await server.logs.createLogClient({ payload: { ...base, message: '' }, expectedStatus })
115 await server.logs.createLogClient({ payload: { ...base, message: 'm'.repeat(2500) }, expectedStatus })
116 })
117
118 it('Should fail with an invalid url', async function () {
119 await server.logs.createLogClient({ payload: { ...base, url: undefined }, expectedStatus })
120 await server.logs.createLogClient({ payload: { ...base, url: 'toto' }, expectedStatus })
121 })
122
123 it('Should fail with an invalid stackTrace', async function () {
124 await server.logs.createLogClient({ payload: { ...base, stackTrace: 's'.repeat(20000) }, expectedStatus })
125 })
126
127 it('Should fail with an invalid userAgent', async function () {
128 await server.logs.createLogClient({ payload: { ...base, userAgent: 's'.repeat(500) }, expectedStatus })
129 })
130
131 it('Should fail with an invalid meta', async function () {
132 await server.logs.createLogClient({ payload: { ...base, meta: 's'.repeat(10000) }, expectedStatus })
133 })
134
135 it('Should succeed with the correct params', async function () {
136 await server.logs.createLogClient({ payload: { ...base, stackTrace: 'stackTrace', meta: '{toto}', userAgent: 'userAgent' } })
137 })
138
139 it('Should rate limit log creation', async function () {
140 let fail = false
141
142 for (let i = 0; i < 10; i++) {
143 try {
144 await server.logs.createLogClient({ token: null, payload: base })
145 } catch {
146 fail = true
147 }
148 }
149
150 expect(fail).to.be.true
151 })
152 })
153
154 after(async function () {
155 await cleanupTests([ server ])
156 })
157})