Bump copyright year
[mailman-postorious.git] / src / postorius / tests / mailman_api_tests / conftest.py
blobba5e1dcfd73a12863cbb55beee5aafa63ccde1e4
1 # Copyright (C) 2019-2022 by the Free Software Foundation, Inc.
3 # This file is part of Postorius
5 # mailman.client is free software: you can redistribute it and/or modify it
6 # under the terms of the GNU Lesser General Public License as published by the
7 # Free Software Foundation, version 3 of the License.
9 # mailman.client is distributed in the hope that it will be useful, but
10 # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11 # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
12 # License for more details.
14 # You should have received a copy of the GNU Lesser General Public License
15 # along with mailman.client. If not, see <http://www.gnu.org/licenses/>.
17 """Testing utilities."""
19 import os
20 import shutil
21 import tempfile
22 from textwrap import dedent
24 import pytest
25 from mailman.config import config
26 from mailman.core import initialize
27 from mailman.core.initialize import INHIBIT_CONFIG_FILE
28 from mailman.testing.helpers import (
29 TestableMaster, reset_the_world, wait_for_webservice)
32 @pytest.fixture(scope='session')
33 def config_initialize(request):
34 # Set up the basic configuration stuff. Turn off path creation until
35 # we've pushed the testing config.
36 config.create_paths = False
37 initialize.initialize_1(INHIBIT_CONFIG_FILE)
38 # Calculate a temporary VAR_DIR directory so that run-time artifacts
39 # of the tests won't tread on the installation's data. This also
40 # makes it easier to clean up after the tests are done, and insures
41 # isolation of test suite runs.
42 var_dir = tempfile.mkdtemp()
43 # We need a test configuration both for the foreground process and any
44 # child processes that get spawned. lazr.config would allow us to do
45 # it all in a string that gets pushed, and we'll do that for the
46 # foreground, but because we may be spawning processes (such as
47 # runners) we'll need a file that we can specify to the with the -C
48 # option. Craft the full test configuration string here, push it, and
49 # also write it out to a temp file for -C.
51 # Create a dummy postfix.cfg file so that the test suite doesn't try
52 # to run the actual postmap command, which may not exist anyway.
53 postfix_cfg = os.path.join(var_dir, 'postfix.cfg')
54 with open(postfix_cfg, 'w') as fp:
55 print(dedent("""
56 [postfix]
57 postmap_command: true
58 transport_file_type: hash
59 """), file=fp)
60 test_config = dedent("""
61 [mailman]
62 layout: testing
63 [paths.testing]
64 var_dir: {}
65 [mta]
66 configuration: {}
67 [devmode]
68 enabled: yes
69 testing: yes
70 recipient: you@yourdomain.com
72 [mta]
73 smtp_port: 9025
74 lmtp_port: 9024
75 incoming: mailman.testing.mta.FakeMTA
77 [webservice]
78 port: 9001
80 [archiver.mhonarc]
81 enable: yes
83 [archiver.mail_archive]
84 enable: yes
86 [archiver.prototype]
87 enable: yes
88 """.format(var_dir, postfix_cfg))
89 config.create_paths = True
90 config.push('test config', test_config)
91 # Initialize everything else.
92 initialize.initialize_2(testing=True)
93 initialize.initialize_3()
95 config_file = os.path.join(var_dir, 'test.cfg')
96 with open(config_file, 'w') as fp:
97 fp.write(test_config)
98 print(file=fp)
99 config.filename = config_file
100 # Start the Mailman's test runner.
101 server = TestableMaster(wait_for_webservice)
102 server.start('rest', 'in')
103 request.addfinalizer(server.stop)
104 # Run the test.
105 yield
106 reset_the_world()
107 # Destroy the test database after the tests are done so that there is
108 # no data in case the tests are rerun with a database layer like mysql
109 # or postgresql which are not deleted in teardown.
110 shutil.rmtree(var_dir)
111 # Prevent the bit of post-processing on the .pop() that creates
112 # directories. We're basically shutting down everything and we don't
113 # need the directories created. Plus, doing so leaves a var directory
114 # turd in the source tree's top-level directory. We do it this way
115 # rather than shutil.rmtree'ing the resulting var directory because
116 # it's possible the user created a valid such directory for
117 # operational or test purposes.
118 config.create_paths = False
119 config.pop('test config')
122 @pytest.fixture(scope='class', autouse=True)
123 def mailman_rest_layer(config_initialize, request):
124 yield
125 request.addfinalizer(reset_the_world)