From 13d9794857dc9d1eb8cea916f8c4297fda4cd3e3 Mon Sep 17 00:00:00 2001 From: Thomas Alrek Date: Wed, 12 Jun 2019 12:39:33 +0200 Subject: [PATCH] Implements test suite for CaptchaV2 --- phpunit.xml | 5 ++ src/Netflex/Site/Support/CaptchaV2.php | 13 ++-- .../CaptchaV2/CaptchaV2.checkBox.test.php | 33 ++++++++++ .../CaptchaV2/CaptchaV2.isValid.test.php | 60 +++++++++++++++++++ .../CaptchaV2/CaptchaV2.scriptTag.test.php | 27 +++++++++ ...2_CheckBoxTest__testMatchesSnapshot__1.txt | 1 + ...ScriptTagTest__testMatchesSnapshots__1.txt | 0 ...ScriptTagTest__testMatchesSnapshots__2.txt | 1 + 8 files changed, 134 insertions(+), 6 deletions(-) create mode 100644 tests/TestSuites/CaptchaV2/CaptchaV2.checkBox.test.php create mode 100644 tests/TestSuites/CaptchaV2/CaptchaV2.isValid.test.php create mode 100644 tests/TestSuites/CaptchaV2/CaptchaV2.scriptTag.test.php create mode 100644 tests/TestSuites/CaptchaV2/__snapshots__/CaptchaV2_CheckBoxTest__testMatchesSnapshot__1.txt create mode 100644 tests/TestSuites/CaptchaV2/__snapshots__/CaptchaV2_ScriptTagTest__testMatchesSnapshots__1.txt create mode 100644 tests/TestSuites/CaptchaV2/__snapshots__/CaptchaV2_ScriptTagTest__testMatchesSnapshots__2.txt diff --git a/phpunit.xml b/phpunit.xml index db1f660..9ddd900 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -15,6 +15,11 @@ verbose="true" > + + + tests/TestSuites/CaptchaV2 + + tests/TestSuites/Common diff --git a/src/Netflex/Site/Support/CaptchaV2.php b/src/Netflex/Site/Support/CaptchaV2.php index fc965b5..a37d396 100644 --- a/src/Netflex/Site/Support/CaptchaV2.php +++ b/src/Netflex/Site/Support/CaptchaV2.php @@ -2,6 +2,7 @@ namespace Netflex\Site\Support; +use NF; use Netflex\Site\Support\ResponseMissingException; use Netflex\Site\Support\GoogleResponseException; use Netflex\Site\Support\ConfigurationMissingException; @@ -22,10 +23,10 @@ private static function getCredentials() { $siteKey = get_setting('captcha_site_key'); if ($siteKey === null) - throw new ConfigurationMissingException("captcha_site_key setting is missing"); + throw new ConfigurationMissingException('captcha_site_key setting is missing'); $siteSecret = get_setting('captcha_site_secret'); if ($siteSecret === null) - throw new ConfigurationMissingException("captcha_site_key setting is missing"); + throw new ConfigurationMissingException('captcha_site_key setting is missing'); return (object)[ 'siteKey' => $siteKey, @@ -37,7 +38,7 @@ private static function getCredentials() * Returns the script tag that has to be included for the captcha to work * @return string Script tag */ - public static function scriptTag($override=NULL) + public static function scriptTag($override = null) { return ($override ?? static::$printTag) ? '' : ''; } @@ -68,7 +69,7 @@ public static function isValid() if (!isset($response)) throw new ResponseMissingException('g-recaptcha-response is missing from $_GET and $_POST'); - $response = json_decode(\NF::$capi->post('https://www.google.com/recaptcha/api/siteverify', [ + $response = json_decode(NF::$capi->post('https://www.google.com/recaptcha/api/siteverify', [ 'form_params' => [ 'secret' => static::getCredentials()->siteSecret, 'response' => $response, @@ -76,8 +77,8 @@ public static function isValid() ] ])->getBody()); - if (isset($response->{"error-codes"})) { - throw new GoogleResponseException($response->{"error-codes"}[0]); + if (isset($response->{'error-codes'})) { + throw new GoogleResponseException($response->{'error-codes'}[0]); } return $response->success; } diff --git a/tests/TestSuites/CaptchaV2/CaptchaV2.checkBox.test.php b/tests/TestSuites/CaptchaV2/CaptchaV2.checkBox.test.php new file mode 100644 index 0000000..2f241ec --- /dev/null +++ b/tests/TestSuites/CaptchaV2/CaptchaV2.checkBox.test.php @@ -0,0 +1,33 @@ +expectException('Netflex\Site\Support\ConfigurationMissingException'); + CaptchaV2::checkBox(); + } + + public function testMatchesSnapshot (): void { + NF::$site->mockVariable('captcha_site_key', 'aabbccddeeff'); + NF::$site->mockVariable('captcha_site_secret', '112233445566'); + $this->assertMatchesSnapshot(CaptchaV2::checkBox(), new TextDriver); + } +} diff --git a/tests/TestSuites/CaptchaV2/CaptchaV2.isValid.test.php b/tests/TestSuites/CaptchaV2/CaptchaV2.isValid.test.php new file mode 100644 index 0000000..952bbb5 --- /dev/null +++ b/tests/TestSuites/CaptchaV2/CaptchaV2.isValid.test.php @@ -0,0 +1,60 @@ +expectException('Netflex\Site\Support\ResponseMissingException'); + CaptchaV2::isValid(); + } + + public function testHandlesRequestException (): void { + $_GET['g-recaptcha-response'] = 'test'; + NF::$capi->mockResponse(new Response(500)); + NF::$site->mockVariable('captcha_site_key', 'aabbccddeeff'); + NF::$site->mockVariable('captcha_site_secret', '112233445566'); + $this->expectException('GuzzleHttp\Exception\ServerException'); + CaptchaV2::isValid(); + } + + public function testHandlesValidCase (): void { + $_GET['g-recaptcha-response'] = 'test'; + NF::$site->mockVariable('captcha_site_key', 'aabbccddeeff'); + NF::$site->mockVariable('captcha_site_secret', '112233445566'); + NF::$capi->mockResponse(new Response(200, ['Content-Type' => 'application/json'], json_encode([ + 'success' => false, + 'error-codes' => [ + 'Task failed successfully' + ] + ]))); + + $this->expectException('Netflex\Site\Support\GoogleResponseException'); + CaptchaV2::isValid(); + } + + public function testHandlesInvalidCase (): void { + $_GET['g-recaptcha-response'] = 'test'; + NF::$site->mockVariable('captcha_site_key', 'aabbccddeeff'); + NF::$site->mockVariable('captcha_site_secret', '112233445566'); + NF::$capi->mockResponse(new Response(200, ['Content-Type' => 'application/json'], json_encode([ + 'success' => true + ]))); + + $this->assertTrue(CaptchaV2::isValid()); + } +} diff --git a/tests/TestSuites/CaptchaV2/CaptchaV2.scriptTag.test.php b/tests/TestSuites/CaptchaV2/CaptchaV2.scriptTag.test.php new file mode 100644 index 0000000..79fbd9e --- /dev/null +++ b/tests/TestSuites/CaptchaV2/CaptchaV2.scriptTag.test.php @@ -0,0 +1,27 @@ +assertMatchesSnapshot(CaptchaV2::scriptTag(), new TextDriver); + $this->assertMatchesSnapshot(CaptchaV2::scriptTag(true), new TextDriver); + } +} diff --git a/tests/TestSuites/CaptchaV2/__snapshots__/CaptchaV2_CheckBoxTest__testMatchesSnapshot__1.txt b/tests/TestSuites/CaptchaV2/__snapshots__/CaptchaV2_CheckBoxTest__testMatchesSnapshot__1.txt new file mode 100644 index 0000000..474c051 --- /dev/null +++ b/tests/TestSuites/CaptchaV2/__snapshots__/CaptchaV2_CheckBoxTest__testMatchesSnapshot__1.txt @@ -0,0 +1 @@ +
\ No newline at end of file diff --git a/tests/TestSuites/CaptchaV2/__snapshots__/CaptchaV2_ScriptTagTest__testMatchesSnapshots__1.txt b/tests/TestSuites/CaptchaV2/__snapshots__/CaptchaV2_ScriptTagTest__testMatchesSnapshots__1.txt new file mode 100644 index 0000000..e69de29 diff --git a/tests/TestSuites/CaptchaV2/__snapshots__/CaptchaV2_ScriptTagTest__testMatchesSnapshots__2.txt b/tests/TestSuites/CaptchaV2/__snapshots__/CaptchaV2_ScriptTagTest__testMatchesSnapshots__2.txt new file mode 100644 index 0000000..061aae9 --- /dev/null +++ b/tests/TestSuites/CaptchaV2/__snapshots__/CaptchaV2_ScriptTagTest__testMatchesSnapshots__2.txt @@ -0,0 +1 @@ + \ No newline at end of file