mirror of
https://github.com/ovh/php-ovh.git
synced 2023-11-05 03:20:26 +01:00
Merge pull request #21 from VincentCasse/master
Welcome to 2.x.x branch :)
This commit is contained in:
commit
a3afac6cf0
10
.travis.yml
10
.travis.yml
@ -1,12 +1,18 @@
|
|||||||
|
addons:
|
||||||
|
apt:
|
||||||
|
packages:
|
||||||
|
- libcurl4-openssl-dev
|
||||||
|
|
||||||
language: php
|
language: php
|
||||||
php:
|
php:
|
||||||
- hhvm
|
- hhvm
|
||||||
|
- 7.0
|
||||||
- 5.6
|
- 5.6
|
||||||
- 5.5
|
- 5.5
|
||||||
- 5.4
|
|
||||||
- 7.0
|
- 7.0
|
||||||
|
|
||||||
before_script:
|
before_script:
|
||||||
- composer install --dev
|
- composer self-update
|
||||||
|
- composer install
|
||||||
|
|
||||||
script: phpunit tests/ApiTest.php
|
script: phpunit tests/ApiTest.php
|
||||||
|
@ -3,13 +3,8 @@
|
|||||||
"description": "Wrapper for OVH APIs",
|
"description": "Wrapper for OVH APIs",
|
||||||
"license": "BSD-3-Clause",
|
"license": "BSD-3-Clause",
|
||||||
"require": {
|
"require": {
|
||||||
"guzzlehttp/guzzle": ">=4.0,<6.0"
|
"guzzlehttp/guzzle": "^6.0"
|
||||||
},
|
},
|
||||||
"authors": [
|
|
||||||
{
|
|
||||||
"name": "Vincent Cassé"
|
|
||||||
}
|
|
||||||
],
|
|
||||||
"autoload": {
|
"autoload": {
|
||||||
"psr-4": {"Ovh\\": "src/"}
|
"psr-4": {"Ovh\\": "src/"}
|
||||||
},
|
},
|
||||||
|
205
src/Api.php
205
src/Api.php
@ -1,5 +1,5 @@
|
|||||||
<?php
|
<?php
|
||||||
# Copyright (c) 2013-2014, OVH SAS.
|
# Copyright (c) 2013-2016, OVH SAS.
|
||||||
# All rights reserved.
|
# All rights reserved.
|
||||||
#
|
#
|
||||||
# Redistribution and use in source and binary forms, with or without
|
# Redistribution and use in source and binary forms, with or without
|
||||||
@ -30,8 +30,9 @@
|
|||||||
|
|
||||||
namespace Ovh;
|
namespace Ovh;
|
||||||
|
|
||||||
use GuzzleHttp\Client as GClient;
|
use GuzzleHttp\Client;
|
||||||
use GuzzleHttp\Stream\Stream;
|
use GuzzleHttp\Psr7\Request;
|
||||||
|
use GuzzleHttp\Psr7\Response;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Wrapper to manage login and exchanges with simpliest Ovh API
|
* Wrapper to manage login and exchanges with simpliest Ovh API
|
||||||
@ -41,74 +42,88 @@ use GuzzleHttp\Stream\Stream;
|
|||||||
* Http connections use guzzle http client api and result of request are
|
* Http connections use guzzle http client api and result of request are
|
||||||
* object from this http wrapper
|
* object from this http wrapper
|
||||||
*
|
*
|
||||||
* @package Ovh
|
* @package Ovh
|
||||||
* @category Ovh
|
* @category Ovh
|
||||||
* @author Vincent Cassé <vincent.casse@ovh.net>
|
|
||||||
*/
|
*/
|
||||||
class Api
|
class Api
|
||||||
{
|
{
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Url to communicate with Ovh API
|
* Url to communicate with Ovh API
|
||||||
|
*
|
||||||
|
* @var array
|
||||||
*/
|
*/
|
||||||
private $endpoints = array(
|
private $endpoints = [
|
||||||
'ovh-eu' => 'https://api.ovh.com/1.0',
|
'ovh-eu' => 'https://api.ovh.com/1.0',
|
||||||
'ovh-ca' => 'https://ca.api.ovh.com/1.0',
|
'ovh-ca' => 'https://ca.api.ovh.com/1.0',
|
||||||
'kimsufi-eu' => 'https://eu.api.kimsufi.com/1.0',
|
'kimsufi-eu' => 'https://eu.api.kimsufi.com/1.0',
|
||||||
'kimsufi-ca' => 'https://ca.api.kimsufi.com/1.0',
|
'kimsufi-ca' => 'https://ca.api.kimsufi.com/1.0',
|
||||||
'soyoustart-eu' => 'https://eu.api.soyoustart.com/1.0',
|
'soyoustart-eu' => 'https://eu.api.soyoustart.com/1.0',
|
||||||
'soyoustart-ca' => 'https://ca.api.soyoustart.com/1.0',
|
'soyoustart-ca' => 'https://ca.api.soyoustart.com/1.0',
|
||||||
'runabove-ca' => 'https://api.runabove.com/1.0');
|
'runabove-ca' => 'https://api.runabove.com/1.0',
|
||||||
|
];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Contain endpoint selected to choose API
|
* Contain endpoint selected to choose API
|
||||||
|
*
|
||||||
|
* @var string
|
||||||
*/
|
*/
|
||||||
private $endpoint = null;
|
private $endpoint = null;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Contain key of the current application
|
* Contain key of the current application
|
||||||
|
*
|
||||||
|
* @var string
|
||||||
*/
|
*/
|
||||||
private $application_key = null;
|
private $application_key = null;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Contain secret of the current application
|
* Contain secret of the current application
|
||||||
|
*
|
||||||
|
* @var string
|
||||||
*/
|
*/
|
||||||
private $application_secret = null;
|
private $application_secret = null;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Contain consumer key of the current application
|
* Contain consumer key of the current application
|
||||||
|
*
|
||||||
|
* @var string
|
||||||
*/
|
*/
|
||||||
private $consumer_key = null;
|
private $consumer_key = null;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Contain delta between local timestamp and api server timestamp
|
* Contain delta between local timestamp and api server timestamp
|
||||||
|
*
|
||||||
|
* @var string
|
||||||
*/
|
*/
|
||||||
private $time_delta = null;
|
private $time_delta = null;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Contain http client connection
|
* Contain http client connection
|
||||||
|
*
|
||||||
|
* @var Client
|
||||||
*/
|
*/
|
||||||
private $http_client = null;
|
private $http_client = null;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Construct a new wrapper instance
|
* Construct a new wrapper instance
|
||||||
*
|
*
|
||||||
* @param $application_key key of your application.
|
* @param string $application_key key of your application.
|
||||||
* For OVH APIs, you can create a application's credentials on https://api.ovh.com/createApp/
|
* For OVH APIs, you can create a application's credentials on
|
||||||
* @param $application_secret secret of your application.
|
* https://api.ovh.com/createApp/
|
||||||
* @param $api_endpoint name of api selected
|
* @param string $application_secret secret of your application.
|
||||||
* @param $consumer_key If you have already a consumer key, this parameter prevent to do a
|
* @param string $api_endpoint name of api selected
|
||||||
* new authentication
|
* @param string $consumer_key If you have already a consumer key, this parameter prevent to do a
|
||||||
* @param $http_client instance of http client
|
* new authentication
|
||||||
|
* @param Client $http_client instance of http client
|
||||||
*
|
*
|
||||||
* @throws InvalidParameterException if one parameter is missing or with bad value
|
* @throws Exceptions\InvalidParameterException if one parameter is missing or with bad value
|
||||||
*/
|
*/
|
||||||
public function __construct(
|
public function __construct(
|
||||||
$application_key,
|
$application_key,
|
||||||
$application_secret,
|
$application_secret,
|
||||||
$api_endpoint,
|
$api_endpoint,
|
||||||
$consumer_key = null,
|
$consumer_key = null,
|
||||||
GClient $http_client = null
|
Client $http_client = null
|
||||||
) {
|
) {
|
||||||
if (!isset($application_key)) {
|
if (!isset($application_key)) {
|
||||||
throw new Exceptions\InvalidParameterException("Application key parameter is empty");
|
throw new Exceptions\InvalidParameterException("Application key parameter is empty");
|
||||||
@ -127,31 +142,34 @@ class Api
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (!isset($http_client)) {
|
if (!isset($http_client)) {
|
||||||
$http_client = new GClient();
|
$http_client = new Client([
|
||||||
$http_client->setDefaultOption('timeout', 30);
|
'timeout' => 30,
|
||||||
$http_client->setDefaultOption('connect_timeout', 5);
|
'connect_timeout' => 5,
|
||||||
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->application_key = $application_key;
|
$this->application_key = $application_key;
|
||||||
$this->endpoint = $this->endpoints[$api_endpoint];
|
$this->endpoint = $this->endpoints[$api_endpoint];
|
||||||
$this->application_secret = $application_secret;
|
$this->application_secret = $application_secret;
|
||||||
$this->http_client = $http_client;
|
$this->http_client = $http_client;
|
||||||
$this->consumer_key = $consumer_key;
|
$this->consumer_key = $consumer_key;
|
||||||
$this->time_delta = null;
|
$this->time_delta = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Calculate time delta between local machine and API's server
|
* Calculate time delta between local machine and API's server
|
||||||
*
|
*
|
||||||
* @throws \GuzzleHttp\Exception\ClientException if http request is an error
|
* @throws \GuzzleHttp\Exception\ClientException if http request is an error
|
||||||
|
* @return int
|
||||||
*/
|
*/
|
||||||
private function calculateTimeDelta()
|
private function calculateTimeDelta()
|
||||||
{
|
{
|
||||||
if (!isset($this->time_delta)) {
|
if (!isset($this->time_delta)) {
|
||||||
$response = $this->http_client->get($this->endpoint . "/auth/time");
|
$response = $this->http_client->get($this->endpoint . "/auth/time");
|
||||||
$serverTimestamp = (int) (String) $response->getBody();
|
$serverTimestamp = (int)(String)$response->getBody();
|
||||||
$this->time_delta = $serverTimestamp - (int) \time();
|
$this->time_delta = $serverTimestamp - (int)\time();
|
||||||
}
|
}
|
||||||
|
|
||||||
return $this->time_delta;
|
return $this->time_delta;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -159,16 +177,17 @@ class Api
|
|||||||
* Request a consumer key from the API and the validation link to
|
* Request a consumer key from the API and the validation link to
|
||||||
* authorize user to validate this consumer key
|
* authorize user to validate this consumer key
|
||||||
*
|
*
|
||||||
* @param $accessRules list of rules your application need.
|
* @param array $accessRules list of rules your application need.
|
||||||
* @param $redirection url to redirect on your website after authentication
|
* @param string $redirection url to redirect on your website after authentication
|
||||||
*
|
*
|
||||||
|
* @return mixed
|
||||||
* @throws \GuzzleHttp\Exception\ClientException if http request is an error
|
* @throws \GuzzleHttp\Exception\ClientException if http request is an error
|
||||||
*/
|
*/
|
||||||
public function requestCredentials(
|
public function requestCredentials(
|
||||||
$accessRules,
|
array $accessRules,
|
||||||
$redirection = null
|
$redirection = null
|
||||||
) {
|
) {
|
||||||
$parameters = new \StdClass();
|
$parameters = new \StdClass();
|
||||||
$parameters->accessRules = $accessRules;
|
$parameters->accessRules = $accessRules;
|
||||||
$parameters->redirection = $redirection;
|
$parameters->redirection = $redirection;
|
||||||
|
|
||||||
@ -181,6 +200,7 @@ class Api
|
|||||||
);
|
);
|
||||||
|
|
||||||
$this->consumer_key = $response["consumerKey"];
|
$this->consumer_key = $response["consumerKey"];
|
||||||
|
|
||||||
return $response;
|
return $response;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -188,42 +208,44 @@ class Api
|
|||||||
* This is the main method of this wrapper. It will
|
* This is the main method of this wrapper. It will
|
||||||
* sign a given query and return its result.
|
* sign a given query and return its result.
|
||||||
*
|
*
|
||||||
* @param $method HTTP method of request (GET,POST,PUT,DELETE)
|
* @param string $method HTTP method of request (GET,POST,PUT,DELETE)
|
||||||
* @param $path relative url of API request
|
* @param string $path relative url of API request
|
||||||
* @param $content body of the request
|
* @param \stdClass|array|null $content body of the request
|
||||||
* @param $is_authenticated if the request use authentication
|
* @param bool $is_authenticated if the request use authentication
|
||||||
*
|
*
|
||||||
|
* @return array
|
||||||
* @throws \GuzzleHttp\Exception\ClientException if http request is an error
|
* @throws \GuzzleHttp\Exception\ClientException if http request is an error
|
||||||
*/
|
*/
|
||||||
private function rawCall(
|
private function rawCall($method, $path, $content = null, $is_authenticated = true)
|
||||||
$method,
|
{
|
||||||
$path,
|
$url = $this->endpoint . $path;
|
||||||
$content = null,
|
$request = new Request($method, $url);
|
||||||
$is_authenticated = true
|
|
||||||
) {
|
|
||||||
$url = $this->endpoint . $path;
|
|
||||||
|
|
||||||
$request = $this->http_client->createRequest(
|
|
||||||
$method,
|
|
||||||
$url
|
|
||||||
);
|
|
||||||
|
|
||||||
if (isset($content) && $method == 'GET') {
|
if (isset($content) && $method == 'GET') {
|
||||||
$query = $request->getQuery();
|
|
||||||
foreach ($content as $key => $value) {
|
$queryString = $request->getUri()->getQuery();
|
||||||
$query->set($key, $value);
|
|
||||||
}
|
$query = false !== strpos($queryString, '&')
|
||||||
$url .= '?'.$query;
|
? explode('&', $queryString)
|
||||||
$body = "";
|
: [];
|
||||||
|
|
||||||
|
$query = array_merge($query, (array)$content);
|
||||||
|
$query = \GuzzleHttp\Psr7\build_query($query);
|
||||||
|
|
||||||
|
$request = $request->withUri($request->getUri()->withQuery($query));
|
||||||
|
$body = "";
|
||||||
} elseif (isset($content)) {
|
} elseif (isset($content)) {
|
||||||
$body = json_encode($content);
|
$body = json_encode($content);
|
||||||
$request->setBody(Stream::factory($body));
|
|
||||||
|
$request->getBody()->write($body);
|
||||||
} else {
|
} else {
|
||||||
$body = "";
|
$body = "";
|
||||||
}
|
}
|
||||||
|
|
||||||
$request->setHeader('Content-Type', 'application/json; charset=utf-8');
|
$headers = [
|
||||||
$request->setHeader('X-Ovh-Application', $this->application_key);
|
'Content-Type' => 'application/json; charset=utf-8',
|
||||||
|
'X-Ovh-Application' => $this->application_key,
|
||||||
|
];
|
||||||
|
|
||||||
if ($is_authenticated) {
|
if ($is_authenticated) {
|
||||||
if (!isset($this->time_delta)) {
|
if (!isset($this->time_delta)) {
|
||||||
@ -231,75 +253,76 @@ class Api
|
|||||||
}
|
}
|
||||||
$now = time() + $this->time_delta;
|
$now = time() + $this->time_delta;
|
||||||
|
|
||||||
$request->setHeader('X-Ovh-Timestamp', $now);
|
$headers['X-Ovh-Timestamp'] = $now;
|
||||||
|
|
||||||
if (isset($this->consumer_key)) {
|
if (isset($this->consumer_key)) {
|
||||||
$toSign = $this->application_secret.'+'.$this->consumer_key.'+'.$method.'+'.$url.'+'.$body.'+'.$now;
|
$toSign = $this->application_secret . '+' . $this->consumer_key . '+' . $method
|
||||||
$signature = '$1$' . sha1($toSign);
|
. '+' . $url . '+' . $body . '+' . $now;
|
||||||
$request->setHeader('X-Ovh-Consumer', $this->consumer_key);
|
$signature = '$1$' . sha1($toSign);
|
||||||
$request->setHeader('X-Ovh-Signature', $signature);
|
$headers['X-Ovh-Consumer'] = $this->consumer_key;
|
||||||
|
$headers['X-Ovh-Signature'] = $signature;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return $this->http_client->send($request)->json();
|
|
||||||
|
/** @var Response $response */
|
||||||
|
$response = $this->http_client->send($request, ['headers' => $headers]);
|
||||||
|
|
||||||
|
return json_decode($response->getBody(), true);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Wrap call to Ovh APIs for GET requests
|
* Wrap call to Ovh APIs for GET requests
|
||||||
*
|
*
|
||||||
* @param $path path ask inside api
|
* @param string $path path ask inside api
|
||||||
* @param $content content to send inside body of request
|
* @param array $content content to send inside body of request
|
||||||
*
|
*
|
||||||
|
* @return array
|
||||||
* @throws \GuzzleHttp\Exception\ClientException if http request is an error
|
* @throws \GuzzleHttp\Exception\ClientException if http request is an error
|
||||||
*/
|
*/
|
||||||
public function get(
|
public function get($path, $content = null)
|
||||||
$path,
|
{
|
||||||
$content = null
|
|
||||||
) {
|
|
||||||
return $this->rawCall("GET", $path, $content);
|
return $this->rawCall("GET", $path, $content);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Wrap call to Ovh APIs for POST requests
|
* Wrap call to Ovh APIs for POST requests
|
||||||
*
|
*
|
||||||
* @param $path path ask inside api
|
* @param string $path path ask inside api
|
||||||
* @param $content content to send inside body of request
|
* @param array $content content to send inside body of request
|
||||||
*
|
*
|
||||||
|
* @return array
|
||||||
* @throws \GuzzleHttp\Exception\ClientException if http request is an error
|
* @throws \GuzzleHttp\Exception\ClientException if http request is an error
|
||||||
*/
|
*/
|
||||||
public function post(
|
public function post($path, $content = null)
|
||||||
$path,
|
{
|
||||||
$content = null
|
|
||||||
) {
|
|
||||||
return $this->rawCall("POST", $path, $content);
|
return $this->rawCall("POST", $path, $content);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Wrap call to Ovh APIs for PUT requests
|
* Wrap call to Ovh APIs for PUT requests
|
||||||
*
|
*
|
||||||
* @param $path path ask inside api
|
* @param string $path path ask inside api
|
||||||
* @param $content content to send inside body of request
|
* @param array $content content to send inside body of request
|
||||||
*
|
*
|
||||||
|
* @return array
|
||||||
* @throws \GuzzleHttp\Exception\ClientException if http request is an error
|
* @throws \GuzzleHttp\Exception\ClientException if http request is an error
|
||||||
*/
|
*/
|
||||||
public function put(
|
public function put($path, $content)
|
||||||
$path,
|
{
|
||||||
$content
|
|
||||||
) {
|
|
||||||
return $this->rawCall("PUT", $path, $content);
|
return $this->rawCall("PUT", $path, $content);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Wrap call to Ovh APIs for DELETE requests
|
* Wrap call to Ovh APIs for DELETE requests
|
||||||
*
|
*
|
||||||
* @param $path path ask inside api
|
* @param string $path path ask inside api
|
||||||
* @param $content content to send inside body of request
|
* @param array $content content to send inside body of request
|
||||||
*
|
*
|
||||||
|
* @return array
|
||||||
* @throws \GuzzleHttp\Exception\ClientException if http request is an error
|
* @throws \GuzzleHttp\Exception\ClientException if http request is an error
|
||||||
*/
|
*/
|
||||||
public function delete(
|
public function delete($path, $content = null)
|
||||||
$path,
|
{
|
||||||
$content = null
|
|
||||||
) {
|
|
||||||
return $this->rawCall("DELETE", $path, $content);
|
return $this->rawCall("DELETE", $path, $content);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
<?php
|
<?php
|
||||||
# Copyright (c) 2013-2014, OVH SAS.
|
# Copyright (c) 2013-2016, OVH SAS.
|
||||||
# All rights reserved.
|
# All rights reserved.
|
||||||
#
|
#
|
||||||
# Redistribution and use in source and binary forms, with or without
|
# Redistribution and use in source and binary forms, with or without
|
||||||
@ -40,7 +40,6 @@ use Exception;
|
|||||||
*
|
*
|
||||||
* @package Ovh
|
* @package Ovh
|
||||||
* @category Exceptions
|
* @category Exceptions
|
||||||
* @author Vincent Cassé <vincent.casse@ovh.net>
|
|
||||||
*/
|
*/
|
||||||
class ApiException extends Exception
|
class ApiException extends Exception
|
||||||
{
|
{
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
<?php
|
<?php
|
||||||
# Copyright (c) 2013-2014, OVH SAS.
|
# Copyright (c) 2013-2016, OVH SAS.
|
||||||
# All rights reserved.
|
# All rights reserved.
|
||||||
#
|
#
|
||||||
# Redistribution and use in source and binary forms, with or without
|
# Redistribution and use in source and binary forms, with or without
|
||||||
@ -40,7 +40,6 @@ use Exception;
|
|||||||
*
|
*
|
||||||
* @package Ovh
|
* @package Ovh
|
||||||
* @category Exceptions
|
* @category Exceptions
|
||||||
* @author Vincent Cassé <vincent.casse@ovh.net>
|
|
||||||
*/
|
*/
|
||||||
class InvalidParameterException extends Exception
|
class InvalidParameterException extends Exception
|
||||||
{
|
{
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
<?php
|
<?php
|
||||||
# Copyright (c) 2013-2014, OVH SAS.
|
# Copyright (c) 2013-2016, OVH SAS.
|
||||||
# All rights reserved.
|
# All rights reserved.
|
||||||
#
|
#
|
||||||
# Redistribution and use in source and binary forms, with or without
|
# Redistribution and use in source and binary forms, with or without
|
||||||
@ -39,7 +39,6 @@ use Exception;
|
|||||||
*
|
*
|
||||||
* @package Ovh
|
* @package Ovh
|
||||||
* @category Exceptions
|
* @category Exceptions
|
||||||
* @author Vincent Cassé <vincent.casse@ovh.net>
|
|
||||||
*/
|
*/
|
||||||
class NotLoggedException extends Exception
|
class NotLoggedException extends Exception
|
||||||
{
|
{
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
<?php
|
<?php
|
||||||
# Copyright (c) 2013-2014, OVH SAS.
|
# Copyright (c) 2013-2016, OVH SAS.
|
||||||
# All rights reserved.
|
# All rights reserved.
|
||||||
#
|
#
|
||||||
# Redistribution and use in source and binary forms, with or without
|
# Redistribution and use in source and binary forms, with or without
|
||||||
@ -28,136 +28,218 @@
|
|||||||
namespace Ovh\tests;
|
namespace Ovh\tests;
|
||||||
|
|
||||||
use GuzzleHttp\Client;
|
use GuzzleHttp\Client;
|
||||||
use GuzzleHttp\Subscriber\Mock;
|
|
||||||
use Ovh\Api;
|
use Ovh\Api;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Functional tests of Api class
|
* Functional tests of Api class
|
||||||
*
|
*
|
||||||
* @package Ovh
|
* @package Ovh
|
||||||
* @category Ovh
|
* @category Ovh
|
||||||
* @author Vincent Cassé <vincent.casse@ovh.net>
|
|
||||||
*/
|
*/
|
||||||
class ApiFunctionalTest extends \PHPUnit_Framework_TestCase {
|
class ApiFunctionalTest extends \PHPUnit_Framework_TestCase
|
||||||
|
{
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Define id to create object
|
* @var string
|
||||||
*/
|
*/
|
||||||
protected function setUp()
|
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_key = getenv('APP_KEY');
|
||||||
$this->application_secret = getenv('APP_SECRET');
|
$this->application_secret = getenv('APP_SECRET');
|
||||||
$this->consumer_key = getenv('CONSUMER');
|
$this->consumer_key = getenv('CONSUMER');
|
||||||
$this->endpoint = getenv('ENDPOINT');
|
$this->endpoint = getenv('ENDPOINT');
|
||||||
$this->rangeIP = '127.0.0.20/32';
|
$this->rangeIP = '127.0.0.20/32';
|
||||||
|
$this->alternativeRangeIP = '127.0.0.30/32';
|
||||||
|
|
||||||
$this->client = new Client();
|
$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
|
* Get private and protected method to unit test it
|
||||||
|
*
|
||||||
|
* @param string $name
|
||||||
|
*
|
||||||
|
* @return \ReflectionMethod
|
||||||
*/
|
*/
|
||||||
protected static function getPrivateMethod($name)
|
protected static function getPrivateMethod($name)
|
||||||
{
|
{
|
||||||
$class = new \ReflectionClass('Ovh\Api');
|
$class = new \ReflectionClass('Ovh\Api');
|
||||||
$method = $class->getMethod($name);
|
$method = $class->getMethod($name);
|
||||||
$method->setAccessible(true);
|
$method->setAccessible(true);
|
||||||
|
|
||||||
return $method;
|
return $method;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get private and protected property to unit test it
|
||||||
|
*
|
||||||
|
* @param string $name
|
||||||
|
*
|
||||||
|
* @return \ReflectionProperty
|
||||||
|
*/
|
||||||
protected static function getPrivateProperty($name)
|
protected static function getPrivateProperty($name)
|
||||||
{
|
{
|
||||||
$class = new \ReflectionClass('Ovh\Api');
|
$class = new \ReflectionClass('Ovh\Api');
|
||||||
$property = $class->getProperty($name);
|
$property = $class->getProperty($name);
|
||||||
$property->setAccessible(true);
|
$property->setAccessible(true);
|
||||||
|
|
||||||
return $property;
|
return $property;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Test if result contains consumerKey and validationUrl
|
* Test if result contains consumerKey and validationUrl
|
||||||
*/
|
*/
|
||||||
public function testIfConsumerKeyIsReplace()
|
public function testIfConsumerKeyIsReplace()
|
||||||
{
|
{
|
||||||
$property = self::getPrivateProperty('consumer_key');
|
$property = self::getPrivateProperty('consumer_key');
|
||||||
$accessRules = json_decode(' [
|
$accessRules = json_decode(' [
|
||||||
{ "method": "GET", "path": "/*" },
|
{ "method": "GET", "path": "/*" },
|
||||||
{ "method": "POST", "path": "/*" },
|
{ "method": "POST", "path": "/*" },
|
||||||
{ "method": "PUT", "path": "/*" },
|
{ "method": "PUT", "path": "/*" },
|
||||||
{ "method": "DELETE", "path": "/*" }
|
{ "method": "DELETE", "path": "/*" }
|
||||||
] ') ;
|
] ');
|
||||||
|
|
||||||
$credentials = $this->api->requestCredentials($accessRules);
|
$credentials = $this->api->requestCredentials($accessRules);
|
||||||
$consumer_key = $property->getValue($this->api);
|
$consumer_key = $property->getValue($this->api);
|
||||||
|
|
||||||
$this->assertEquals( $consumer_key , $credentials["consumerKey"]);
|
$this->assertEquals($consumer_key, $credentials["consumerKey"]);
|
||||||
$this->assertNotEquals( $consumer_key, $this->consumer_key );
|
$this->assertNotEquals($consumer_key, $this->consumer_key);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Test if put request on me
|
* Test if post request on me
|
||||||
*/
|
*/
|
||||||
public function testPutRestrictionAccessIp()
|
public function testPostRestrictionAccessIp()
|
||||||
{
|
{
|
||||||
$api = new Api($this->application_key, $this->application_secret, $this->endpoint, $this->consumer_key, $this->client);
|
$this->assertNull(
|
||||||
$invoker = self::getPrivateMethod('rawCall');
|
$this->api->post('/me/accessRestriction/ip', ['ip' => $this->rangeIP, 'rule' => 'deny', 'warning' => true])
|
||||||
$params = new \StdClass();
|
);
|
||||||
$params->ip = $this->rangeIP;
|
|
||||||
$params->rule = "deny";
|
|
||||||
$params->warning = true;
|
|
||||||
|
|
||||||
$result = $invoker->invokeArgs($api, array('POST', '/me/accessRestriction/ip', $params)) ;
|
$this->assertNull(
|
||||||
$this->assertNull( $result );
|
$this->api->post('/me/accessRestriction/ip', ['ip' => $this->alternativeRangeIP,
|
||||||
}
|
'rule' => 'deny',
|
||||||
|
'warning' => true,
|
||||||
|
])
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Test if get request on /me
|
* Test if get request on /me
|
||||||
*/
|
*/
|
||||||
public function testGetRestrictionAccessIP()
|
public function testGetRestrictionAccessIP()
|
||||||
{
|
{
|
||||||
$api = new Api($this->application_key, $this->application_secret, $this->endpoint, $this->consumer_key, $this->client);
|
$result = $this->api->get('/me/accessRestriction/ip');
|
||||||
$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)) ;
|
|
||||||
|
|
||||||
if ($restriction["ip"] == $this->rangeIP) {
|
$restrictionIps = [];
|
||||||
$this->assertTrue(true);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
foreach ($result as $restrictionId) {
|
||||||
* Test if delete request on /me
|
$restriction = $this->api->get('/me/accessRestriction/ip/' . $restrictionId);
|
||||||
*/
|
|
||||||
public function testDeleteRestrictionAccessIP()
|
|
||||||
{
|
|
||||||
$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')) ;
|
$restrictionIps[] = $restriction['ip'];
|
||||||
foreach ($result as $restrictionId) {
|
}
|
||||||
$restriction = $invoker->invokeArgs($api, array('GET', '/me/accessRestriction/ip/' . $restrictionId)) ;
|
|
||||||
|
|
||||||
if ($restriction["ip"] == $this->rangeIP) {
|
$this->assertContains($this->rangeIP, $restrictionIps);
|
||||||
$result = $invoker->invokeArgs($api, array('DELETE', '/me/accessRestriction/ip/'. $restrictionId)) ;
|
$this->assertContains($this->alternativeRangeIP, $restrictionIps);
|
||||||
$this->assertNull( $result );
|
}
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Test if request without authentication works
|
* Test if delete request on /me
|
||||||
*/
|
*/
|
||||||
public function testIfRequestWithoutAuthenticationWorks()
|
public function testPutRestrictionAccessIP()
|
||||||
{
|
{
|
||||||
$api = new Api($this->application_key, $this->application_secret, $this->endpoint, NULL, $this->client);
|
$result = $this->api->get('/me/accessRestriction/ip');
|
||||||
$invoker = self::getPrivateMethod('rawCall');
|
|
||||||
$result = $invoker->invokeArgs($api, array('GET', '/xdsl/incidents')) ;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
$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']);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
<?php
|
<?php
|
||||||
# Copyright (c) 2013-2014, OVH SAS.
|
# Copyright (c) 2013-2016, OVH SAS.
|
||||||
# All rights reserved.
|
# All rights reserved.
|
||||||
#
|
#
|
||||||
# Redistribution and use in source and binary forms, with or without
|
# Redistribution and use in source and binary forms, with or without
|
||||||
@ -27,140 +27,257 @@
|
|||||||
|
|
||||||
namespace Ovh\tests;
|
namespace Ovh\tests;
|
||||||
|
|
||||||
use Ovh\Api;
|
|
||||||
|
|
||||||
use GuzzleHttp\Client;
|
use GuzzleHttp\Client;
|
||||||
use GuzzleHttp\Subscriber\Mock;
|
use GuzzleHttp\Middleware;
|
||||||
|
use GuzzleHttp\Psr7\Response;
|
||||||
|
use Ovh\Api;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Test Api class
|
* Test Api class
|
||||||
*
|
*
|
||||||
* @package Ovh
|
* @package Ovh
|
||||||
* @category Ovh
|
* @category Ovh
|
||||||
* @author Vincent Cassé <vincent.casse@ovh.net>
|
|
||||||
*/
|
*/
|
||||||
class ApiTest extends \PHPUnit_Framework_TestCase {
|
class ApiTest extends \PHPUnit_Framework_TestCase
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @var Client
|
||||||
|
*/
|
||||||
|
private $client;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Define id to create object
|
* @var string
|
||||||
*/
|
*/
|
||||||
protected function setUp()
|
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_key = 'app_key';
|
||||||
$this->application_secret = 'app_secret';
|
$this->application_secret = 'app_secret';
|
||||||
$this->consumer_key = 'consumer';
|
$this->consumer_key = 'consumer';
|
||||||
$this->endpoint = 'ovh-eu';
|
$this->endpoint = 'ovh-eu';
|
||||||
|
|
||||||
$this->client = new Client();
|
$this->client = new Client();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get private and protected method to unit test it
|
* Get private and protected method to unit test it
|
||||||
|
*
|
||||||
|
* @param string $name
|
||||||
|
*
|
||||||
|
* @return \ReflectionMethod
|
||||||
*/
|
*/
|
||||||
protected static function getPrivateMethod($name)
|
protected static function getPrivateMethod($name)
|
||||||
{
|
{
|
||||||
$class = new \ReflectionClass('Ovh\Api');
|
$class = new \ReflectionClass('Ovh\Api');
|
||||||
$method = $class->getMethod($name);
|
$method = $class->getMethod($name);
|
||||||
$method->setAccessible(true);
|
$method->setAccessible(true);
|
||||||
|
|
||||||
return $method;
|
return $method;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get private and protected property to unit test it
|
||||||
|
*
|
||||||
|
* @param string $name
|
||||||
|
*
|
||||||
|
* @return \ReflectionProperty
|
||||||
|
*/
|
||||||
protected static function getPrivateProperty($name)
|
protected static function getPrivateProperty($name)
|
||||||
{
|
{
|
||||||
$class = new \ReflectionClass('Ovh\Api');
|
$class = new \ReflectionClass('Ovh\Api');
|
||||||
$property = $class->getProperty($name);
|
$property = $class->getProperty($name);
|
||||||
$property->setAccessible(true);
|
$property->setAccessible(true);
|
||||||
|
|
||||||
return $property;
|
return $property;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Test the compute of time delta
|
* Test missing $application_key
|
||||||
*/
|
*/
|
||||||
public function testTimeDeltaCompute()
|
public function testMissingApplicationKey()
|
||||||
{
|
{
|
||||||
$delay = 10;
|
$this->setExpectedException('\\Ovh\\Exceptions\\InvalidParameterException', 'Application key');
|
||||||
$mock = new Mock([
|
new Api(null, $this->application_secret, $this->endpoint, $this->consumer_key, $this->client);
|
||||||
"HTTP/1.1 200 OK\r\n\r\n". (time()-$delay),
|
}
|
||||||
]);
|
|
||||||
$this->client->getEmitter()->attach($mock);
|
|
||||||
|
|
||||||
$invoker = self::getPrivateMethod('calculateTimeDelta');
|
/**
|
||||||
$property = self::getPrivateProperty('time_delta');
|
* Test missing $application_secret
|
||||||
$api = new Api($this->application_key, $this->application_secret, $this->endpoint, $this->consumer_key, $this->client);
|
*/
|
||||||
$invoker->invokeArgs($api, array()) ;
|
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);
|
* Test missing $api_endpoint
|
||||||
$this->assertEquals($time_delta, $delay * -1 );
|
*/
|
||||||
}
|
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
|
* Test bad $api_endpoint
|
||||||
*/
|
*/
|
||||||
public function testIfConsumerKeyIsReplace()
|
public function testBadApiEndpoint()
|
||||||
{
|
{
|
||||||
$delay = 10;
|
$this->setExpectedException('\\Ovh\\Exceptions\\InvalidParameterException', 'Unknown');
|
||||||
$mock = new Mock([
|
new Api($this->application_key, $this->application_secret, 'i_am_invalid', $this->consumer_key, $this->client);
|
||||||
"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);
|
|
||||||
|
|
||||||
$property = self::getPrivateProperty('consumer_key');
|
/**
|
||||||
$api = new Api($this->application_key, $this->application_secret, $this->endpoint, $this->consumer_key, $this->client);
|
* Test creating Client if none is provided
|
||||||
$accessRules = array( json_decode(' { "method": "GET", "path": "/*" } ') );
|
*/
|
||||||
|
public function testClientCreation()
|
||||||
|
{
|
||||||
|
$api = new Api($this->application_key, $this->application_secret, $this->endpoint, $this->consumer_key);
|
||||||
|
|
||||||
$credentials = $api->requestCredentials($accessRules);
|
$this->assertInstanceOf('\\GuzzleHttp\\Client', $api->getHttpClient());
|
||||||
$consumer_key = $property->getValue($api);
|
}
|
||||||
|
|
||||||
$this->assertEquals( $consumer_key , $credentials["consumerKey"]);
|
/**
|
||||||
$this->assertNotEquals( $consumer_key, $this->consumer_key );
|
* Test the compute of time delta
|
||||||
}
|
*/
|
||||||
|
public function testTimeDeltaCompute()
|
||||||
|
{
|
||||||
|
$delay = 10;
|
||||||
|
|
||||||
/**
|
$handlerStack = $this->client->getConfig('handler');
|
||||||
* Test invalid applicationKey
|
$handlerStack->push(Middleware::mapResponse(function (Response $response) {
|
||||||
*/
|
|
||||||
public function testInvalidApplicationKey()
|
$body = $response->getBody();
|
||||||
{
|
$body->write(time() - 10);
|
||||||
$this->setExpectedException(
|
|
||||||
'\GuzzleHttp\Exception\ClientException'
|
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;
|
$handlerStack = $this->client->getConfig('handler');
|
||||||
$mock = new Mock([
|
$handlerStack->push(Middleware::mapResponse(function (Response $response) {
|
||||||
"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);
|
|
||||||
|
|
||||||
$property = self::getPrivateProperty('consumer_key');
|
$body = $response->getBody();
|
||||||
$api = new Api($this->application_key, $this->application_secret, $this->endpoint, $this->consumer_key, $this->client);
|
$body->write('{\"message\":\"Invalid application key\"}');
|
||||||
$accessRules = array( json_decode(' { "method": "GET", "path": "/*" } ') );
|
|
||||||
|
|
||||||
$credentials = $api->requestCredentials($accessRules);
|
return $response
|
||||||
$consumer_key = $property->getValue($api);
|
->withStatus(401, 'POUET')
|
||||||
|
->withHeader('Content-Type', 'application/json; charset=utf-8')
|
||||||
|
->withHeader('Content-Length', 37)
|
||||||
|
->withBody($body);
|
||||||
|
}));
|
||||||
|
|
||||||
$this->assertEquals( $consumer_key , $credentials["consumerKey"]);
|
$property = self::getPrivateProperty('consumer_key');
|
||||||
$this->assertNotEquals( $consumer_key, $this->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);
|
||||||
* Test invalid rights
|
$consumer_key = $property->getValue($api);
|
||||||
*/
|
|
||||||
public function testInvalidRight()
|
$this->assertEquals($consumer_key, $credentials["consumerKey"]);
|
||||||
{
|
$this->assertNotEquals($consumer_key, $this->consumer_key);
|
||||||
$this->setExpectedException(
|
}
|
||||||
'\GuzzleHttp\Exception\ClientException'
|
|
||||||
|
/**
|
||||||
|
* Test invalid rights
|
||||||
|
*/
|
||||||
|
public function testInvalidRight()
|
||||||
|
{
|
||||||
|
$this->setExpectedException(
|
||||||
|
'\GuzzleHttp\Exception\ClientException'
|
||||||
);
|
);
|
||||||
|
|
||||||
$delay = 10;
|
$handlerStack = $this->client->getConfig('handler');
|
||||||
$mock = new Mock([
|
$handlerStack->push(Middleware::mapResponse(function (Response $response) {
|
||||||
"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);
|
|
||||||
|
|
||||||
$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');
|
return $response
|
||||||
$invoker->invokeArgs($api, array('GET', '/me')) ;
|
->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());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user