diff options
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/test_server.py | 40 |
1 files changed, 39 insertions, 1 deletions
diff --git a/tests/test_server.py b/tests/test_server.py index 4644ecc..888568f 100644 --- a/tests/test_server.py +++ b/tests/test_server.py @@ -225,7 +225,7 @@ class ServerTests(unittest.TestCase): encoding="utf-8", ) config = load_config(config_file) - for target, mtime in (("alpha", 1990), ("beta", 1700), ("gamma", 2000)): + for target, mtime in (("alpha", 1990), ("beta", 1940), ("gamma", 2000)): directory = self.upload_dir / target directory.mkdir(parents=True) (directory / "x.png").write_bytes(PNG) @@ -287,6 +287,44 @@ class ServerTests(unittest.TestCase): with self.subTest(prefix=prefix), self.assertRaises(ValueError): load_config(config_file) + def test_watcher_uses_per_stanza_subject_and_body_header(self): + config_file = Path(self.temp.name) / "watcher.toml" + config_file.write_text( + '[GLOBAL]\nsend_as = "uploads@example.com"\nsmtp_server = "localhost"\n' + "smtp_port = 25\ntls = false\nstarttls = false\n\n" + '[custom]\nrecipient = "a@example.com"\nsubject = "Logo entry"\n' + 'body_header = """\nHello reviewers,\nA new entry arrived.\n"""\n\n' + '[plain]\nrecipient = "b@example.com"\n', + encoding="utf-8", + ) + config = load_config(config_file) + for target in ("custom", "plain"): + directory = self.upload_dir / target + directory.mkdir(parents=True) + (directory / "x.png").write_bytes(PNG) + os.utime(directory / "x.png", (1990, 1990)) + smtp_context = MagicMock() + with patch("watcher.smtplib.SMTP", return_value=smtp_context): + send_recent_upload_notifications(config, self.upload_dir, now=2000) + + custom, plain = (call.args[0] for call in smtp_context.__enter__.return_value.send_message.call_args_list) + self.assertEqual(custom["Subject"], "Logo entry") + self.assertTrue(custom.get_content().startswith("Hello reviewers,\nA new entry arrived.\n\n---")) + self.assertEqual(plain["Subject"], "New upload received") + self.assertTrue(plain.get_content().startswith("A new file has been uploaded to your private drop.")) + + def test_rejects_invalid_subject_and_body_header(self): + config_file = Path(self.temp.name) / "config.toml" + for setting in ('subject = "two\\nlines"', 'subject = " "', 'body_header = "bell\\u0007"', "subject = 5"): + config_file.write_text( + '[GLOBAL]\nsend_as = "uploads@example.com"\nsmtp_server = "localhost"\n' + "smtp_port = 25\ntls = false\nstarttls = false\n\n" + f'[logo-contest]\nrecipient = "reviewer@example.com"\n{setting}\n', + encoding="utf-8", + ) + with self.subTest(setting=setting), self.assertRaises(ValueError): + load_config(config_file) + def test_watcher_skips_smtp_when_nothing_is_new(self): config_file = Path(self.temp.name) / "watcher.toml" config_file.write_text( |
