APITest.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. <?php
  2. /**
  3. * Copyright (c) 2013 Tom Needham <tom@owncloud.com>
  4. * This file is licensed under the Affero General Public License version 3 or
  5. * later.
  6. * See the COPYING-README file.
  7. */
  8. namespace Test;
  9. use OCP\IRequest;
  10. class APITest extends \Test\TestCase {
  11. // Helps build a response variable
  12. /**
  13. * @param string $message
  14. */
  15. function buildResponse($shipped, $data, $code, $message=null) {
  16. $resp = new \OC\OCS\Result($data, $code, $message);
  17. $resp->addHeader('KEY', 'VALUE');
  18. return [
  19. 'shipped' => $shipped,
  20. 'response' => $resp,
  21. 'app' => $this->getUniqueID('testapp_'),
  22. ];
  23. }
  24. // Validate details of the result
  25. /**
  26. * @param \OC\OCS\Result $result
  27. */
  28. function checkResult($result, $success) {
  29. // Check response is of correct type
  30. $this->assertInstanceOf(\OC\OCS\Result::class, $result);
  31. // Check if it succeeded
  32. /** @var $result \OC\OCS\Result */
  33. $this->assertEquals($success, $result->succeeded());
  34. }
  35. /**
  36. * @return array
  37. */
  38. public function versionDataScriptNameProvider() {
  39. return [
  40. // Valid script name
  41. [
  42. '/master/ocs/v2.php',
  43. true,
  44. ],
  45. // Invalid script names
  46. [
  47. '/master/ocs/v2.php/someInvalidPathName',
  48. false,
  49. ],
  50. [
  51. '/master/ocs/v1.php',
  52. false,
  53. ],
  54. [
  55. '',
  56. false,
  57. ],
  58. ];
  59. }
  60. /**
  61. * @dataProvider versionDataScriptNameProvider
  62. * @param string $scriptName
  63. * @param bool $expected
  64. */
  65. public function testIsV2($scriptName, $expected) {
  66. $request = $this->getMockBuilder(IRequest::class)
  67. ->disableOriginalConstructor()
  68. ->getMock();
  69. $request
  70. ->expects($this->once())
  71. ->method('getScriptName')
  72. ->will($this->returnValue($scriptName));
  73. $this->assertEquals($expected, $this->invokePrivate(new \OC_API, 'isV2', [$request]));
  74. }
  75. function dataProviderTestOneResult() {
  76. return [
  77. [100, true],
  78. [101, false],
  79. [997, false],
  80. ];
  81. }
  82. /**
  83. * @dataProvider dataProviderTestOneResult
  84. *
  85. * @param $statusCode
  86. * @param $succeeded
  87. */
  88. public function testOneResult($statusCode, $succeeded) {
  89. // Setup some data arrays
  90. $data1 = [
  91. 'users' => [
  92. 'tom' => [
  93. 'key' => 'value',
  94. ],
  95. 'frank' => [
  96. 'key' => 'value',
  97. ],
  98. ]];
  99. // Test merging one success result
  100. $response = $this->buildResponse(true, $data1, $statusCode);
  101. $result = \OC_API::mergeResponses([$response]);
  102. $this->assertEquals($response['response'], $result);
  103. $this->checkResult($result, $succeeded);
  104. }
  105. function dataProviderTestMergeResponses() {
  106. return [
  107. // Two shipped success results
  108. [true, 100, true, 100, true],
  109. // Two shipped results, one success and one failure
  110. [true, 100, true, 998, false],
  111. // Two shipped results, both failure
  112. [true, 997, true, 998, false],
  113. // Two third party success results
  114. [false, 100, false, 100, true],
  115. // Two third party results, one success and one failure
  116. [false, 100, false, 998, false],
  117. // Two third party results, both failure
  118. [false, 997, false, 998, false],
  119. // One of each, both success
  120. [false, 100, true, 100, true],
  121. [true, 100, false, 100, true],
  122. // One of each, both failure
  123. [false, 997, true, 998, false],
  124. // One of each, shipped success
  125. [false, 997, true, 100, true],
  126. // One of each, third party success
  127. [false, 100, true, 998, false],
  128. ];
  129. }
  130. /**
  131. * @dataProvider dataProviderTestMergeResponses
  132. *
  133. * Test the merging of multiple responses
  134. * @param $statusCode1
  135. * @param $statusCode2
  136. * @param $succeeded
  137. */
  138. public function testMultipleMergeResponses($shipped1, $statusCode1, $shipped2, $statusCode2, $succeeded){
  139. // Tests that app responses are merged correctly
  140. // Setup some data arrays
  141. $data1 = array(
  142. 'users' => array(
  143. 'tom' => array(
  144. 'key' => 'value',
  145. ),
  146. 'frank' => array(
  147. 'key' => 'value',
  148. ),
  149. ));
  150. $data2 = array(
  151. 'users' => array(
  152. 'tom' => array(
  153. 'key' => 'newvalue',
  154. ),
  155. 'jan' => array(
  156. 'key' => 'value',
  157. ),
  158. ));
  159. // Two shipped success results
  160. $result = \OC_API::mergeResponses(array(
  161. $this->buildResponse($shipped1, $data1, $statusCode1, "message1"),
  162. $this->buildResponse($shipped2, $data2, $statusCode2, "message2"),
  163. ));
  164. $this->checkResult($result, $succeeded);
  165. $resultData = $result->getData();
  166. $resultMeta = $result->getMeta();
  167. $resultHeaders = $result->getHeaders();
  168. $resultStatusCode = $result->getStatusCode();
  169. $this->assertArrayHasKey('jan', $resultData['users']);
  170. $this->assertArrayHasKey('KEY', $resultHeaders);
  171. // check if the returned status message matches the selected status code
  172. if ($resultStatusCode === 997) {
  173. $this->assertEquals('message1', $resultMeta['message']);
  174. } elseif ($resultStatusCode === 998) {
  175. $this->assertEquals('message2', $resultMeta['message']);
  176. } elseif ($resultStatusCode === 100) {
  177. $this->assertEquals(null, $resultMeta['message']);
  178. }
  179. }
  180. }