2014-08-22 18:22:16 +02:00
|
|
|
<?php
|
2017-01-02 13:03:30 +01:00
|
|
|
# Copyright (c) 2013-2017, OVH SAS.
|
2014-08-22 18:22:16 +02:00
|
|
|
# All rights reserved.
|
|
|
|
#
|
|
|
|
# Redistribution and use in source and binary forms, with or without
|
|
|
|
# modification, are permitted provided that the following conditions are met:
|
|
|
|
#
|
|
|
|
# * Redistributions of source code must retain the above copyright
|
|
|
|
# notice, this list of conditions and the following disclaimer.
|
|
|
|
# * Redistributions in binary form must reproduce the above copyright
|
|
|
|
# notice, this list of conditions and the following disclaimer in the
|
|
|
|
# documentation and/or other materials provided with the distribution.
|
|
|
|
# * Neither the name of OVH SAS nor the
|
|
|
|
# names of its contributors may be used to endorse or promote products
|
|
|
|
# derived from this software without specific prior written permission.
|
|
|
|
#
|
|
|
|
# THIS SOFTWARE IS PROVIDED BY OVH SAS AND CONTRIBUTORS ``AS IS'' AND ANY
|
|
|
|
# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
|
|
|
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
# DISCLAIMED. IN NO EVENT SHALL OVH SAS AND CONTRIBUTORS BE LIABLE FOR ANY
|
|
|
|
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
|
|
|
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
|
|
|
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
|
|
|
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
|
|
|
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
|
|
|
namespace Ovh\tests;
|
|
|
|
|
|
|
|
use GuzzleHttp\Client;
|
2020-12-08 13:10:01 +01:00
|
|
|
use GuzzleHttp\Exception\ClientException;
|
2014-08-22 18:22:16 +02:00
|
|
|
use Ovh\Api;
|
2020-12-08 13:10:01 +01:00
|
|
|
use PHPUnit\Framework\TestCase;
|
2014-08-22 18:22:16 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Functional tests of Api class
|
|
|
|
*
|
2015-08-06 15:15:19 +02:00
|
|
|
* @package Ovh
|
2014-08-22 18:22:16 +02:00
|
|
|
* @category Ovh
|
|
|
|
*/
|
2020-12-08 13:10:01 +01:00
|
|
|
class ApiFunctionalTest extends TestCase
|
2015-08-06 15:15:19 +02:00
|
|
|
{
|
2014-08-22 18:22:16 +02:00
|
|
|
|
2015-08-06 15:15:19 +02:00
|
|
|
/**
|
|
|
|
* @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
|
|
|
|
*/
|
2020-12-08 13:10:01 +01:00
|
|
|
protected function setUp() :void
|
2014-08-22 18:22:16 +02:00
|
|
|
{
|
2023-03-31 19:12:25 +02:00
|
|
|
foreach (['APP_KEY', 'APP_SECRET', 'CONSUMER', 'ENDPOINT'] as $envName) {
|
|
|
|
if (!getenv($envName)) {
|
|
|
|
$this->markTestSkipped("Skip test due to missing $envName variable");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-06 15:15:19 +02:00
|
|
|
$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';
|
2014-08-22 18:22:16 +02:00
|
|
|
|
|
|
|
$this->client = new Client();
|
2015-08-06 15:15:19 +02:00
|
|
|
$this->api = new Api(
|
|
|
|
$this->application_key,
|
|
|
|
$this->application_secret,
|
|
|
|
$this->endpoint,
|
|
|
|
$this->consumer_key,
|
|
|
|
$this->client
|
|
|
|
);
|
2014-08-22 18:22:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get private and protected method to unit test it
|
2015-08-06 15:15:19 +02:00
|
|
|
*
|
|
|
|
* @param string $name
|
|
|
|
*
|
|
|
|
* @return \ReflectionMethod
|
2014-08-22 18:22:16 +02:00
|
|
|
*/
|
|
|
|
protected static function getPrivateMethod($name)
|
|
|
|
{
|
2015-08-06 15:15:19 +02:00
|
|
|
$class = new \ReflectionClass('Ovh\Api');
|
2014-08-22 18:22:16 +02:00
|
|
|
$method = $class->getMethod($name);
|
|
|
|
$method->setAccessible(true);
|
2015-08-06 15:15:19 +02:00
|
|
|
|
2014-08-22 18:22:16 +02:00
|
|
|
return $method;
|
|
|
|
}
|
|
|
|
|
2015-08-06 15:15:19 +02:00
|
|
|
/**
|
|
|
|
* Get private and protected property to unit test it
|
|
|
|
*
|
|
|
|
* @param string $name
|
|
|
|
*
|
|
|
|
* @return \ReflectionProperty
|
|
|
|
*/
|
2014-08-22 18:22:16 +02:00
|
|
|
protected static function getPrivateProperty($name)
|
|
|
|
{
|
2015-08-06 15:15:19 +02:00
|
|
|
$class = new \ReflectionClass('Ovh\Api');
|
2014-08-22 18:22:16 +02:00
|
|
|
$property = $class->getProperty($name);
|
|
|
|
$property->setAccessible(true);
|
2015-08-06 15:15:19 +02:00
|
|
|
|
2014-08-22 18:22:16 +02:00
|
|
|
return $property;
|
|
|
|
}
|
|
|
|
|
2015-08-06 15:15:19 +02:00
|
|
|
/**
|
|
|
|
* Test if result contains consumerKey and validationUrl
|
|
|
|
*/
|
|
|
|
public function testIfConsumerKeyIsReplace()
|
|
|
|
{
|
|
|
|
$property = self::getPrivateProperty('consumer_key');
|
|
|
|
$accessRules = json_decode(' [
|
2014-08-22 18:22:16 +02:00
|
|
|
{ "method": "GET", "path": "/*" },
|
|
|
|
{ "method": "POST", "path": "/*" },
|
|
|
|
{ "method": "PUT", "path": "/*" },
|
|
|
|
{ "method": "DELETE", "path": "/*" }
|
2015-08-06 15:15:19 +02:00
|
|
|
] ');
|
2014-08-22 18:22:16 +02:00
|
|
|
|
2015-08-06 15:15:19 +02:00
|
|
|
$credentials = $this->api->requestCredentials($accessRules);
|
|
|
|
$consumer_key = $property->getValue($this->api);
|
|
|
|
|
2023-05-31 11:09:01 +02:00
|
|
|
$this->assertSame($consumer_key, $credentials["consumerKey"]);
|
2015-08-06 15:15:19 +02:00
|
|
|
$this->assertNotEquals($consumer_key, $this->consumer_key);
|
|
|
|
}
|
2014-08-22 18:22:16 +02:00
|
|
|
|
2015-08-06 15:15:19 +02:00
|
|
|
/**
|
|
|
|
* Test if post request on me
|
|
|
|
*/
|
|
|
|
public function testPostRestrictionAccessIp()
|
|
|
|
{
|
|
|
|
$this->assertNull(
|
|
|
|
$this->api->post('/me/accessRestriction/ip', ['ip' => $this->rangeIP, 'rule' => 'deny', 'warning' => true])
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertNull(
|
|
|
|
$this->api->post('/me/accessRestriction/ip', ['ip' => $this->alternativeRangeIP,
|
|
|
|
'rule' => 'deny',
|
|
|
|
'warning' => true,
|
|
|
|
])
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test if get request on /me
|
|
|
|
*/
|
|
|
|
public function testGetRestrictionAccessIP()
|
|
|
|
{
|
|
|
|
$result = $this->api->get('/me/accessRestriction/ip');
|
|
|
|
|
|
|
|
$restrictionIps = [];
|
|
|
|
|
|
|
|
foreach ($result as $restrictionId) {
|
|
|
|
$restriction = $this->api->get('/me/accessRestriction/ip/' . $restrictionId);
|
|
|
|
|
|
|
|
$restrictionIps[] = $restriction['ip'];
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->assertContains($this->rangeIP, $restrictionIps);
|
|
|
|
$this->assertContains($this->alternativeRangeIP, $restrictionIps);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test if delete request on /me
|
|
|
|
*/
|
|
|
|
public function testPutRestrictionAccessIP()
|
|
|
|
{
|
|
|
|
$result = $this->api->get('/me/accessRestriction/ip');
|
|
|
|
|
2022-08-03 20:28:35 +02:00
|
|
|
foreach ($result as $restrictionId) {
|
|
|
|
$restriction = $this->api->get('/me/accessRestriction/ip/' . $restrictionId);
|
2015-08-06 15:15:19 +02:00
|
|
|
|
2022-08-03 20:28:35 +02:00
|
|
|
if (in_array($restriction["ip"], [$this->rangeIP, $this->alternativeRangeIP])) {
|
|
|
|
$this->assertNull(
|
|
|
|
$this->api->put('/me/accessRestriction/ip/' . $restrictionId, ['rule' => 'accept', 'warning' => true])
|
|
|
|
);
|
2015-08-06 15:15:19 +02:00
|
|
|
|
2022-08-03 20:28:35 +02:00
|
|
|
$restriction = $this->api->get('/me/accessRestriction/ip/' . $restrictionId);
|
2023-05-31 11:09:01 +02:00
|
|
|
$this->assertSame('accept', $restriction['rule']);
|
2022-08-03 20:28:35 +02:00
|
|
|
}
|
|
|
|
}
|
2015-08-06 15:15:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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');
|
2022-08-03 20:28:35 +02:00
|
|
|
$result = $invoker->invokeArgs($api, ['GET', '/xdsl/incidents']);
|
|
|
|
$this->assertIsObject($result);
|
2015-08-06 15:15:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test Api::get
|
|
|
|
*/
|
|
|
|
public function testApiGetWithParameters()
|
|
|
|
{
|
2020-12-08 13:10:01 +01:00
|
|
|
$this->expectException(ClientException::class);
|
2015-08-06 15:15:19 +02:00
|
|
|
|
|
|
|
$this->api->get('/me/accessRestriction/ip', ['foo' => 'bar']);
|
|
|
|
}
|
2016-01-19 14:10:44 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Test Api::get, should build valide signature
|
|
|
|
*/
|
|
|
|
public function testApiGetWithQueryString()
|
|
|
|
{
|
2022-08-03 20:28:35 +02:00
|
|
|
$result = $this->api->get('/me/api/credential', ['status' => 'pendingValidation']);
|
|
|
|
$this->assertIsArray($result);
|
2016-01-19 14:10:44 +01:00
|
|
|
}
|
2016-09-03 21:53:50 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Test APi::get without authentication
|
|
|
|
*/
|
|
|
|
public function testApiGetWithoutAuthentication()
|
|
|
|
{
|
2022-08-03 20:28:35 +02:00
|
|
|
$api = new Api(null, null, $this->endpoint, null, $this->client);
|
|
|
|
$result = $api->get('/hosting/web/moduleList', null, null, false);
|
|
|
|
$this->assertIsArray($result);
|
2016-09-03 21:53:50 +02:00
|
|
|
}
|
2015-08-06 15:15:19 +02:00
|
|
|
}
|