OCSControllerTest.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. <?php
  2. /**
  3. * ownCloud - App Framework
  4. *
  5. * @author Bernhard Posselt
  6. * @copyright 2015 Bernhard Posselt <dev@bernhard-posselt.com>
  7. *
  8. * This library is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
  10. * License as published by the Free Software Foundation; either
  11. * version 3 of the License, or any later version.
  12. *
  13. * This library is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public
  19. * License along with this library. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. */
  22. namespace Test\AppFramework\Controller;
  23. use OC\AppFramework\Http\Request;
  24. use OCP\AppFramework\Http\DataResponse;
  25. use OCP\AppFramework\Http\EmptyContentSecurityPolicy;
  26. use OCP\AppFramework\OCSController;
  27. use OCP\IConfig;
  28. use OCP\Security\ISecureRandom;
  29. class ChildOCSController extends OCSController {}
  30. class OCSControllerTest extends \Test\TestCase {
  31. public function testCors() {
  32. $request = new Request(
  33. [
  34. 'server' => [
  35. 'HTTP_ORIGIN' => 'test',
  36. ],
  37. ],
  38. $this->getMockBuilder(ISecureRandom::class)
  39. ->disableOriginalConstructor()
  40. ->getMock(),
  41. $this->getMockBuilder(IConfig::class)
  42. ->disableOriginalConstructor()
  43. ->getMock()
  44. );
  45. $controller = new ChildOCSController('app', $request, 'verbs',
  46. 'headers', 100);
  47. $response = $controller->preflightedCors();
  48. $headers = $response->getHeaders();
  49. $this->assertEquals('test', $headers['Access-Control-Allow-Origin']);
  50. $this->assertEquals('verbs', $headers['Access-Control-Allow-Methods']);
  51. $this->assertEquals('headers', $headers['Access-Control-Allow-Headers']);
  52. $this->assertEquals('false', $headers['Access-Control-Allow-Credentials']);
  53. $this->assertEquals(100, $headers['Access-Control-Max-Age']);
  54. }
  55. public function testXML() {
  56. $controller = new ChildOCSController('app', new Request(
  57. [],
  58. $this->getMockBuilder(ISecureRandom::class)
  59. ->disableOriginalConstructor()
  60. ->getMock(),
  61. $this->getMockBuilder(IConfig::class)
  62. ->disableOriginalConstructor()
  63. ->getMock()
  64. ));
  65. $controller->setOCSVersion(1);
  66. $expected = "<?xml version=\"1.0\"?>\n" .
  67. "<ocs>\n" .
  68. " <meta>\n" .
  69. " <status>ok</status>\n" .
  70. " <statuscode>100</statuscode>\n" .
  71. " <message>OK</message>\n" .
  72. " <totalitems></totalitems>\n" .
  73. " <itemsperpage></itemsperpage>\n" .
  74. " </meta>\n" .
  75. " <data>\n" .
  76. " <test>hi</test>\n" .
  77. " </data>\n" .
  78. "</ocs>\n";
  79. $params = new DataResponse(['test' => 'hi']);
  80. $response = $controller->buildResponse($params, 'xml');
  81. $this->assertSame(EmptyContentSecurityPolicy::class, get_class($response->getContentSecurityPolicy()));
  82. $this->assertEquals($expected, $response->render());
  83. }
  84. public function testJSON() {
  85. $controller = new ChildOCSController('app', new Request(
  86. [],
  87. $this->getMockBuilder(ISecureRandom::class)
  88. ->disableOriginalConstructor()
  89. ->getMock(),
  90. $this->getMockBuilder(IConfig::class)
  91. ->disableOriginalConstructor()
  92. ->getMock()
  93. ));
  94. $controller->setOCSVersion(1);
  95. $expected = '{"ocs":{"meta":{"status":"ok","statuscode":100,"message":"OK",' .
  96. '"totalitems":"","itemsperpage":""},"data":{"test":"hi"}}}';
  97. $params = new DataResponse(['test' => 'hi']);
  98. $response = $controller->buildResponse($params, 'json');
  99. $this->assertSame(EmptyContentSecurityPolicy::class, get_class($response->getContentSecurityPolicy()));
  100. $this->assertEquals($expected, $response->render());
  101. $this->assertEquals($expected, $response->render());
  102. }
  103. public function testXMLV2() {
  104. $controller = new ChildOCSController('app', new Request(
  105. [],
  106. $this->getMockBuilder(ISecureRandom::class)
  107. ->disableOriginalConstructor()
  108. ->getMock(),
  109. $this->getMockBuilder(IConfig::class)
  110. ->disableOriginalConstructor()
  111. ->getMock()
  112. ));
  113. $controller->setOCSVersion(2);
  114. $expected = "<?xml version=\"1.0\"?>\n" .
  115. "<ocs>\n" .
  116. " <meta>\n" .
  117. " <status>ok</status>\n" .
  118. " <statuscode>200</statuscode>\n" .
  119. " <message>OK</message>\n" .
  120. " </meta>\n" .
  121. " <data>\n" .
  122. " <test>hi</test>\n" .
  123. " </data>\n" .
  124. "</ocs>\n";
  125. $params = new DataResponse(['test' => 'hi']);
  126. $response = $controller->buildResponse($params, 'xml');
  127. $this->assertSame(EmptyContentSecurityPolicy::class, get_class($response->getContentSecurityPolicy()));
  128. $this->assertEquals($expected, $response->render());
  129. }
  130. public function testJSONV2() {
  131. $controller = new ChildOCSController('app', new Request(
  132. [],
  133. $this->getMockBuilder(ISecureRandom::class)
  134. ->disableOriginalConstructor()
  135. ->getMock(),
  136. $this->getMockBuilder(IConfig::class)
  137. ->disableOriginalConstructor()
  138. ->getMock()
  139. ));
  140. $controller->setOCSVersion(2);
  141. $expected = '{"ocs":{"meta":{"status":"ok","statuscode":200,"message":"OK"},"data":{"test":"hi"}}}';
  142. $params = new DataResponse(['test' => 'hi']);
  143. $response = $controller->buildResponse($params, 'json');
  144. $this->assertSame(EmptyContentSecurityPolicy::class, get_class($response->getContentSecurityPolicy()));
  145. $this->assertEquals($expected, $response->render());
  146. }
  147. }