Add integration tests [MAILPOET-2015]

This commit is contained in:
wxa
2019-07-04 20:20:06 +03:00
committed by M. Shull
parent b174a55d07
commit d36c9b44e4
6 changed files with 161 additions and 4 deletions

View File

@ -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() {

View File

@ -0,0 +1,35 @@
<?php
namespace MailPoet\Test\Config;
use MailPoet\Config\Session;
class SessionTest extends \MailPoetTest {
function _before() {
$this->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();
}
}
}

View File

@ -0,0 +1,52 @@
<?php
namespace MailPoet\Test\Subscription;
use Carbon\Carbon;
use Codeception\Util\Fixtures;
use MailPoet\Models\Subscriber;
use MailPoet\Models\SubscriberIP;
use MailPoet\Subscription\Captcha;
use MailPoet\WP\Functions as WPFunctions;
class CaptchaTest extends \MailPoetTest {
function _before() {
$this->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();
}
}

View File

@ -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);

View File

@ -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);

View File

@ -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);