BlockLegacyClientPluginTest.php 4.0 KB

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