diff --git a/tests/ApiFunctionalTest.php b/tests/ApiFunctionalTest.php index 415fd24..61ddb02 100644 --- a/tests/ApiFunctionalTest.php +++ b/tests/ApiFunctionalTest.php @@ -28,136 +28,220 @@ namespace Ovh\tests; use GuzzleHttp\Client; -use GuzzleHttp\Subscriber\Mock; use Ovh\Api; /** * Functional tests of Api class * - * @package Ovh + * @package Ovh * @category Ovh - * @author Vincent Cassé + * @author Vincent Cassé + * @author Thierry Goettelmann */ -class ApiFunctionalTest extends \PHPUnit_Framework_TestCase { +class ApiFunctionalTest extends \PHPUnit_Framework_TestCase +{ - /** - * Define id to create object - */ - protected function setUp() + /** + * @var string + */ + private $application_key; + + /** + * @var string + */ + private $application_secret; + + /** + * @var string + */ + private $consumer_key; + + /** + * @var string + */ + private $endpoint; + + /** + * @var string + */ + private $rangeIP; + + /** + * @var string + */ + private $alternativeRangeIP; + + /** + * @var Client + */ + private $client; + + /** + * @var Api + */ + private $api; + + /** + * Define id to create object + */ + protected function setUp() { - $this->application_key = getenv('APP_KEY'); - $this->application_secret = getenv('APP_SECRET'); - $this->consumer_key = getenv('CONSUMER'); - $this->endpoint = getenv('ENDPOINT'); - $this->rangeIP = '127.0.0.20/32'; + $this->application_key = getenv('APP_KEY'); + $this->application_secret = getenv('APP_SECRET'); + $this->consumer_key = getenv('CONSUMER'); + $this->endpoint = getenv('ENDPOINT'); + $this->rangeIP = '127.0.0.20/32'; + $this->alternativeRangeIP = '127.0.0.30/32'; $this->client = new Client(); - $this->api = new Api($this->application_key, $this->application_secret, $this->endpoint, $this->consumer_key, $this->client); - + $this->api = new Api( + $this->application_key, + $this->application_secret, + $this->endpoint, + $this->consumer_key, + $this->client + ); } /** * Get private and protected method to unit test it + * + * @param string $name + * + * @return \ReflectionMethod */ protected static function getPrivateMethod($name) { - $class = new \ReflectionClass('Ovh\Api'); + $class = new \ReflectionClass('Ovh\Api'); $method = $class->getMethod($name); $method->setAccessible(true); + return $method; } + /** + * Get private and protected property to unit test it + * + * @param string $name + * + * @return \ReflectionProperty + */ protected static function getPrivateProperty($name) { - $class = new \ReflectionClass('Ovh\Api'); + $class = new \ReflectionClass('Ovh\Api'); $property = $class->getProperty($name); $property->setAccessible(true); + return $property; } - /** - * Test if result contains consumerKey and validationUrl - */ - public function testIfConsumerKeyIsReplace() - { - $property = self::getPrivateProperty('consumer_key'); - $accessRules = json_decode(' [ + /** + * Test if result contains consumerKey and validationUrl + */ + public function testIfConsumerKeyIsReplace() + { + $property = self::getPrivateProperty('consumer_key'); + $accessRules = json_decode(' [ { "method": "GET", "path": "/*" }, { "method": "POST", "path": "/*" }, { "method": "PUT", "path": "/*" }, { "method": "DELETE", "path": "/*" } - ] ') ; + ] '); - $credentials = $this->api->requestCredentials($accessRules); - $consumer_key = $property->getValue($this->api); + $credentials = $this->api->requestCredentials($accessRules); + $consumer_key = $property->getValue($this->api); - $this->assertEquals( $consumer_key , $credentials["consumerKey"]); - $this->assertNotEquals( $consumer_key, $this->consumer_key ); - } + $this->assertEquals($consumer_key, $credentials["consumerKey"]); + $this->assertNotEquals($consumer_key, $this->consumer_key); + } - /** - * Test if put request on me - */ - public function testPutRestrictionAccessIp() - { - $api = new Api($this->application_key, $this->application_secret, $this->endpoint, $this->consumer_key, $this->client); - $invoker = self::getPrivateMethod('rawCall'); - $params = new \StdClass(); - $params->ip = $this->rangeIP; - $params->rule = "deny"; - $params->warning = true; + /** + * Test if post request on me + */ + public function testPostRestrictionAccessIp() + { + $this->assertNull( + $this->api->post('/me/accessRestriction/ip', ['ip' => $this->rangeIP, 'rule' => 'deny', 'warning' => true]) + ); - $result = $invoker->invokeArgs($api, array('POST', '/me/accessRestriction/ip', $params)) ; - $this->assertNull( $result ); - } + $this->assertNull( + $this->api->post('/me/accessRestriction/ip', ['ip' => $this->alternativeRangeIP, + 'rule' => 'deny', + 'warning' => true, + ]) + ); + } - /** - * Test if get request on /me - */ - public function testGetRestrictionAccessIP() - { - $api = new Api($this->application_key, $this->application_secret, $this->endpoint, $this->consumer_key, $this->client); - $invoker = self::getPrivateMethod('rawCall'); - $result = $invoker->invokeArgs($api, array('GET', '/me/accessRestriction/ip')) ; - foreach ($result as $restrictionId) { - $restriction = $invoker->invokeArgs($api, array('GET', '/me/accessRestriction/ip/' . $restrictionId)) ; + /** + * Test if get request on /me + */ + public function testGetRestrictionAccessIP() + { + $result = $this->api->get('/me/accessRestriction/ip'); - if ($restriction["ip"] == $this->rangeIP) { - $this->assertTrue(true); - break; - } - } - } + $restrictionIps = []; - /** - * Test if delete request on /me - */ - public function testDeleteRestrictionAccessIP() - { - $api = new Api($this->application_key, $this->application_secret, $this->endpoint, $this->consumer_key, $this->client); - $invoker = self::getPrivateMethod('rawCall'); + foreach ($result as $restrictionId) { + $restriction = $this->api->get('/me/accessRestriction/ip/' . $restrictionId); - $result = $invoker->invokeArgs($api, array('GET', '/me/accessRestriction/ip')) ; - foreach ($result as $restrictionId) { - $restriction = $invoker->invokeArgs($api, array('GET', '/me/accessRestriction/ip/' . $restrictionId)) ; + $restrictionIps[] = $restriction['ip']; + } - if ($restriction["ip"] == $this->rangeIP) { - $result = $invoker->invokeArgs($api, array('DELETE', '/me/accessRestriction/ip/'. $restrictionId)) ; - $this->assertNull( $result ); - break; - } - } - } + $this->assertContains($this->rangeIP, $restrictionIps); + $this->assertContains($this->alternativeRangeIP, $restrictionIps); + } - /** - * Test if request without authentication works - */ - public function testIfRequestWithoutAuthenticationWorks() - { - $api = new Api($this->application_key, $this->application_secret, $this->endpoint, NULL, $this->client); - $invoker = self::getPrivateMethod('rawCall'); - $result = $invoker->invokeArgs($api, array('GET', '/xdsl/incidents')) ; - } + /** + * Test if delete request on /me + */ + public function testPutRestrictionAccessIP() + { + $result = $this->api->get('/me/accessRestriction/ip'); + $restrictionId = array_pop($result); + + $this->assertNull( + $this->api->put('/me/accessRestriction/ip/' . $restrictionId, ['rule' => 'accept', 'warning' => true]) + ); + + $restriction = $this->api->get('/me/accessRestriction/ip/' . $restrictionId); + $this->assertEquals('accept', $restriction['rule']); + } + + /** + * Test if delete request on /me + */ + public function testDeleteRestrictionAccessIP() + { + $result = $this->api->get('/me/accessRestriction/ip'); + foreach ($result as $restrictionId) { + $restriction = $this->api->get('/me/accessRestriction/ip/' . $restrictionId); + + if (in_array($restriction["ip"], [$this->rangeIP, $this->alternativeRangeIP])) { + $result = $this->api->delete('/me/accessRestriction/ip/' . $restrictionId); + $this->assertNull($result); + break; + } + } + } + + /** + * Test if request without authentication works + */ + public function testIfRequestWithoutAuthenticationWorks() + { + $api = new Api($this->application_key, $this->application_secret, $this->endpoint, null, $this->client); + $invoker = self::getPrivateMethod('rawCall'); + $invoker->invokeArgs($api, ['GET', '/xdsl/incidents']); + } + + /** + * Test Api::get + */ + public function testApiGetWithParameters() + { + $this->setExpectedException('\\GuzzleHttp\\Exception\\ClientException', '400'); + + $this->api->get('/me/accessRestriction/ip', ['foo' => 'bar']); + } } - diff --git a/tests/ApiTest.php b/tests/ApiTest.php index 4788124..6a4c8da 100644 --- a/tests/ApiTest.php +++ b/tests/ApiTest.php @@ -27,140 +27,259 @@ namespace Ovh\tests; -use Ovh\Api; - use GuzzleHttp\Client; -use GuzzleHttp\Subscriber\Mock; +use GuzzleHttp\Middleware; +use GuzzleHttp\Psr7\Response; +use Ovh\Api; /** * Test Api class * - * @package Ovh + * @package Ovh * @category Ovh - * @author Vincent Cassé + * @author Vincent Cassé + * @author Thierry Goettelmann */ -class ApiTest extends \PHPUnit_Framework_TestCase { +class ApiTest extends \PHPUnit_Framework_TestCase +{ + /** + * @var Client + */ + private $client; - /** - * Define id to create object - */ - protected function setUp() + /** + * @var string + */ + private $application_key; + + /** + * @var string + */ + private $consumer_key; + + /** + * @var string + */ + private $endpoint; + + /** + * @var string + */ + private $application_secret; + + /** + * Define id to create object + */ + protected function setUp() { - $this->application_key = 'app_key'; - $this->application_secret = 'app_secret'; - $this->consumer_key = 'consumer'; - $this->endpoint = 'ovh-eu'; + $this->application_key = 'app_key'; + $this->application_secret = 'app_secret'; + $this->consumer_key = 'consumer'; + $this->endpoint = 'ovh-eu'; $this->client = new Client(); } /** * Get private and protected method to unit test it + * + * @param string $name + * + * @return \ReflectionMethod */ protected static function getPrivateMethod($name) { - $class = new \ReflectionClass('Ovh\Api'); + $class = new \ReflectionClass('Ovh\Api'); $method = $class->getMethod($name); $method->setAccessible(true); + return $method; } + /** + * Get private and protected property to unit test it + * + * @param string $name + * + * @return \ReflectionProperty + */ protected static function getPrivateProperty($name) { - $class = new \ReflectionClass('Ovh\Api'); + $class = new \ReflectionClass('Ovh\Api'); $property = $class->getProperty($name); $property->setAccessible(true); + return $property; } - /** - * Test the compute of time delta - */ - public function testTimeDeltaCompute() - { - $delay = 10; - $mock = new Mock([ - "HTTP/1.1 200 OK\r\n\r\n". (time()-$delay), - ]); - $this->client->getEmitter()->attach($mock); + /** + * Test missing $application_key + */ + public function testMissingApplicationKey() + { + $this->setExpectedException('\\Ovh\\Exceptions\\InvalidParameterException', 'Application key'); + new Api(null, $this->application_secret, $this->endpoint, $this->consumer_key, $this->client); + } - $invoker = self::getPrivateMethod('calculateTimeDelta'); - $property = self::getPrivateProperty('time_delta'); - $api = new Api($this->application_key, $this->application_secret, $this->endpoint, $this->consumer_key, $this->client); - $invoker->invokeArgs($api, array()) ; + /** + * Test missing $application_secret + */ + public function testMissingApplicationSecret() + { + $this->setExpectedException('\\Ovh\\Exceptions\\InvalidParameterException', 'Application secret'); + new Api($this->application_key, null, $this->endpoint, $this->consumer_key, $this->client); + } - $time_delta = $property->getValue($api); - $this->assertNotNull($time_delta); - $this->assertEquals($time_delta, $delay * -1 ); - } + /** + * Test missing $api_endpoint + */ + public function testMissingApiEndpoint() + { + $this->setExpectedException('\\Ovh\\Exceptions\\InvalidParameterException', 'Endpoint'); + new Api($this->application_key, $this->application_secret, null, $this->consumer_key, $this->client); + } - /** - * Test if consumer key is replaced - */ - public function testIfConsumerKeyIsReplace() - { - $delay = 10; - $mock = new Mock([ - "HTTP/1.1 200 OK\r\n\r\n". '{"validationUrl":"https://api.ovh.com/login/?credentialToken=token","consumerKey":"consumer_remote","state":"pendingValidation"}' - ]); - $this->client->getEmitter()->attach($mock); + /** + * Test bad $api_endpoint + */ + public function testBadApiEndpoint() + { + $this->setExpectedException('\\Ovh\\Exceptions\\InvalidParameterException', 'Unknown'); + new Api($this->application_key, $this->application_secret, 'i_am_invalid', $this->consumer_key, $this->client); + } - $property = self::getPrivateProperty('consumer_key'); - $api = new Api($this->application_key, $this->application_secret, $this->endpoint, $this->consumer_key, $this->client); - $accessRules = array( json_decode(' { "method": "GET", "path": "/*" } ') ); + /** + * Test creating Client if none is provided + */ + public function testClientCreation() + { + $api = new Api($this->application_key, $this->application_secret, $this->endpoint, $this->consumer_key); - $credentials = $api->requestCredentials($accessRules); - $consumer_key = $property->getValue($api); + $this->assertInstanceOf('\\GuzzleHttp\\Client', $api->getHttpClient()); + } - $this->assertEquals( $consumer_key , $credentials["consumerKey"]); - $this->assertNotEquals( $consumer_key, $this->consumer_key ); - } + /** + * Test the compute of time delta + */ + public function testTimeDeltaCompute() + { + $delay = 10; - /** - * Test invalid applicationKey - */ - public function testInvalidApplicationKey() - { - $this->setExpectedException( - '\GuzzleHttp\Exception\ClientException' + $handlerStack = $this->client->getConfig('handler'); + $handlerStack->push(Middleware::mapResponse(function (Response $response) { + + $body = $response->getBody(); + $body->write(time() - 10); + + return $response + ->withStatus(200) + ->withBody($body); + })); + + $invoker = self::getPrivateMethod('calculateTimeDelta'); + $property = self::getPrivateProperty('time_delta'); + + $api = new Api($this->application_key, $this->application_secret, $this->endpoint, $this->consumer_key, $this->client); + $invoker->invokeArgs($api, []); + + $time_delta = $property->getValue($api); + $this->assertNotNull($time_delta); + $this->assertEquals($time_delta, $delay * -1); + } + + /** + * Test if consumer key is replaced + */ + public function testIfConsumerKeyIsReplace() + { + $handlerStack = $this->client->getConfig('handler'); + $handlerStack->push(Middleware::mapResponse(function (Response $response) { + + $body = $response->getBody(); + $body->write('{"validationUrl":"https://api.ovh.com/login/?credentialToken=token","consumerKey":"consumer_remote","state":"pendingValidation"}'); + + return $response + ->withStatus(200) + ->withBody($body); + })); + + $property = self::getPrivateProperty('consumer_key'); + + $api = new Api($this->application_key, $this->application_secret, $this->endpoint, $this->consumer_key, $this->client); + $accessRules = [json_decode(' { "method": "GET", "path": "/*" } ')]; + + $credentials = $api->requestCredentials($accessRules); + + $consumer_key = $property->getValue($api); + + $this->assertEquals($consumer_key, $credentials["consumerKey"]); + $this->assertNotEquals($consumer_key, $this->consumer_key); + } + + /** + * Test invalid applicationKey + */ + public function testInvalidApplicationKey() + { + $this->setExpectedException( + '\GuzzleHttp\Exception\ClientException' ); - $delay = 10; - $mock = new Mock([ - "HTTP/1.1 401 Unauthorized\r\nContent-Type: application/json; charset=utf-8\r\nContent-Length: 37\r\n\r\n{\"message\":\"Invalid application key\"}" - ]); - $this->client->getEmitter()->attach($mock); + $handlerStack = $this->client->getConfig('handler'); + $handlerStack->push(Middleware::mapResponse(function (Response $response) { - $property = self::getPrivateProperty('consumer_key'); - $api = new Api($this->application_key, $this->application_secret, $this->endpoint, $this->consumer_key, $this->client); - $accessRules = array( json_decode(' { "method": "GET", "path": "/*" } ') ); + $body = $response->getBody(); + $body->write('{\"message\":\"Invalid application key\"}'); - $credentials = $api->requestCredentials($accessRules); - $consumer_key = $property->getValue($api); + return $response + ->withStatus(401, 'POUET') + ->withHeader('Content-Type', 'application/json; charset=utf-8') + ->withHeader('Content-Length', 37) + ->withBody($body); + })); - $this->assertEquals( $consumer_key , $credentials["consumerKey"]); - $this->assertNotEquals( $consumer_key, $this->consumer_key ); - } + $property = self::getPrivateProperty('consumer_key'); + $api = new Api($this->application_key, $this->application_secret, $this->endpoint, $this->consumer_key, $this->client); + $accessRules = [json_decode(' { "method": "GET", "path": "/*" } ')]; - /** - * Test invalid rights - */ - public function testInvalidRight() - { - $this->setExpectedException( - '\GuzzleHttp\Exception\ClientException' + $credentials = $api->requestCredentials($accessRules); + $consumer_key = $property->getValue($api); + + $this->assertEquals($consumer_key, $credentials["consumerKey"]); + $this->assertNotEquals($consumer_key, $this->consumer_key); + } + + /** + * Test invalid rights + */ + public function testInvalidRight() + { + $this->setExpectedException( + '\GuzzleHttp\Exception\ClientException' ); - $delay = 10; - $mock = new Mock([ - "HTTP/1.1 403 Forbidden\r\nContent-Type: application/json; charset=utf-8\r\nContent-Length: 37\r\n\r\n{\"message\":\"Invalid credentials\"}" - ]); - $this->client->getEmitter()->attach($mock); + $handlerStack = $this->client->getConfig('handler'); + $handlerStack->push(Middleware::mapResponse(function (Response $response) { - $api = new Api($this->application_key, $this->application_secret, $this->endpoint, $this->consumer_key, $this->client); + $body = $response->getBody(); + $body->write('{\"message\":\"Invalid credentials\"}'); - $invoker = self::getPrivateMethod('rawCall'); - $invoker->invokeArgs($api, array('GET', '/me')) ; - } + return $response + ->withStatus(403) + ->withHeader('Content-Type', 'application/json; charset=utf-8') + ->withHeader('Content-Length', 37) + ->withBody($body); + })); + + $api = new Api($this->application_key, $this->application_secret, $this->endpoint, $this->consumer_key, $this->client); + + $invoker = self::getPrivateMethod('rawCall'); + $invoker->invokeArgs($api, ['GET', '/me']); + } + + public function testGetConsumerKey() + { + $api = new Api($this->application_key, $this->application_secret, $this->endpoint, $this->consumer_key, $this->client); + $this->assertEquals($this->consumer_key, $api->getConsumerKey()); + } } -