|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/** |
| 6 | + * SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors |
| 7 | + * SPDX-License-Identifier: AGPL-3.0-or-later |
| 8 | + */ |
| 9 | + |
| 10 | +namespace OCA\DAV\Tests\unit\DAV\Security; |
| 11 | + |
| 12 | +use OCA\DAV\Connector\Sabre\Exception\TooManyRequests; |
| 13 | +use OCA\DAV\DAV\Security\RateLimiting; |
| 14 | +use OCP\IAppConfig; |
| 15 | +use OCP\IUser; |
| 16 | +use OCP\IUserSession; |
| 17 | +use OCP\Security\RateLimiting\ILimiter; |
| 18 | +use OCP\Security\RateLimiting\IRateLimitExceededException; |
| 19 | +use PHPUnit\Framework\MockObject\MockObject; |
| 20 | +use Test\TestCase; |
| 21 | + |
| 22 | +class RateLimitingTest extends TestCase { |
| 23 | + private IUserSession $userSession; |
| 24 | + private IAppConfig&MockObject $config; |
| 25 | + private ILimiter&MockObject $limiter; |
| 26 | + private RateLimiting $rateLimiting; |
| 27 | + private string $userId = 'user123'; |
| 28 | + |
| 29 | + protected function setUp(): void { |
| 30 | + parent::setUp(); |
| 31 | + |
| 32 | + $this->userSession = $this->createMock(IUserSession::class); |
| 33 | + $this->config = $this->createMock(IAppConfig::class); |
| 34 | + $this->limiter = $this->createMock(ILimiter::class); |
| 35 | + |
| 36 | + $this->rateLimiting = new RateLimiting( |
| 37 | + $this->userSession, |
| 38 | + $this->config, |
| 39 | + $this->limiter, |
| 40 | + ); |
| 41 | + } |
| 42 | + |
| 43 | + public function testNoUserObject(): void { |
| 44 | + $this->userSession->expects($this->once()) |
| 45 | + ->method('getUser') |
| 46 | + ->willReturn(null); |
| 47 | + $this->limiter->expects($this->never()) |
| 48 | + ->method('registerUserRequest'); |
| 49 | + |
| 50 | + $this->rateLimiting->check(); |
| 51 | + } |
| 52 | + |
| 53 | + public function testRegisterShareRequest(): void { |
| 54 | + $user = $this->createMock(IUser::class); |
| 55 | + $this->userSession->expects($this->once()) |
| 56 | + ->method('getUser') |
| 57 | + ->willReturn($user); |
| 58 | + $this->config->method('getValueInt') |
| 59 | + ->willReturnCallback(static function (string $app, string $key, int $default): int { |
| 60 | + return match ($key) { |
| 61 | + 'rateLimitShareAddressbookOrCalendar' => 7, |
| 62 | + 'rateLimitPeriodShareAddressbookOrCalendar' => 600, |
| 63 | + default => $default, |
| 64 | + }; |
| 65 | + }); |
| 66 | + $this->limiter->expects($this->once()) |
| 67 | + ->method('registerUserRequest') |
| 68 | + ->with( |
| 69 | + 'share-addressbook-or-calendar', |
| 70 | + 7, |
| 71 | + 600, |
| 72 | + $user, |
| 73 | + ); |
| 74 | + |
| 75 | + $this->rateLimiting->check(); |
| 76 | + } |
| 77 | + |
| 78 | + public function testShareRequestRateLimitExceeded(): void { |
| 79 | + $user = $this->createMock(IUser::class); |
| 80 | + $this->userSession->expects($this->once()) |
| 81 | + ->method('getUser') |
| 82 | + ->willReturn($user); |
| 83 | + $this->config->method('getValueInt') |
| 84 | + ->willReturnArgument(2); |
| 85 | + $this->limiter->expects($this->once()) |
| 86 | + ->method('registerUserRequest') |
| 87 | + ->with( |
| 88 | + 'share-addressbook-or-calendar', |
| 89 | + 20, |
| 90 | + 3600, |
| 91 | + $user, |
| 92 | + ) |
| 93 | + ->willThrowException($this->createMock(IRateLimitExceededException::class)); |
| 94 | + |
| 95 | + $this->expectException(TooManyRequests::class); |
| 96 | + |
| 97 | + $this->rateLimiting->check(); |
| 98 | + } |
| 99 | +} |
0 commit comments