From d36c9b44e4e68dbde047d1621bbfc7578be2a9dd Mon Sep 17 00:00:00 2001 From: wxa Date: Thu, 4 Jul 2019 20:20:06 +0300 Subject: [PATCH] Add integration tests [MAILPOET-2015] --- .../API/JSON/v1/SubscribersTest.php | 43 +++++++++++++-- tests/integration/Config/SessionTest.php | 35 +++++++++++++ .../integration/Subscription/CaptchaTest.php | 52 +++++++++++++++++++ tests/integration/Subscription/FormTest.php | 19 +++++++ .../Subscription/ThrottlingTest.php | 2 +- tests/integration/Subscription/UrlTest.php | 14 +++++ 6 files changed, 161 insertions(+), 4 deletions(-) create mode 100644 tests/integration/Config/SessionTest.php create mode 100644 tests/integration/Subscription/CaptchaTest.php diff --git a/tests/integration/API/JSON/v1/SubscribersTest.php b/tests/integration/API/JSON/v1/SubscribersTest.php index e3c55b5969..24c7ef86d7 100644 --- a/tests/integration/API/JSON/v1/SubscribersTest.php +++ b/tests/integration/API/JSON/v1/SubscribersTest.php @@ -20,6 +20,7 @@ use MailPoet\Models\Setting; use MailPoet\Models\SubscriberSegment; use MailPoet\Settings\SettingsController; use MailPoet\Subscribers\Source; +use MailPoet\Subscription\Captcha; class SubscribersTest extends \MailPoetTest { @@ -479,8 +480,8 @@ class SubscribersTest extends \MailPoetTest { expect($response->status)->equals(APIResponse::STATUS_OK); } - function testItCannotSubscribeWithoutCaptchaWhenEnabled() { - $this->settings->set('re_captcha', ['enabled' => true]); + function testItCannotSubscribeWithoutReCaptchaWhenEnabled() { + $this->settings->set('captcha', ['type' => Captcha::TYPE_RECAPTCHA]); $response = $this->endpoint->subscribe([ $this->obfuscatedEmail => 'toto@mailpoet.com', 'form_id' => $this->form->id, @@ -488,7 +489,43 @@ class SubscribersTest extends \MailPoetTest { ]); expect($response->status)->equals(APIResponse::STATUS_BAD_REQUEST); expect($response->errors[0]['message'])->equals('Please check the CAPTCHA.'); - $this->settings->set('re_captcha', []); + $this->settings->set('captcha', []); + } + + function testItCannotSubscribeWithoutBuiltInCaptchaWhenEnabled() { + $this->settings->set('captcha', ['type' => Captcha::TYPE_BUILTIN]); + $email = 'toto@mailpoet.com'; + $subscriber = Subscriber::create(); + $subscriber->email = $email; + $subscriber->count_confirmations = 1; + $subscriber->save(); + $response = $this->endpoint->subscribe([ + $this->obfuscatedEmail => $email, + 'form_id' => $this->form->id, + $this->obfuscatedSegments => [$this->segment_1->id, $this->segment_2->id], + ]); + expect($response->status)->equals(APIResponse::STATUS_BAD_REQUEST); + expect($response->errors[0]['message'])->equals('Please check the CAPTCHA.'); + $this->settings->set('captcha', []); + } + + function testItCanSubscribeWithBuiltInCaptchaWhenEnabled() { + $this->settings->set('captcha', ['type' => Captcha::TYPE_BUILTIN]); + $email = 'toto@mailpoet.com'; + $subscriber = Subscriber::create(); + $subscriber->email = $email; + $subscriber->count_confirmations = 1; + $subscriber->save(); + $captcha_value = 'ihg5w'; + $_SESSION[Captcha::SESSION_KEY] = $captcha_value; + $response = $this->endpoint->subscribe([ + $this->obfuscatedEmail => $email, + 'form_id' => $this->form->id, + $this->obfuscatedSegments => [$this->segment_1->id, $this->segment_2->id], + 'captcha' => $captcha_value, + ]); + expect($response->status)->equals(APIResponse::STATUS_OK); + $this->settings->set('captcha', []); } function testItCannotSubscribeWithoutMandatoryCustomField() { diff --git a/tests/integration/Config/SessionTest.php b/tests/integration/Config/SessionTest.php new file mode 100644 index 0000000000..bb59bf1d8d --- /dev/null +++ b/tests/integration/Config/SessionTest.php @@ -0,0 +1,35 @@ +destroySessionIfExists(); + } + + function testItStartsSessionIfItIsNotStarted() { + expect(session_id())->isEmpty(); + $session = new Session; + $result = $session->init(); + expect($result)->equals(true); + expect(session_id())->notEmpty(); + session_destroy(); + } + + function testItDoesNotStartSessionIfItIsAlreadyStarted() { + session_start(); + expect(session_id())->notEmpty(); + $session = new Session; + $result = $session->init(); + expect($result)->equals(false); + expect(session_id())->notEmpty(); + session_destroy(); + } + + private function destroySessionIfExists() { + if (session_id()) { + session_destroy(); + } + } +} diff --git a/tests/integration/Subscription/CaptchaTest.php b/tests/integration/Subscription/CaptchaTest.php new file mode 100644 index 0000000000..85f6bbef50 --- /dev/null +++ b/tests/integration/Subscription/CaptchaTest.php @@ -0,0 +1,52 @@ +captcha = new Captcha(new WPFunctions); + $_SERVER['REMOTE_ADDR'] = '127.0.0.1'; + } + + function testItDoesNotRequireCaptchaForTheFirstSubscription() { + $email = 'non-existent-subscriber@example.com'; + $result = $this->captcha->isRequired($email); + expect($result)->equals(false); + } + + function testItRequiresCaptchaForRepeatedRecipient() { + $subscriber = Subscriber::create(); + $subscriber->hydrate(Fixtures::get('subscriber_template')); + $subscriber->count_confirmations = 1; + $subscriber->save(); + $result = $this->captcha->isRequired($subscriber->email); + expect($result)->equals(true); + } + + function testItRequiresCaptchaForRepeatedIPAddress() { + $ip = SubscriberIP::create(); + $ip->ip = '127.0.0.1'; + $ip->created_at = Carbon::now()->subMinutes(1); + $ip->save(); + $email = 'non-existent-subscriber@example.com'; + $result = $this->captcha->isRequired($email); + expect($result)->equals(true); + } + + function testItRendersImage() { + expect_that(empty($_SESSION[Captcha::SESSION_KEY])); + $image = $this->captcha->renderImage(null, null, true); + expect($image)->notEmpty(); + expect_that(!empty($_SESSION[Captcha::SESSION_KEY])); + } + + function _after() { + SubscriberIP::deleteMany(); + } +} diff --git a/tests/integration/Subscription/FormTest.php b/tests/integration/Subscription/FormTest.php index cb86432a1e..ab13f75b23 100644 --- a/tests/integration/Subscription/FormTest.php +++ b/tests/integration/Subscription/FormTest.php @@ -3,6 +3,8 @@ namespace MailPoet\Test\Subscription; use Codeception\Stub; use MailPoet\API\JSON\API; +use MailPoet\API\JSON\ErrorResponse; +use MailPoet\API\JSON\Response; use MailPoet\DI\ContainerWrapper; use MailPoet\Form\Util\FieldNameObfuscator; use MailPoet\Models\Form as FormModel; @@ -120,6 +122,23 @@ class FormTest extends \MailPoetTest { expect($result['mailpoet_success'])->null(); } + function testItDoesNotSubscribeAndRedirectsToRedirectUrlIfPresent() { + $redirect_url = 'http://test/'; + $url_helper = Stub::make(UrlHelper::class, [ + 'redirectTo' => function($params) { + return $params; + }, + ], $this); + $api = Stub::makeEmpty(API::class, [ + 'processRoute' => function() use ($redirect_url) { + return new ErrorResponse([], ['redirect_url' => $redirect_url], Response::STATUS_BAD_REQUEST); + }, + ], $this); + $form_controller = new Form($api, $url_helper); + $result = $form_controller->onSubmit($this->request_data); + expect($result)->equals($redirect_url); + } + function _after() { wp_delete_post($this->post); \ORM::raw_execute('TRUNCATE ' . SegmentModel::$_table); diff --git a/tests/integration/Subscription/ThrottlingTest.php b/tests/integration/Subscription/ThrottlingTest.php index 2534562b9a..90ed4b6b90 100644 --- a/tests/integration/Subscription/ThrottlingTest.php +++ b/tests/integration/Subscription/ThrottlingTest.php @@ -47,7 +47,7 @@ class ThrottlingTest extends \MailPoetTest { $ip2 = SubscriberIP::create(); $ip2->ip = '127.0.0.1'; - $ip2->created_at = Carbon::now()->subDays(1)->subSeconds(1); + $ip2->created_at = Carbon::now()->subMonths(1)->subSeconds(1); $ip2->save(); expect(SubscriberIP::count())->equals(2); diff --git a/tests/integration/Subscription/UrlTest.php b/tests/integration/Subscription/UrlTest.php index 635fc4d808..e9e16ca889 100644 --- a/tests/integration/Subscription/UrlTest.php +++ b/tests/integration/Subscription/UrlTest.php @@ -16,6 +16,20 @@ class UrlTest extends \MailPoetTest { $populator->up(); } + function testItReturnsTheCaptchaUrl() { + $url = Url::getCaptchaUrl(); + expect($url)->notNull(); + expect($url)->contains('action=captcha'); + expect($url)->contains('endpoint=subscription'); + } + + function testItReturnsTheCaptchaImageUrl() { + $url = Url::getCaptchaImageUrl(250, 100); + expect($url)->notNull(); + expect($url)->contains('action=captchaImage'); + expect($url)->contains('endpoint=subscription'); + } + function testItReturnsTheConfirmationUrl() { // preview $url = Url::getConfirmationUrl(null);