ApiFunctionalTest.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. <?php
  2. # Copyright (c) 2013-2016, OVH SAS.
  3. # All rights reserved.
  4. #
  5. # Redistribution and use in source and binary forms, with or without
  6. # modification, are permitted provided that the following conditions are met:
  7. #
  8. # * Redistributions of source code must retain the above copyright
  9. # notice, this list of conditions and the following disclaimer.
  10. # * Redistributions in binary form must reproduce the above copyright
  11. # notice, this list of conditions and the following disclaimer in the
  12. # documentation and/or other materials provided with the distribution.
  13. # * Neither the name of OVH SAS nor the
  14. # names of its contributors may be used to endorse or promote products
  15. # derived from this software without specific prior written permission.
  16. #
  17. # THIS SOFTWARE IS PROVIDED BY OVH SAS AND CONTRIBUTORS ``AS IS'' AND ANY
  18. # EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  19. # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  20. # DISCLAIMED. IN NO EVENT SHALL OVH SAS AND CONTRIBUTORS BE LIABLE FOR ANY
  21. # DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  22. # (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  23. # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  24. # ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  25. # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  26. # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27. namespace Ovh\tests;
  28. use GuzzleHttp\Client;
  29. use Ovh\Api;
  30. /**
  31. * Functional tests of Api class
  32. *
  33. * @package Ovh
  34. * @category Ovh
  35. */
  36. class ApiFunctionalTest extends \PHPUnit_Framework_TestCase
  37. {
  38. /**
  39. * @var string
  40. */
  41. private $application_key;
  42. /**
  43. * @var string
  44. */
  45. private $application_secret;
  46. /**
  47. * @var string
  48. */
  49. private $consumer_key;
  50. /**
  51. * @var string
  52. */
  53. private $endpoint;
  54. /**
  55. * @var string
  56. */
  57. private $rangeIP;
  58. /**
  59. * @var string
  60. */
  61. private $alternativeRangeIP;
  62. /**
  63. * @var Client
  64. */
  65. private $client;
  66. /**
  67. * @var Api
  68. */
  69. private $api;
  70. /**
  71. * Define id to create object
  72. */
  73. protected function setUp()
  74. {
  75. $this->application_key = getenv('APP_KEY');
  76. $this->application_secret = getenv('APP_SECRET');
  77. $this->consumer_key = getenv('CONSUMER');
  78. $this->endpoint = getenv('ENDPOINT');
  79. $this->rangeIP = '127.0.0.20/32';
  80. $this->alternativeRangeIP = '127.0.0.30/32';
  81. $this->client = new Client();
  82. $this->api = new Api(
  83. $this->application_key,
  84. $this->application_secret,
  85. $this->endpoint,
  86. $this->consumer_key,
  87. $this->client
  88. );
  89. }
  90. /**
  91. * Get private and protected method to unit test it
  92. *
  93. * @param string $name
  94. *
  95. * @return \ReflectionMethod
  96. */
  97. protected static function getPrivateMethod($name)
  98. {
  99. $class = new \ReflectionClass('Ovh\Api');
  100. $method = $class->getMethod($name);
  101. $method->setAccessible(true);
  102. return $method;
  103. }
  104. /**
  105. * Get private and protected property to unit test it
  106. *
  107. * @param string $name
  108. *
  109. * @return \ReflectionProperty
  110. */
  111. protected static function getPrivateProperty($name)
  112. {
  113. $class = new \ReflectionClass('Ovh\Api');
  114. $property = $class->getProperty($name);
  115. $property->setAccessible(true);
  116. return $property;
  117. }
  118. /**
  119. * Test if result contains consumerKey and validationUrl
  120. */
  121. public function testIfConsumerKeyIsReplace()
  122. {
  123. $property = self::getPrivateProperty('consumer_key');
  124. $accessRules = json_decode(' [
  125. { "method": "GET", "path": "/*" },
  126. { "method": "POST", "path": "/*" },
  127. { "method": "PUT", "path": "/*" },
  128. { "method": "DELETE", "path": "/*" }
  129. ] ');
  130. $credentials = $this->api->requestCredentials($accessRules);
  131. $consumer_key = $property->getValue($this->api);
  132. $this->assertEquals($consumer_key, $credentials["consumerKey"]);
  133. $this->assertNotEquals($consumer_key, $this->consumer_key);
  134. }
  135. /**
  136. * Test if post request on me
  137. */
  138. public function testPostRestrictionAccessIp()
  139. {
  140. $this->assertNull(
  141. $this->api->post('/me/accessRestriction/ip', ['ip' => $this->rangeIP, 'rule' => 'deny', 'warning' => true])
  142. );
  143. $this->assertNull(
  144. $this->api->post('/me/accessRestriction/ip', ['ip' => $this->alternativeRangeIP,
  145. 'rule' => 'deny',
  146. 'warning' => true,
  147. ])
  148. );
  149. }
  150. /**
  151. * Test if get request on /me
  152. */
  153. public function testGetRestrictionAccessIP()
  154. {
  155. $result = $this->api->get('/me/accessRestriction/ip');
  156. $restrictionIps = [];
  157. foreach ($result as $restrictionId) {
  158. $restriction = $this->api->get('/me/accessRestriction/ip/' . $restrictionId);
  159. $restrictionIps[] = $restriction['ip'];
  160. }
  161. $this->assertContains($this->rangeIP, $restrictionIps);
  162. $this->assertContains($this->alternativeRangeIP, $restrictionIps);
  163. }
  164. /**
  165. * Test if delete request on /me
  166. */
  167. public function testPutRestrictionAccessIP()
  168. {
  169. $result = $this->api->get('/me/accessRestriction/ip');
  170. $restrictionId = array_pop($result);
  171. $this->assertNull(
  172. $this->api->put('/me/accessRestriction/ip/' . $restrictionId, ['rule' => 'accept', 'warning' => true])
  173. );
  174. $restriction = $this->api->get('/me/accessRestriction/ip/' . $restrictionId);
  175. $this->assertEquals('accept', $restriction['rule']);
  176. }
  177. /**
  178. * Test if delete request on /me
  179. */
  180. public function testDeleteRestrictionAccessIP()
  181. {
  182. $result = $this->api->get('/me/accessRestriction/ip');
  183. foreach ($result as $restrictionId) {
  184. $restriction = $this->api->get('/me/accessRestriction/ip/' . $restrictionId);
  185. if (in_array($restriction["ip"], [$this->rangeIP, $this->alternativeRangeIP])) {
  186. $result = $this->api->delete('/me/accessRestriction/ip/' . $restrictionId);
  187. $this->assertNull($result);
  188. break;
  189. }
  190. }
  191. }
  192. /**
  193. * Test if request without authentication works
  194. */
  195. public function testIfRequestWithoutAuthenticationWorks()
  196. {
  197. $api = new Api($this->application_key, $this->application_secret, $this->endpoint, null, $this->client);
  198. $invoker = self::getPrivateMethod('rawCall');
  199. $invoker->invokeArgs($api, ['GET', '/xdsl/incidents']);
  200. }
  201. /**
  202. * Test Api::get
  203. */
  204. public function testApiGetWithParameters()
  205. {
  206. $this->setExpectedException('\\GuzzleHttp\\Exception\\ClientException', '400');
  207. $this->api->get('/me/accessRestriction/ip', ['foo' => 'bar']);
  208. }
  209. /**
  210. * Test Api::get, should build valide signature
  211. */
  212. public function testApiGetWithQueryString()
  213. {
  214. $this->api->get('/me/api/credential', ['status' => 'pendingValidation']);
  215. }
  216. }