def test_invalid_receiver_some(self) -> None: """Mail sent, invalid recipients in response""" EmailSettings.connection_security = "NONE" EmailSettings.port = 25 handler = AIOHandler() with AiosmtpdServerManager(handler): with EmailMixin.get_mail_connection() as mail_client: receivers = self.receivers + ["invalidQexample1.com"] response = EmailMixin.send_email( mail_client, self.sender, receivers, subject="A mail from intevation-test: plain text", content= "Hi you\r\nThis is a nice content line with only plain text!", html=False, ) self.assertEqual( response.get("invalidQexample1.com", tuple()), (550, b"invalid eMail address"), ) self.assertEqual("250 Message accepted for delivery", handler.ret_status) self.assertEqual(len(handler.emails), 1) self.assertEqual(handler.emails[0]["from"], self.sender) self.assertEqual(handler.emails[0]["to"], self.receivers) self.assertNotEqual(handler.emails[0]["to"], receivers)
def test_sender_with_name(self) -> None: EmailSettings.connection_security = "NONE" EmailSettings.port = 25 sender = Address("Name of sender", addr_spec=self.sender) handler = AIOHandler() with AiosmtpdServerManager(handler): with EmailMixin.get_mail_connection() as mail_client: response = EmailMixin.send_email( mail_client, sender, self.receivers, subject="Test-email", content="Hi\r\nThis is some plain text content!", html=False, ) self.assertEqual(len(response), 0) self.assertEqual("250 Message accepted for delivery", handler.ret_status) self.assertEqual(len(handler.emails), 1) self.assertEqual(handler.emails[0]["from"], self.sender) self.assertEqual(handler.emails[0]["to"], self.receivers) self.assertIn( f"From: Name of sender <{self.sender}>", handler.emails[0]["data"], ) self.assertNotIn( 'Content-Type: text/html; charset="utf-8"', handler.emails[0]["data"], )
def test_forget_password_no_user_found(self) -> None: self.set_models({"organization/1": {"url": None}}) handler = AIOHandler() with AiosmtpdServerManager(handler): response = self.request("user.forget_password", {"email": "*****@*****.**"}) self.assert_status_code(response, 200) assert not handler.emails
def test_connection_wrong_port(self) -> None: """Server started on 25, client tries on 26""" EmailSettings.connection_security = "NONE" EmailSettings.port = 25 handler = AIOHandler() with AiosmtpdServerManager(handler): EmailSettings.port = 26 with pytest.raises(ConnectionRefusedError): EmailMixin.get_mail_connection().__enter__()
def test_self_signed_not_accepted(self) -> None: EmailSettings.connection_security = "STARTTLS" EmailSettings.port = 587 EmailSettings.accept_self_signed_certificate = False with AiosmtpdServerManager(AIOHandler()): with pytest.raises( ssl.SSLCertVerificationError, match="certificate verify failed: self signed certificate", ): EmailMixin.get_mail_connection().__enter__()
def test_connection_interrupted(self) -> None: EmailSettings.connection_security = "NONE" handler = AIOHandler() with AiosmtpdServerManager(handler) as server: with EmailMixin.get_mail_connection() as mail_client: with pytest.raises(smtplib.SMTPServerDisconnected): server.stop() EmailMixin.send_email( mail_client, self.sender, self.receivers, subject="Test-email", content="Hi\r\nThis is some plain text content!", html=False, )
def test_authentication_wrong_credentials(self) -> None: EmailSettings.connection_security = "NONE" EmailSettings.port = 25 EmailSettings.user = self.sender EmailSettings.password = "******" handler = AIOHandler() with AiosmtpdServerManager(handler, auth=True): with pytest.raises(smtplib.SMTPAuthenticationError) as e: with EmailMixin.get_mail_connection(): pass self.assertEqual(e.value.smtp_code, 535) self.assertEqual(e.value.smtp_error, b"5.7.8 Authentication credentials invalid") self.assertEqual(len(handler.emails), 0)
def test_forget_password_two_users_with_email(self) -> None: self.set_models({ "organization/1": { "url": "https://openslides.example.com" }, "user/1": { "email": "*****@*****.**" }, "user/2": { "email": "*****@*****.**", "username": "******" }, "user/3": { "email": "*****@*****.**", "username": "******" }, }) start_time = int(time()) handler = AIOHandler() with AiosmtpdServerManager(handler): response = self.request("user.forget_password", {"email": "*****@*****.**"}) self.assert_status_code(response, 200) user = self.get_model("user/1") assert user.get("last_email_send", 0) >= start_time user2 = self.get_model("user/2") assert user2.get("last_email_send", 0) >= start_time user3 = self.get_model("user/3") assert user3.get("last_email_send", 0) == 0 assert handler.emails[0]["from"] == EmailSettings.default_from_email assert handler.emails[0]["to"][0] == "*****@*****.**" assert ("For completeness your username: admin" in handler.emails[0]["data"] or "For completeness your username: test2" in handler.emails[0]["data"]) assert "https://openslides.example.com" in handler.emails[0]["data"] assert handler.emails[1]["from"] == EmailSettings.default_from_email assert handler.emails[1]["to"][0] == "*****@*****.**" assert ("For completeness your username: test2" in handler.emails[1]["data"] or "For completeness your username: admin" in handler.emails[1]["data"]) assert "https://openslides.example.com" in handler.emails[1]["data"]
def test_forget_password_send_mail_correct(self) -> None: self.set_models({ "organization/1": { "url": None }, "user/1": { "email": "*****@*****.**" } }) start_time = int(time()) handler = AIOHandler() with AiosmtpdServerManager(handler): response = self.request("user.forget_password", {"email": "*****@*****.**"}) self.assert_status_code(response, 200) user = self.get_model("user/1") assert user.get("last_email_send", 0) >= start_time assert handler.emails[0]["from"] == EmailSettings.default_from_email assert "Reset your OpenSlides password" in handler.emails[0]["data"]
def test_forget_password_wrong_email_password(self) -> None: EmailSettings.password = "******" EmailSettings.user = "******" self.set_models({ "organization/1": { "url": None }, "user/1": { "email": "*****@*****.**" } }) handler = AIOHandler() with AiosmtpdServerManager(handler): response = self.request("user.forget_password", {"email": "*****@*****.**"}) self.assert_status_code(response, 400) assert ( "The server was configured improperly. Please contact your administrator." in response.json["message"])
def test_authentication_not_authenticated(self) -> None: EmailSettings.connection_security = "NONE" EmailSettings.port = 25 handler = AIOHandler() with AiosmtpdServerManager(handler, auth=True): with EmailMixin.get_mail_connection() as mail_client: with pytest.raises(smtplib.SMTPSenderRefused) as e: EmailMixin.send_email( mail_client, self.sender, self.receivers, subject="Test-email", content="Hi\r\nThis is some plain text content!", html=False, ) self.assertEqual(e.value.sender, "*****@*****.**") self.assertEqual(e.value.smtp_code, 530) self.assertEqual(e.value.smtp_error, b"5.7.0 Authentication required") self.assertEqual(len(handler.emails), 0)
def test_authentication_no_encryption(self) -> None: EmailSettings.connection_security = "NONE" EmailSettings.port = 25 EmailSettings.user = self.sender EmailSettings.password = self.password handler = AIOHandler() with AiosmtpdServerManager(handler, auth=True): with EmailMixin.get_mail_connection() as mail_client: response = EmailMixin.send_email( mail_client, self.sender, self.receivers, subject="Test-email", content="Hi\r\nThis is some plain text content!", html=False, ) self.assertEqual(len(response), 0) self.assertEqual("250 Message accepted for delivery", handler.ret_status) self.assertEqual(len(handler.emails), 1)
def test_send_html_email(self) -> None: EmailSettings.connection_security = "NONE" EmailSettings.port = 25 handler = AIOHandler() with AiosmtpdServerManager(handler): with EmailMixin.get_mail_connection() as mail_client: response = EmailMixin.send_email( mail_client, self.sender, self.receivers, subject= "A mail from intevation-test: html with generated plain", content=""" <html> <body> <p>Hello dear customer,<br> really nice to meet <strong>you</strong> in html with a <strong>strong you</strong></p> <p>Besides the HTML there is also an auto-generated plain text version</p> </body> </html> """, ) self.assertEqual(len(response), 0) self.assertEqual("250 Message accepted for delivery", handler.ret_status) self.assertEqual(len(handler.emails), 1) self.assertEqual(handler.emails[0]["from"], self.sender) self.assertEqual(handler.emails[0]["to"], self.receivers) self.assertIn( 'Content-Type: text/plain; charset="utf-8"', handler.emails[0]["data"], ) self.assertIn( 'Content-Type: text/html; charset="utf-8"', handler.emails[0]["data"], )
def test_send_starttls(self) -> None: EmailSettings.connection_security = "STARTTLS" EmailSettings.accept_self_signed_certificate = True EmailSettings.port = 587 handler = AIOHandler() with AiosmtpdServerManager(handler): with EmailMixin.get_mail_connection() as mail_client: response = EmailMixin.send_email( mail_client, self.sender, self.receivers, subject="Test-email", content="Hi\r\nThis is some plain text content!", html=False, ) self.assertEqual(len(response), 0) self.assertEqual("250 Message accepted for delivery", handler.ret_status) self.assertEqual(len(handler.emails), 1) self.assertEqual(handler.emails[0]["from"], self.sender) self.assertEqual(handler.emails[0]["to"], self.receivers) self.assertIn("Hi\r\nThis is some plain text content!", handler.emails[0]["data"])