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

Support validation when value contains response #31

Merged

Conversation

akrabat
Copy link
Contributor

@akrabat akrabat commented Feb 22, 2017

If the user passes in the response value directly to isValid(), e.g. by taking the g-recaptcha-response value directly, then this should work!

Fixes #30

If the user passes in the response value directly to isValid(), e.g. by
taking the `g-recaptcha-response` value directly, then this should work!

Fixes zendframework#30
@akrabat akrabat force-pushed the element-name-g-recaptcha-response branch from 0f669fd to 6076a48 Compare February 22, 2017 10:11
@akrabat
Copy link
Contributor Author

akrabat commented Feb 22, 2017

Note that we set the SSL Transport to TLS in the tests specifically because Travis uses PHP 5.6.5. From PHP 5.6.7, the Socket adapter uses TLS by default, but in 5.6.5, it uses SSLv3, which Google doesn't accept.

@@ -27,6 +27,10 @@
<!-- Enable this if you have installed ZendService\ReCaptcha on the
include_path or via Composer. -->
<env name="TESTS_ZEND_CAPTCHA_RECAPTCHA_SUPPORT" value="false" />
<!-- Change these if you want to use your own keys -->
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are these real values, or mocked up ones? If we're using real values, these should be encrypted in the travis-ci config, or else somebody else may abuse them (unless google has "test values")

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would then:

  • add the link to the comment
  • describe what they represent, since they are fake values

@@ -260,8 +260,11 @@ public function isValid($value, $context = null)
}

$service = $this->getService();
if (array_key_exists($value, $context)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nothing guarantees that $value is a string or integer here.

@@ -260,8 +260,11 @@ public function isValid($value, $context = null)
}

$service = $this->getService();
if (array_key_exists($value, $context)) {
$value = $context[$value];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of overriding $value, do use an } else { and call $res = $service->verify(...) with a different parameters. This avoids having a semantic change in the meaning of $value

public function testValidationForDifferentElementName()
{
$captcha = new ReCaptcha([
'site_key' => getenv('TESTS_ZEND_SERVICE_RECAPTCHA_SITE_KEY'),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should probably skip these tests if either of these variables are not defined (see comment above about committed secrets)

Copy link
Contributor Author

@akrabat akrabat Feb 22, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

setUp() has a guard for the 'TESTS_ZEND_CAPTCHA_RECAPTCHA_SUPPORT environment variable. Do we need a separate one for these keys?

I think no.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed with @akrabat , though I've noted a problem with the "skip" message in setUp().

public function testValidationForResponseElementName()
{
$captcha = new ReCaptcha([
'site_key' => getenv('TESTS_ZEND_SERVICE_RECAPTCHA_SITE_KEY'),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should probably skip these tests if either of these variables are not defined (see comment above about committed secrets)

'site_key' => getenv('TESTS_ZEND_SERVICE_RECAPTCHA_SITE_KEY'),
'secret_key' => getenv('TESTS_ZEND_SERVICE_RECAPTCHA_SECRET_KEY'),
]);
$captcha->getService()->setIp('127.0.0.1');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Avoid repeating a getService() call

'site_key' => getenv('TESTS_ZEND_SERVICE_RECAPTCHA_SITE_KEY'),
'secret_key' => getenv('TESTS_ZEND_SERVICE_RECAPTCHA_SECRET_KEY'),
]);
$captcha->getService()->setIp('127.0.0.1');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Avoid repeating a getService() call

$captcha->getService()->setIp('127.0.0.1');
$captcha->getService()->setHttpClient($this->getHttpClient());

$response = getenv('TESTS_ZEND_SERVICE_RECAPTCHA_RESPONSE');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, so I assume the environment vars are actual mock values?

$this->assertTrue($captcha->isValid($value, $context));
}

private function getHttpClient()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Return type in docblock, please

$socket->setOptions([
'ssltransport' => 'tls',
]);
return new HttpClient(null, [
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This indenting is not PSR-2 compliant

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes it is!

$ vendor/bin/phpcs test/ReCaptchaTest.php 
.

Time: 101ms; Memory: 6Mb

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Definitely PSR-2 compliant; single argument spanning multiple lines. If both arguments were arrays, then you would need to start each on a new line, but because it's only one, can be done this way.

@akrabat
Copy link
Contributor Author

akrabat commented Feb 22, 2017

Updated as per @Ocramius' comments.

Travis passed: https://travis-ci.org/zendframework/zend-captcha/builds/204158887

Copy link
Member

@weierophinney weierophinney left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good! The only concern I have is that the setUp() validation of the TESTS_ZEND_CAPTCHA_RECAPTCHA_SUPPORT constant emits a misleading message when the constant is missing. I realize that's from a previous commit somewhere, but might as well change it now, since these tests rely on it.

Once fixed, feel free to merge!

@@ -28,14 +30,6 @@ public function setUp()
if (! getenv('TESTS_ZEND_CAPTCHA_RECAPTCHA_SUPPORT')) {
$this->markTestSkipped('Enable TESTS_ZEND_CAPTCHA_RECAPTCHA_SUPPORT to test PDF render');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this message needs to be different, as recaptcha has nothing to do with PDF...

public function testValidationForDifferentElementName()
{
$captcha = new ReCaptcha([
'site_key' => getenv('TESTS_ZEND_SERVICE_RECAPTCHA_SITE_KEY'),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed with @akrabat , though I've noted a problem with the "skip" message in setUp().

$socket->setOptions([
'ssltransport' => 'tls',
]);
return new HttpClient(null, [
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Definitely PSR-2 compliant; single argument spanning multiple lines. If both arguments were arrays, then you would need to start each on a new line, but because it's only one, can be done this way.

@akrabat akrabat added this to the 2.7.1 milestone Feb 22, 2017
@akrabat akrabat force-pushed the element-name-g-recaptcha-response branch from 1286ce9 to 64346d0 Compare February 23, 2017 07:46
@akrabat akrabat merged commit 64346d0 into zendframework:master Feb 23, 2017
akrabat added a commit that referenced this pull request Feb 23, 2017
@akrabat akrabat deleted the element-name-g-recaptcha-response branch February 23, 2017 08:07
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants