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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
|
<?php
declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2017 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace Test\Security\RateLimiting;
use OC\Security\RateLimiting\Backend\IBackend;
use OC\Security\RateLimiting\Limiter;
use OCP\IUser;
use Test\TestCase;
class LimiterTest extends TestCase {
/** @var IBackend|\PHPUnit\Framework\MockObject\MockObject */
private $backend;
/** @var Limiter */
private $limiter;
protected function setUp(): void {
parent::setUp();
$this->backend = $this->createMock(IBackend::class);
$this->limiter = new Limiter(
$this->backend
);
}
public function testRegisterAnonRequestExceeded() {
$this->expectException(\OC\Security\RateLimiting\Exception\RateLimitExceededException::class);
$this->expectExceptionMessage('Rate limit exceeded');
$this->backend
->expects($this->once())
->method('getAttempts')
->with(
'MyIdentifier',
'4664f0d9c88dcb7552be47b37bb52ce35977b2e60e1ac13757cf625f31f87050a41f3da064887fa87d49fd042e4c8eb20de8f10464877d3959677ab011b73a47'
)
->willReturn(101);
$this->limiter->registerAnonRequest('MyIdentifier', 100, 100, '127.0.0.1');
}
public function testRegisterAnonRequestSuccess() {
$this->backend
->expects($this->once())
->method('getAttempts')
->with(
'MyIdentifier',
'4664f0d9c88dcb7552be47b37bb52ce35977b2e60e1ac13757cf625f31f87050a41f3da064887fa87d49fd042e4c8eb20de8f10464877d3959677ab011b73a47'
)
->willReturn(99);
$this->backend
->expects($this->once())
->method('registerAttempt')
->with(
'MyIdentifier',
'4664f0d9c88dcb7552be47b37bb52ce35977b2e60e1ac13757cf625f31f87050a41f3da064887fa87d49fd042e4c8eb20de8f10464877d3959677ab011b73a47',
100
);
$this->limiter->registerAnonRequest('MyIdentifier', 100, 100, '127.0.0.1');
}
public function testRegisterUserRequestExceeded() {
$this->expectException(\OC\Security\RateLimiting\Exception\RateLimitExceededException::class);
$this->expectExceptionMessage('Rate limit exceeded');
/** @var IUser|\PHPUnit\Framework\MockObject\MockObject $user */
$user = $this->createMock(IUser::class);
$user
->expects($this->once())
->method('getUID')
->willReturn('MyUid');
$this->backend
->expects($this->once())
->method('getAttempts')
->with(
'MyIdentifier',
'ddb2ec50fa973fd49ecf3d816f677c8095143e944ad10485f30fb3dac85c13a346dace4dae2d0a15af91867320957bfd38a43d9eefbb74fe6919e15119b6d805'
)
->willReturn(101);
$this->limiter->registerUserRequest('MyIdentifier', 100, 100, $user);
}
public function testRegisterUserRequestSuccess() {
/** @var IUser|\PHPUnit\Framework\MockObject\MockObject $user */
$user = $this->createMock(IUser::class);
$user
->expects($this->once())
->method('getUID')
->willReturn('MyUid');
$this->backend
->expects($this->once())
->method('getAttempts')
->with(
'MyIdentifier',
'ddb2ec50fa973fd49ecf3d816f677c8095143e944ad10485f30fb3dac85c13a346dace4dae2d0a15af91867320957bfd38a43d9eefbb74fe6919e15119b6d805'
)
->willReturn(99);
$this->backend
->expects($this->once())
->method('registerAttempt')
->with(
'MyIdentifier',
'ddb2ec50fa973fd49ecf3d816f677c8095143e944ad10485f30fb3dac85c13a346dace4dae2d0a15af91867320957bfd38a43d9eefbb74fe6919e15119b6d805',
100
);
$this->limiter->registerUserRequest('MyIdentifier', 100, 100, $user);
}
}
|