From 73f0427d58ebad437aecb8a0b47f8748b80a3f28 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Laur=C3=A9line=20Gu=C3=A9rin?= <zebuline@entrouvert.com>
Date: Tue, 21 Sep 2021 10:32:20 +0200
Subject: [PATCH 1/4] misc: split emails tests (#36977)

---
 tests/test_emails.py | 221 +++++++++++++++++++++++++++++++++++++++++++
 tests/test_misc.py   | 206 ----------------------------------------
 2 files changed, 221 insertions(+), 206 deletions(-)
 create mode 100644 tests/test_emails.py

diff --git a/tests/test_emails.py b/tests/test_emails.py
new file mode 100644
index 00000000..fae246d1
--- /dev/null
+++ b/tests/test_emails.py
@@ -0,0 +1,221 @@
+import base64
+import os
+import pwd
+import socket
+
+import pytest
+from quixote import cleanup
+
+from wcs.qommon.emails import docutils  # noqa pylint: disable=unused-import
+from wcs.qommon.emails import email as send_email
+from wcs.qommon.upload_storage import PicklableUpload
+
+from .utilities import clean_temporary_pub, create_temporary_pub
+
+
+def setup_module(module):
+    cleanup()
+
+
+def teardown_module(module):
+    clean_temporary_pub()
+
+
+def test_email_signature_plain(emails):
+    pub = create_temporary_pub()
+    pub.cfg['emails'] = {'footer': 'Footer\nText'}
+    send_email('test', mail_body='Hello', email_rcpt='test@localhost', want_html=False)
+    assert emails.count() == 1
+    assert not emails.emails['test']['msg'].is_multipart()
+    assert b'Footer\nText' in emails.emails['test']['msg'].get_payload(decode=True)
+
+
+def test_email_from(emails):
+    pub = create_temporary_pub()
+    send_email('test', mail_body='Hello', email_rcpt='test@localhost', want_html=False)
+    assert emails.count() == 1
+    assert emails.emails['test']['from'] == '%s@%s' % (pwd.getpwuid(os.getuid())[0], socket.getfqdn())
+
+    pub.cfg['emails'] = {'from': 'foo@localhost'}
+    send_email('test', mail_body='Hello', email_rcpt='test@localhost', want_html=False)
+    assert emails.count() == 1
+    assert emails.emails['test']['from'] == 'foo@localhost'
+    assert emails.emails['test']['msg']['From'] == 'foo@localhost'
+
+    if not pub.site_options.has_section('variables'):
+        pub.site_options.add_section('variables')
+    pub.site_options.set('variables', 'global_title', 'HELLO')
+    send_email('test', mail_body='Hello', email_rcpt='test@localhost', want_html=False)
+    assert emails.count() == 1
+    assert emails.emails['test']['from'] == 'foo@localhost'
+    assert emails.emails['test']['msg']['From'] in (
+        '=?utf-8?q?HELLO?= <foo@localhost>',
+        'HELLO <foo@localhost>',
+    )
+
+
+@pytest.mark.skipif('docutils is None')
+def test_email_signature_rst(emails):
+    pub = create_temporary_pub()
+    pub.cfg['emails'] = {'footer': 'Footer\nText'}
+    send_email('test', mail_body='Hello', email_rcpt='test@localhost')
+    assert emails.count() == 1
+    assert emails.emails['test']['msg'].is_multipart()
+    assert emails.emails['test']['msg'].get_content_subtype() == 'alternative'
+    assert emails.emails['test']['msg'].get_payload()[0].get_content_type() == 'text/plain'
+    assert emails.emails['test']['msg'].get_payload()[1].get_content_type() == 'text/html'
+    assert b'Footer\nText' in emails.emails['test']['msg'].get_payload()[0].get_payload(decode=True)
+    assert b'>Footer<' in emails.emails['test']['msg'].get_payload()[1].get_payload(decode=True)
+
+
+@pytest.mark.skipif('docutils is None')
+def test_email_signature_rst_pipes(emails):
+    pub = create_temporary_pub()
+    pub.cfg['emails'] = {'footer': '| Footer\n| Text'}
+    send_email('test', mail_body='Hello', email_rcpt='test@localhost')
+    assert emails.count() == 1
+    assert emails.emails['test']['msg'].is_multipart()
+    assert emails.emails['test']['msg'].get_content_subtype() == 'alternative'
+    assert emails.emails['test']['msg'].get_payload()[0].get_content_type() == 'text/plain'
+    assert emails.emails['test']['msg'].get_payload()[1].get_content_type() == 'text/html'
+    assert b'Footer\nText' in emails.emails['test']['msg'].get_payload()[0].get_payload(decode=True)
+    assert b'>Footer<' in emails.emails['test']['msg'].get_payload()[1].get_payload(decode=True)
+
+
+def test_email_plain_with_attachments(emails):
+    create_temporary_pub()
+
+    jpg = PicklableUpload('test.jpeg', 'image/jpeg')
+    with open(os.path.join(os.path.dirname(__file__), 'image-with-gps-data.jpeg'), 'rb') as fd:
+        jpg_content = fd.read()
+    jpg.receive([jpg_content])
+    txt = PicklableUpload('test.txt', 'text/plain')
+    txt.receive([b'foo-text-bar'])
+    odt = PicklableUpload('test.odt', 'application/vnd.oasis.opendocument.text')
+    with open(os.path.join(os.path.dirname(__file__), 'template.odt'), 'rb') as fd:
+        odt_content = fd.read()
+    odt.receive([odt_content])
+
+    send_email('jpg', mail_body='Hello', email_rcpt='test@localhost', want_html=False, attachments=[jpg])
+    assert emails.count() == 1
+    assert emails.emails['jpg']['msg'].is_multipart()
+    assert emails.emails['jpg']['msg'].get_content_subtype() == 'mixed'
+    assert emails.emails['jpg']['msg'].get_payload()[0].get_content_type() == 'text/plain'
+    assert emails.emails['jpg']['msg'].get_payload()[1].get_content_type() == 'image/jpeg'
+    assert base64.b64decode(emails.emails['jpg']['msg'].get_payload()[1].get_payload()) == jpg_content
+
+    send_email('txt', mail_body='Hello', email_rcpt='test@localhost', want_html=False, attachments=[txt])
+    assert emails.emails['txt']['msg'].is_multipart()
+    assert emails.emails['txt']['msg'].get_content_subtype() == 'mixed'
+    assert emails.emails['txt']['msg'].get_payload()[0].get_content_type() == 'text/plain'
+    assert emails.emails['txt']['msg'].get_payload()[1].get_content_type() == 'text/plain'
+    assert emails.emails['txt']['msg'].get_payload()[1].get_payload(decode=True) == b'foo-text-bar'
+
+    send_email(
+        'jpgodt', mail_body='Hello', email_rcpt='test@localhost', want_html=False, attachments=[jpg, odt]
+    )
+    assert emails.emails['jpgodt']['msg'].is_multipart()
+    assert emails.emails['jpgodt']['msg'].get_content_subtype() == 'mixed'
+    assert emails.emails['jpgodt']['msg'].get_payload()[0].get_content_type() == 'text/plain'
+    assert emails.emails['jpgodt']['msg'].get_payload()[1].get_content_type() == 'image/jpeg'
+    assert (
+        emails.emails['jpgodt']['msg'].get_payload()[2].get_content_type()
+        == 'application/vnd.oasis.opendocument.text'
+    )
+    assert base64.b64decode(emails.emails['jpgodt']['msg'].get_payload()[1].get_payload()) == jpg_content
+    assert base64.b64decode(emails.emails['jpgodt']['msg'].get_payload()[2].get_payload()) == odt_content
+
+    unknown = PicklableUpload('test.eo', 'x-foo/x-bar')
+    unknown.receive([b'barfoo'])
+    send_email(
+        'unknown', mail_body='Hello', email_rcpt='test@localhost', want_html=False, attachments=[unknown]
+    )
+    assert emails.emails['unknown']['msg'].is_multipart()
+    assert emails.emails['unknown']['msg'].get_content_subtype() == 'mixed'
+    assert emails.emails['unknown']['msg'].get_payload()[0].get_content_type() == 'text/plain'
+    assert emails.emails['unknown']['msg'].get_payload()[1].get_content_type() == 'x-foo/x-bar'
+    assert emails.emails['unknown']['msg'].get_payload()[1].get_payload(decode=False).strip() == 'YmFyZm9v'
+
+    send_email(
+        'test-bad-attachment',
+        mail_body='Hello',
+        email_rcpt='test@localhost',
+        want_html=False,
+        attachments=['foobad'],
+    )
+    assert not emails.emails['test-bad-attachment']['msg'].is_multipart()
+
+    assert emails.count() == 5
+
+
+@pytest.mark.skipif('docutils is None')
+def test_email_plain_and_html_with_attachments(emails):
+    pub = create_temporary_pub()
+    pub.cfg['emails'] = {'footer': 'Footer\nText'}
+    jpg = PicklableUpload('test.jpeg', 'image/jpeg')
+    with open(os.path.join(os.path.dirname(__file__), 'image-with-gps-data.jpeg'), 'rb') as fd:
+        jpg.receive([fd.read()])
+
+    send_email('test', mail_body='Hello', email_rcpt='test@localhost', attachments=[jpg])
+    assert emails.count() == 1
+    assert emails.emails['test']['msg'].is_multipart()
+    assert emails.emails['test']['msg'].get_content_subtype() == 'mixed'
+    assert emails.emails['test']['msg'].get_payload()[0].is_multipart()
+    assert emails.emails['test']['msg'].get_payload()[0].get_content_subtype() == 'alternative'
+    assert emails.emails['test']['msg'].get_payload()[0].get_payload()[0].get_content_type() == 'text/plain'
+    assert emails.emails['test']['msg'].get_payload()[0].get_payload()[1].get_content_type() == 'text/html'
+    assert emails.emails['test']['msg'].get_payload()[1].get_content_type() == 'image/jpeg'
+
+
+@pytest.mark.skipif('docutils is None')
+def test_email_with_enumeration(emails):
+    pub = create_temporary_pub()
+    pub.cfg['emails'] = {'footer': 'Footer\nText'}
+    mail_body = '''
+A. FooAlpha1
+B. FooAlpha2
+
+1. Num1
+2. Num2
+
+M. Francis Kuntz
+
+'''
+    send_email('test', mail_body=mail_body, email_rcpt='test@localhost')
+    assert emails.count() == 1
+    assert emails.emails['test']['msg'].is_multipart()
+    assert emails.emails['test']['msg'].get_content_subtype() == 'alternative'
+    assert emails.emails['test']['msg'].get_payload()[0].get_content_type() == 'text/plain'
+    assert emails.emails['test']['msg'].get_payload()[1].get_content_type() == 'text/html'
+    html = emails.emails['test']['msg'].get_payload()[1].get_payload(decode=True)
+    assert html.count(b'<ol') == 1
+    assert b'<ul' not in html
+    assert b'arabic simple' in html
+    assert b'M. Francis Kuntz' in html
+
+
+@pytest.mark.skipif('docutils is None')
+def test_email_with_unexpected_transition(emails):
+    create_temporary_pub()
+    mail_body = '''
+Value:
+ A
+
+Other value:
+ ?????????
+
+Plop:
+ C
+
+bye,
+'''
+    send_email('test', mail_body=mail_body, email_rcpt='test@localhost')
+    assert emails.count() == 1
+    assert emails.emails['test']['msg'].is_multipart()
+    assert emails.emails['test']['msg'].get_content_subtype() == 'alternative'
+    assert emails.emails['test']['msg'].get_payload()[0].get_content_type() == 'text/plain'
+    assert emails.emails['test']['msg'].get_payload()[1].get_content_type() == 'text/html'
+    text = emails.emails['test']['msg'].get_payload()[0].get_payload(decode=True)
+    html = emails.emails['test']['msg'].get_payload()[1].get_payload(decode=True)
+    assert text.count(b'\n ?????????\n') == 1
+    assert html.count(b'<dd>?????????</dd>') == 1
diff --git a/tests/test_misc.py b/tests/test_misc.py
index 702178f1..f5332412 100644
--- a/tests/test_misc.py
+++ b/tests/test_misc.py
@@ -1,10 +1,7 @@
-import base64
 import datetime
 import json
 import os
-import pwd
 import re
-import socket
 import time
 from unittest import mock
 
@@ -19,8 +16,6 @@ from wcs.admin.settings import FileTypesDirectory
 from wcs.fields import StringField
 from wcs.qommon import evalutils, force_str
 from wcs.qommon.backoffice.listing import pagination_links
-from wcs.qommon.emails import docutils  # noqa pylint: disable=unused-import
-from wcs.qommon.emails import email as send_email
 from wcs.qommon.form import FileSizeWidget
 from wcs.qommon.http_request import HTTPRequest
 from wcs.qommon.humantime import humanduration2seconds, seconds2humanduration
@@ -34,7 +29,6 @@ from wcs.qommon.misc import (
     parse_isotime,
     simplify,
 )
-from wcs.qommon.upload_storage import PicklableUpload
 from wcs.scripts import Script
 from wcs.wf.jump import JumpWorkflowStatusItem
 from wcs.workflows import Workflow
@@ -407,152 +401,6 @@ def test_pagination():
     assert '(1-500/1000)' in get_texts(pagination_links(0, 501, 1000))  # 500 is the max
 
 
-def test_email_signature_plain(emails):
-    pub = create_temporary_pub()
-    pub.cfg['emails'] = {'footer': 'Footer\nText'}
-    send_email('test', mail_body='Hello', email_rcpt='test@localhost', want_html=False)
-    assert emails.count() == 1
-    assert not emails.emails['test']['msg'].is_multipart()
-    assert b'Footer\nText' in emails.emails['test']['msg'].get_payload(decode=True)
-
-
-def test_email_from(emails):
-    pub = create_temporary_pub()
-    send_email('test', mail_body='Hello', email_rcpt='test@localhost', want_html=False)
-    assert emails.count() == 1
-    assert emails.emails['test']['from'] == '%s@%s' % (pwd.getpwuid(os.getuid())[0], socket.getfqdn())
-
-    pub.cfg['emails'] = {'from': 'foo@localhost'}
-    send_email('test', mail_body='Hello', email_rcpt='test@localhost', want_html=False)
-    assert emails.count() == 1
-    assert emails.emails['test']['from'] == 'foo@localhost'
-    assert emails.emails['test']['msg']['From'] == 'foo@localhost'
-
-    if not pub.site_options.has_section('variables'):
-        pub.site_options.add_section('variables')
-    pub.site_options.set('variables', 'global_title', 'HELLO')
-    send_email('test', mail_body='Hello', email_rcpt='test@localhost', want_html=False)
-    assert emails.count() == 1
-    assert emails.emails['test']['from'] == 'foo@localhost'
-    assert emails.emails['test']['msg']['From'] in (
-        '=?utf-8?q?HELLO?= <foo@localhost>',
-        'HELLO <foo@localhost>',
-    )
-
-
-@pytest.mark.skipif('docutils is None')
-def test_email_signature_rst(emails):
-    pub = create_temporary_pub()
-    pub.cfg['emails'] = {'footer': 'Footer\nText'}
-    send_email('test', mail_body='Hello', email_rcpt='test@localhost')
-    assert emails.count() == 1
-    assert emails.emails['test']['msg'].is_multipart()
-    assert emails.emails['test']['msg'].get_content_subtype() == 'alternative'
-    assert emails.emails['test']['msg'].get_payload()[0].get_content_type() == 'text/plain'
-    assert emails.emails['test']['msg'].get_payload()[1].get_content_type() == 'text/html'
-    assert b'Footer\nText' in emails.emails['test']['msg'].get_payload()[0].get_payload(decode=True)
-    assert b'>Footer<' in emails.emails['test']['msg'].get_payload()[1].get_payload(decode=True)
-
-
-@pytest.mark.skipif('docutils is None')
-def test_email_signature_rst_pipes(emails):
-    pub = create_temporary_pub()
-    pub.cfg['emails'] = {'footer': '| Footer\n| Text'}
-    send_email('test', mail_body='Hello', email_rcpt='test@localhost')
-    assert emails.count() == 1
-    assert emails.emails['test']['msg'].is_multipart()
-    assert emails.emails['test']['msg'].get_content_subtype() == 'alternative'
-    assert emails.emails['test']['msg'].get_payload()[0].get_content_type() == 'text/plain'
-    assert emails.emails['test']['msg'].get_payload()[1].get_content_type() == 'text/html'
-    assert b'Footer\nText' in emails.emails['test']['msg'].get_payload()[0].get_payload(decode=True)
-    assert b'>Footer<' in emails.emails['test']['msg'].get_payload()[1].get_payload(decode=True)
-
-
-def test_email_plain_with_attachments(emails):
-    create_temporary_pub()
-
-    jpg = PicklableUpload('test.jpeg', 'image/jpeg')
-    with open(os.path.join(os.path.dirname(__file__), 'image-with-gps-data.jpeg'), 'rb') as fd:
-        jpg_content = fd.read()
-    jpg.receive([jpg_content])
-    txt = PicklableUpload('test.txt', 'text/plain')
-    txt.receive([b'foo-text-bar'])
-    odt = PicklableUpload('test.odt', 'application/vnd.oasis.opendocument.text')
-    with open(os.path.join(os.path.dirname(__file__), 'template.odt'), 'rb') as fd:
-        odt_content = fd.read()
-    odt.receive([odt_content])
-
-    send_email('jpg', mail_body='Hello', email_rcpt='test@localhost', want_html=False, attachments=[jpg])
-    assert emails.count() == 1
-    assert emails.emails['jpg']['msg'].is_multipart()
-    assert emails.emails['jpg']['msg'].get_content_subtype() == 'mixed'
-    assert emails.emails['jpg']['msg'].get_payload()[0].get_content_type() == 'text/plain'
-    assert emails.emails['jpg']['msg'].get_payload()[1].get_content_type() == 'image/jpeg'
-    assert base64.b64decode(emails.emails['jpg']['msg'].get_payload()[1].get_payload()) == jpg_content
-
-    send_email('txt', mail_body='Hello', email_rcpt='test@localhost', want_html=False, attachments=[txt])
-    assert emails.emails['txt']['msg'].is_multipart()
-    assert emails.emails['txt']['msg'].get_content_subtype() == 'mixed'
-    assert emails.emails['txt']['msg'].get_payload()[0].get_content_type() == 'text/plain'
-    assert emails.emails['txt']['msg'].get_payload()[1].get_content_type() == 'text/plain'
-    assert emails.emails['txt']['msg'].get_payload()[1].get_payload(decode=True) == b'foo-text-bar'
-
-    send_email(
-        'jpgodt', mail_body='Hello', email_rcpt='test@localhost', want_html=False, attachments=[jpg, odt]
-    )
-    assert emails.emails['jpgodt']['msg'].is_multipart()
-    assert emails.emails['jpgodt']['msg'].get_content_subtype() == 'mixed'
-    assert emails.emails['jpgodt']['msg'].get_payload()[0].get_content_type() == 'text/plain'
-    assert emails.emails['jpgodt']['msg'].get_payload()[1].get_content_type() == 'image/jpeg'
-    assert (
-        emails.emails['jpgodt']['msg'].get_payload()[2].get_content_type()
-        == 'application/vnd.oasis.opendocument.text'
-    )
-    assert base64.b64decode(emails.emails['jpgodt']['msg'].get_payload()[1].get_payload()) == jpg_content
-    assert base64.b64decode(emails.emails['jpgodt']['msg'].get_payload()[2].get_payload()) == odt_content
-
-    unknown = PicklableUpload('test.eo', 'x-foo/x-bar')
-    unknown.receive([b'barfoo'])
-    send_email(
-        'unknown', mail_body='Hello', email_rcpt='test@localhost', want_html=False, attachments=[unknown]
-    )
-    assert emails.emails['unknown']['msg'].is_multipart()
-    assert emails.emails['unknown']['msg'].get_content_subtype() == 'mixed'
-    assert emails.emails['unknown']['msg'].get_payload()[0].get_content_type() == 'text/plain'
-    assert emails.emails['unknown']['msg'].get_payload()[1].get_content_type() == 'x-foo/x-bar'
-    assert emails.emails['unknown']['msg'].get_payload()[1].get_payload(decode=False).strip() == 'YmFyZm9v'
-
-    send_email(
-        'test-bad-attachment',
-        mail_body='Hello',
-        email_rcpt='test@localhost',
-        want_html=False,
-        attachments=['foobad'],
-    )
-    assert not emails.emails['test-bad-attachment']['msg'].is_multipart()
-
-    assert emails.count() == 5
-
-
-@pytest.mark.skipif('docutils is None')
-def test_email_plain_and_html_with_attachments(emails):
-    pub = create_temporary_pub()
-    pub.cfg['emails'] = {'footer': 'Footer\nText'}
-    jpg = PicklableUpload('test.jpeg', 'image/jpeg')
-    with open(os.path.join(os.path.dirname(__file__), 'image-with-gps-data.jpeg'), 'rb') as fd:
-        jpg.receive([fd.read()])
-
-    send_email('test', mail_body='Hello', email_rcpt='test@localhost', attachments=[jpg])
-    assert emails.count() == 1
-    assert emails.emails['test']['msg'].is_multipart()
-    assert emails.emails['test']['msg'].get_content_subtype() == 'mixed'
-    assert emails.emails['test']['msg'].get_payload()[0].is_multipart()
-    assert emails.emails['test']['msg'].get_payload()[0].get_content_subtype() == 'alternative'
-    assert emails.emails['test']['msg'].get_payload()[0].get_payload()[0].get_content_type() == 'text/plain'
-    assert emails.emails['test']['msg'].get_payload()[0].get_payload()[1].get_content_type() == 'text/html'
-    assert emails.emails['test']['msg'].get_payload()[1].get_content_type() == 'image/jpeg'
-
-
 def test_cache():
     cache.set('hello', 'world')
     assert cache.get('hello') == 'world'
@@ -578,60 +426,6 @@ def test_normalize_geolocation():
     assert normalize_geolocation({'lat': 0.0, 'lon': -400.0}) == {'lat': 0.0, 'lon': -40.0}
 
 
-@pytest.mark.skipif('docutils is None')
-def test_email_with_enumeration(emails):
-    pub = create_temporary_pub()
-    pub.cfg['emails'] = {'footer': 'Footer\nText'}
-    mail_body = '''
-A. FooAlpha1
-B. FooAlpha2
-
-1. Num1
-2. Num2
-
-M. Francis Kuntz
-
-'''
-    send_email('test', mail_body=mail_body, email_rcpt='test@localhost')
-    assert emails.count() == 1
-    assert emails.emails['test']['msg'].is_multipart()
-    assert emails.emails['test']['msg'].get_content_subtype() == 'alternative'
-    assert emails.emails['test']['msg'].get_payload()[0].get_content_type() == 'text/plain'
-    assert emails.emails['test']['msg'].get_payload()[1].get_content_type() == 'text/html'
-    html = emails.emails['test']['msg'].get_payload()[1].get_payload(decode=True)
-    assert html.count(b'<ol') == 1
-    assert b'<ul' not in html
-    assert b'arabic simple' in html
-    assert b'M. Francis Kuntz' in html
-
-
-@pytest.mark.skipif('docutils is None')
-def test_email_with_unexpected_transition(emails):
-    create_temporary_pub()
-    mail_body = '''
-Value:
- A
-
-Other value:
- ?????????
-
-Plop:
- C
-
-bye,
-'''
-    send_email('test', mail_body=mail_body, email_rcpt='test@localhost')
-    assert emails.count() == 1
-    assert emails.emails['test']['msg'].is_multipart()
-    assert emails.emails['test']['msg'].get_content_subtype() == 'alternative'
-    assert emails.emails['test']['msg'].get_payload()[0].get_content_type() == 'text/plain'
-    assert emails.emails['test']['msg'].get_payload()[1].get_content_type() == 'text/html'
-    text = emails.emails['test']['msg'].get_payload()[0].get_payload(decode=True)
-    html = emails.emails['test']['msg'].get_payload()[1].get_payload(decode=True)
-    assert text.count(b'\n ?????????\n') == 1
-    assert html.count(b'<dd>?????????</dd>') == 1
-
-
 def test_dict_from_prefix():
     d = evalutils.dict_from_prefix('var1', {})
     assert d == {}
-- 
2.33.0

