-
-
Notifications
You must be signed in to change notification settings - Fork 5k
Expand file tree
/
Copy pathRateLimitingTest.php
More file actions
99 lines (84 loc) · 2.55 KB
/
RateLimitingTest.php
File metadata and controls
99 lines (84 loc) · 2.55 KB
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
<?php
declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace OCA\DAV\Tests\unit\DAV\Security;
use OCA\DAV\Connector\Sabre\Exception\TooManyRequests;
use OCA\DAV\DAV\Security\RateLimiting;
use OCP\IAppConfig;
use OCP\IUser;
use OCP\IUserSession;
use OCP\Security\RateLimiting\ILimiter;
use OCP\Security\RateLimiting\IRateLimitExceededException;
use PHPUnit\Framework\MockObject\MockObject;
use Test\TestCase;
class RateLimitingTest extends TestCase {
private IUserSession $userSession;
private IAppConfig&MockObject $config;
private ILimiter&MockObject $limiter;
private RateLimiting $rateLimiting;
private string $userId = 'user123';
protected function setUp(): void {
parent::setUp();
$this->userSession = $this->createMock(IUserSession::class);
$this->config = $this->createMock(IAppConfig::class);
$this->limiter = $this->createMock(ILimiter::class);
$this->rateLimiting = new RateLimiting(
$this->userSession,
$this->config,
$this->limiter,
);
}
public function testNoUserObject(): void {
$this->userSession->expects($this->once())
->method('getUser')
->willReturn(null);
$this->limiter->expects($this->never())
->method('registerUserRequest');
$this->rateLimiting->check();
}
public function testRegisterShareRequest(): void {
$user = $this->createMock(IUser::class);
$this->userSession->expects($this->once())
->method('getUser')
->willReturn($user);
$this->config->method('getValueInt')
->willReturnCallback(static function (string $app, string $key, int $default): int {
return match ($key) {
'rateLimitShareAddressbookOrCalendar' => 7,
'rateLimitPeriodShareAddressbookOrCalendar' => 600,
default => $default,
};
});
$this->limiter->expects($this->once())
->method('registerUserRequest')
->with(
'share-addressbook-or-calendar',
7,
600,
$user,
);
$this->rateLimiting->check();
}
public function testShareRequestRateLimitExceeded(): void {
$user = $this->createMock(IUser::class);
$this->userSession->expects($this->once())
->method('getUser')
->willReturn($user);
$this->config->method('getValueInt')
->willReturnArgument(2);
$this->limiter->expects($this->once())
->method('registerUserRequest')
->with(
'share-addressbook-or-calendar',
20,
3600,
$user,
)
->willThrowException($this->createMock(IRateLimitExceededException::class));
$this->expectException(TooManyRequests::class);
$this->rateLimiting->check();
}
}