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