]>
Commit | Line | Data |
---|---|---|
a1587156 | 1 | /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */ |
490b595a | 2 | |
41d1d075 | 3 | import { expect } from 'chai' |
c55e3d72 | 4 | import { wait } from '@shared/core-utils' |
4c7e60bc | 5 | import { HttpStatusCode } from '@shared/models' |
c55e3d72 | 6 | import { cleanupTests, createSingleServer, PeerTubeServer, setAccessTokensToServers } from '@shared/server-commands' |
490b595a C |
7 | |
8 | describe('Test application behind a reverse proxy', function () { | |
254d3579 | 9 | let server: PeerTubeServer |
d23dd9fb | 10 | let videoId: string |
490b595a C |
11 | |
12 | before(async function () { | |
13 | this.timeout(30000) | |
c1340a6a C |
14 | |
15 | const config = { | |
16 | rates_limit: { | |
17 | api: { | |
18 | max: 50, | |
19 | window: 5000 | |
20 | }, | |
21 | signup: { | |
22 | max: 3, | |
23 | window: 5000 | |
24 | }, | |
25 | login: { | |
26 | max: 20 | |
27 | } | |
28 | }, | |
29 | signup: { | |
30 | limit: 20 | |
31 | } | |
32 | } | |
33 | ||
254d3579 | 34 | server = await createSingleServer(1, config) |
490b595a C |
35 | await setAccessTokensToServers([ server ]) |
36 | ||
89d241a7 | 37 | const { uuid } = await server.videos.upload() |
d23dd9fb | 38 | videoId = uuid |
490b595a C |
39 | }) |
40 | ||
41 | it('Should view a video only once with the same IP by default', async function () { | |
6b616860 C |
42 | this.timeout(20000) |
43 | ||
89d241a7 C |
44 | await server.videos.view({ id: videoId }) |
45 | await server.videos.view({ id: videoId }) | |
490b595a | 46 | |
6b616860 C |
47 | // Wait the repeatable job |
48 | await wait(8000) | |
49 | ||
89d241a7 | 50 | const video = await server.videos.get({ id: videoId }) |
d23dd9fb | 51 | expect(video.views).to.equal(1) |
490b595a C |
52 | }) |
53 | ||
54 | it('Should view a video 2 times with the X-Forwarded-For header set', async function () { | |
6b616860 C |
55 | this.timeout(20000) |
56 | ||
89d241a7 C |
57 | await server.videos.view({ id: videoId, xForwardedFor: '0.0.0.1,127.0.0.1' }) |
58 | await server.videos.view({ id: videoId, xForwardedFor: '0.0.0.2,127.0.0.1' }) | |
490b595a | 59 | |
6b616860 C |
60 | // Wait the repeatable job |
61 | await wait(8000) | |
62 | ||
89d241a7 | 63 | const video = await server.videos.get({ id: videoId }) |
d23dd9fb | 64 | expect(video.views).to.equal(3) |
490b595a C |
65 | }) |
66 | ||
67 | it('Should view a video only once with the same client IP in the X-Forwarded-For header', async function () { | |
6b616860 C |
68 | this.timeout(20000) |
69 | ||
89d241a7 C |
70 | await server.videos.view({ id: videoId, xForwardedFor: '0.0.0.4,0.0.0.3,::ffff:127.0.0.1' }) |
71 | await server.videos.view({ id: videoId, xForwardedFor: '0.0.0.5,0.0.0.3,127.0.0.1' }) | |
490b595a | 72 | |
6b616860 C |
73 | // Wait the repeatable job |
74 | await wait(8000) | |
75 | ||
89d241a7 | 76 | const video = await server.videos.get({ id: videoId }) |
d23dd9fb | 77 | expect(video.views).to.equal(4) |
490b595a C |
78 | }) |
79 | ||
80 | it('Should view a video two times with a different client IP in the X-Forwarded-For header', async function () { | |
6b616860 C |
81 | this.timeout(20000) |
82 | ||
89d241a7 C |
83 | await server.videos.view({ id: videoId, xForwardedFor: '0.0.0.8,0.0.0.6,127.0.0.1' }) |
84 | await server.videos.view({ id: videoId, xForwardedFor: '0.0.0.8,0.0.0.7,127.0.0.1' }) | |
490b595a | 85 | |
6b616860 C |
86 | // Wait the repeatable job |
87 | await wait(8000) | |
88 | ||
89d241a7 | 89 | const video = await server.videos.get({ id: videoId }) |
d23dd9fb | 90 | expect(video.views).to.equal(6) |
490b595a C |
91 | }) |
92 | ||
93 | it('Should rate limit logins', async function () { | |
94 | const user = { username: 'root', password: 'fail' } | |
95 | ||
e79d0ba5 | 96 | for (let i = 0; i < 19; i++) { |
89d241a7 | 97 | await server.login.login({ user, expectedStatus: HttpStatusCode.BAD_REQUEST_400 }) |
490b595a C |
98 | } |
99 | ||
89d241a7 | 100 | await server.login.login({ user, expectedStatus: HttpStatusCode.TOO_MANY_REQUESTS_429 }) |
490b595a C |
101 | }) |
102 | ||
c1340a6a | 103 | it('Should rate limit signup', async function () { |
2fa9c40e C |
104 | for (let i = 0; i < 10; i++) { |
105 | try { | |
89d241a7 | 106 | await server.users.register({ username: 'test' + i }) |
2fa9c40e C |
107 | } catch { |
108 | // empty | |
109 | } | |
c1340a6a C |
110 | } |
111 | ||
89d241a7 | 112 | await server.users.register({ username: 'test42', expectedStatus: HttpStatusCode.TOO_MANY_REQUESTS_429 }) |
c1340a6a C |
113 | }) |
114 | ||
115 | it('Should not rate limit failed signup', async function () { | |
116 | this.timeout(30000) | |
117 | ||
118 | await wait(7000) | |
119 | ||
120 | for (let i = 0; i < 3; i++) { | |
89d241a7 | 121 | await server.users.register({ username: 'test' + i, expectedStatus: HttpStatusCode.CONFLICT_409 }) |
c1340a6a C |
122 | } |
123 | ||
89d241a7 | 124 | await server.users.register({ username: 'test43', expectedStatus: HttpStatusCode.NO_CONTENT_204 }) |
c1340a6a C |
125 | |
126 | }) | |
127 | ||
128 | it('Should rate limit API calls', async function () { | |
129 | this.timeout(30000) | |
130 | ||
131 | await wait(7000) | |
132 | ||
c1e5bd23 C |
133 | for (let i = 0; i < 100; i++) { |
134 | try { | |
89d241a7 | 135 | await server.videos.get({ id: videoId }) |
c1e5bd23 C |
136 | } catch { |
137 | // don't care if it fails | |
138 | } | |
c1340a6a C |
139 | } |
140 | ||
89d241a7 | 141 | await server.videos.get({ id: videoId, expectedStatus: HttpStatusCode.TOO_MANY_REQUESTS_429 }) |
c1340a6a C |
142 | }) |
143 | ||
7c3b7976 C |
144 | after(async function () { |
145 | await cleanupTests([ server ]) | |
490b595a C |
146 | }) |
147 | }) |