-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #228 from wmde/moderate-emails
Implement proper email block list
- Loading branch information
Showing
7 changed files
with
84 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -175,6 +175,19 @@ public function testGivenValidRequest_withIncompletePayment_confirmationEmailIsN | |
$useCase->addDonation( $request ); | ||
} | ||
|
||
public function testGivenValidRequest_withBlockedEmail_confirmationEmailIsNotSent(): void { | ||
$mockNotifier = $this->createMock( DonationNotifier::class ); | ||
$mockNotifier->expects( $this->never() )->method( 'sendConfirmationFor' ); | ||
|
||
$useCase = $this->makeUseCase( | ||
policyValidator: $this->makeEmailBlockedModerationService(), | ||
notifier: $mockNotifier | ||
); | ||
|
||
$request = $this->newValidAddDonationRequestWithEmail( '[email protected]' ); | ||
$useCase->addDonation( $request ); | ||
} | ||
|
||
public function testGivenValidRequestWithPolicyViolation_donationIsModerated(): void { | ||
$useCase = $this->makeUseCase( policyValidator: $this->makeFakeFailingModerationService() ); | ||
|
||
|
@@ -291,21 +304,6 @@ public function testEventIsEmittedAfterDonationWasStored(): void { | |
$this->assertInstanceOf( CompanyContactName::class, $events[0]->getDonor()->getName() ); | ||
} | ||
|
||
public function testWhenEmailAddressIsBlacklisted_donationIsMarkedAsCancelled(): void { | ||
$repository = $this->makeDonationRepositoryStub(); | ||
$useCase = $this->makeUseCase( | ||
idGenerator: new StaticDonationIdRepository(), | ||
repository: $repository, | ||
policyValidator: $this->makeFakeAutodeletingPolicyValidator() | ||
); | ||
|
||
$useCase->addDonation( $this->newValidAddDonationRequestWithEmail( '[email protected]' ) ); | ||
$donation = $repository->getDonationById( StaticDonationIdRepository::DONATION_ID ); | ||
|
||
$this->assertNotNull( $donation ); | ||
$this->assertTrue( $donation->isCancelled() ); | ||
} | ||
|
||
public function testOptingIntoDonationReceipt_persistedInDonor(): void { | ||
$repository = $this->makeDonationRepositoryStub(); | ||
$useCase = $this->makeUseCase( | ||
|
@@ -426,10 +424,11 @@ private function makeFakeFailingModerationService(): ModerationService { | |
return $validator; | ||
} | ||
|
||
private function makeFakeAutodeletingPolicyValidator(): ModerationService { | ||
private function makeEmailBlockedModerationService(): ModerationService { | ||
$result = new ModerationResult(); | ||
$result->addModerationReason( new ModerationReason( ModerationIdentifier::EMAIL_BLOCKED ) ); | ||
$validator = $this->createStub( ModerationService::class ); | ||
$validator->method( 'moderateDonationRequest' )->willReturn( new ModerationResult() ); | ||
$validator->method( 'isAutoDeleted' )->willReturn( true ); | ||
$validator->method( 'moderateDonationRequest' )->willReturn( $result ); | ||
return $validator; | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,7 +6,9 @@ | |
|
||
use PHPUnit\Framework\TestCase; | ||
use WMDE\Fundraising\DonationContext\Domain\Model\DonorType; | ||
use WMDE\Fundraising\DonationContext\Domain\Model\ModerationIdentifier; | ||
use WMDE\Fundraising\DonationContext\Tests\Data\ValidAddDonationRequest; | ||
use WMDE\Fundraising\DonationContext\UseCases\AddDonation\AddDonationValidationResult; | ||
use WMDE\Fundraising\DonationContext\UseCases\AddDonation\Moderation\ModerationService; | ||
use WMDE\Fundraising\PaymentContext\UseCases\CreatePayment\PaymentParameters; | ||
use WMDE\FunValidators\ConstraintViolation; | ||
|
@@ -103,12 +105,12 @@ private function newFailingTextPolicyValidator(): TextPolicyValidator { | |
} | ||
|
||
/** @dataProvider allowedEmailAddressProvider */ | ||
public function testWhenEmailAddressIsNotForbidden_isAutoDeletedReturnsFalse( string $emailAddress ): void { | ||
public function testWhenEmailAddressIsNotOnBlockList_needsModerationReturnsFalse( string $emailAddress ): void { | ||
$policyValidator = $this->newPolicyValidatorWithForbiddenEmails(); | ||
$request = ValidAddDonationRequest::getRequest(); | ||
$request->setDonorEmailAddress( $emailAddress ); | ||
|
||
$this->assertFalse( $policyValidator->isAutoDeleted( ValidAddDonationRequest::getRequest() ) ); | ||
$this->assertFalse( $policyValidator->needsModeration( $request ) ); | ||
} | ||
|
||
/** | ||
|
@@ -123,12 +125,17 @@ public static function allowedEmailAddressProvider(): array { | |
} | ||
|
||
/** @dataProvider forbiddenEmailsProvider */ | ||
public function testWhenEmailAddressIsForbidden_isAutoDeletedReturnsTrue( string $emailAddress ): void { | ||
public function testWhenEmailAddressIsOnBlockList_needsModerationReturnsTrue( string $emailAddress ): void { | ||
$policyValidator = $this->newPolicyValidatorWithForbiddenEmails(); | ||
$request = ValidAddDonationRequest::getRequest(); | ||
$request->setDonorEmailAddress( $emailAddress ); | ||
|
||
$this->assertTrue( $policyValidator->isAutoDeleted( $request ) ); | ||
$result = $policyValidator->moderateDonationRequest( $request ); | ||
|
||
$this->assertTrue( $result->needsModeration() ); | ||
$this->assertCount( 1, $result->getViolations() ); | ||
$this->assertSame( ModerationIdentifier::EMAIL_BLOCKED, $result->getViolations()[0]->getModerationIdentifier() ); | ||
$this->assertSame( AddDonationValidationResult::SOURCE_DONOR_EMAIL, $result->getViolations()[0]->getSource() ); | ||
} | ||
|
||
/** | ||
|
@@ -137,25 +144,24 @@ public function testWhenEmailAddressIsForbidden_isAutoDeletedReturnsTrue( string | |
public static function forbiddenEmailsProvider(): array { | ||
return [ | ||
[ '[email protected]' ], | ||
[ '[email protected]' ], | ||
[ '[email protected]' ] | ||
[ '[email protected]' ] | ||
]; | ||
} | ||
|
||
private function newPolicyValidatorWithForbiddenEmails(): ModerationService { | ||
return new ModerationService( | ||
$this->newSucceedingAmountValidator(), | ||
$this->newSucceedingTextPolicyValidator(), | ||
[ '/^blocked.person@bar\.baz$/', '/@example.com$/i' ] | ||
[ '[email protected]', 'foo@example.com' ] | ||
); | ||
} | ||
|
||
public function testGivenAnonymousDonorWithEmailData_itIgnoresForbiddenEmails(): void { | ||
public function testGivenAnonymousDonorWithEmailData_itDoesNotModerateEmail(): void { | ||
$policyValidator = $this->newPolicyValidatorWithForbiddenEmails(); | ||
$request = ValidAddDonationRequest::getRequest(); | ||
$request->setDonorType( DonorType::ANONYMOUS() ); | ||
$request->setDonorEmailAddress( '[email protected]' ); | ||
|
||
$this->assertFalse( $policyValidator->isAutoDeleted( $request ) ); | ||
$this->assertFalse( $policyValidator->needsModeration( $request ) ); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters