APITest.php 4.7 KB

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