From 4dc36cceec768774e08399e596570b26f5fb8548 Mon Sep 17 00:00:00 2001 From: Sam Date: Mon, 2 Oct 2023 10:56:20 +0200 Subject: [PATCH] API - handle encoded password in EMAIL_URL --- fittrackee/emails/email.py | 5 +++- fittrackee/tests/emails/test_email_service.py | 23 +++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/fittrackee/emails/email.py b/fittrackee/emails/email.py index 70b00f5e..5a6ef3be 100644 --- a/fittrackee/emails/email.py +++ b/fittrackee/emails/email.py @@ -4,6 +4,7 @@ import ssl from email.mime.multipart import MIMEMultipart from email.mime.text import MIMEText from typing import Dict, List, Optional, Type, Union +from urllib.parse import unquote from babel.support import Translations from flask import Flask @@ -142,7 +143,9 @@ class EmailService: 'use_tls': True if parsed_url.query == 'tls=True' else False, 'use_ssl': True if parsed_url.query == 'ssl=True' else False, 'username': credentials[0], - 'password': credentials[1], + 'password': ( + None if credentials[1] is None else unquote(credentials[1]) + ), } @property diff --git a/fittrackee/tests/emails/test_email_service.py b/fittrackee/tests/emails/test_email_service.py index 25b347b6..7b0f0de7 100644 --- a/fittrackee/tests/emails/test_email_service.py +++ b/fittrackee/tests/emails/test_email_service.py @@ -1,4 +1,5 @@ from unittest.mock import Mock, patch +from urllib.parse import quote import pytest from flask import Flask @@ -43,6 +44,7 @@ class TestEmailServiceUrlParser(CallArgsMixin): @staticmethod def assert_parsed_email(url: str) -> None: parsed_email = email_service.parse_email_url(url) + assert parsed_email['username'] is None assert parsed_email['password'] is None assert parsed_email['host'] == 'localhost' @@ -60,7 +62,9 @@ class TestEmailServiceUrlParser(CallArgsMixin): def test_it_parses_email_url(self) -> None: url = 'smtp://test@example.com:12345678@localhost:25' + parsed_email = email_service.parse_email_url(url) + assert parsed_email['username'] == 'test@example.com' assert parsed_email['password'] == '12345678' assert parsed_email['host'] == 'localhost' @@ -70,7 +74,9 @@ class TestEmailServiceUrlParser(CallArgsMixin): def test_it_parses_email_url_with_tls(self) -> None: url = 'smtp://test@example.com:12345678@localhost:587?tls=True' + parsed_email = email_service.parse_email_url(url) + assert parsed_email['username'] == 'test@example.com' assert parsed_email['password'] == '12345678' assert parsed_email['host'] == 'localhost' @@ -80,7 +86,9 @@ class TestEmailServiceUrlParser(CallArgsMixin): def test_it_parses_email_url_with_ssl(self) -> None: url = 'smtp://test@example.com:12345678@localhost:465?ssl=True' + parsed_email = email_service.parse_email_url(url) + assert parsed_email['username'] == 'test@example.com' assert parsed_email['password'] == '12345678' assert parsed_email['host'] == 'localhost' @@ -88,6 +96,21 @@ class TestEmailServiceUrlParser(CallArgsMixin): assert parsed_email['use_tls'] is False assert parsed_email['use_ssl'] is True + def test_it_parses_email_url_with_encoded_password(self) -> None: + username = "user_name@example.com" + password = "passwordWith@And&And?" + encoded_password = quote(password) + url = f"smtp://{username}:{encoded_password}@localhost:465?ssl=True" + + parsed_email = email_service.parse_email_url(url) + + assert parsed_email['username'] == username + assert parsed_email['password'] == password + assert parsed_email['host'] == 'localhost' + assert parsed_email['port'] == 465 + assert parsed_email['use_tls'] is False + assert parsed_email['use_ssl'] is True + class TestEmailServiceSend(CallArgsMixin): email_data = {