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