AnonymousOptionsTest.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2018 Robin Appelman <robin@icewind.nl>
  4. *
  5. * @author Bastien Durel <bastien@durel.org>
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  7. * @author Julius Härtl <jus@bitgrid.net>
  8. * @author Robin Appelman <robin@icewind.nl>
  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\DAV;
  27. use OCA\DAV\Connector\Sabre\AnonymousOptionsPlugin;
  28. use Sabre\DAV\Auth\Backend\BasicCallBack;
  29. use Sabre\DAV\Auth\Plugin;
  30. use Sabre\DAV\Server;
  31. use Sabre\HTTP\ResponseInterface;
  32. use Sabre\HTTP\Sapi;
  33. use Test\TestCase;
  34. class AnonymousOptionsTest extends TestCase {
  35. private function sendRequest($method, $path, $userAgent = '') {
  36. $server = new Server();
  37. $server->addPlugin(new AnonymousOptionsPlugin());
  38. $server->addPlugin(new Plugin(new BasicCallBack(function () {
  39. return false;
  40. })));
  41. $server->httpRequest->setMethod($method);
  42. $server->httpRequest->setUrl($path);
  43. $server->httpRequest->setHeader('User-Agent', $userAgent);
  44. $server->sapi = new SapiMock();
  45. $server->exec();
  46. return $server->httpResponse;
  47. }
  48. public function testAnonymousOptionsRoot(): void {
  49. $response = $this->sendRequest('OPTIONS', '');
  50. $this->assertEquals(401, $response->getStatus());
  51. }
  52. public function testAnonymousOptionsNonRoot(): void {
  53. $response = $this->sendRequest('OPTIONS', 'foo');
  54. $this->assertEquals(401, $response->getStatus());
  55. }
  56. public function testAnonymousOptionsNonRootSubDir(): void {
  57. $response = $this->sendRequest('OPTIONS', 'foo/bar');
  58. $this->assertEquals(401, $response->getStatus());
  59. }
  60. public function testAnonymousOptionsRootOffice(): void {
  61. $response = $this->sendRequest('OPTIONS', '', 'Microsoft Office does strange things');
  62. $this->assertEquals(200, $response->getStatus());
  63. }
  64. public function testAnonymousOptionsNonRootOffice(): void {
  65. $response = $this->sendRequest('OPTIONS', 'foo', 'Microsoft Office does strange things');
  66. $this->assertEquals(200, $response->getStatus());
  67. }
  68. public function testAnonymousOptionsNonRootSubDirOffice(): void {
  69. $response = $this->sendRequest('OPTIONS', 'foo/bar', 'Microsoft Office does strange things');
  70. $this->assertEquals(200, $response->getStatus());
  71. }
  72. public function testAnonymousHead(): void {
  73. $response = $this->sendRequest('HEAD', '', 'Microsoft Office does strange things');
  74. $this->assertEquals(200, $response->getStatus());
  75. }
  76. public function testAnonymousHeadNoOffice(): void {
  77. $response = $this->sendRequest('HEAD', '');
  78. $this->assertEquals(401, $response->getStatus(), 'curl');
  79. }
  80. }
  81. class SapiMock extends Sapi {
  82. /**
  83. * Overriding this so nothing is ever echo'd.
  84. *
  85. * @return void
  86. */
  87. public static function sendResponse(ResponseInterface $response): void {
  88. }
  89. }