OCSControllerTest.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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. }
  31. class OCSControllerTest extends \Test\TestCase {
  32. public function testCors() {
  33. $request = new Request(
  34. [
  35. 'server' => [
  36. 'HTTP_ORIGIN' => 'test',
  37. ],
  38. ],
  39. $this->getMockBuilder(ISecureRandom::class)
  40. ->disableOriginalConstructor()
  41. ->getMock(),
  42. $this->getMockBuilder(IConfig::class)
  43. ->disableOriginalConstructor()
  44. ->getMock()
  45. );
  46. $controller = new ChildOCSController('app', $request, 'verbs',
  47. 'headers', 100);
  48. $response = $controller->preflightedCors();
  49. $headers = $response->getHeaders();
  50. $this->assertEquals('test', $headers['Access-Control-Allow-Origin']);
  51. $this->assertEquals('verbs', $headers['Access-Control-Allow-Methods']);
  52. $this->assertEquals('headers', $headers['Access-Control-Allow-Headers']);
  53. $this->assertEquals('false', $headers['Access-Control-Allow-Credentials']);
  54. $this->assertEquals(100, $headers['Access-Control-Max-Age']);
  55. }
  56. public function testXML() {
  57. $controller = new ChildOCSController('app', new Request(
  58. [],
  59. $this->getMockBuilder(ISecureRandom::class)
  60. ->disableOriginalConstructor()
  61. ->getMock(),
  62. $this->getMockBuilder(IConfig::class)
  63. ->disableOriginalConstructor()
  64. ->getMock()
  65. ));
  66. $controller->setOCSVersion(1);
  67. $expected = "<?xml version=\"1.0\"?>\n" .
  68. "<ocs>\n" .
  69. " <meta>\n" .
  70. " <status>ok</status>\n" .
  71. " <statuscode>100</statuscode>\n" .
  72. " <message>OK</message>\n" .
  73. " <totalitems></totalitems>\n" .
  74. " <itemsperpage></itemsperpage>\n" .
  75. " </meta>\n" .
  76. " <data>\n" .
  77. " <test>hi</test>\n" .
  78. " </data>\n" .
  79. "</ocs>\n";
  80. $params = new DataResponse(['test' => 'hi']);
  81. $response = $controller->buildResponse($params, 'xml');
  82. $this->assertSame(EmptyContentSecurityPolicy::class, get_class($response->getContentSecurityPolicy()));
  83. $this->assertEquals($expected, $response->render());
  84. }
  85. public function testJSON() {
  86. $controller = new ChildOCSController('app', new Request(
  87. [],
  88. $this->getMockBuilder(ISecureRandom::class)
  89. ->disableOriginalConstructor()
  90. ->getMock(),
  91. $this->getMockBuilder(IConfig::class)
  92. ->disableOriginalConstructor()
  93. ->getMock()
  94. ));
  95. $controller->setOCSVersion(1);
  96. $expected = '{"ocs":{"meta":{"status":"ok","statuscode":100,"message":"OK",' .
  97. '"totalitems":"","itemsperpage":""},"data":{"test":"hi"}}}';
  98. $params = new DataResponse(['test' => 'hi']);
  99. $response = $controller->buildResponse($params, 'json');
  100. $this->assertSame(EmptyContentSecurityPolicy::class, get_class($response->getContentSecurityPolicy()));
  101. $this->assertEquals($expected, $response->render());
  102. $this->assertEquals($expected, $response->render());
  103. }
  104. public function testXMLV2() {
  105. $controller = new ChildOCSController('app', new Request(
  106. [],
  107. $this->getMockBuilder(ISecureRandom::class)
  108. ->disableOriginalConstructor()
  109. ->getMock(),
  110. $this->getMockBuilder(IConfig::class)
  111. ->disableOriginalConstructor()
  112. ->getMock()
  113. ));
  114. $controller->setOCSVersion(2);
  115. $expected = "<?xml version=\"1.0\"?>\n" .
  116. "<ocs>\n" .
  117. " <meta>\n" .
  118. " <status>ok</status>\n" .
  119. " <statuscode>200</statuscode>\n" .
  120. " <message>OK</message>\n" .
  121. " </meta>\n" .
  122. " <data>\n" .
  123. " <test>hi</test>\n" .
  124. " </data>\n" .
  125. "</ocs>\n";
  126. $params = new DataResponse(['test' => 'hi']);
  127. $response = $controller->buildResponse($params, 'xml');
  128. $this->assertSame(EmptyContentSecurityPolicy::class, get_class($response->getContentSecurityPolicy()));
  129. $this->assertEquals($expected, $response->render());
  130. }
  131. public function testJSONV2() {
  132. $controller = new ChildOCSController('app', new Request(
  133. [],
  134. $this->getMockBuilder(ISecureRandom::class)
  135. ->disableOriginalConstructor()
  136. ->getMock(),
  137. $this->getMockBuilder(IConfig::class)
  138. ->disableOriginalConstructor()
  139. ->getMock()
  140. ));
  141. $controller->setOCSVersion(2);
  142. $expected = '{"ocs":{"meta":{"status":"ok","statuscode":200,"message":"OK"},"data":{"test":"hi"}}}';
  143. $params = new DataResponse(['test' => 'hi']);
  144. $response = $controller->buildResponse($params, 'json');
  145. $this->assertSame(EmptyContentSecurityPolicy::class, get_class($response->getContentSecurityPolicy()));
  146. $this->assertEquals($expected, $response->render());
  147. }
  148. }