From 1be563fa40bcd696ee80c7eba5c4ac326e365af4 Mon Sep 17 00:00:00 2001 From: Marien Fressinaud Date: Thu, 22 Jun 2023 10:38:39 +0200 Subject: [PATCH] tec: Setup a default Mailbox during seeding I also changed the default SMTP address so it matches with the Mailbox address. This allows to have the same mailbox to send and receive emails. It will facilitate the process of creating/answering to tickets by email. --- .env | 4 +- docs/developers/greenmail.md | 11 ++++- env.sample | 2 +- src/Command/SeedsCommand.php | 67 +++++++++++----------------- src/Repository/MailboxRepository.php | 3 ++ tests/Command/SeedsCommandTest.php | 2 + 6 files changed, 44 insertions(+), 45 deletions(-) diff --git a/.env b/.env index 484665d1..3f78a00d 100644 --- a/.env +++ b/.env @@ -14,5 +14,5 @@ DATABASE_URL="postgresql://postgres:postgres@database:5432/bileto?serverVersion= # Configure your mail server. It is used to send email notifications to the users. # Uncomment both MAILER_* lines and set them to your needs. # More documentation at https://symfony.com/doc/7.0/mailer.html (“Transport Setup”). -MAILER_DSN=smtp://noreply%40example.com:secret@mailserver:3025 -MAILER_FROM=noreply@example.com +MAILER_DSN=smtp://support%40example.com:secret@mailserver:3025 +MAILER_FROM=support@example.com diff --git a/docs/developers/greenmail.md b/docs/developers/greenmail.md index 7314c680..7e05fbb5 100644 --- a/docs/developers/greenmail.md +++ b/docs/developers/greenmail.md @@ -21,9 +21,16 @@ Only SMTP and IMAP protocols are served as we only need these at the moment. SMTP is already configured in Bileto to use the GreenMail server. You can find the configuration in the file [`.env`](/.env). -It uses the address `noreply@example.com` to send emails. +It uses the address `support@example.com` to send emails. -You can configure a mailbox with IMAP in the interface. +When you setup the database, it also configures an IMAP Mailbox with the same address. +If it’s not, run: + +```console +$ ./docker/bin/console db:seeds:load +``` + +You can configure more mailboxes with IMAP in the interface. As an admin, go to the “Settings > Mailboxes” and create a new mailbox. Configure it with the following information: diff --git a/env.sample b/env.sample index c0502839..a734eef3 100644 --- a/env.sample +++ b/env.sample @@ -15,4 +15,4 @@ APP_SECRET=change-me # Uncomment both MAILER_* lines and set them to your needs. # More documentation at https://symfony.com/doc/7.0/mailer.html (“Transport Setup”). # MAILER_DSN=smtp://user:pass@mail.example.com:465 -# MAILER_FROM=noreply@example.com +# MAILER_FROM=support@example.com diff --git a/src/Command/SeedsCommand.php b/src/Command/SeedsCommand.php index c9e46b10..ab8dc1d9 100644 --- a/src/Command/SeedsCommand.php +++ b/src/Command/SeedsCommand.php @@ -7,11 +7,13 @@ namespace App\Command; use App\Repository\AuthorizationRepository; +use App\Repository\MailboxRepository; use App\Repository\MessageRepository; use App\Repository\OrganizationRepository; use App\Repository\RoleRepository; use App\Repository\TicketRepository; use App\Repository\UserRepository; +use App\Security\Encryptor; use App\Utils\Random; use Doctrine\ORM\EntityManagerInterface; use Symfony\Component\Console\Attribute\AsCommand; @@ -27,48 +29,19 @@ )] class SeedsCommand extends Command { - private string $environment; - - private EntityManagerInterface $entityManager; - - private AuthorizationRepository $authorizationRepository; - - private MessageRepository $messageRepository; - - private OrganizationRepository $orgaRepository; - - private RoleRepository $roleRepository; - - private TicketRepository $ticketRepository; - - private UserRepository $userRepository; - - private UserPasswordHasherInterface $passwordHasher; - public function __construct( - string $environment, - EntityManagerInterface $entityManager, - AuthorizationRepository $authorizationRepository, - MessageRepository $messageRepository, - OrganizationRepository $orgaRepository, - RoleRepository $roleRepository, - TicketRepository $ticketRepository, - UserRepository $userRepository, - UserPasswordHasherInterface $passwordHasher, + private string $environment, + private EntityManagerInterface $entityManager, + private AuthorizationRepository $authorizationRepository, + private MailboxRepository $mailboxRepository, + private MessageRepository $messageRepository, + private OrganizationRepository $orgaRepository, + private RoleRepository $roleRepository, + private TicketRepository $ticketRepository, + private UserRepository $userRepository, + private Encryptor $encryptor, + private UserPasswordHasherInterface $passwordHasher, ) { - $this->environment = $environment; - - $this->entityManager = $entityManager; - - $this->authorizationRepository = $authorizationRepository; - $this->messageRepository = $messageRepository; - $this->orgaRepository = $orgaRepository; - $this->roleRepository = $roleRepository; - $this->ticketRepository = $ticketRepository; - $this->userRepository = $userRepository; - - $this->passwordHasher = $passwordHasher; - parent::__construct(); } @@ -185,6 +158,20 @@ protected function execute(InputInterface $input, OutputInterface $output): int $this->authorizationRepository->grant($userCharlie, $roleUser, $orgaFriendlyCoorp); } + // Seed mailboxes + $this->mailboxRepository->findOneOrCreateBy([ + 'name' => 'support@example.com', + ], [ + 'host' => 'mailserver', + 'protocol' => 'imap', + 'port' => 3143, + 'encryption' => 'none', + 'username' => 'support@example.com', + 'password' => $this->encryptor->encrypt('secret'), + 'authentication' => 'normal', + 'folder' => 'INBOX', + ]); + // Seed tickets $ticketEmails = $this->ticketRepository->findOneOrCreateBy([ 'title' => 'My emails are not received', diff --git a/src/Repository/MailboxRepository.php b/src/Repository/MailboxRepository.php index 46524b16..4c18e83f 100644 --- a/src/Repository/MailboxRepository.php +++ b/src/Repository/MailboxRepository.php @@ -17,10 +17,13 @@ * @method Mailbox|null findOneBy(array $criteria, array $orderBy = null) * @method Mailbox[] findAll() * @method Mailbox[] findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null) + * + * @method Mailbox findOneOrCreateBy(array $criteria, array $valuesToCreate = [], bool $flush = false) */ class MailboxRepository extends ServiceEntityRepository implements UidGeneratorInterface { use UidGeneratorTrait; + use FindOrCreateTrait; public function __construct(ManagerRegistry $registry) { diff --git a/tests/Command/SeedsCommandTest.php b/tests/Command/SeedsCommandTest.php index ee97ee82..f8ec9d32 100644 --- a/tests/Command/SeedsCommandTest.php +++ b/tests/Command/SeedsCommandTest.php @@ -7,6 +7,7 @@ namespace App\Tests\Command; use App\Tests\CommandTestsHelper; +use App\Tests\Factory\MailboxFactory; use App\Tests\Factory\OrganizationFactory; use App\Tests\Factory\RoleFactory; use App\Tests\Factory\UserFactory; @@ -29,5 +30,6 @@ public function testExecute(): void $this->assertSame(3, RoleFactory::count()); $this->assertSame(4, OrganizationFactory::count()); $this->assertSame(3, UserFactory::count()); + $this->assertSame(1, MailboxFactory::count()); } }