BlockLegacyClientPluginTest.php 4.0 KB

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