Initial commit: CloudOps infrastructure platform
This commit is contained in:
@@ -0,0 +1,56 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Mautic\UserBundle\Tests\Model;
|
||||
|
||||
use Mautic\CoreBundle\Test\MauticMysqlTestCase;
|
||||
use Mautic\UserBundle\Entity\Role;
|
||||
use Mautic\UserBundle\Entity\RoleRepository;
|
||||
use Mautic\UserBundle\Entity\User;
|
||||
use Mautic\UserBundle\Form\Validator\Constraints\NotWeak;
|
||||
use PHPUnit\Framework\Assert;
|
||||
use Symfony\Component\PasswordHasher\Hasher\PasswordHasherFactoryInterface;
|
||||
use Symfony\Component\Validator\ConstraintViolation;
|
||||
use Symfony\Component\Validator\Validator\ValidatorInterface;
|
||||
|
||||
final class PasswordStrengthEstimatorModelTest extends MauticMysqlTestCase
|
||||
{
|
||||
private PasswordHasherFactoryInterface $passwordHasher;
|
||||
|
||||
private RoleRepository $roleRepository;
|
||||
|
||||
private ValidatorInterface $validator;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
$this->passwordHasher = self::getContainer()->get('security.password_hasher_factory');
|
||||
$this->roleRepository = $this->em->getRepository(Role::class);
|
||||
$this->validator = static::getContainer()->get('validator');
|
||||
}
|
||||
|
||||
public function testThatItIsNotPossibleToCreateAnUserWithAWeakPassword(): void
|
||||
{
|
||||
$simplePassword = '11111111';
|
||||
|
||||
$user = new User();
|
||||
$user->setFirstName('First Name');
|
||||
$user->setLastName('LastName');
|
||||
$user->setUsername('username');
|
||||
$user->setEmail('some@email.domain');
|
||||
$user->setPlainPassword($simplePassword);
|
||||
$user->setPassword($this->passwordHasher->getPasswordHasher($user)->hash($simplePassword));
|
||||
$user->setRole($this->roleRepository->findAll()[0]);
|
||||
$violations = $this->validator->validate($user);
|
||||
$hasNotWeakConstraintViolation = false;
|
||||
|
||||
/** @var ConstraintViolation $violation */
|
||||
foreach ($violations as $violation) {
|
||||
$hasNotWeakConstraintViolation |= $violation->getConstraint() instanceof NotWeak;
|
||||
}
|
||||
|
||||
Assert::assertGreaterThanOrEqual(1, count($violations));
|
||||
Assert::assertTrue((bool) $hasNotWeakConstraintViolation);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,191 @@
|
||||
<?php
|
||||
|
||||
namespace Mautic\UserBundle\Tests\Model;
|
||||
|
||||
use Doctrine\ORM\EntityManager;
|
||||
use Mautic\CoreBundle\Helper\CoreParametersHelper;
|
||||
use Mautic\CoreBundle\Helper\UserHelper;
|
||||
use Mautic\CoreBundle\Security\Permissions\CorePermissions;
|
||||
use Mautic\CoreBundle\Translation\Translator;
|
||||
use Mautic\EmailBundle\Helper\MailHelper;
|
||||
use Mautic\UserBundle\Entity\User;
|
||||
use Mautic\UserBundle\Entity\UserToken;
|
||||
use Mautic\UserBundle\Model\UserModel;
|
||||
use Mautic\UserBundle\Model\UserToken\UserTokenServiceInterface;
|
||||
use PHPUnit\Framework\MockObject\MockObject;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Psr\Log\LoggerInterface;
|
||||
use Symfony\Bundle\FrameworkBundle\Routing\Router;
|
||||
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
|
||||
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
|
||||
use Symfony\Contracts\Translation\TranslatorInterface;
|
||||
|
||||
class UserModelTest extends TestCase
|
||||
{
|
||||
private UserModel $userModel;
|
||||
|
||||
/**
|
||||
* @var MockObject&MailHelper
|
||||
*/
|
||||
private MockObject $mailHelper;
|
||||
|
||||
/**
|
||||
* @var MockObject&EntityManager
|
||||
*/
|
||||
private MockObject $entityManager;
|
||||
|
||||
/**
|
||||
* @var MockObject&Router
|
||||
*/
|
||||
private MockObject $router;
|
||||
|
||||
/**
|
||||
* @var MockObject&TranslatorInterface
|
||||
*/
|
||||
private MockObject $translator;
|
||||
|
||||
/**
|
||||
* @var MockObject&User
|
||||
*/
|
||||
private MockObject $user;
|
||||
|
||||
/**
|
||||
* @var MockObject&UserToken
|
||||
*/
|
||||
private MockObject $userToken;
|
||||
|
||||
/**
|
||||
* @var MockObject&UserTokenServiceInterface
|
||||
*/
|
||||
private MockObject $userTokenService;
|
||||
|
||||
/**
|
||||
* @var MockObject&LoggerInterface
|
||||
*/
|
||||
private MockObject $logger;
|
||||
|
||||
public function setUp(): void
|
||||
{
|
||||
$this->mailHelper = $this->createMock(MailHelper::class);
|
||||
$this->userTokenService = $this->createMock(UserTokenServiceInterface::class);
|
||||
$this->entityManager = $this->createMock(EntityManager::class);
|
||||
$this->user = $this->createMock(User::class);
|
||||
$this->router = $this->createMock(Router::class);
|
||||
$this->translator = $this->createMock(Translator::class);
|
||||
$this->userToken = $this->createMock(UserToken::class);
|
||||
$this->logger = $this->createMock(LoggerInterface::class);
|
||||
|
||||
$this->userModel = new UserModel(
|
||||
$this->mailHelper,
|
||||
$this->userTokenService,
|
||||
$this->entityManager,
|
||||
$this->createMock(CorePermissions::class),
|
||||
$this->createMock(EventDispatcherInterface::class),
|
||||
$this->router,
|
||||
$this->translator,
|
||||
$this->createMock(UserHelper::class),
|
||||
$this->logger,
|
||||
$this->createMock(CoreParametersHelper::class)
|
||||
);
|
||||
}
|
||||
|
||||
public function testThatItSendsResetPasswordEmailAndRouterGetsCalledWithCorrectParamters(): void
|
||||
{
|
||||
$this->userTokenService->expects($this->once())
|
||||
->method('generateSecret')
|
||||
->willReturn($this->userToken);
|
||||
|
||||
$this->mailHelper
|
||||
->method('getMailer')
|
||||
->willReturn($this->mailHelper);
|
||||
|
||||
$this->mailHelper->expects($this->once())
|
||||
->method('send');
|
||||
|
||||
$this->userTokenService->expects($this->once())
|
||||
->method('generateSecret')
|
||||
->willReturn($this->userToken);
|
||||
|
||||
$this->router->expects($this->once())
|
||||
->method('generate')
|
||||
->with('mautic_user_passwordresetconfirm', ['token' => null], UrlGeneratorInterface::ABSOLUTE_URL);
|
||||
|
||||
$this->translator
|
||||
->expects($this->any())
|
||||
->method('trans')
|
||||
->willReturn('test');
|
||||
|
||||
$this->userModel->sendResetEmail($this->user);
|
||||
}
|
||||
|
||||
public function testThatDatabaseErrorThrowsRuntimeExceptionAndItIsLoggedWhenWeTryToSaveTokenToTheDatabaseWhenWeSendResetPasswordEmail(): void
|
||||
{
|
||||
$errorMessage = 'Some error message';
|
||||
|
||||
$this->expectException(\RuntimeException::class);
|
||||
|
||||
$this->entityManager->expects($this->once())
|
||||
->method('flush')
|
||||
->willThrowException(new \Exception($errorMessage));
|
||||
|
||||
$this->logger->expects($this->once())
|
||||
->method('error')
|
||||
->with($errorMessage);
|
||||
|
||||
$this->userModel->sendResetEmail($this->user);
|
||||
}
|
||||
|
||||
public function testEmailUser(): void
|
||||
{
|
||||
$email = 'a@test.com';
|
||||
$name = 'name';
|
||||
$toMail = [$email => $name];
|
||||
$subject = 'subject';
|
||||
$content = 'content';
|
||||
|
||||
$this->user->expects($this->once())
|
||||
->method('getEmail')
|
||||
->willReturn($email);
|
||||
|
||||
$this->user->expects($this->once())
|
||||
->method('getName')
|
||||
->willReturn($name);
|
||||
|
||||
$this->mailHelper->expects($this->once())
|
||||
->method('getMailer')
|
||||
->willReturn($this->mailHelper);
|
||||
|
||||
$this->mailHelper->expects($this->once())
|
||||
->method('setTo')
|
||||
->with($toMail)
|
||||
->willReturn(true);
|
||||
|
||||
$this->mailHelper->expects($this->once())
|
||||
->method('send');
|
||||
|
||||
// Means no erros.
|
||||
$this->userModel->emailUser($this->user, $subject, $content);
|
||||
}
|
||||
|
||||
public function testSendMailToEmailAddresses(): void
|
||||
{
|
||||
$toMails = ['a@test.com', 'b@test.com'];
|
||||
$subject = 'subject';
|
||||
$content = 'content';
|
||||
|
||||
$this->mailHelper->expects($this->once())
|
||||
->method('getMailer')
|
||||
->willReturn($this->mailHelper);
|
||||
|
||||
$this->mailHelper->expects($this->once())
|
||||
->method('setTo')
|
||||
->with($toMails)
|
||||
->willReturn(true);
|
||||
|
||||
$this->mailHelper->expects($this->once())
|
||||
->method('send');
|
||||
|
||||
// Means no erros.
|
||||
$this->userModel->sendMailToEmailAddresses($toMails, $subject, $content);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,87 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Mautic\UserBundle\Tests\Model\UserToken;
|
||||
|
||||
use Mautic\CoreBundle\Helper\RandomHelper\RandomHelperInterface;
|
||||
use Mautic\UserBundle\Entity\User;
|
||||
use Mautic\UserBundle\Entity\UserToken;
|
||||
use Mautic\UserBundle\Entity\UserTokenRepositoryInterface;
|
||||
use Mautic\UserBundle\Model\UserToken\UserTokenService;
|
||||
use PHPUnit\Framework\MockObject\MockObject;
|
||||
|
||||
class UserTokenServiceTest extends \PHPUnit\Framework\TestCase
|
||||
{
|
||||
/**
|
||||
* @var MockObject|RandomHelperInterface
|
||||
*/
|
||||
private MockObject $randomHelperMock;
|
||||
|
||||
/**
|
||||
* @var MockObject|UserTokenRepositoryInterface
|
||||
*/
|
||||
private MockObject $userTokenRepositoryMock;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->randomHelperMock = $this->createMock(RandomHelperInterface::class);
|
||||
$this->userTokenRepositoryMock = $this->createMock(UserTokenRepositoryInterface::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests second attempt for generating secret if not unique secret was generated first time.
|
||||
*/
|
||||
public function testGenerateSecret(): void
|
||||
{
|
||||
$secretLength = 6;
|
||||
$randomSecret = 'secret';
|
||||
$token = new UserToken();
|
||||
$token->setAuthorizator('test-secret');
|
||||
|
||||
$this->randomHelperMock->expects($this->exactly(2))
|
||||
->method('generate')
|
||||
->with($secretLength)
|
||||
->willReturn($randomSecret);
|
||||
|
||||
$this->userTokenRepositoryMock->expects($this->exactly(2))
|
||||
->method('isSecretUnique')
|
||||
->with($randomSecret)
|
||||
->willReturnOnConsecutiveCalls(
|
||||
false, // Test second attempt to get unique secret
|
||||
true // Ok now
|
||||
);
|
||||
|
||||
$userTokenService = $this->getUserTokenService();
|
||||
$secretToken = $userTokenService->generateSecret($token, $secretLength);
|
||||
$this->assertSame($randomSecret, $secretToken->getSecret());
|
||||
$this->assertTrue($secretToken->isOneTimeOnly());
|
||||
$this->assertNull($secretToken->getExpiration());
|
||||
}
|
||||
|
||||
public function testVerify(): void
|
||||
{
|
||||
$token = new UserToken();
|
||||
$user = new User();
|
||||
$authorizator = 'authorizator';
|
||||
$token->setUser($user)
|
||||
->setOneTimeOnly(true)
|
||||
->setExpiration(null)
|
||||
->setAuthorizator($authorizator);
|
||||
|
||||
$this->userTokenRepositoryMock->expects($this->once())
|
||||
->method('verify')
|
||||
->with($token)
|
||||
->willReturn(true);
|
||||
|
||||
$this->assertTrue($this->getUserTokenService()->verify($token));
|
||||
}
|
||||
|
||||
private function getUserTokenService(): UserTokenService
|
||||
{
|
||||
return new UserTokenService(
|
||||
$this->randomHelperMock,
|
||||
$this->userTokenRepositoryMock
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user