BlockLegacyClientPluginTest.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2016, ownCloud, Inc.
  5. *
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  7. * @author Joas Schilling <coding@schilljs.com>
  8. * @author Lukas Reschke <lukas@statuscode.ch>
  9. * @author Morris Jobke <hey@morrisjobke.de>
  10. * @author Roeland Jago Douma <roeland@famdouma.nl>
  11. * @author Thomas Müller <thomas.mueller@tmit.eu>
  12. *
  13. * @license AGPL-3.0
  14. *
  15. * This code is free software: you can redistribute it and/or modify
  16. * it under the terms of the GNU Affero General Public License, version 3,
  17. * as published by the Free Software Foundation.
  18. *
  19. * This program is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. * GNU Affero General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU Affero General Public License, version 3,
  25. * along with this program. If not, see <http://www.gnu.org/licenses/>
  26. *
  27. */
  28. namespace OCA\DAV\Tests\unit\Connector\Sabre;
  29. use OCA\DAV\Connector\Sabre\BlockLegacyClientPlugin;
  30. use OCP\IConfig;
  31. use PHPUnit\Framework\MockObject\MockObject;
  32. use Test\TestCase;
  33. use Sabre\HTTP\RequestInterface;
  34. /**
  35. * Class BlockLegacyClientPluginTest
  36. *
  37. * @package OCA\DAV\Tests\unit\Connector\Sabre
  38. */
  39. class BlockLegacyClientPluginTest extends TestCase {
  40. /** @var IConfig|MockObject */
  41. private $config;
  42. /** @var BlockLegacyClientPlugin */
  43. private $blockLegacyClientVersionPlugin;
  44. protected function setUp(): void {
  45. parent::setUp();
  46. $this->config = $this->createMock(IConfig::class);
  47. $this->blockLegacyClientVersionPlugin = new BlockLegacyClientPlugin($this->config);
  48. }
  49. public function oldDesktopClientProvider(): array {
  50. return [
  51. ['Mozilla/5.0 (Windows) mirall/1.5.0'],
  52. ['Mozilla/5.0 (Bogus Text) mirall/1.6.9'],
  53. ];
  54. }
  55. /**
  56. * @dataProvider oldDesktopClientProvider
  57. */
  58. public function testBeforeHandlerException(string $userAgent): void {
  59. $this->expectException(\Sabre\DAV\Exception\Forbidden::class);
  60. $this->expectExceptionMessage('Unsupported client version.');
  61. /** @var RequestInterface|MockObject $request */
  62. $request = $this->createMock('\Sabre\HTTP\RequestInterface');
  63. $request
  64. ->expects($this->once())
  65. ->method('getHeader')
  66. ->with('User-Agent')
  67. ->willReturn($userAgent);
  68. $this->config
  69. ->expects($this->once())
  70. ->method('getSystemValue')
  71. ->with('minimum.supported.desktop.version', '2.3.0')
  72. ->willReturn('1.7.0');
  73. $this->blockLegacyClientVersionPlugin->beforeHandler($request);
  74. }
  75. public function newAndAlternateDesktopClientProvider(): array {
  76. return [
  77. ['Mozilla/5.0 (Windows) mirall/1.7.0'],
  78. ['Mozilla/5.0 (Bogus Text) mirall/1.9.3'],
  79. ['Mozilla/5.0 (Not Our Client But Old Version) LegacySync/1.1.0'],
  80. ];
  81. }
  82. /**
  83. * @dataProvider newAndAlternateDesktopClientProvider
  84. */
  85. public function testBeforeHandlerSuccess(string $userAgent): void {
  86. /** @var RequestInterface|MockObject $request */
  87. $request = $this->createMock(RequestInterface::class);
  88. $request
  89. ->expects($this->once())
  90. ->method('getHeader')
  91. ->with('User-Agent')
  92. ->willReturn($userAgent);
  93. $this->config
  94. ->expects($this->once())
  95. ->method('getSystemValue')
  96. ->with('minimum.supported.desktop.version', '2.3.0')
  97. ->willReturn('1.7.0');
  98. $this->blockLegacyClientVersionPlugin->beforeHandler($request);
  99. }
  100. public function testBeforeHandlerNoUserAgent(): void {
  101. /** @var RequestInterface|MockObject $request */
  102. $request = $this->createMock(RequestInterface::class);
  103. $request
  104. ->expects($this->once())
  105. ->method('getHeader')
  106. ->with('User-Agent')
  107. ->willReturn(null);
  108. $this->blockLegacyClientVersionPlugin->beforeHandler($request);
  109. }
  110. }