|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/** |
| 6 | + * @copyright Copyright (c) 2022 Julius Härtl <[email protected]> |
| 7 | + * |
| 8 | + * @author Julius Härtl <[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 | + |
| 27 | +namespace OCA\Richdocuments\Middleware; |
| 28 | + |
| 29 | +use OCA\Richdocuments\Db\WopiMapper; |
| 30 | +use OCP\IConfig; |
| 31 | +use OCP\IRequest; |
| 32 | +use Psr\Log\LoggerInterface; |
| 33 | + |
| 34 | +class WOPIMiddlewareTest extends \PHPUnit\Framework\TestCase { |
| 35 | + /** |
| 36 | + * @var IConfig|(IConfig&\PHPUnit\Framework\MockObject\MockObject)|\PHPUnit\Framework\MockObject\MockObject |
| 37 | + */ |
| 38 | + private $config; |
| 39 | + /** |
| 40 | + * @var IRequest|(IRequest&\PHPUnit\Framework\MockObject\MockObject)|\PHPUnit\Framework\MockObject\MockObject |
| 41 | + */ |
| 42 | + private $request; |
| 43 | + /** |
| 44 | + * @var WopiMapper|(WopiMapper&\PHPUnit\Framework\MockObject\MockObject)|\PHPUnit\Framework\MockObject\MockObject |
| 45 | + */ |
| 46 | + private $wopiMapper; |
| 47 | + /** |
| 48 | + * @var \PHPUnit\Framework\MockObject\MockObject|LoggerInterface|(LoggerInterface&\PHPUnit\Framework\MockObject\MockObject) |
| 49 | + */ |
| 50 | + private $logger; |
| 51 | + private WOPIMiddleware $middleware; |
| 52 | + |
| 53 | + public function setUp(): void { |
| 54 | + parent::setUp(); |
| 55 | + $this->config = $this->createMock(IConfig::class); |
| 56 | + $this->request = $this->createMock(IRequest::class); |
| 57 | + $this->wopiMapper = $this->createMock(WopiMapper::class); |
| 58 | + $this->logger = $this->createMock(LoggerInterface::class); |
| 59 | + $this->middleware = new WOPIMiddleware( |
| 60 | + $this->config, |
| 61 | + $this->request, |
| 62 | + $this->wopiMapper, |
| 63 | + $this->logger, |
| 64 | + ); |
| 65 | + } |
| 66 | + |
| 67 | + /** @dataProvider dataAllow */ |
| 68 | + public function testAllow($ip, $allowList, $result) { |
| 69 | + $this->request->expects($this->once()) |
| 70 | + ->method('getRemoteAddress') |
| 71 | + ->willReturn($ip); |
| 72 | + $this->config->expects(self::any()) |
| 73 | + ->method('getAppValue') |
| 74 | + ->willReturn($allowList); |
| 75 | + self::assertEquals($result, $this->middleware->isWOPIAllowed()); |
| 76 | + } |
| 77 | + |
| 78 | + public function dataAllow() { |
| 79 | + return [ |
| 80 | + ['192.168.178.1', '192.168.178.1', true], |
| 81 | + ['192.168.178.1', '192.168.178.2', false], |
| 82 | + ['192.168.178.1', '192.168.178.1/24', true], |
| 83 | + ['192.168.178.230', '192.168.178.1/24', true], |
| 84 | + ['192.168.179.1', '192.168.178.1/24', false], |
| 85 | + ['10.0.0.10', '10.0.0.0/8', true], |
| 86 | + ['2001:0DB8:8280:97e8:6c18:0000:a53f:0001', '2001:0DB8:8280:97e8:6c18:0000:a53f:0001', true], |
| 87 | + ['2001:0DB8:8280:97e8:6c18:0000:a53f:0001', '2001:0DB8:8280:97e8:6c18:0000:a53f:0001/128', true], |
| 88 | + ['2001:0DB8:8280:97e8:6c18:0000:a53f:0001', '2001:0DB8:8280::/48', true], |
| 89 | + ['2001:0DB8:8180:97e8:6c18:0000:a53f:0001', '2001:0DB8:8280::/48', false], |
| 90 | + ['2001:0DB8:8180:97e8:6c18:0000:a53f:0001', '2001:0DB8::/32', true], |
| 91 | + ]; |
| 92 | + } |
| 93 | +} |
0 commit comments