|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/* |
| 6 | + * @copyright 2023 Christoph Wurst <[email protected]> |
| 7 | + * |
| 8 | + * @author 2023 Christoph Wurst <[email protected]> |
| 9 | + * |
| 10 | + * @license GNU AGPL version 3 or any later version |
| 11 | + * |
| 12 | + * This program is free software: you can redistribute it and/or modify |
| 13 | + * it under the terms of the GNU Affero General Public License as |
| 14 | + * published by the Free Software Foundation, either version 3 of the |
| 15 | + * License, or (at your option) any later version. |
| 16 | + * |
| 17 | + * This program is distributed in the hope that it will be useful, |
| 18 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 19 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 20 | + * GNU Affero General Public License for more details. |
| 21 | + * |
| 22 | + * You should have received a copy of the GNU Affero General Public License |
| 23 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 24 | + */ |
| 25 | + |
| 26 | +namespace OCA\DAV\Tests\unit\CalDAV\Security; |
| 27 | + |
| 28 | +use OC\Security\RateLimiting\Exception\RateLimitExceededException; |
| 29 | +use OC\Security\RateLimiting\Limiter; |
| 30 | +use OCA\DAV\CalDAV\CalDavBackend; |
| 31 | +use OCA\DAV\CalDAV\Security\RateLimitingPlugin; |
| 32 | +use OCA\DAV\Connector\Sabre\Exception\TooManyRequests; |
| 33 | +use OCP\IConfig; |
| 34 | +use OCP\IUser; |
| 35 | +use OCP\IUserManager; |
| 36 | +use PHPUnit\Framework\MockObject\MockObject; |
| 37 | +use Psr\Log\LoggerInterface; |
| 38 | +use Sabre\DAV\Exception\Forbidden; |
| 39 | +use Test\TestCase; |
| 40 | + |
| 41 | +class RateLimitingPluginTest extends TestCase { |
| 42 | + |
| 43 | + private Limiter|MockObject $limiter; |
| 44 | + private CalDavBackend|MockObject $caldavBackend; |
| 45 | + private IUserManager|MockObject $userManager; |
| 46 | + private LoggerInterface|MockObject $logger; |
| 47 | + private IConfig|MockObject $config; |
| 48 | + private string $userId = 'user123'; |
| 49 | + private RateLimitingPlugin $plugin; |
| 50 | + |
| 51 | + protected function setUp(): void { |
| 52 | + parent::setUp(); |
| 53 | + |
| 54 | + $this->limiter = $this->createMock(Limiter::class); |
| 55 | + $this->userManager = $this->createMock(IUserManager::class); |
| 56 | + $this->caldavBackend = $this->createMock(CalDavBackend::class); |
| 57 | + $this->logger = $this->createMock(LoggerInterface::class); |
| 58 | + $this->config = $this->createMock(IConfig::class); |
| 59 | + $this->plugin = new RateLimitingPlugin( |
| 60 | + $this->limiter, |
| 61 | + $this->userManager, |
| 62 | + $this->caldavBackend, |
| 63 | + $this->logger, |
| 64 | + $this->config, |
| 65 | + $this->userId, |
| 66 | + ); |
| 67 | + } |
| 68 | + |
| 69 | + public function testNoUserObject(): void { |
| 70 | + $this->limiter->expects(self::never()) |
| 71 | + ->method('registerUserRequest'); |
| 72 | + |
| 73 | + $this->plugin->beforeBind('calendars/foo/cal'); |
| 74 | + } |
| 75 | + |
| 76 | + public function testUnrelated(): void { |
| 77 | + $user = $this->createMock(IUser::class); |
| 78 | + $this->userManager->expects(self::once()) |
| 79 | + ->method('get') |
| 80 | + ->with($this->userId) |
| 81 | + ->willReturn($user); |
| 82 | + $this->limiter->expects(self::never()) |
| 83 | + ->method('registerUserRequest'); |
| 84 | + |
| 85 | + $this->plugin->beforeBind('foo/bar'); |
| 86 | + } |
| 87 | + |
| 88 | + public function testRegisterCalendarCreation(): void { |
| 89 | + $user = $this->createMock(IUser::class); |
| 90 | + $this->userManager->expects(self::once()) |
| 91 | + ->method('get') |
| 92 | + ->with($this->userId) |
| 93 | + ->willReturn($user); |
| 94 | + $this->config |
| 95 | + ->method('getAppValue') |
| 96 | + ->with('dav') |
| 97 | + ->willReturnArgument(2); |
| 98 | + $this->limiter->expects(self::once()) |
| 99 | + ->method('registerUserRequest') |
| 100 | + ->with( |
| 101 | + 'caldav-create-calendar', |
| 102 | + 10, |
| 103 | + 3600, |
| 104 | + $user, |
| 105 | + ); |
| 106 | + |
| 107 | + $this->plugin->beforeBind('calendars/foo/cal'); |
| 108 | + } |
| 109 | + |
| 110 | + public function testCalendarCreationRateLimitExceeded(): void { |
| 111 | + $user = $this->createMock(IUser::class); |
| 112 | + $this->userManager->expects(self::once()) |
| 113 | + ->method('get') |
| 114 | + ->with($this->userId) |
| 115 | + ->willReturn($user); |
| 116 | + $this->config |
| 117 | + ->method('getAppValue') |
| 118 | + ->with('dav') |
| 119 | + ->willReturnArgument(2); |
| 120 | + $this->limiter->expects(self::once()) |
| 121 | + ->method('registerUserRequest') |
| 122 | + ->with( |
| 123 | + 'caldav-create-calendar', |
| 124 | + 10, |
| 125 | + 3600, |
| 126 | + $user, |
| 127 | + ) |
| 128 | + ->willThrowException(new RateLimitExceededException()); |
| 129 | + $this->expectException(TooManyRequests::class); |
| 130 | + |
| 131 | + $this->plugin->beforeBind('calendars/foo/cal'); |
| 132 | + } |
| 133 | + |
| 134 | + public function testCalendarLimitReached(): void { |
| 135 | + $user = $this->createMock(IUser::class); |
| 136 | + $this->userManager->expects(self::once()) |
| 137 | + ->method('get') |
| 138 | + ->with($this->userId) |
| 139 | + ->willReturn($user); |
| 140 | + $user->method('getUID')->willReturn('user123'); |
| 141 | + $this->config |
| 142 | + ->method('getAppValue') |
| 143 | + ->with('dav') |
| 144 | + ->willReturnArgument(2); |
| 145 | + $this->limiter->expects(self::once()) |
| 146 | + ->method('registerUserRequest') |
| 147 | + ->with( |
| 148 | + 'caldav-create-calendar', |
| 149 | + 10, |
| 150 | + 3600, |
| 151 | + $user, |
| 152 | + ); |
| 153 | + $this->caldavBackend->expects(self::once()) |
| 154 | + ->method('getCalendarsForUserCount') |
| 155 | + ->with('principals/users/user123') |
| 156 | + ->willReturn(27); |
| 157 | + $this->caldavBackend->expects(self::once()) |
| 158 | + ->method('getSubscriptionsForUserCount') |
| 159 | + ->with('principals/users/user123') |
| 160 | + ->willReturn(3); |
| 161 | + $this->expectException(Forbidden::class); |
| 162 | + |
| 163 | + $this->plugin->beforeBind('calendars/foo/cal'); |
| 164 | + } |
| 165 | + |
| 166 | +} |
0 commit comments