From b993b5f8933c4e7880bd4fb12683b7ddc2ef54b4 Mon Sep 17 00:00:00 2001
From: Camille Simiand <camille.simiand@tetras-libre.fr>
Date: Mon, 13 Dec 2021 12:11:50 +0100
Subject: [PATCH] Add email tests

---
 .../ResetPasswordControllerTest.php           | 52 +++++++++++++++++++
 1 file changed, 52 insertions(+)
 create mode 100644 tests/functional/ResetPasswordControllerTest.php

diff --git a/tests/functional/ResetPasswordControllerTest.php b/tests/functional/ResetPasswordControllerTest.php
new file mode 100644
index 0000000..0487400
--- /dev/null
+++ b/tests/functional/ResetPasswordControllerTest.php
@@ -0,0 +1,52 @@
+<?php
+
+namespace App\Tests\Controller;
+
+use Symfony\Bundle\FrameworkBundle\KernelBrowser;
+use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
+use Symfony\Component\DomCrawler\Form;
+
+class ResetPasswordControllerTest extends WebTestCase
+{
+    private KernelBrowser $client;
+    private Form $form;
+    private const VERIFIED_USER_EMAIL = 'defaultUser@localhost.com';
+    private const UNVERIFIED_USER_EMAIL = 'notRegistered@localhost.com';
+
+    protected function setUp(): void
+    {
+        $this->client = static::createClient();
+        $crawler = $this->client->request('GET', '/reset-password');
+        $this->assertResponseIsSuccessful();
+
+        $this->client->enableProfiler();
+
+        $submit_button = $crawler->selectButton('Reset password');
+        $this->form = $submit_button->form();
+    }
+
+    public function testVerifiedUserShouldReceiveAnEmailLinkToResetHisPassword(): void
+    {
+        $this->form['reset_password_request_form[email]'] = self::VERIFIED_USER_EMAIL;
+        $this->client->submit($this->form);
+
+        $emailMessage = $this->getMailerMessage(0);
+
+        $this->assertEmailCount(1, null);
+        $this->assertEmailTextBodyContains($emailMessage, 'To reset your password, please visit the following link');
+        $this->assertEmailAddressContains(
+            $emailMessage,
+            'To',
+            self::VERIFIED_USER_EMAIL,
+            'When resetting a password, an already registered user should receive an email with the reset password link'
+        );
+    }
+
+    public function testUnverifiedUserShouldNotReceiveAnEmailLinkToResetHisPassword(): void
+    {
+        $this->form['reset_password_request_form[email]'] = self::UNVERIFIED_USER_EMAIL;
+        $this->client->submit($this->form);
+
+        $this->assertEmailCount(0);
+    }
+}
-- 
GitLab