File: test_reply_changes.py

package info (click to toggle)
rss2email 1%3A3.14-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,264 kB
  • sloc: python: 2,565; xml: 156; sh: 6; makefile: 2
file content (116 lines) | stat: -rw-r--r-- 3,887 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import mailbox
import os
import shutil
import sys
import tempfile
import unittest
from pathlib import Path
from textwrap import dedent
from typing import List, Optional, Dict, Any

sys.path.insert(0, os.path.dirname(__file__))
from util.execcontext import ExecContext
from util.tempmaildir import TemporaryMaildir


class TestReplyChanges(unittest.TestCase):

    def setUp(self) -> None:
        self.maildir = TemporaryMaildir()
        self.tmpdir = tempfile.TemporaryDirectory()
        self.feed_path = Path(self.tmpdir.name, "feed.xml")


    def tearDown(self) -> None:
        self.maildir.cleanup()
        self.tmpdir.cleanup()


    def test_default(self):
        config = self._config()
        self._call(config)

        messages = self.maildir.inbox_messages()
        self.assertEqual(1, len(messages))
        self.assertIsNone(messages[0]['In-Reply-To'])


    def test_off(self):
        config = self._config({"reply-changes": False})
        self._call(config)

        messages = self.maildir.inbox_messages()
        self.assertEqual(1, len(messages))
        self.assertIsNone(messages[0]['In-Reply-To'])


    def test_on(self):
        config = self._config({"reply-changes": True})
        self._call(config)

        messages = self.maildir.inbox_messages()
        self.assertEqual(2, len(messages))
        self.assertIsNone(messages[0]['In-Reply-To'])
        self.assertEqual(messages[0]['Message-ID'], messages[1]['In-Reply-To'])


    def _call(self, config: str):
        with ExecContext(config) as ctx:
            shutil.copyfile("data/nodejs/feed1.xml", str(self.feed_path))
            ctx.call("run")
            shutil.copyfile("data/nodejs/feed2.xml", str(self.feed_path))
            ctx.call("run")


    def test_switch(self):
        config = self._config({"reply-changes": False})
        with ExecContext(config) as ctx:
            shutil.copyfile("data/nodejs/feed1.xml", str(self.feed_path))
            ctx.call("run")
            ctx.change_config({"reply-changes": True})
            ctx.call("run")
            shutil.copyfile("data/nodejs/feed2.xml", str(self.feed_path))
            ctx.call("run")

        messages = self.maildir.inbox_messages() # type: List[mailbox.MaildirMessage]
        self.assertEqual(2, len(messages))
        self.assertIsNone(messages[0]['In-Reply-To'])
        self.assertEqual(messages[0]['Message-ID'], messages[1]['In-Reply-To'])


    def test_no_send(self):
        config = self._config({"reply-changes": True})
        with ExecContext(config) as ctx:
            shutil.copyfile("data/nodejs/feed1.xml", str(self.feed_path))
            ctx.call("run")
            shutil.copyfile("data/nodejs/feed2.xml", str(self.feed_path))
            ctx.call("run", "--no-send")
            shutil.copyfile("data/nodejs/feed3.xml", str(self.feed_path))
            ctx.call("run")

        messages = self.maildir.inbox_messages() # type: List[mailbox.MaildirMessage]
        self.assertEqual(2, len(messages))
        self.assertIsNone(messages[0]['In-Reply-To'])
        self.assertEqual(messages[0]['Message-ID'], messages[1]['In-Reply-To'])


    def _config(self, options: Optional[Dict[str, Any]] = None):
        config = dedent("""\
            [DEFAULT]
            to = mbox@mail.example
            email-protocol = maildir
            maildir-path = {maildir_path}
            maildir-mailbox = {maildir_mailbox}
            """).format(maildir_path=self.maildir.path,
                        maildir_mailbox=self.maildir.inbox_name,
                        url=self.feed_path.absolute())

        if options is not None:
            config += "".join("{0} = {1}\n".format(name, value) for name, value in options.items())

        config += dedent("""\
            [feed.test-feed]
            url = file:{url}
            """).format(url=self.feed_path.absolute())

        return config