Skip to content
This repository has been archived by the owner on Jan 14, 2020. It is now read-only.

Commit

Permalink
Merge pull request #45 from apility/feature/catpchav2-test-suite
Browse files Browse the repository at this point in the history
Test suite for CaptchaV2
  • Loading branch information
thomas-alrek authored Jun 12, 2019
2 parents 8b4d110 + 13d9794 commit ae58edb
Show file tree
Hide file tree
Showing 8 changed files with 134 additions and 6 deletions.
5 changes: 5 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@
verbose="true"
>
<testsuites>
<testsuite name="CaptchaV2 Tests">
<directory suffix=".test.php">
tests/TestSuites/CaptchaV2
</directory>
</testsuite>
<testsuite name="Common Tests">
<directory suffix=".test.php">
tests/TestSuites/Common
Expand Down
13 changes: 7 additions & 6 deletions src/Netflex/Site/Support/CaptchaV2.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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,
Expand All @@ -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) ? '<script src="https://www.google.com/recaptcha/api.js"></script>' : '';
}
Expand Down Expand Up @@ -68,16 +69,16 @@ 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,
'remoteip' => $_SERVER['REMOTE_ADDR']
]
])->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;
}
Expand Down
33 changes: 33 additions & 0 deletions tests/TestSuites/CaptchaV2/CaptchaV2.checkBox.test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

use PHPUnit\Framework\TestCase;
use Netflex\Site\Support\CaptchaV2;
use Spatie\Snapshots\MatchesSnapshots;

final class CaptchaV2_CheckBoxTest extends TestCase
{
use MatchesSnapshots;

/**
* Call this template method before each test method is run.
*/
protected function setUp(): void
{
require_once('src/Netflex/Site/Support/CaptchaV2.php');
require_once('src/functions/common/functions_system.php');
require_once('src/Netflex/Site/Support/GoogleResponseException.php');
require_once('src/Netflex/Site/Support/ResponseMissingException.php');
require_once('src/Netflex/Site/Support/ConfigurationMissingException.php');
}

public function testThrowsExceptionWhenConfiguationMissing (): void {
$this->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);
}
}
60 changes: 60 additions & 0 deletions tests/TestSuites/CaptchaV2/CaptchaV2.isValid.test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

use GuzzleHttp\Psr7\Response;
use PHPUnit\Framework\TestCase;
use Netflex\Site\Support\CaptchaV2;

final class CaptchaV2_IsValidTest extends TestCase
{
/**
* Call this template method before each test method is run.
*/
protected function setUp(): void
{
require_once('src/Netflex/Site/Support/CaptchaV2.php');
require_once('src/functions/common/functions_system.php');
require_once('src/Netflex/Site/Support/GoogleResponseException.php');
require_once('src/Netflex/Site/Support/ResponseMissingException.php');
require_once('src/Netflex/Site/Support/ConfigurationMissingException.php');
}

public function testThrowsExceptionWhenParametersMissing (): void {
$this->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());
}
}
27 changes: 27 additions & 0 deletions tests/TestSuites/CaptchaV2/CaptchaV2.scriptTag.test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

use PHPUnit\Framework\TestCase;
use Netflex\Site\Support\CaptchaV2;
use Spatie\Snapshots\MatchesSnapshots;

final class CaptchaV2_ScriptTagTest extends TestCase
{
use MatchesSnapshots;

/**
* Call this template method before each test method is run.
*/
protected function setUp(): void
{
require_once('src/Netflex/Site/Support/CaptchaV2.php');
require_once('src/functions/common/functions_system.php');
require_once('src/Netflex/Site/Support/GoogleResponseException.php');
require_once('src/Netflex/Site/Support/ResponseMissingException.php');
require_once('src/Netflex/Site/Support/ConfigurationMissingException.php');
}

public function testMatchesSnapshots (): void {
$this->assertMatchesSnapshot(CaptchaV2::scriptTag(), new TextDriver);
$this->assertMatchesSnapshot(CaptchaV2::scriptTag(true), new TextDriver);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<div class="g-recaptcha" data-sitekey="aabbccddeeff"></div>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<script src="https://www.google.com/recaptcha/api.js"></script>

0 comments on commit ae58edb

Please sign in to comment.