Skip to content

Commit 23d3cae

Browse files
committed
security-package/issues/105: Cover ReCaptchaContact module with integration test
1 parent e6af9b2 commit 23d3cae

File tree

3 files changed

+138
-96
lines changed

3 files changed

+138
-96
lines changed

ReCaptchaContact/Test/Integration/ContactFormTest.php

Lines changed: 87 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,7 @@
2020
use PHPUnit\Framework\MockObject\MockObject;
2121

2222
/**
23-
* Test for \Magento\ReCaptchaContact\Observer\ContactFormObserver class.
24-
*
2523
* @magentoAppArea frontend
26-
* @magentoDbIsolation enabled
2724
* @magentoAppIsolation enabled
2825
*/
2926
class ContactFormTest extends AbstractController
@@ -60,6 +57,11 @@ protected function setUp()
6057
$this->_objectManager->addSharedInstance($captchaValidatorMock, Validator::class);
6158
}
6259

60+
/**
61+
* @magentoConfigFixture base_website customer/captcha/enable 0
62+
* @magentoConfigFixture base_website recaptcha_frontend/type_invisible/public_key test_public_key
63+
* @magentoConfigFixture base_website recaptcha_frontend/type_invisible/private_key test_private_key
64+
*/
6365
public function testGetRequestIfReCaptchaIsDisabled()
6466
{
6567
$this->initConfig(0, 'test_public_key', 'test_private_key');
@@ -68,7 +70,8 @@ public function testGetRequestIfReCaptchaIsDisabled()
6870
}
6971

7072
/**
71-
* @magentoConfigFixture default_store recaptcha_frontend/type_for/contact invisible
73+
* @magentoConfigFixture base_website customer/captcha/enable 0
74+
* @magentoConfigFixture base_website recaptcha_frontend/type_for/contact invisible
7275
*/
7376
public function testGetRequestIfReCaptchaKeysAreNotConfigured()
7477
{
@@ -78,7 +81,10 @@ public function testGetRequestIfReCaptchaKeysAreNotConfigured()
7881
}
7982

8083
/**
81-
* @magentoConfigFixture default_store recaptcha_frontend/type_for/contact invisible
84+
* @magentoConfigFixture base_website customer/captcha/enable 0
85+
* @magentoConfigFixture base_website recaptcha_frontend/type_invisible/public_key test_public_key
86+
* @magentoConfigFixture base_website recaptcha_frontend/type_invisible/private_key test_private_key
87+
* @magentoConfigFixture base_website recaptcha_frontend/type_for/contact invisible
8288
*/
8389
public function testGetRequestIfReCaptchaIsEnabled()
8490
{
@@ -87,51 +93,78 @@ public function testGetRequestIfReCaptchaIsEnabled()
8793
$this->checkSuccessfulGetResponse(true);
8894
}
8995

90-
public function testPostRequestIfReCaptchaKeysAreNotConfigured()
96+
/**
97+
* @magentoConfigFixture base_website customer/captcha/enable 0
98+
* @magentoConfigFixture base_website recaptcha_frontend/type_invisible/public_key test_public_key
99+
* @magentoConfigFixture base_website recaptcha_frontend/type_invisible/private_key test_private_key
100+
*/
101+
public function testPostRequestIfReCaptchaIsDisabled()
91102
{
92-
$this->initConfig(1, null, null);
103+
$this->initConfig(0, 'test_public_key', 'test_private_key');
93104

94-
$this->checkSuccessfulPostResponse(true);
105+
$this->checkPostResponse(true);
95106
}
96107

97-
public function testPostRequestIfReCaptchaIsDisabled()
108+
/**
109+
* @magentoConfigFixture base_website customer/captcha/enable 0
110+
* @magentoConfigFixture base_website recaptcha_frontend/type_for/contact invisible
111+
*/
112+
public function testPostRequestIfReCaptchaKeysAreNotConfigured()
98113
{
99-
$this->initConfig(0, 'test_public_key', 'test_private_key');
114+
$this->initConfig(1, null, null);
100115

101-
$this->checkSuccessfulPostResponse(true);
116+
$this->checkPostResponse(true);
102117
}
103118

119+
/**
120+
* @magentoConfigFixture base_website customer/captcha/enable 0
121+
* @magentoConfigFixture base_website recaptcha_frontend/type_invisible/public_key test_public_key
122+
* @magentoConfigFixture base_website recaptcha_frontend/type_invisible/private_key test_private_key
123+
* @magentoConfigFixture base_website recaptcha_frontend/type_for/contact invisible
124+
*/
104125
public function testPostRequestWithSuccessfulReCaptchaValidation()
105126
{
106127
$this->initConfig(1, 'test_public_key', 'test_private_key');
107128
$this->captchaValidationResultMock->expects($this->once())->method('isValid')->willReturn(true);
108129

109-
$this->checkSuccessfulPostResponse(
130+
$this->checkPostResponse(
110131
true,
111-
[CaptchaResponseResolverInterface::PARAM_RECAPTCHA => 'test_response']
132+
[
133+
CaptchaResponseResolverInterface::PARAM_RECAPTCHA => 'test',
134+
]
112135
);
113136
}
114137

115-
public function testPostRequestWithFailedReCaptchaValidation()
138+
/**
139+
* @magentoConfigFixture base_website customer/captcha/enable 0
140+
* @magentoConfigFixture base_website recaptcha_frontend/type_invisible/public_key test_public_key
141+
* @magentoConfigFixture base_website recaptcha_frontend/type_invisible/private_key test_private_key
142+
* @magentoConfigFixture base_website recaptcha_frontend/type_for/contact invisible
143+
*/
144+
public function testPostRequestIfReCaptchaParameterIsMissed()
116145
{
117146
$this->initConfig(1, 'test_public_key', 'test_private_key');
118-
$this->captchaValidationResultMock->expects($this->once())->method('isValid')->willReturn(false);
119147

120-
$this->checkSuccessfulPostResponse(
121-
false,
122-
[CaptchaResponseResolverInterface::PARAM_RECAPTCHA => 'test_response']
123-
);
148+
$this->expectException(InputException::class);
149+
$this->expectExceptionMessage('Can not resolve reCAPTCHA parameter.');
150+
151+
$this->checkPostResponse(false);
124152
}
125153

126-
public function testPostRequestIfReCaptchaParameterIsMissed()
154+
/**
155+
* @magentoConfigFixture base_website customer/captcha/enable 0
156+
* @magentoConfigFixture base_website recaptcha_frontend/type_invisible/public_key test_public_key
157+
* @magentoConfigFixture base_website recaptcha_frontend/type_invisible/private_key test_private_key
158+
* @magentoConfigFixture base_website recaptcha_frontend/type_for/contact invisible
159+
*/
160+
public function testPostRequestWithFailedReCaptchaValidation()
127161
{
128162
$this->initConfig(1, 'test_public_key', 'test_private_key');
129-
$this->captchaValidationResultMock->expects($this->never())->method('isValid');
130-
$this->expectException(InputException::class);
131-
$this->expectExceptionMessage('Can not resolve reCAPTCHA parameter.');
163+
$this->captchaValidationResultMock->expects($this->once())->method('isValid')->willReturn(false);
132164

133-
$this->checkSuccessfulPostResponse(
134-
false
165+
$this->checkPostResponse(
166+
false,
167+
[CaptchaResponseResolverInterface::PARAM_RECAPTCHA => 'test']
135168
);
136169
}
137170

@@ -153,28 +186,28 @@ private function checkSuccessfulGetResponse($shouldContainReCaptcha = false)
153186
}
154187

155188
/**
156-
* @param bool $result
189+
* @param bool $isSuccessfulRequest
157190
* @param array $postValues
158-
* @throws LocalizedException
159-
* @throws \Magento\Framework\Exception\NoSuchEntityException
160191
*/
161-
private function checkSuccessfulPostResponse(bool $result, array $postValues = [])
192+
private function checkPostResponse(bool $isSuccessfulRequest, array $postValues = [])
162193
{
163-
$this->getRequest()->setPostValue(array_replace_recursive(
164-
[
165-
'form_key' => $this->formKey->getFormKey(),
166-
'name' => 'customer name',
167-
'comment' => 'comment',
168-
'email' => '[email protected]',
169-
],
170-
$postValues
171-
))->setMethod(HttpRequest::METHOD_POST);
194+
$this->getRequest()
195+
->setMethod(HttpRequest::METHOD_POST)
196+
->setPostValue(array_replace_recursive(
197+
[
198+
'form_key' => $this->formKey->getFormKey(),
199+
'name' => 'customer name',
200+
'comment' => 'comment',
201+
'email' => '[email protected]',
202+
],
203+
$postValues
204+
));
172205

173206
$this->dispatch('contact/index/post');
174207

175208
$this->assertRedirect($this->stringContains('contact/index'));
176209

177-
if ($result) {
210+
if ($isSuccessfulRequest) {
178211
$this->assertSessionMessages(
179212
$this->contains(
180213
"Thanks for contacting us with your comments and questions. We'll respond to you very soon."
@@ -197,9 +230,20 @@ private function checkSuccessfulPostResponse(bool $result, array $postValues = [
197230
*/
198231
private function initConfig(?int $enabled, ?string $public, ?string $private): void
199232
{
200-
$this->mutableScopeConfig->setValue('recaptcha_frontend/type_for/newsletter', null, ScopeInterface::SCOPE_WEBSITE);
201-
$this->mutableScopeConfig->setValue('recaptcha_frontend/type_for/contact', $enabled ? 'invisible' : null, ScopeInterface::SCOPE_WEBSITE);
202-
$this->mutableScopeConfig->setValue('recaptcha_frontend/type_invisible/public_key', $public, ScopeInterface::SCOPE_WEBSITE);
203-
$this->mutableScopeConfig->setValue('recaptcha_frontend/type_invisible/private_key', $private, ScopeInterface::SCOPE_WEBSITE);
233+
$this->mutableScopeConfig->setValue(
234+
'recaptcha_frontend/type_for/contact',
235+
$enabled ? 'invisible' : null,
236+
ScopeInterface::SCOPE_WEBSITE
237+
);
238+
$this->mutableScopeConfig->setValue(
239+
'recaptcha_frontend/type_invisible/public_key',
240+
$public,
241+
ScopeInterface::SCOPE_WEBSITE
242+
);
243+
$this->mutableScopeConfig->setValue(
244+
'recaptcha_frontend/type_invisible/private_key',
245+
$private,
246+
ScopeInterface::SCOPE_WEBSITE
247+
);
204248
}
205249
}

ReCaptchaUser/Test/Integration/ForgotPasswordFormTest.php

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,6 @@ public function testGetRequestIfReCaptchaIsDisabled()
7575
/**
7676
* @magentoAdminConfigFixture admin/captcha/enable 0
7777
* @magentoAdminConfigFixture recaptcha_backend/type_for/user_forgot_password invisible
78-
* @magentoConfigFixture default_store recaptcha_backend/type_for/user_forgot_password invisible
7978
*/
8079
public function testGetRequestIfReCaptchaKeysAreNotConfigured()
8180
{
@@ -87,7 +86,6 @@ public function testGetRequestIfReCaptchaKeysAreNotConfigured()
8786
* @magentoAdminConfigFixture recaptcha_backend/type_invisible/public_key test_public_key
8887
* @magentoAdminConfigFixture recaptcha_backend/type_invisible/private_key test_private_key
8988
* @magentoAdminConfigFixture recaptcha_backend/type_for/user_forgot_password invisible
90-
* @magentoConfigFixture default_store recaptcha_backend/type_for/user_forgot_password invisible
9189
*/
9290
public function testGetRequestIfReCaptchaIsEnabled()
9391
{
@@ -108,7 +106,6 @@ public function testPostRequestIfReCaptchaIsDisabled()
108106
* @magentoAdminConfigFixture admin/captcha/enable 0
109107
* @magentoAdminConfigFixture admin/captcha/always_for/backend_forgotpassword 0
110108
* @magentoAdminConfigFixture recaptcha_backend/type_for/user_forgot_password invisible
111-
* @magentoConfigFixture default_store recaptcha_backend/type_for/user_forgot_password invisible
112109
*/
113110
public function testPostRequestIfReCaptchaKeysAreNotConfigured()
114111
{
@@ -120,7 +117,6 @@ public function testPostRequestIfReCaptchaKeysAreNotConfigured()
120117
* @magentoAdminConfigFixture recaptcha_backend/type_invisible/public_key test_public_key
121118
* @magentoAdminConfigFixture recaptcha_backend/type_invisible/private_key test_private_key
122119
* @magentoAdminConfigFixture recaptcha_backend/type_for/user_forgot_password invisible
123-
* @magentoConfigFixture default_store recaptcha_backend/type_for/user_forgot_password invisible
124120
*/
125121
public function testPostRequestWithSuccessfulReCaptchaValidation()
126122
{
@@ -138,18 +134,19 @@ public function testPostRequestWithSuccessfulReCaptchaValidation()
138134
* @magentoAdminConfigFixture recaptcha_backend/type_invisible/public_key test_public_key
139135
* @magentoAdminConfigFixture recaptcha_backend/type_invisible/private_key test_private_key
140136
* @magentoAdminConfigFixture recaptcha_backend/type_for/user_forgot_password invisible
141-
* @magentoConfigFixture default_store recaptcha_backend/type_for/user_forgot_password invisible
142137
* @expectedException \Magento\Framework\Exception\InputException
143138
* @expectedExceptionMessage Can not resolve reCAPTCHA parameter.
144139
*/
145140
public function testPostRequestIfReCaptchaParameterIsMissed()
146141
{
147-
$this->getRequest()->setPostValue(
148-
[
149-
'form_key' => $this->formKey->getFormKey(),
150-
'email' => '[email protected]'
151-
]
152-
);
142+
$this->getRequest()
143+
->setMethod(HttpRequest::METHOD_POST)
144+
->setPostValue(
145+
[
146+
'form_key' => $this->formKey->getFormKey(),
147+
'email' => '[email protected]'
148+
]
149+
);
153150
$this->dispatch('backend/admin/auth/forgotpassword');
154151

155152
self::assertEmpty($this->transportMock->getSentMessage());
@@ -160,19 +157,20 @@ public function testPostRequestIfReCaptchaParameterIsMissed()
160157
* @magentoAdminConfigFixture recaptcha_backend/type_invisible/public_key test_public_key
161158
* @magentoAdminConfigFixture recaptcha_backend/type_invisible/private_key test_private_key
162159
* @magentoAdminConfigFixture recaptcha_backend/type_for/user_forgot_password invisible
163-
* @magentoConfigFixture default_store recaptcha_backend/type_for/user_forgot_password invisible
164160
*/
165161
public function testPostRequestWithFailedReCaptchaValidation()
166162
{
167163
$this->captchaValidationResultMock->expects($this->once())->method('isValid')->willReturn(false);
168164

169-
$this->getRequest()->setPostValue(
170-
[
171-
'form_key' => $this->formKey->getFormKey(),
172-
'email' => '[email protected]',
173-
CaptchaResponseResolverInterface::PARAM_RECAPTCHA => 'test',
174-
]
175-
);
165+
$this->getRequest()
166+
->setMethod(HttpRequest::METHOD_POST)
167+
->setPostValue(
168+
[
169+
'form_key' => $this->formKey->getFormKey(),
170+
'email' => '[email protected]',
171+
CaptchaResponseResolverInterface::PARAM_RECAPTCHA => 'test',
172+
]
173+
);
176174
$this->dispatch('backend/admin/auth/forgotpassword');
177175

178176
$this->assertSessionMessages(

0 commit comments

Comments
 (0)