Merge branch 'feature-crypto-endpoints' of https://github.com/exportsmedia/cloudflare-php into feature-crypto-endpoints

# Conflicts:
#	src/Endpoints/Crypto.php
#	src/Endpoints/TLS.php
#	tests/Endpoints/CryptoTest.php
This commit is contained in:
Michael Markoski
2019-05-25 10:31:07 -05:00
14 changed files with 322 additions and 41 deletions

View File

@@ -19,13 +19,13 @@ class Crypto implements API
* @param string $zoneID The ID of the zone * @param string $zoneID The ID of the zone
* @return string|false * @return string|false
*/ */
public function getSSLSetting($zoneID) public function getSSLSetting(string $zoneID)
{ {
$return = $this->adapter->get( $return = $this->adapter->get(
'zones/' . $zoneID . '/settings/ssl' 'zones/' . $zoneID . '/settings/ssl'
); );
$body = json_decode($return->getBody()); $body = json_decode($return->getBody());
if ($body->success) { if (isset($body->result)) {
return $body->result->value; return $body->result->value;
} }
return false; return false;
@@ -37,13 +37,13 @@ class Crypto implements API
* @param string $zoneID The ID of the zone * @param string $zoneID The ID of the zone
* @return array|false * @return array|false
*/ */
public function getSSLVerificationStatus($zoneID) public function getSSLVerificationStatus(string $zoneID)
{ {
$return = $this->adapter->get( $return = $this->adapter->get(
'zones/' . $zoneID . '/ssl/verification' 'zones/' . $zoneID . '/ssl/verification'
); );
$body = json_decode($return->getBody()); $body = json_decode($return->getBody());
if ($body->result) { if (isset($body->result)) {
return $body; return $body;
} }
return false; return false;
@@ -55,13 +55,13 @@ class Crypto implements API
* @param string $zoneID The ID of the zone * @param string $zoneID The ID of the zone
* @return string|false * @return string|false
*/ */
public function getOpportunisticEncryptionSetting($zoneID) public function getOpportunisticEncryptionSetting(string $zoneID)
{ {
$return = $this->adapter->get( $return = $this->adapter->get(
'zones/' . $zoneID . '/settings/opportunistic_encryption' 'zones/' . $zoneID . '/settings/opportunistic_encryption'
); );
$body = json_decode($return->getBody()); $body = json_decode($return->getBody());
if ($body->success) { if (isset($body->result)) {
return $body->result->value; return $body->result->value;
} }
return false; return false;
@@ -73,38 +73,50 @@ class Crypto implements API
* @param string $zoneID The ID of the zone * @param string $zoneID The ID of the zone
* @return string|false * @return string|false
*/ */
public function getOnionRoutingSetting($zoneID) public function getOnionRoutingSetting(string $zoneID)
{ {
$return = $this->adapter->get( $return = $this->adapter->get(
'zones/' . $zoneID . '/settings/opportunistic_onion' 'zones/' . $zoneID . '/settings/opportunistic_onion'
); );
$body = json_decode($return->getBody()); $body = json_decode($return->getBody());
if ($body->success) { if (isset($body->result)) {
return $body->result; return $body->result;
} }
return false; return false;
} }
public function getHTTPSRedirectSetting($zoneID) /**
* Get the HTTPS Redirect setting for the zone
*
* @param string $zoneID The ID of the zone
* @return string|false
*/
public function getHTTPSRedirectSetting(string $zoneID)
{ {
$return = $this->adapter->get( $return = $this->adapter->get(
'zones/' . $zoneID . '/settings/always_use_https' 'zones/' . $zoneID . '/settings/always_use_https'
); );
$body = json_decode($return->getBody()); $body = json_decode($return->getBody());
if ($body->success) { if (isset($body->result)) {
return $body->result->value; return $body->result;
} }
return false; return false;
} }
public function getHTTPSRewritesSetting($zoneID) /**
* Get the HTTPS Rewrite setting for the zone
*
* @param string $zoneID The ID of the zone
* @return string|false
*/
public function getHTTPSRewritesSetting(string $zoneID)
{ {
$return = $this->adapter->get( $return = $this->adapter->get(
'zones/' . $zoneID . '/settings/automatic_https_rewrites' 'zones/' . $zoneID . '/settings/automatic_https_rewrites'
); );
$body = json_decode($return->getBody()); $body = json_decode($return->getBody());
if ($body->success) { if (isset($body->result)) {
return $body->result->value; return $body->result;
} }
return false; return false;
} }
@@ -116,7 +128,7 @@ class Crypto implements API
* @param string $value The value of the zone setting * @param string $value The value of the zone setting
* @return bool * @return bool
*/ */
public function updateSSLSetting($zoneID, $value) public function updateSSLSetting(string $zoneID, string $value)
{ {
$return = $this->adapter->patch( $return = $this->adapter->patch(
'zones/' . $zoneID . '/settings/ssl', 'zones/' . $zoneID . '/settings/ssl',
@@ -125,7 +137,7 @@ class Crypto implements API
] ]
); );
$body = json_decode($return->getBody()); $body = json_decode($return->getBody());
if ($body->success) { if (isset($body->success) && $body->success == true) {
return true; return true;
} }
return false; return false;
@@ -138,7 +150,7 @@ class Crypto implements API
* @param string $value The value of the zone setting * @param string $value The value of the zone setting
* @return bool * @return bool
*/ */
public function updateHTTPSRedirectSetting($zoneID, $value) public function updateHTTPSRedirectSetting(string $zoneID, string $value)
{ {
$return = $this->adapter->patch( $return = $this->adapter->patch(
'zones/' . $zoneID . '/settings/always_use_https', 'zones/' . $zoneID . '/settings/always_use_https',
@@ -147,7 +159,7 @@ class Crypto implements API
] ]
); );
$body = json_decode($return->getBody()); $body = json_decode($return->getBody());
if ($body->success) { if (isset($body->success) && $body->success == true) {
return true; return true;
} }
return false; return false;
@@ -160,7 +172,7 @@ class Crypto implements API
* @param string $value The value of the zone setting * @param string $value The value of the zone setting
* @return bool * @return bool
*/ */
public function updateHTTPSRewritesSetting($zoneID, $value) public function updateHTTPSRewritesSetting(string $zoneID, string $value)
{ {
$return = $this->adapter->patch( $return = $this->adapter->patch(
'zones/' . $zoneID . '/settings/automatic_https_rewrites', 'zones/' . $zoneID . '/settings/automatic_https_rewrites',
@@ -169,7 +181,7 @@ class Crypto implements API
] ]
); );
$body = json_decode($return->getBody()); $body = json_decode($return->getBody());
if ($body->success) { if (isset($body->success) && $body->success == true) {
return true; return true;
} }
return false; return false;
@@ -182,7 +194,7 @@ class Crypto implements API
* @param string $value The value of the zone setting * @param string $value The value of the zone setting
* @return bool * @return bool
*/ */
public function updateOpportunisticEncryptionSetting($zoneID, $value) public function updateOpportunisticEncryptionSetting(string $zoneID, string $value)
{ {
$return = $this->adapter->patch( $return = $this->adapter->patch(
'zones/' . $zoneID . '/settings/opportunistic_encryption', 'zones/' . $zoneID . '/settings/opportunistic_encryption',
@@ -191,7 +203,7 @@ class Crypto implements API
] ]
); );
$body = json_decode($return->getBody()); $body = json_decode($return->getBody());
if ($body->success) { if (isset($body->success) && $body->success == true) {
return true; return true;
} }
return false; return false;
@@ -204,7 +216,7 @@ class Crypto implements API
* @param string $value The value of the zone setting * @param string $value The value of the zone setting
* @return bool * @return bool
*/ */
public function updateOnionRoutingSetting($zoneID, $value) public function updateOnionRoutingSetting(string $zoneID, string $value)
{ {
$return = $this->adapter->patch( $return = $this->adapter->patch(
'zones/' . $zoneID . '/settings/opportunistic_onion', 'zones/' . $zoneID . '/settings/opportunistic_onion',
@@ -213,7 +225,7 @@ class Crypto implements API
] ]
); );
$body = json_decode($return->getBody()); $body = json_decode($return->getBody());
if ($body->success) { if (isset($body->success) && $body->success == true) {
return true; return true;
} }
return false; return false;

View File

@@ -31,7 +31,7 @@ class TLS implements API
'zones/' . $zoneID . '/settings/tls_client_auth' 'zones/' . $zoneID . '/settings/tls_client_auth'
); );
$body = json_decode($return->getBody()); $body = json_decode($return->getBody());
if ($body->success) { if (isset($body->result)) {
return $body->result->value; return $body->result->value;
} }
return false; return false;
@@ -50,7 +50,7 @@ class TLS implements API
['value' => 'on'] ['value' => 'on']
); );
$body = json_decode($return->getBody()); $body = json_decode($return->getBody());
if ($body->success) { if (isset($body->success) && $body->success == true) {
return true; return true;
} }
return false; return false;
@@ -69,7 +69,7 @@ class TLS implements API
['value' => 'off'] ['value' => 'off']
); );
$body = json_decode($return->getBody()); $body = json_decode($return->getBody());
if ($body->success) { if (isset($body->success) && $body->success == true) {
return true; return true;
} }
return false; return false;
@@ -91,7 +91,7 @@ class TLS implements API
] ]
); );
$body = json_decode($return->getBody()); $body = json_decode($return->getBody());
if ($body->success) { if (isset($body->success) && $body->success == true) {
return true; return true;
} }
return false; return false;
@@ -113,7 +113,7 @@ class TLS implements API
] ]
); );
$body = json_decode($return->getBody()); $body = json_decode($return->getBody());
if ($body->success) { if (isset($body->success) && $body->success == true) {
return true; return true;
} }
return false; return false;

View File

@@ -15,8 +15,8 @@ class CryptoTest extends TestCase
$this->equalTo('zones/c2547eb745079dac9320b638f5e225cf483cc5cfdda41/settings/ssl') $this->equalTo('zones/c2547eb745079dac9320b638f5e225cf483cc5cfdda41/settings/ssl')
); );
$zoneSSLSetting = new \Cloudflare\API\Endpoints\Crypto($mock); $cryptoMock = new \Cloudflare\API\Endpoints\Crypto($mock);
$result = $zoneSSLSetting->getSSLSetting('c2547eb745079dac9320b638f5e225cf483cc5cfdda41'); $result = $cryptoMock->getSSLSetting('c2547eb745079dac9320b638f5e225cf483cc5cfdda41');
$this->assertEquals('off', $result); $this->assertEquals('off', $result);
} }
@@ -34,17 +34,88 @@ class CryptoTest extends TestCase
$this->equalTo('zones/c2547eb745079dac9320b638f5e225cf483cc5cfdda41/ssl/verification') $this->equalTo('zones/c2547eb745079dac9320b638f5e225cf483cc5cfdda41/ssl/verification')
); );
$zoneVerificationStatus = new \Cloudflare\API\Endpoints\Crypto($mock); $cryptoMock = new \Cloudflare\API\Endpoints\Crypto($mock);
$result = $zoneVerificationStatus->getSSLVerificationStatus('c2547eb745079dac9320b638f5e225cf483cc5cfdda41'); $result = $cryptoMock->getSSLVerificationStatus('c2547eb745079dac9320b638f5e225cf483cc5cfdda41');
$this->assertObjectHasAttribute('result', $result); $this->assertObjectHasAttribute('result', $result);
$this->assertEquals('active', $result->result{0}->certificate_status); $this->assertEquals('active', $result->result{0}->certificate_status);
} }
public function testGetOpportunisticEncryptionSetting()
{
$response = $this->getPsr7JsonResponseForFixture('Endpoints/getOpportunisticEncryptionSetting.json');
$mock = $this->getMockBuilder(\Cloudflare\API\Adapter\Adapter::class)->getMock();
$mock->method('get')->willReturn($response);
$mock->expects($this->once())
->method('get')
->with(
$this->equalTo('zones/c2547eb745079dac9320b638f5e225cf483cc5cfdda41/settings/opportunistic_encryption')
);
$cryptoMock = new \Cloudflare\API\Endpoints\Crypto($mock);
$result = $cryptoMock->getOpportunisticEncryptionSetting('c2547eb745079dac9320b638f5e225cf483cc5cfdda41');
$this->assertEquals('off', $result);
}
public function testGetOnionRoutingSetting()
{
$response = $this->getPsr7JsonResponseForFixture('Endpoints/getOnionRoutingSetting.json');
$mock = $this->getMockBuilder(\Cloudflare\API\Adapter\Adapter::class)->getMock();
$mock->method('get')->willReturn($response);
$mock->expects($this->once())
->method('get')
->with(
$this->equalTo('zones/c2547eb745079dac9320b638f5e225cf483cc5cfdda41/settings/opportunistic_onion')
);
$cryptoMock = new \Cloudflare\API\Endpoints\Crypto($mock);
$result = $cryptoMock->getOnionRoutingSetting('c2547eb745079dac9320b638f5e225cf483cc5cfdda41');
$this->assertEquals('off', $result);
}
public function testGetHTTPSRedirectSetting()
{
$response = $this->getPsr7JsonResponseForFixture('Endpoints/getHTTPSRedirectSetting.json');
$mock = $this->getMockBuilder(\Cloudflare\API\Adapter\Adapter::class)->getMock();
$mock->method('get')->willReturn($response);
$mock->expects($this->once())
->method('get')
->with(
$this->equalTo('zones/c2547eb745079dac9320b638f5e225cf483cc5cfdda41/settings/always_use_https')
);
$cryptoMock = new \Cloudflare\API\Endpoints\Crypto($mock);
$result = $cryptoMock->getHTTPSRedirectSetting('c2547eb745079dac9320b638f5e225cf483cc5cfdda41');
$this->assertEquals('off', $result);
}
public function testGetHTTPSRewritesSetting()
{
$response = $this->getPsr7JsonResponseForFixture('Endpoints/getHTTPSRewritesSetting.json');
$mock = $this->getMockBuilder(\Cloudflare\API\Adapter\Adapter::class)->getMock();
$mock->method('get')->willReturn($response);
$mock->expects($this->once())
->method('get')
->with(
$this->equalTo('zones/c2547eb745079dac9320b638f5e225cf483cc5cfdda41/settings/automatic_https_rewrites')
);
$cryptoMock = new \Cloudflare\API\Endpoints\Crypto($mock);
$result = $cryptoMock->getHTTPSRewritesSetting('c2547eb745079dac9320b638f5e225cf483cc5cfdda41');
$this->assertEquals('off', $result);
}
public function testUpdateSSLSetting() public function testUpdateSSLSetting()
{ {
@@ -60,11 +131,90 @@ class CryptoTest extends TestCase
$this->equalTo(['value' => 'full']) $this->equalTo(['value' => 'full'])
); );
$zoneSSLSetting = new \Cloudflare\API\Endpoints\Crypto($mock); $cryptoMock = new \Cloudflare\API\Endpoints\Crypto($mock);
$result = $zoneSSLSetting->updateSSLSetting('c2547eb745079dac9320b638f5e225cf483cc5cfdda41', 'full'); $result = $cryptoMock->updateSSLSetting('c2547eb745079dac9320b638f5e225cf483cc5cfdda41', 'full');
$this->assertTrue($result); $this->assertTrue($result);
} }
public function testUpdateHTTPSRedirectSetting()
{
$response = $this->getPsr7JsonResponseForFixture('Endpoints/updateHTTPSRedirectSetting.json');
$mock = $this->getMockBuilder(\Cloudflare\API\Adapter\Adapter::class)->getMock();
$mock->method('patch')->willReturn($response);
$mock->expects($this->once())
->method('patch')
->with(
$this->equalTo('zones/c2547eb745079dac9320b638f5e225cf483cc5cfdda41/settings/always_use_https'),
$this->equalTo(['value' => 'off'])
);
$cryptoMock = new \Cloudflare\API\Endpoints\Crypto($mock);
$result = $cryptoMock->updateHTTPSRedirectSetting('c2547eb745079dac9320b638f5e225cf483cc5cfdda41', 'off');
$this->assertTrue($result);
}
public function testUpdateHTTPSRewritesSetting()
{
$response = $this->getPsr7JsonResponseForFixture('Endpoints/updateHTTPSRewritesSetting.json');
$mock = $this->getMockBuilder(\Cloudflare\API\Adapter\Adapter::class)->getMock();
$mock->method('patch')->willReturn($response);
$mock->expects($this->once())
->method('patch')
->with(
$this->equalTo('zones/c2547eb745079dac9320b638f5e225cf483cc5cfdda41/settings/automatic_https_rewrites'),
$this->equalTo(['value' => 'off'])
);
$cryptoMock = new \Cloudflare\API\Endpoints\Crypto($mock);
$result = $cryptoMock->updateHTTPSRewritesSetting('c2547eb745079dac9320b638f5e225cf483cc5cfdda41', 'off');
$this->assertTrue($result);
}
public function testUpdateOpportunisticEncryptionSetting()
{
$response = $this->getPsr7JsonResponseForFixture('Endpoints/updateOpportunisticEncryptionSetting.json');
$mock = $this->getMockBuilder(\Cloudflare\API\Adapter\Adapter::class)->getMock();
$mock->method('patch')->willReturn($response);
$mock->expects($this->once())
->method('patch')
->with(
$this->equalTo('zones/c2547eb745079dac9320b638f5e225cf483cc5cfdda41/settings/opportunistic_encryption'),
$this->equalTo(['value' => 'off'])
);
$cryptoMock = new \Cloudflare\API\Endpoints\Crypto($mock);
$result = $cryptoMock->updateOpportunisticEncryptionSetting('c2547eb745079dac9320b638f5e225cf483cc5cfdda41', 'off');
$this->assertTrue($result);
}
public function testUpdateOnionRoutingSetting()
{
$response = $this->getPsr7JsonResponseForFixture('Endpoints/updateOnionRoutingSetting.json');
$mock = $this->getMockBuilder(\Cloudflare\API\Adapter\Adapter::class)->getMock();
$mock->method('patch')->willReturn($response);
$mock->expects($this->once())
->method('patch')
->with(
$this->equalTo('zones/c2547eb745079dac9320b638f5e225cf483cc5cfdda41/settings/opportunistic_onion'),
$this->equalTo(['value' => 'off'])
);
$cryptoMock = new \Cloudflare\API\Endpoints\Crypto($mock);
$result = $cryptoMock->updateOnionRoutingSetting('c2547eb745079dac9320b638f5e225cf483cc5cfdda41', 'off');
$this->assertTrue($result);
}
} }

View File

@@ -9,6 +9,25 @@
class TLSTest extends TestCase class TLSTest extends TestCase
{ {
public function testGetTLSClientAuth()
{
$response = $this->getPsr7JsonResponseForFixture('Endpoints/getTLSClientAuth.json');
$mock = $this->getMockBuilder(\Cloudflare\API\Adapter\Adapter::class)->getMock();
$mock->method('get')->willReturn($response);
$mock->expects($this->once())
->method('get')
->with(
$this->equalTo('zones/c2547eb745079dac9320b638f5e225cf483cc5cfdda41/settings/tls_client_auth')
);
$tlsMock = new \Cloudflare\API\Endpoints\TLS($mock);
$result = $tlsMock->getTLSClientAuth('c2547eb745079dac9320b638f5e225cf483cc5cfdda41');
$this->assertEquals('off', $result);
}
public function testEnableTLS13() public function testEnableTLS13()
{ {
$response = $this->getPsr7JsonResponseForFixture('Endpoints/enableTLS13.json'); $response = $this->getPsr7JsonResponseForFixture('Endpoints/enableTLS13.json');
@@ -23,8 +42,8 @@ class TLSTest extends TestCase
$this->equalTo(['value' => 'on']) $this->equalTo(['value' => 'on'])
); );
$zoneTLSSettings = new \Cloudflare\API\Endpoints\TLS($mock); $tlsMock = new \Cloudflare\API\Endpoints\TLS($mock);
$result = $zoneTLSSettings->enableTLS13('c2547eb745079dac9320b638f5e225cf483cc5cfdda41', true); $result = $tlsMock->enableTLS13('c2547eb745079dac9320b638f5e225cf483cc5cfdda41', true);
$this->assertTrue($result); $this->assertTrue($result);
} }
@@ -43,8 +62,8 @@ class TLSTest extends TestCase
$this->equalTo(['value' => 'off']) $this->equalTo(['value' => 'off'])
); );
$zoneTLSSettings = new \Cloudflare\API\Endpoints\TLS($mock); $tlsMock = new \Cloudflare\API\Endpoints\TLS($mock);
$result = $zoneTLSSettings->disableTLS13('c2547eb745079dac9320b638f5e225cf483cc5cfdda41', true); $result = $tlsMock->disableTLS13('c2547eb745079dac9320b638f5e225cf483cc5cfdda41', true);
$this->assertTrue($result); $this->assertTrue($result);
} }
@@ -63,8 +82,28 @@ class TLSTest extends TestCase
$this->equalTo(['value' => '1.1']) $this->equalTo(['value' => '1.1'])
); );
$zoneTLSSettings = new \Cloudflare\API\Endpoints\TLS($mock); $tlsMock = new \Cloudflare\API\Endpoints\TLS($mock);
$result = $zoneTLSSettings->changeMinimumTLSVersion('c2547eb745079dac9320b638f5e225cf483cc5cfdda41', '1.1'); $result = $tlsMock->changeMinimumTLSVersion('c2547eb745079dac9320b638f5e225cf483cc5cfdda41', '1.1');
$this->assertTrue($result);
}
public function testUpdateTLSClientAuth()
{
$response = $this->getPsr7JsonResponseForFixture('Endpoints/updateTLSClientAuth.json');
$mock = $this->getMockBuilder(\Cloudflare\API\Adapter\Adapter::class)->getMock();
$mock->method('patch')->willReturn($response);
$mock->expects($this->once())
->method('patch')
->with(
$this->equalTo('zones/c2547eb745079dac9320b638f5e225cf483cc5cfdda41/settings/tls_client_auth'),
$this->equalTo(['value' => 'off'])
);
$tlsMock = new \Cloudflare\API\Endpoints\TLS($mock);
$result = $tlsMock->updateTLSClientAuth('c2547eb745079dac9320b638f5e225cf483cc5cfdda41', 'off');
$this->assertTrue($result); $this->assertTrue($result);
} }

View File

@@ -0,0 +1,6 @@
{
"success": true,
"errors": [],
"messages": [],
"result": "off"
}

View File

@@ -0,0 +1,6 @@
{
"success": true,
"errors": [],
"messages": [],
"result": "off"
}

View File

@@ -0,0 +1,6 @@
{
"success": true,
"errors": [],
"messages": [],
"result": "off"
}

View File

@@ -0,0 +1,11 @@
{
"success": true,
"errors": [],
"messages": [],
"result": {
"id": "opportunistic_encryption",
"value": "off",
"editable": true,
"modified_on": "2014-01-01T05:20:00.12345Z"
}
}

View File

@@ -0,0 +1,11 @@
{
"success": true,
"errors": [],
"messages": [],
"result": {
"id": "tls_client_auth",
"value": "off",
"editable": true,
"modified_on": "2014-01-01T05:20:00.12345Z"
}
}

View File

@@ -0,0 +1,6 @@
{
"success": true,
"errors": [],
"messages": [],
"result": "off"
}

View File

@@ -0,0 +1,6 @@
{
"success": true,
"errors": [],
"messages": [],
"result": "off"
}

View File

@@ -0,0 +1,6 @@
{
"success": true,
"errors": [],
"messages": [],
"result": "off"
}

View File

@@ -0,0 +1,11 @@
{
"success": true,
"errors": [],
"messages": [],
"result": {
"id": "opportunistic_encryption",
"value": "on",
"editable": true,
"modified_on": "2014-01-01T05:20:00.12345Z"
}
}

View File

@@ -0,0 +1,11 @@
{
"success": true,
"errors": [],
"messages": [],
"result": {
"id": "tls_client_auth",
"value": "off",
"editable": true,
"modified_on": "2014-01-01T05:20:00.12345Z"
}
}