From 598ba5c1d1b586d6c06bd5d9172db5811ca427e8 Mon Sep 17 00:00:00 2001 From: Michael Date: Sat, 25 May 2019 22:53:56 -0500 Subject: [PATCH 1/3] add firewall settings endpoints, tests --- src/Endpoints/FirewallSettings.php | 135 ++++++++++++++++++ tests/Endpoints/FirewallSettingsTest.php | 121 ++++++++++++++++ tests/Endpoints/UARulesTest.php | 8 +- .../getBrowserIntegrityCheckSetting.json | 11 ++ .../Endpoints/getChallengeTTLSetting.json | 11 ++ .../Endpoints/getSecurityLevelSetting.json | 11 ++ .../updateBrowserIntegrityCheckSetting.json | 11 ++ .../Endpoints/updateChallengeTTLSetting.json | 11 ++ .../Endpoints/updateSecurityLevelSetting.json | 11 ++ 9 files changed, 326 insertions(+), 4 deletions(-) create mode 100644 src/Endpoints/FirewallSettings.php create mode 100644 tests/Endpoints/FirewallSettingsTest.php create mode 100644 tests/Fixtures/Endpoints/getBrowserIntegrityCheckSetting.json create mode 100644 tests/Fixtures/Endpoints/getChallengeTTLSetting.json create mode 100644 tests/Fixtures/Endpoints/getSecurityLevelSetting.json create mode 100644 tests/Fixtures/Endpoints/updateBrowserIntegrityCheckSetting.json create mode 100644 tests/Fixtures/Endpoints/updateChallengeTTLSetting.json create mode 100644 tests/Fixtures/Endpoints/updateSecurityLevelSetting.json diff --git a/src/Endpoints/FirewallSettings.php b/src/Endpoints/FirewallSettings.php new file mode 100644 index 0000000..5bd9735 --- /dev/null +++ b/src/Endpoints/FirewallSettings.php @@ -0,0 +1,135 @@ +adapter = $adapter; + } + + /** + * Get the Security Level feature for a zone. + * + * @param string $zoneID The ID of the zone + * @return string|false + */ + public function getSecurityLevelSetting(string $zoneID) + { + $return = $this->adapter->get( + 'zones/' . $zoneID . '/settings/security_level' + ); + $body = json_decode($return->getBody()); + if (isset($body->result)) { + return $body->result->value; + } + return false; + } + + /** + * Get the Challenge TTL feature for a zone. + * + * @param string $zoneID The ID of the zone + * @return integer|false + */ + public function getChallengeTTLSetting(string $zoneID) + { + $return = $this->adapter->get( + 'zones/' . $zoneID . '/settings/challenge_ttl' + ); + $body = json_decode($return->getBody()); + if (isset($body->result)) { + return $body->result->value; + } + return false; + } + + /** + * Get the Browser Integrity Check feature for a zone. + * + * @param string $zoneID The ID of the zone + * @return string|false + */ + public function getBrowserIntegrityCheckSetting(string $zoneID) + { + $return = $this->adapter->get( + 'zones/' . $zoneID . '/settings/browser_check' + ); + $body = json_decode($return->getBody()); + if (isset($body->result)) { + return $body->result->value; + } + return false; + } + + /** + * Update the Security Level setting for the zone + * + * @param string $zoneID The ID of the zone + * @param string $value The value of the zone setting + * @return bool + */ + public function updateSecurityLevelSetting(string $zoneID, string $value) + { + $return = $this->adapter->patch( + 'zones/' . $zoneID . '/settings/security_level', + [ + 'value' => $value, + ] + ); + $body = json_decode($return->getBody()); + if (isset($body->success) && $body->success == true) { + return true; + } + return false; + } + + /** + * Update the Challenge TTL setting for the zone + * + * @param string $zoneID The ID of the zone + * @param integer $value The value of the zone setting + * @return bool + */ + public function updateChallengeTTLSetting(string $zoneID, integer $value) + { + $return = $this->adapter->patch( + 'zones/' . $zoneID . '/settings/challenge_ttl', + [ + 'value' => $value, + ] + ); + $body = json_decode($return->getBody()); + if (isset($body->success) && $body->success == true) { + return true; + } + return false; + } + + /** + * Update the Browser Integrity Check setting for the zone + * + * @param string $zoneID The ID of the zone + * @param string $value The value of the zone setting + * @return bool + */ + public function updateBrowserIntegrityCheckSetting(string $zoneID, string $value) + { + $return = $this->adapter->patch( + 'zones/' . $zoneID . '/settings/browser_check', + [ + 'value' => $value, + ] + ); + $body = json_decode($return->getBody()); + if (isset($body->success) && $body->success == true) { + return true; + } + return false; + } +} diff --git a/tests/Endpoints/FirewallSettingsTest.php b/tests/Endpoints/FirewallSettingsTest.php new file mode 100644 index 0000000..fc99562 --- /dev/null +++ b/tests/Endpoints/FirewallSettingsTest.php @@ -0,0 +1,121 @@ +getPsr7JsonResponseForFixture('Endpoints/getSecurityLevelSetting.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/security_level') + ); + + $firewallSettingsMock = new \Cloudflare\API\Endpoints\FirewallSettings($mock); + $result = $firewallSettingsMock->getSecurityLevelSetting('c2547eb745079dac9320b638f5e225cf483cc5cfdda41'); + + $this->assertEquals('medium', $result); + } + + public function testGetChallengeTTLSetting() + { + $response = $this->getPsr7JsonResponseForFixture('Endpoints/getChallengeTTLSetting.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/challenge_ttl') + ); + + $firewallSettingsMock = new \Cloudflare\API\Endpoints\FirewallSettings($mock); + $result = $firewallSettingsMock->getChallengeTTLSetting('c2547eb745079dac9320b638f5e225cf483cc5cfdda41'); + + $this->assertEquals(1800, $result); + } + + public function testGetBrowserIntegrityCheckSetting() + { + $response = $this->getPsr7JsonResponseForFixture('Endpoints/getBrowserIntegrityCheckSetting.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/browser_check') + ); + + $firewallSettingsMock = new \Cloudflare\API\Endpoints\FirewallSettings($mock); + $result = $firewallSettingsMock->getBrowserIntegrityCheckSetting('c2547eb745079dac9320b638f5e225cf483cc5cfdda41'); + + $this->assertEquals('on', $result); + } + + public function testUpdateSecurityLevelSetting() + { + $response = $this->getPsr7JsonResponseForFixture('Endpoints/updateSecurityLevelSetting.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' => 'medium']) + ); + + $firewallSettingsMock = new \Cloudflare\API\Endpoints\FirewallSettings($mock); + $result = $firewallSettingsMock->updateSecurityLevelSetting('c2547eb745079dac9320b638f5e225cf483cc5cfdda41', 'medium'); + + $this->assertTrue($result); + } + + public function testUpdateChallengeTTLSetting() + { + $response = $this->getPsr7JsonResponseForFixture('Endpoints/updateChallengeTTLSetting.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/challenge_ttl'), + $this->equalTo(['value' => 1800]) + ); + + $firewallSettingsMock = new \Cloudflare\API\Endpoints\FirewallSettings($mock); + $result = $firewallSettingsMock->updateChallengeTTLSetting('c2547eb745079dac9320b638f5e225cf483cc5cfdda41', 1800); + + $this->assertTrue($result); + } + + public function testUpdateBrowserIntegrityCheckSetting() + { + $response = $this->getPsr7JsonResponseForFixture('Endpoints/updateBrowserIntegrityCheckSetting.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/browser_check'), + $this->equalTo(['value' => 'on']) + ); + + $firewallSettingsMock = new \Cloudflare\API\Endpoints\FirewallSettings($mock); + $result = $firewallSettingsMock->updateBrowserIntegrityCheckSetting('c2547eb745079dac9320b638f5e225cf483cc5cfdda41', 'on'); + + $this->assertTrue($result); + } +} diff --git a/tests/Endpoints/UARulesTest.php b/tests/Endpoints/UARulesTest.php index fec94b5..8406745 100644 --- a/tests/Endpoints/UARulesTest.php +++ b/tests/Endpoints/UARulesTest.php @@ -19,10 +19,10 @@ class UARulesTest extends TestCase ->method('get') ->with( $this->equalTo('zones/023e105f4ecef8ad9ca31a8372d0c353/firewall/ua_rules'), - $this->equalTo([ - 'page' => 1, - 'per_page' => 20 - ]) + $this->equalTo([ + 'page' => 1, + 'per_page' => 20 + ]) ); $zones = new \Cloudflare\API\Endpoints\UARules($mock); diff --git a/tests/Fixtures/Endpoints/getBrowserIntegrityCheckSetting.json b/tests/Fixtures/Endpoints/getBrowserIntegrityCheckSetting.json new file mode 100644 index 0000000..cdfe4ee --- /dev/null +++ b/tests/Fixtures/Endpoints/getBrowserIntegrityCheckSetting.json @@ -0,0 +1,11 @@ +{ + "success": true, + "errors": [], + "messages": [], + "result": { + "id": "browser_check", + "value": "on", + "editable": true, + "modified_on": "2014-01-01T05:20:00.12345Z" + } +} \ No newline at end of file diff --git a/tests/Fixtures/Endpoints/getChallengeTTLSetting.json b/tests/Fixtures/Endpoints/getChallengeTTLSetting.json new file mode 100644 index 0000000..ee6edf1 --- /dev/null +++ b/tests/Fixtures/Endpoints/getChallengeTTLSetting.json @@ -0,0 +1,11 @@ +{ + "success": true, + "errors": [], + "messages": [], + "result": { + "id": "challenge_ttl", + "value": 1800, + "editable": true, + "modified_on": "2014-01-01T05:20:00.12345Z" + } +} \ No newline at end of file diff --git a/tests/Fixtures/Endpoints/getSecurityLevelSetting.json b/tests/Fixtures/Endpoints/getSecurityLevelSetting.json new file mode 100644 index 0000000..bbbf241 --- /dev/null +++ b/tests/Fixtures/Endpoints/getSecurityLevelSetting.json @@ -0,0 +1,11 @@ +{ + "success": true, + "errors": [], + "messages": [], + "result": { + "id": "security_level", + "value": "medium", + "editable": true, + "modified_on": "2014-01-01T05:20:00.12345Z" + } +} \ No newline at end of file diff --git a/tests/Fixtures/Endpoints/updateBrowserIntegrityCheckSetting.json b/tests/Fixtures/Endpoints/updateBrowserIntegrityCheckSetting.json new file mode 100644 index 0000000..cdfe4ee --- /dev/null +++ b/tests/Fixtures/Endpoints/updateBrowserIntegrityCheckSetting.json @@ -0,0 +1,11 @@ +{ + "success": true, + "errors": [], + "messages": [], + "result": { + "id": "browser_check", + "value": "on", + "editable": true, + "modified_on": "2014-01-01T05:20:00.12345Z" + } +} \ No newline at end of file diff --git a/tests/Fixtures/Endpoints/updateChallengeTTLSetting.json b/tests/Fixtures/Endpoints/updateChallengeTTLSetting.json new file mode 100644 index 0000000..ee6edf1 --- /dev/null +++ b/tests/Fixtures/Endpoints/updateChallengeTTLSetting.json @@ -0,0 +1,11 @@ +{ + "success": true, + "errors": [], + "messages": [], + "result": { + "id": "challenge_ttl", + "value": 1800, + "editable": true, + "modified_on": "2014-01-01T05:20:00.12345Z" + } +} \ No newline at end of file diff --git a/tests/Fixtures/Endpoints/updateSecurityLevelSetting.json b/tests/Fixtures/Endpoints/updateSecurityLevelSetting.json new file mode 100644 index 0000000..bbbf241 --- /dev/null +++ b/tests/Fixtures/Endpoints/updateSecurityLevelSetting.json @@ -0,0 +1,11 @@ +{ + "success": true, + "errors": [], + "messages": [], + "result": { + "id": "security_level", + "value": "medium", + "editable": true, + "modified_on": "2014-01-01T05:20:00.12345Z" + } +} \ No newline at end of file From bd10d7f833ae2e902913bfb0a9f547b255c191d6 Mon Sep 17 00:00:00 2001 From: Michael Date: Sat, 25 May 2019 22:58:10 -0500 Subject: [PATCH 2/3] fix security endpoint --- tests/Endpoints/FirewallSettingsTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Endpoints/FirewallSettingsTest.php b/tests/Endpoints/FirewallSettingsTest.php index fc99562..b8bc289 100644 --- a/tests/Endpoints/FirewallSettingsTest.php +++ b/tests/Endpoints/FirewallSettingsTest.php @@ -69,7 +69,7 @@ class FirewallSettingsTest extends TestCase $mock->expects($this->once()) ->method('patch') ->with( - $this->equalTo('zones/c2547eb745079dac9320b638f5e225cf483cc5cfdda41/settings/opportunistic_encryption'), + $this->equalTo('zones/c2547eb745079dac9320b638f5e225cf483cc5cfdda41/settings/security_level'), $this->equalTo(['value' => 'medium']) ); From 7343b7cc461d93141eba7b46c4844601cce0e538 Mon Sep 17 00:00:00 2001 From: Michael Date: Sat, 25 May 2019 23:02:40 -0500 Subject: [PATCH 3/3] change param type --- src/Endpoints/FirewallSettings.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Endpoints/FirewallSettings.php b/src/Endpoints/FirewallSettings.php index 5bd9735..36e49fd 100644 --- a/src/Endpoints/FirewallSettings.php +++ b/src/Endpoints/FirewallSettings.php @@ -93,10 +93,10 @@ class FirewallSettings implements API * Update the Challenge TTL setting for the zone * * @param string $zoneID The ID of the zone - * @param integer $value The value of the zone setting + * @param int $value The value of the zone setting * @return bool */ - public function updateChallengeTTLSetting(string $zoneID, integer $value) + public function updateChallengeTTLSetting(string $zoneID, int $value) { $return = $this->adapter->patch( 'zones/' . $zoneID . '/settings/challenge_ttl',