mirror of
https://github.com/simple-login/app.git
synced 2025-02-25 00:03:03 +08:00
Add decode_text()
This commit is contained in:
parent
efc6b32ce0
commit
091ff3ad2c
2 changed files with 39 additions and 0 deletions
|
@ -746,6 +746,17 @@ def encode_text(text: str, encoding: EmailEncoding = EmailEncoding.NO) -> str:
|
|||
return text
|
||||
|
||||
|
||||
def decode_text(text: str, encoding: EmailEncoding = EmailEncoding.NO) -> str:
|
||||
if encoding == EmailEncoding.QUOTED:
|
||||
decoded = quopri.decodestring(text.encode("utf-8"))
|
||||
return str(decoded, "utf-8")
|
||||
elif encoding == EmailEncoding.BASE64:
|
||||
decoded = base64.b64decode(text.encode("utf-8"))
|
||||
return str(decoded, "utf-8")
|
||||
else: # 7bit - no encoding
|
||||
return text
|
||||
|
||||
|
||||
def add_header(msg: Message, text_header, html_header) -> Message:
|
||||
if msg.get_content_type() == "text/plain":
|
||||
encoding = get_encoding(msg)
|
||||
|
|
|
@ -23,6 +23,7 @@ from app.email_utils import (
|
|||
EmailEncoding,
|
||||
replace,
|
||||
should_disable,
|
||||
decode_text,
|
||||
)
|
||||
from app.extensions import db
|
||||
from app.models import User, CustomDomain, Alias, Contact, EmailLog
|
||||
|
@ -554,6 +555,33 @@ def test_encode_text():
|
|||
assert encode_text("mèo méo", EmailEncoding.QUOTED) == "m=C3=A8o m=C3=A9o"
|
||||
|
||||
|
||||
def test_decode_text():
|
||||
assert decode_text("") == ""
|
||||
assert decode_text("ascii") == "ascii"
|
||||
|
||||
assert (
|
||||
decode_text(encode_text("ascii", EmailEncoding.BASE64), EmailEncoding.BASE64)
|
||||
== "ascii"
|
||||
)
|
||||
assert (
|
||||
decode_text(
|
||||
encode_text("mèo méo 🇪🇺", EmailEncoding.BASE64), EmailEncoding.BASE64
|
||||
)
|
||||
== "mèo méo 🇪🇺"
|
||||
)
|
||||
|
||||
assert (
|
||||
decode_text(encode_text("ascii", EmailEncoding.QUOTED), EmailEncoding.QUOTED)
|
||||
== "ascii"
|
||||
)
|
||||
assert (
|
||||
decode_text(
|
||||
encode_text("mèo méo 🇪🇺", EmailEncoding.QUOTED), EmailEncoding.QUOTED
|
||||
)
|
||||
== "mèo méo 🇪🇺"
|
||||
)
|
||||
|
||||
|
||||
def test_should_disable(flask_client):
|
||||
user = User.create(
|
||||
email="a@b.c",
|
||||
|
|
Loading…
Reference in a new issue